DefaultObjectConverter.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Jint.Native;
  5. using Jint.Native.Array;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint
  10. {
  11. internal static class DefaultObjectConverter
  12. {
  13. private static Dictionary<Type, Func<Engine, object, JsValue>> _typeMappers = new()
  14. {
  15. { typeof(bool), (engine, v) => (bool)v ? JsBoolean.True : JsBoolean.False },
  16. { typeof(byte), (engine, v) => JsNumber.Create((byte)v) },
  17. { typeof(char), (engine, v) => JsString.Create((char)v) },
  18. { typeof(DateTime), (engine, v) => engine.Realm.Intrinsics.Date.Construct((DateTime)v) },
  19. { typeof(DateTimeOffset), (engine, v) => engine.Realm.Intrinsics.Date.Construct((DateTimeOffset)v) },
  20. { typeof(decimal), (engine, v) => (JsValue)(double)(decimal)v },
  21. { typeof(double), (engine, v) => (JsValue)(double)v },
  22. { typeof(short), (engine, v) => JsNumber.Create((short)v) },
  23. { typeof(int), (engine, v) => JsNumber.Create((int)v) },
  24. { typeof(long), (engine, v) => (JsValue)(long)v },
  25. { typeof(sbyte), (engine, v) => JsNumber.Create((sbyte)v) },
  26. { typeof(float), (engine, v) => (JsValue)(float)v },
  27. { typeof(string), (engine, v) => JsString.Create((string)v) },
  28. { typeof(ushort), (engine, v) => JsNumber.Create((ushort)v) },
  29. { typeof(uint), (engine, v) => JsNumber.Create((uint)v) },
  30. { typeof(ulong), (engine, v) => JsNumber.Create((ulong)v) },
  31. {
  32. typeof(System.Text.RegularExpressions.Regex),
  33. (engine, v) => engine.Realm.Intrinsics.RegExp.Construct((System.Text.RegularExpressions.Regex)v, ((System.Text.RegularExpressions.Regex)v).ToString(), "")
  34. }
  35. };
  36. public static bool TryConvert(Engine engine, object value, out JsValue result)
  37. {
  38. result = null;
  39. var valueType = value.GetType();
  40. var typeMappers = _typeMappers;
  41. if (typeMappers.TryGetValue(valueType, out var typeMapper))
  42. {
  43. result = typeMapper(engine, value);
  44. }
  45. else
  46. {
  47. if (value is Array a)
  48. {
  49. // racy, we don't care, worst case we'll catch up later
  50. Interlocked.CompareExchange(ref _typeMappers,
  51. new Dictionary<Type, Func<Engine, object, JsValue>>(typeMappers)
  52. {
  53. [valueType] = ConvertArray
  54. }, typeMappers);
  55. result = ConvertArray(engine, a);
  56. return result is not null;
  57. }
  58. if (value is IConvertible convertible && TryConvertConvertible(engine, convertible, out result))
  59. {
  60. return true;
  61. }
  62. if (value is Delegate d)
  63. {
  64. result = new DelegateWrapper(engine, d);
  65. }
  66. else
  67. {
  68. var t = value.GetType();
  69. if (!engine.Options.Interop.AllowSystemReflection
  70. && t.Namespace?.StartsWith("System.Reflection") == true)
  71. {
  72. const string message = "Cannot access System.Reflection namespace, check Engine's interop options";
  73. ExceptionHelper.ThrowInvalidOperationException(message);
  74. }
  75. if (t.IsEnum)
  76. {
  77. var ut = Enum.GetUnderlyingType(t);
  78. if (ut == typeof(ulong))
  79. {
  80. result = JsNumber.Create(Convert.ToDouble(value));
  81. }
  82. else
  83. {
  84. if (ut == typeof(uint) || ut == typeof(long))
  85. {
  86. result = JsNumber.Create(Convert.ToInt64(value));
  87. }
  88. else
  89. {
  90. result = JsNumber.Create(Convert.ToInt32(value));
  91. }
  92. }
  93. }
  94. else
  95. {
  96. result = engine.Options.Interop.WrapObjectHandler.Invoke(engine, value);
  97. }
  98. // if no known type could be guessed, use the default of wrapping using using ObjectWrapper.
  99. }
  100. }
  101. return result is not null;
  102. }
  103. private static bool TryConvertConvertible(Engine engine, IConvertible convertible, out JsValue result)
  104. {
  105. result = convertible.GetTypeCode() switch
  106. {
  107. TypeCode.Boolean => convertible.ToBoolean(engine.Options.Culture) ? JsBoolean.True : JsBoolean.False,
  108. TypeCode.Byte => JsNumber.Create(convertible.ToByte(engine.Options.Culture)),
  109. TypeCode.Char => JsString.Create(convertible.ToChar(engine.Options.Culture)),
  110. TypeCode.Double => JsNumber.Create(convertible.ToDouble(engine.Options.Culture)),
  111. TypeCode.SByte => JsNumber.Create(convertible.ToSByte(engine.Options.Culture)),
  112. TypeCode.Int16 => JsNumber.Create(convertible.ToInt16(engine.Options.Culture)),
  113. TypeCode.Int32 => JsNumber.Create(convertible.ToInt32(engine.Options.Culture)),
  114. TypeCode.UInt16 => JsNumber.Create(convertible.ToUInt16(engine.Options.Culture)),
  115. TypeCode.Int64 => JsNumber.Create(convertible.ToInt64(engine.Options.Culture)),
  116. TypeCode.Single => JsNumber.Create(convertible.ToSingle(engine.Options.Culture)),
  117. TypeCode.String => JsString.Create(convertible.ToString(engine.Options.Culture)),
  118. TypeCode.UInt32 => JsNumber.Create(convertible.ToUInt32(engine.Options.Culture)),
  119. TypeCode.UInt64 => JsNumber.Create(convertible.ToUInt64(engine.Options.Culture)),
  120. TypeCode.DateTime => engine.Realm.Intrinsics.Date.Construct(convertible.ToDateTime(engine.Options.Culture)),
  121. TypeCode.Decimal => JsNumber.Create(convertible.ToDecimal(engine.Options.Culture)),
  122. TypeCode.DBNull => JsValue.Null,
  123. TypeCode.Empty => JsValue.Null,
  124. _ => null
  125. };
  126. return result is not null;
  127. }
  128. private static JsValue ConvertArray(Engine e, object v)
  129. {
  130. var array = (Array)v;
  131. var arrayLength = (uint)array.Length;
  132. var jsArray = new ArrayInstance(e, arrayLength)
  133. {
  134. _prototype = e.Realm.Intrinsics.Array.PrototypeObject
  135. };
  136. for (uint i = 0; i < arrayLength; ++i)
  137. {
  138. var jsItem = JsValue.FromObject(e, array.GetValue(i));
  139. jsArray.WriteArrayValue(i, new PropertyDescriptor(jsItem, PropertyFlag.ConfigurableEnumerableWritable));
  140. }
  141. jsArray.SetOwnProperty(CommonProperties.Length,
  142. new PropertyDescriptor(arrayLength, PropertyFlag.OnlyWritable));
  143. return jsArray;
  144. }
  145. }
  146. }