ClrFunctionInstance.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. }
  19. public ClrFunctionInstance(Engine engine, Func<JsValue, JsValue[], JsValue> func)
  20. : this(engine, func, 0)
  21. {
  22. }
  23. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  24. {
  25. try
  26. {
  27. var result = _func(thisObject, arguments);
  28. return result;
  29. }
  30. catch (InvalidCastException)
  31. {
  32. throw new JavaScriptException(Engine.TypeError);
  33. }
  34. }
  35. }
  36. }