DelegateWrapper.cs 949 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using Jint.Native;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  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, ObjectInstance prototype) : base(engine, prototype, null, null)
  16. {
  17. _engine = engine;
  18. _d = d;
  19. }
  20. public override object Call(object thisObject, object[] arguments)
  21. {
  22. // initialize Return flag
  23. _engine.CurrentExecutionContext.Return = Undefined.Instance;
  24. _d.DynamicInvoke(arguments);
  25. return _engine.CurrentExecutionContext.Return;
  26. }
  27. }
  28. }