DefaultObjectConverter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Globalization;
  3. using System.Runtime.CompilerServices;
  4. using System.Threading;
  5. using Jint.Native;
  6. using Jint.Native.Object;
  7. using Jint.Runtime;
  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, Type? type, [NotNullWhen(true)] out JsValue? result)
  37. {
  38. result = null;
  39. Type valueType = ObjectWrapper.GetClrType(value, type);
  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. return result is not null;
  66. }
  67. if ((engine.Options.ExperimentalFeatures & ExperimentalFeature.TaskInterop) != ExperimentalFeature.None)
  68. {
  69. if (value is Task task)
  70. {
  71. result = JsValue.ConvertAwaitableToPromise(engine, task);
  72. return result is not null;
  73. }
  74. #if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
  75. if (value is ValueTask valueTask)
  76. {
  77. result = JsValue.ConvertAwaitableToPromise(engine, valueTask);
  78. return result is not null;
  79. }
  80. #endif
  81. }
  82. #if NET8_0_OR_GREATER
  83. if (value is System.Text.Json.Nodes.JsonValue jsonValue)
  84. {
  85. result = ConvertSystemTextJsonValue(engine, jsonValue);
  86. return result is not null;
  87. }
  88. #endif
  89. var t = value.GetType();
  90. if (!engine.Options.Interop.AllowSystemReflection
  91. && t.Namespace?.StartsWith("System.Reflection", StringComparison.Ordinal) == true)
  92. {
  93. const string Message = "Cannot access System.Reflection namespace, check Engine's interop options";
  94. ExceptionHelper.ThrowInvalidOperationException(Message);
  95. }
  96. if (t.IsEnum)
  97. {
  98. var ut = Enum.GetUnderlyingType(t);
  99. if (ut == typeof(ulong))
  100. {
  101. result = JsNumber.Create(Convert.ToDouble(value, CultureInfo.InvariantCulture));
  102. }
  103. else
  104. {
  105. if (ut == typeof(uint) || ut == typeof(long))
  106. {
  107. result = JsNumber.Create(Convert.ToInt64(value, CultureInfo.InvariantCulture));
  108. }
  109. else
  110. {
  111. result = JsNumber.Create(Convert.ToInt32(value, CultureInfo.InvariantCulture));
  112. }
  113. }
  114. }
  115. else
  116. {
  117. // check global cache, have we already wrapped the value?
  118. if (engine._objectWrapperCache?.TryGetValue(value, out var cached) == true)
  119. {
  120. result = cached;
  121. }
  122. else
  123. {
  124. var wrapped = engine.Options.Interop.WrapObjectHandler.Invoke(engine, value, type);
  125. if (ReferenceEquals(wrapped?.GetPrototypeOf(), engine.Realm.Intrinsics.Object.PrototypeObject)
  126. && engine._typeReferences?.TryGetValue(t, out var typeReference) == true)
  127. {
  128. wrapped.SetPrototypeOf(typeReference);
  129. }
  130. result = wrapped;
  131. if (engine.Options.Interop.TrackObjectWrapperIdentity && wrapped is not null)
  132. {
  133. engine._objectWrapperCache ??= new ConditionalWeakTable<object, ObjectInstance>();
  134. engine._objectWrapperCache.Add(value, wrapped);
  135. }
  136. }
  137. }
  138. // if no known type could be guessed, use the default of wrapping using using ObjectWrapper.
  139. }
  140. return result is not null;
  141. }
  142. #if NET8_0_OR_GREATER
  143. private static JsValue? ConvertSystemTextJsonValue(Engine engine, System.Text.Json.Nodes.JsonValue value)
  144. {
  145. return value.GetValueKind() switch
  146. {
  147. System.Text.Json.JsonValueKind.Object => JsValue.FromObject(engine, value),
  148. System.Text.Json.JsonValueKind.Array => JsValue.FromObject(engine, value),
  149. System.Text.Json.JsonValueKind.String => JsString.Create(value.ToString()),
  150. #pragma warning disable IL2026, IL3050
  151. System.Text.Json.JsonValueKind.Number => value.TryGetValue<int>(out var intValue) ? JsNumber.Create(intValue) : System.Text.Json.JsonSerializer.Deserialize<double>(value),
  152. #pragma warning restore IL2026, IL3050
  153. System.Text.Json.JsonValueKind.True => JsBoolean.True,
  154. System.Text.Json.JsonValueKind.False => JsBoolean.False,
  155. System.Text.Json.JsonValueKind.Undefined => JsValue.Undefined,
  156. System.Text.Json.JsonValueKind.Null => JsValue.Null,
  157. _ => null
  158. };
  159. }
  160. #endif
  161. private static bool TryConvertConvertible(Engine engine, IConvertible convertible, [NotNullWhen(true)] out JsValue? result)
  162. {
  163. result = convertible.GetTypeCode() switch
  164. {
  165. TypeCode.Boolean => convertible.ToBoolean(engine.Options.Culture) ? JsBoolean.True : JsBoolean.False,
  166. TypeCode.Byte => JsNumber.Create(convertible.ToByte(engine.Options.Culture)),
  167. TypeCode.Char => JsString.Create(convertible.ToChar(engine.Options.Culture)),
  168. TypeCode.Double => JsNumber.Create(convertible.ToDouble(engine.Options.Culture)),
  169. TypeCode.SByte => JsNumber.Create(convertible.ToSByte(engine.Options.Culture)),
  170. TypeCode.Int16 => JsNumber.Create(convertible.ToInt16(engine.Options.Culture)),
  171. TypeCode.Int32 => JsNumber.Create(convertible.ToInt32(engine.Options.Culture)),
  172. TypeCode.UInt16 => JsNumber.Create(convertible.ToUInt16(engine.Options.Culture)),
  173. TypeCode.Int64 => JsNumber.Create(convertible.ToInt64(engine.Options.Culture)),
  174. TypeCode.Single => JsNumber.Create(convertible.ToSingle(engine.Options.Culture)),
  175. TypeCode.String => JsString.Create(convertible.ToString(engine.Options.Culture)),
  176. TypeCode.UInt32 => JsNumber.Create(convertible.ToUInt32(engine.Options.Culture)),
  177. TypeCode.UInt64 => JsNumber.Create(convertible.ToUInt64(engine.Options.Culture)),
  178. TypeCode.DateTime => engine.Realm.Intrinsics.Date.Construct(convertible.ToDateTime(engine.Options.Culture)),
  179. TypeCode.Decimal => JsNumber.Create(convertible.ToDecimal(engine.Options.Culture)),
  180. TypeCode.DBNull => JsValue.Null,
  181. TypeCode.Empty => JsValue.Null,
  182. _ => null
  183. };
  184. return result is not null;
  185. }
  186. private static JsArray ConvertArray(Engine e, object v)
  187. {
  188. var array = (Array) v;
  189. var arrayLength = (uint) array.Length;
  190. var values = new JsValue[arrayLength];
  191. for (uint i = 0; i < arrayLength; ++i)
  192. {
  193. values[i] = JsValue.FromObject(e, array.GetValue(i));
  194. }
  195. return new JsArray(e, values);
  196. }
  197. }
  198. }