|
@@ -69,67 +69,80 @@ namespace Jint
|
|
|
if (value is Delegate d)
|
|
|
{
|
|
|
result = new DelegateWrapper(engine, d);
|
|
|
+ return result is not null;
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ if (value is Task task)
|
|
|
+ {
|
|
|
+ result = JsValue.ConvertAwaitableToPromise(engine, task);
|
|
|
+ return result is not null;
|
|
|
+ }
|
|
|
+
|
|
|
+#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
|
|
|
+ if (value is ValueTask valueTask)
|
|
|
+ {
|
|
|
+ result = JsValue.ConvertAwaitableToPromise(engine, valueTask);
|
|
|
+ return result is not null;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ var t = value.GetType();
|
|
|
+
|
|
|
+ if (!engine.Options.Interop.AllowSystemReflection
|
|
|
+ && t.Namespace?.StartsWith("System.Reflection", StringComparison.Ordinal) == true)
|
|
|
+ {
|
|
|
+ const string Message = "Cannot access System.Reflection namespace, check Engine's interop options";
|
|
|
+ ExceptionHelper.ThrowInvalidOperationException(Message);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (t.IsEnum)
|
|
|
{
|
|
|
- var t = value.GetType();
|
|
|
+ var ut = Enum.GetUnderlyingType(t);
|
|
|
|
|
|
- if (!engine.Options.Interop.AllowSystemReflection
|
|
|
- && t.Namespace?.StartsWith("System.Reflection", StringComparison.Ordinal) == true)
|
|
|
+ if (ut == typeof(ulong))
|
|
|
{
|
|
|
- const string Message = "Cannot access System.Reflection namespace, check Engine's interop options";
|
|
|
- ExceptionHelper.ThrowInvalidOperationException(Message);
|
|
|
+ result = JsNumber.Create(Convert.ToDouble(value, CultureInfo.InvariantCulture));
|
|
|
}
|
|
|
-
|
|
|
- if (t.IsEnum)
|
|
|
+ else
|
|
|
{
|
|
|
- var ut = Enum.GetUnderlyingType(t);
|
|
|
-
|
|
|
- if (ut == typeof(ulong))
|
|
|
+ if (ut == typeof(uint) || ut == typeof(long))
|
|
|
{
|
|
|
- result = JsNumber.Create(Convert.ToDouble(value, CultureInfo.InvariantCulture));
|
|
|
+ result = JsNumber.Create(Convert.ToInt64(value, CultureInfo.InvariantCulture));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (ut == typeof(uint) || ut == typeof(long))
|
|
|
- {
|
|
|
- result = JsNumber.Create(Convert.ToInt64(value, CultureInfo.InvariantCulture));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- result = JsNumber.Create(Convert.ToInt32(value, CultureInfo.InvariantCulture));
|
|
|
- }
|
|
|
+ result = JsNumber.Create(Convert.ToInt32(value, CultureInfo.InvariantCulture));
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // check global cache, have we already wrapped the value?
|
|
|
+ if (engine._objectWrapperCache?.TryGetValue(value, out var cached) == true)
|
|
|
+ {
|
|
|
+ result = cached;
|
|
|
+ }
|
|
|
else
|
|
|
{
|
|
|
- // check global cache, have we already wrapped the value?
|
|
|
- if (engine._objectWrapperCache?.TryGetValue(value, out var cached) == true)
|
|
|
+ var wrapped = engine.Options.Interop.WrapObjectHandler.Invoke(engine, value, type);
|
|
|
+
|
|
|
+ if (ReferenceEquals(wrapped?.GetPrototypeOf(), engine.Realm.Intrinsics.Object.PrototypeObject)
|
|
|
+ && engine._typeReferences?.TryGetValue(t, out var typeReference) == true)
|
|
|
{
|
|
|
- result = cached;
|
|
|
+ wrapped.SetPrototypeOf(typeReference);
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- var wrapped = engine.Options.Interop.WrapObjectHandler.Invoke(engine, value, type);
|
|
|
-
|
|
|
- if (ReferenceEquals(wrapped?.GetPrototypeOf(), engine.Realm.Intrinsics.Object.PrototypeObject)
|
|
|
- && engine._typeReferences?.TryGetValue(t, out var typeReference) == true)
|
|
|
- {
|
|
|
- wrapped.SetPrototypeOf(typeReference);
|
|
|
- }
|
|
|
|
|
|
- result = wrapped;
|
|
|
+ result = wrapped;
|
|
|
|
|
|
- if (engine.Options.Interop.TrackObjectWrapperIdentity && wrapped is not null)
|
|
|
- {
|
|
|
- engine._objectWrapperCache ??= new ConditionalWeakTable<object, ObjectInstance>();
|
|
|
- engine._objectWrapperCache.Add(value, wrapped);
|
|
|
- }
|
|
|
+ if (engine.Options.Interop.TrackObjectWrapperIdentity && wrapped is not null)
|
|
|
+ {
|
|
|
+ engine._objectWrapperCache ??= new ConditionalWeakTable<object, ObjectInstance>();
|
|
|
+ engine._objectWrapperCache.Add(value, wrapped);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // if no known type could be guessed, use the default of wrapping using using ObjectWrapper.
|
|
|
}
|
|
|
+
|
|
|
+ // if no known type could be guessed, use the default of wrapping using using ObjectWrapper.
|
|
|
}
|
|
|
|
|
|
return result is not null;
|