using System; using Jint.Native; using Jint.Native.Function; using Jint.Runtime.Descriptors; namespace Jint.Runtime.Interop { /// /// Wraps a Clr method into a FunctionInstance /// public sealed class ClrFunctionInstance : FunctionInstance, IEquatable { private readonly string _name; internal readonly Func _func; public ClrFunctionInstance( Engine engine, string name, Func func, int length = 0, PropertyFlag lengthFlags = PropertyFlag.AllForbidden) : base(engine, engine.Realm, name != null ? new JsString(name) : null) { _name = name; _func = func; _prototype = engine._originalIntrinsics.Function.PrototypeObject; _length = lengthFlags == PropertyFlag.AllForbidden ? PropertyDescriptor.AllForbiddenDescriptor.ForNumber(length) : new PropertyDescriptor(JsNumber.Create(length), lengthFlags); } public override JsValue Call(JsValue thisObject, JsValue[] arguments) => _func(thisObject, arguments); public override bool Equals(JsValue obj) { if (ReferenceEquals(null, obj)) { return false; } if (!(obj is ClrFunctionInstance s)) { return false; } return Equals(s); } public bool Equals(ClrFunctionInstance other) { if (ReferenceEquals(null, other)) { return false; } if (ReferenceEquals(this, other)) { return true; } if (_func == other._func) { return true; } return false; } public override string ToString() => "function " + _name + "() { [native code] }"; } }