LuaValue.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. using System.Globalization;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using Lua.Internal;
  5. using Lua.Runtime;
  6. namespace Lua;
  7. public enum LuaValueType : byte
  8. {
  9. Nil,
  10. Boolean,
  11. String,
  12. Number,
  13. Function,
  14. Thread,
  15. LightUserData,
  16. UserData,
  17. Table
  18. }
  19. [StructLayout(LayoutKind.Auto)]
  20. public readonly struct LuaValue : IEquatable<LuaValue>
  21. {
  22. public static readonly LuaValue Nil = default;
  23. public readonly LuaValueType Type;
  24. readonly double value;
  25. readonly object? referenceValue;
  26. internal LuaValue(LuaValueType type, double value, object? referenceValue)
  27. {
  28. Type = type;
  29. this.value = value;
  30. this.referenceValue = referenceValue;
  31. }
  32. public bool TryRead<T>(out T result)
  33. {
  34. var t = typeof(T);
  35. switch (Type)
  36. {
  37. case LuaValueType.Number:
  38. if (t == typeof(float))
  39. {
  40. var v = (float)value;
  41. result = Unsafe.As<float, T>(ref v);
  42. return true;
  43. }
  44. else if (t == typeof(double))
  45. {
  46. var v = value;
  47. result = Unsafe.As<double, T>(ref v);
  48. return true;
  49. }
  50. else if (t == typeof(int))
  51. {
  52. if (!MathEx.IsInteger(value))
  53. {
  54. break;
  55. }
  56. var v = (int)value;
  57. result = Unsafe.As<int, T>(ref v);
  58. return true;
  59. }
  60. else if (t == typeof(long))
  61. {
  62. if (!MathEx.IsInteger(value))
  63. {
  64. break;
  65. }
  66. var v = (long)value;
  67. result = Unsafe.As<long, T>(ref v);
  68. return true;
  69. }
  70. else if (t == typeof(object))
  71. {
  72. result = (T)(object)value;
  73. return true;
  74. }
  75. else
  76. {
  77. break;
  78. }
  79. case LuaValueType.Boolean:
  80. if (t == typeof(bool))
  81. {
  82. var v = value != 0;
  83. result = Unsafe.As<bool, T>(ref v);
  84. return true;
  85. }
  86. else if (t == typeof(object))
  87. {
  88. result = (T)(object)value;
  89. return true;
  90. }
  91. else
  92. {
  93. break;
  94. }
  95. case LuaValueType.String:
  96. if (t == typeof(string))
  97. {
  98. var v = referenceValue!;
  99. result = Unsafe.As<object, T>(ref v);
  100. return true;
  101. }
  102. else if (t == typeof(double))
  103. {
  104. result = default!;
  105. return TryParseToDouble(out Unsafe.As<T, double>(ref result));
  106. }
  107. else if (t == typeof(object))
  108. {
  109. result = (T)referenceValue!;
  110. return true;
  111. }
  112. else
  113. {
  114. break;
  115. }
  116. case LuaValueType.Function:
  117. if (t == typeof(LuaFunction) || t.IsSubclassOf(typeof(LuaFunction)))
  118. {
  119. var v = referenceValue!;
  120. result = Unsafe.As<object, T>(ref v);
  121. return true;
  122. }
  123. else if (t == typeof(object))
  124. {
  125. result = (T)referenceValue!;
  126. return true;
  127. }
  128. else
  129. {
  130. break;
  131. }
  132. case LuaValueType.Thread:
  133. if (t == typeof(LuaThread))
  134. {
  135. var v = referenceValue!;
  136. result = Unsafe.As<object, T>(ref v);
  137. return true;
  138. }
  139. else if (t == typeof(object))
  140. {
  141. result = (T)referenceValue!;
  142. return true;
  143. }
  144. else
  145. {
  146. break;
  147. }
  148. case LuaValueType.LightUserData:
  149. {
  150. if (referenceValue is T tValue)
  151. {
  152. result = tValue;
  153. return true;
  154. }
  155. break;
  156. }
  157. case LuaValueType.UserData:
  158. if (t == typeof(ILuaUserData) || typeof(ILuaUserData).IsAssignableFrom(t))
  159. {
  160. if (referenceValue is T tValue)
  161. {
  162. result = tValue;
  163. return true;
  164. }
  165. break;
  166. }
  167. else if (t == typeof(object))
  168. {
  169. result = (T)referenceValue!;
  170. return true;
  171. }
  172. else
  173. {
  174. break;
  175. }
  176. case LuaValueType.Table:
  177. if (t == typeof(LuaTable))
  178. {
  179. var v = referenceValue!;
  180. result = Unsafe.As<object, T>(ref v);
  181. return true;
  182. }
  183. else if (t == typeof(object))
  184. {
  185. result = (T)referenceValue!;
  186. return true;
  187. }
  188. else
  189. {
  190. break;
  191. }
  192. }
  193. result = default!;
  194. return false;
  195. }
  196. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  197. internal bool TryReadBool(out bool result)
  198. {
  199. if (Type == LuaValueType.Boolean)
  200. {
  201. result = value != 0;
  202. return true;
  203. }
  204. result = default!;
  205. return false;
  206. }
  207. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  208. internal bool TryReadNumber(out double result)
  209. {
  210. if (Type == LuaValueType.Number)
  211. {
  212. result = value;
  213. return true;
  214. }
  215. result = default!;
  216. return false;
  217. }
  218. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  219. internal bool TryReadTable(out LuaTable result)
  220. {
  221. if (Type == LuaValueType.Table)
  222. {
  223. var v = referenceValue!;
  224. result = Unsafe.As<object, LuaTable>(ref v);
  225. return true;
  226. }
  227. result = default!;
  228. return false;
  229. }
  230. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  231. internal bool TryReadFunction(out LuaFunction result)
  232. {
  233. if (Type == LuaValueType.Function)
  234. {
  235. var v = referenceValue!;
  236. result = Unsafe.As<object, LuaFunction>(ref v);
  237. return true;
  238. }
  239. result = default!;
  240. return false;
  241. }
  242. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  243. internal bool TryReadString(out string result)
  244. {
  245. if (Type == LuaValueType.String)
  246. {
  247. var v = referenceValue!;
  248. result = Unsafe.As<object, string>(ref v);
  249. return true;
  250. }
  251. result = default!;
  252. return false;
  253. }
  254. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  255. internal bool TryReadDouble(out double result)
  256. {
  257. if (Type == LuaValueType.Number)
  258. {
  259. result = value;
  260. return true;
  261. }
  262. return TryParseToDouble(out result);
  263. }
  264. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  265. internal static bool TryReadOrSetDouble(ref LuaValue luaValue, out double result)
  266. {
  267. if (luaValue.Type == LuaValueType.Number)
  268. {
  269. result = luaValue.value;
  270. return true;
  271. }
  272. if (luaValue.TryParseToDouble(out result))
  273. {
  274. luaValue = result;
  275. return true;
  276. }
  277. return false;
  278. }
  279. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  280. internal double UnsafeReadDouble()
  281. {
  282. return value;
  283. }
  284. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  285. internal string UnsafeReadString()
  286. {
  287. return Unsafe.As<string>(referenceValue!);
  288. }
  289. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  290. internal object UnsafeReadObject()
  291. {
  292. return Unsafe.As<object>(referenceValue!);
  293. }
  294. bool TryParseToDouble(out double result)
  295. {
  296. if (Type != LuaValueType.String)
  297. {
  298. result = default!;
  299. return false;
  300. }
  301. var str = Unsafe.As<string>(referenceValue!);
  302. var span = str.AsSpan().Trim();
  303. if (span.Length == 0)
  304. {
  305. result = default!;
  306. return false;
  307. }
  308. var sign = 1;
  309. var first = span[0];
  310. if (first is '+')
  311. {
  312. sign = 1;
  313. span = span[1..];
  314. }
  315. else if (first is '-')
  316. {
  317. sign = -1;
  318. span = span[1..];
  319. }
  320. if (span.Length > 2 && span[0] is '0' && span[1] is 'x' or 'X')
  321. {
  322. // TODO: optimize
  323. try
  324. {
  325. var d = HexConverter.ToDouble(span) * sign;
  326. result = d;
  327. return true;
  328. }
  329. catch (FormatException)
  330. {
  331. result = default!;
  332. return false;
  333. }
  334. }
  335. else
  336. {
  337. return double.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
  338. }
  339. }
  340. public T Read<T>()
  341. {
  342. if (!TryRead<T>(out var result))
  343. {
  344. throw new InvalidOperationException($"Cannot convert LuaValueType.{Type} to {typeof(T).FullName}.");
  345. }
  346. return result;
  347. }
  348. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  349. internal T UnsafeRead<T>()
  350. {
  351. switch (Type)
  352. {
  353. case LuaValueType.Boolean:
  354. {
  355. var v = value != 0;
  356. return Unsafe.As<bool, T>(ref v);
  357. }
  358. case LuaValueType.Number:
  359. {
  360. var v = value;
  361. return Unsafe.As<double, T>(ref v);
  362. }
  363. case LuaValueType.String:
  364. case LuaValueType.Thread:
  365. case LuaValueType.Function:
  366. case LuaValueType.Table:
  367. case LuaValueType.LightUserData:
  368. case LuaValueType.UserData:
  369. {
  370. var v = referenceValue!;
  371. return Unsafe.As<object, T>(ref v);
  372. }
  373. }
  374. return default!;
  375. }
  376. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  377. public bool ToBoolean()
  378. {
  379. if (Type == LuaValueType.Boolean)
  380. {
  381. return value != 0;
  382. }
  383. if (Type is LuaValueType.Nil)
  384. {
  385. return false;
  386. }
  387. return true;
  388. }
  389. public static LuaValue FromObject(object obj)
  390. {
  391. return obj switch
  392. {
  393. null => Nil,
  394. LuaValue luaValue => luaValue,
  395. bool boolValue => boolValue,
  396. double doubleValue => doubleValue,
  397. string stringValue => stringValue,
  398. LuaFunction luaFunction => luaFunction,
  399. LuaTable luaTable => luaTable,
  400. LuaThread luaThread => luaThread,
  401. ILuaUserData userData => FromUserData(userData),
  402. int intValue => intValue,
  403. long longValue => longValue,
  404. float floatValue => floatValue,
  405. _ => new(obj)
  406. };
  407. }
  408. public static LuaValue FromUserData(ILuaUserData? userData)
  409. {
  410. if (userData is null)
  411. {
  412. return Nil;
  413. }
  414. return new(userData);
  415. }
  416. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  417. LuaValue(object obj)
  418. {
  419. Type = LuaValueType.LightUserData;
  420. referenceValue = obj;
  421. }
  422. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  423. public LuaValue(bool value)
  424. {
  425. Type = LuaValueType.Boolean;
  426. this.value = value ? 1 : 0;
  427. }
  428. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  429. public LuaValue(double value)
  430. {
  431. Type = LuaValueType.Number;
  432. this.value = value;
  433. }
  434. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  435. public LuaValue(string value)
  436. {
  437. Type = LuaValueType.String;
  438. referenceValue = value;
  439. }
  440. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  441. public LuaValue(LuaFunction value)
  442. {
  443. Type = LuaValueType.Function;
  444. referenceValue = value;
  445. }
  446. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  447. public LuaValue(LuaTable value)
  448. {
  449. Type = LuaValueType.Table;
  450. referenceValue = value;
  451. }
  452. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  453. public LuaValue(LuaThread value)
  454. {
  455. Type = LuaValueType.Thread;
  456. referenceValue = value;
  457. }
  458. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  459. public LuaValue(ILuaUserData value)
  460. {
  461. Type = LuaValueType.UserData;
  462. referenceValue = value;
  463. }
  464. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  465. public static implicit operator LuaValue(bool value)
  466. {
  467. return new(value);
  468. }
  469. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  470. public static implicit operator LuaValue(double value)
  471. {
  472. return new(value);
  473. }
  474. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  475. public static implicit operator LuaValue(string value)
  476. {
  477. return new(value);
  478. }
  479. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  480. public static implicit operator LuaValue(LuaTable value)
  481. {
  482. return new(value);
  483. }
  484. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  485. public static implicit operator LuaValue(LuaFunction value)
  486. {
  487. return new(value);
  488. }
  489. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  490. public static implicit operator LuaValue(LuaThread value)
  491. {
  492. return new(value);
  493. }
  494. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  495. public override int GetHashCode()
  496. {
  497. return Type switch
  498. {
  499. LuaValueType.Nil => 0,
  500. LuaValueType.Boolean or LuaValueType.Number => value.GetHashCode(),
  501. LuaValueType.String => Unsafe.As<string>(referenceValue)!.GetHashCode(),
  502. _ => referenceValue!.GetHashCode()
  503. };
  504. }
  505. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  506. public bool Equals(LuaValue other)
  507. {
  508. if (other.Type != Type)
  509. {
  510. return false;
  511. }
  512. return Type switch
  513. {
  514. LuaValueType.Nil => true,
  515. LuaValueType.Boolean or LuaValueType.Number => other.value == value,
  516. LuaValueType.String => Unsafe.As<string>(other.referenceValue) == Unsafe.As<string>(referenceValue),
  517. _ => other.referenceValue == referenceValue
  518. };
  519. }
  520. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  521. public bool EqualsForDict(LuaValue other)
  522. {
  523. return other.Type == Type && Type switch
  524. {
  525. LuaValueType.Boolean or LuaValueType.Number => other.value == value,
  526. LuaValueType.String => Unsafe.As<string>(other.referenceValue) == Unsafe.As<string>(referenceValue),
  527. _ => other.referenceValue == referenceValue
  528. };
  529. }
  530. public override bool Equals(object? obj)
  531. {
  532. return obj is LuaValue value1 && Equals(value1);
  533. }
  534. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  535. public static bool operator ==(LuaValue a, LuaValue b)
  536. {
  537. return a.Equals(b);
  538. }
  539. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  540. public static bool operator !=(LuaValue a, LuaValue b)
  541. {
  542. return !a.Equals(b);
  543. }
  544. public override string ToString()
  545. {
  546. return Type switch
  547. {
  548. LuaValueType.Nil => "nil",
  549. LuaValueType.Boolean => Read<bool>() ? "true" : "false",
  550. LuaValueType.String => Read<string>(),
  551. LuaValueType.Number => Read<double>().ToString(CultureInfo.InvariantCulture),
  552. LuaValueType.Function => $"function: {referenceValue!.GetHashCode()}",
  553. LuaValueType.Thread => $"thread: {referenceValue!.GetHashCode()}",
  554. LuaValueType.Table => $"table: {referenceValue!.GetHashCode()}",
  555. LuaValueType.LightUserData => $"userdata: {referenceValue!.GetHashCode()}",
  556. LuaValueType.UserData => $"userdata: {referenceValue!.GetHashCode()}",
  557. _ => ""
  558. };
  559. }
  560. public string TypeToString()
  561. {
  562. return ToString(Type);
  563. }
  564. public static string ToString(LuaValueType type)
  565. {
  566. return type switch
  567. {
  568. LuaValueType.Nil => "nil",
  569. LuaValueType.Boolean => "boolean",
  570. LuaValueType.String => "string",
  571. LuaValueType.Number => "number",
  572. LuaValueType.Function => "function",
  573. LuaValueType.Thread => "thread",
  574. LuaValueType.Table => "table",
  575. LuaValueType.LightUserData => "light userdata",
  576. LuaValueType.UserData => "userdata",
  577. _ => ""
  578. };
  579. }
  580. public static bool TryGetLuaValueType(Type type, out LuaValueType result)
  581. {
  582. if (type == typeof(double) || type == typeof(float) || type == typeof(int) || type == typeof(long))
  583. {
  584. result = LuaValueType.Number;
  585. return true;
  586. }
  587. else if (type == typeof(bool))
  588. {
  589. result = LuaValueType.Boolean;
  590. return true;
  591. }
  592. else if (type == typeof(string))
  593. {
  594. result = LuaValueType.String;
  595. return true;
  596. }
  597. else if (type == typeof(LuaFunction) || type.IsSubclassOf(typeof(LuaFunction)))
  598. {
  599. result = LuaValueType.Function;
  600. return true;
  601. }
  602. else if (type == typeof(LuaTable))
  603. {
  604. result = LuaValueType.Table;
  605. return true;
  606. }
  607. else if (type == typeof(LuaThread))
  608. {
  609. result = LuaValueType.Thread;
  610. return true;
  611. }
  612. else if (type == typeof(ILuaUserData) || type.IsAssignableFrom(typeof(ILuaUserData)))
  613. {
  614. result = LuaValueType.UserData;
  615. return true;
  616. }
  617. result = default;
  618. return false;
  619. }
  620. internal ValueTask<int> CallToStringAsync(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
  621. {
  622. if (this.TryGetMetamethod(context.State, Metamethods.ToString, out var metamethod))
  623. {
  624. var stack = context.Thread.Stack;
  625. stack.Push(metamethod);
  626. stack.Push(this);
  627. return LuaVirtualMachine.Call(context.Thread, stack.Count - 2, stack.Count - 2, cancellationToken);
  628. }
  629. else
  630. {
  631. context.Thread.Stack.Push(ToString());
  632. return default;
  633. }
  634. }
  635. }