ClrFunctionInstance.cs 1.2 KB

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