DelegateWrapper.cs 814 B

12345678910111213141516171819202122232425262728
  1. using System;
  2. using System.Linq;
  3. using Jint.Native;
  4. using Jint.Native.Function;
  5. namespace Jint.Runtime.Interop
  6. {
  7. /// <summary>
  8. /// Represents a FunctionInstance wrapper around a CLR method. This is used by user to pass
  9. /// custom methods to the engine.
  10. /// </summary>
  11. public sealed class DelegateWrapper : FunctionInstance
  12. {
  13. private readonly Engine _engine;
  14. private readonly Delegate _d;
  15. public DelegateWrapper(Engine engine, Delegate d) : base(engine, null, null, false)
  16. {
  17. _engine = engine;
  18. _d = d;
  19. }
  20. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  21. {
  22. return JsValue.FromObject(Engine, _d.DynamicInvoke(arguments.Select(x => x.ToObject()).ToArray()));
  23. }
  24. }
  25. }