-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make CLR helper functions as member of
ClrHelper
object that be inj…
…ected into Engine. Misc changes.
- Loading branch information
Showing
10 changed files
with
338 additions
and
341 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,85 @@ | ||
using Jint.Native; | ||
namespace Jint.Runtime.Interop; | ||
|
||
namespace Jint.Runtime.Interop | ||
using Jint.Native; | ||
|
||
public class ClrHelper | ||
{ | ||
public static class ClrHelper | ||
private readonly InteropOptions _interopOptions; | ||
|
||
internal ClrHelper(InteropOptions interopOptions) | ||
{ | ||
public static JsValue ClrUnwrap(JsValue thisObject, JsValue[] arguments) | ||
{ | ||
var arg = arguments.At(0); | ||
if (arg is ObjectWrapper obj) | ||
{ | ||
return new ObjectWrapper(obj.Engine, obj.Target); | ||
} | ||
return arg; | ||
} | ||
_interopOptions = interopOptions; | ||
} | ||
|
||
public static JsValue ClrWrap(JsValue thisObject, JsValue[] arguments) | ||
{ | ||
var arg = arguments.At(0); | ||
if (arg is ObjectWrapper obj && arguments.At(1) is TypeReference type) | ||
{ | ||
return new ObjectWrapper(obj.Engine, obj.Target, type.ReferenceType); | ||
} | ||
return arg; | ||
} | ||
/// <summary> | ||
/// Call JsValue.ToString(), mainly for NamespaceReference. | ||
/// </summary> | ||
public JsValue ToString(JsValue value) | ||
{ | ||
return value.ToString(); | ||
} | ||
|
||
public static JsValue ClrType(JsValue thisObject, JsValue[] arguments) | ||
{ | ||
var arg = arguments.At(0); | ||
if (arg is ObjectWrapper obj) | ||
{ | ||
return TypeReference.CreateTypeReference(obj.Engine, obj.ClrType); | ||
} | ||
return JsValue.Undefined; | ||
} | ||
/// <summary> | ||
/// Cast `obj as ISomeInterface` to `obj` | ||
/// </summary> | ||
public JsValue Unwrap(ObjectWrapper obj) | ||
{ | ||
return new ObjectWrapper(obj.Engine, obj.Target); | ||
} | ||
|
||
public static JsValue ClrToString(JsValue thisObject, JsValue[] arguments) | ||
/// <summary> | ||
/// Cast `obj` to `obj as ISomeInterface` | ||
/// </summary> | ||
public JsValue Wrap(ObjectWrapper obj, TypeReference type) | ||
{ | ||
if (!type.ReferenceType.IsInstanceOfType(obj.Target)) | ||
{ | ||
return arguments.At(0).ToString(); | ||
ExceptionHelper.ThrowTypeError(type.Engine.Realm, "Argument obj must be an instance of type"); | ||
} | ||
return new ObjectWrapper(obj.Engine, obj.Target, type.ReferenceType); | ||
} | ||
|
||
/// <summary> | ||
/// Get `TypeReference(ISomeInterface)` from `obj as ISomeInterface` | ||
/// </summary> | ||
public JsValue TypeOf(ObjectWrapper obj) | ||
{ | ||
MustAllowGetType(); | ||
return TypeReference.CreateTypeReference(obj.Engine, obj.ClrType); | ||
} | ||
|
||
/// <summary> | ||
/// Cast `TypeReference(SomeClass)` to `ObjectWrapper(SomeClass)` | ||
/// </summary> | ||
public JsValue TypeToObject(TypeReference type) | ||
{ | ||
MustAllowGetType(); | ||
var engine = type.Engine; | ||
return engine.Options.Interop.WrapObjectHandler.Invoke(engine, type.ReferenceType, null) ?? JsValue.Undefined; | ||
} | ||
|
||
public static JsValue ClrTypeToObject(JsValue thisObject, JsValue[] arguments) | ||
/// <summary> | ||
/// Cast `ObjectWrapper(SomeClass)` to `TypeReference(SomeClass)` | ||
/// </summary> | ||
public JsValue ObjectToType(ObjectWrapper obj) | ||
{ | ||
MustAllowGetType(); | ||
if (obj.Target is Type t) | ||
{ | ||
return TypeReference.CreateTypeReference(obj.Engine, t); | ||
} | ||
else | ||
{ | ||
var arg = arguments.At(0); | ||
if (arg is TypeReference tr) | ||
{ | ||
var engine = tr.Engine; | ||
return engine.Options.Interop.WrapObjectHandler.Invoke(engine, tr.ReferenceType, null) ?? JsValue.Undefined; | ||
} | ||
return JsValue.Undefined; | ||
ExceptionHelper.ThrowArgumentException("Must be an ObjectWrapper of Type", "obj"); | ||
} | ||
return JsValue.Undefined; | ||
} | ||
|
||
public static JsValue ClrObjectToType(JsValue thisObject, JsValue[] arguments) | ||
private void MustAllowGetType() | ||
{ | ||
if (!_interopOptions.AllowGetType) | ||
{ | ||
var arg = arguments.At(0); | ||
if (arg is ObjectWrapper obj && obj.Target is Type t) | ||
{ | ||
return TypeReference.CreateTypeReference(obj.Engine, t); | ||
} | ||
return JsValue.Undefined; | ||
ExceptionHelper.ThrowInvalidOperationException("Invalid when Engine.Options.Interop.AllowGetType == false"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
namespace Jint.Runtime.Interop.Reflection; | ||
|
||
namespace Jint.Runtime.Interop.Reflection | ||
internal sealed class NestedTypeAccessor : ReflectionAccessor | ||
{ | ||
internal sealed class NestedTypeAccessor : ReflectionAccessor | ||
private readonly TypeReference _typeReference; | ||
|
||
public NestedTypeAccessor(TypeReference typeReference, string name) : base(typeof(Type), name) | ||
{ | ||
private readonly TypeReference _typeReference; | ||
public NestedTypeAccessor(TypeReference typeReference, string name) : base(typeof(Type), name) | ||
{ | ||
_typeReference = typeReference; | ||
} | ||
_typeReference = typeReference; | ||
} | ||
|
||
public override bool Writable => false; | ||
public override bool Writable => false; | ||
|
||
protected override object? DoGetValue(object target) | ||
{ | ||
return _typeReference; | ||
} | ||
protected override object? DoGetValue(object target) | ||
{ | ||
return _typeReference; | ||
} | ||
|
||
protected override void DoSetValue(object target, object? value) | ||
{ | ||
} | ||
protected override void DoSetValue(object target, object? value) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters