using System;
using Jint.Native;
using Jint.Native.Function;
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;
FastAddProperty("length", length, false, false, false);
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)
{
throw new JavaScriptException(Engine.TypeError);
}
}
}
}