ClrFunctionInstance.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using Jint.Native;
  3. using Jint.Native.Function;
  4. namespace Jint.Runtime.Interop
  5. {
  6. /// <summary>
  7. /// Wraps a Clr method into a FunctionInstance
  8. /// </summary>
  9. public sealed class ClrFunctionInstance : FunctionInstance
  10. {
  11. private readonly Func<JsValue, JsValue[], JsValue> _func;
  12. public ClrFunctionInstance(Engine engine, Func<JsValue, JsValue[], JsValue> func, int length)
  13. : base(engine, null, null, false)
  14. {
  15. _func = func;
  16. Prototype = engine.Function.PrototypeObject;
  17. FastAddProperty("length", length, false, false, false);
  18. Extensible = true;
  19. }
  20. public ClrFunctionInstance(Engine engine, Func<JsValue, JsValue[], JsValue> func)
  21. : this(engine, func, 0)
  22. {
  23. }
  24. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  25. {
  26. try
  27. {
  28. var result = _func(thisObject, arguments);
  29. return result;
  30. }
  31. catch (InvalidCastException)
  32. {
  33. throw new JavaScriptException(Engine.TypeError);
  34. }
  35. }
  36. }
  37. }