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 { private readonly Func _func; public ClrFunctionInstance(Engine engine, Func func, int length) : base(engine, null, null, false) { _func = func; Prototype = engine.Function.PrototypeObject; SetOwnProperty("length", new PropertyDescriptor(length, PropertyFlag.AllForbidden)); Extensible = true; } public ClrFunctionInstance(Engine engine, Func func) : this(engine, func, 0) { } public override JsValue Call(JsValue thisObject, JsValue[] arguments) { try { var result = _func(thisObject, arguments); return result; } catch (InvalidCastException) { ExceptionHelper.ThrowTypeError(Engine); return null; } } } }