BindFunctionInstance.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Linq;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. namespace Jint.Native.Function
  6. {
  7. public class BindFunctionInstance : FunctionInstance, IConstructor
  8. {
  9. public BindFunctionInstance(Engine engine) : base(engine, new string[0], null, false)
  10. {
  11. }
  12. public object TargetFunction { get; set; }
  13. public object BoundThis { get; set; }
  14. public object[] BoundArgs { get; set; }
  15. public override object Call(object thisObject, object[] arguments)
  16. {
  17. var f = TargetFunction as FunctionInstance;
  18. if (f == null)
  19. {
  20. throw new JavaScriptException(Engine.TypeError);
  21. }
  22. return f.Call(BoundThis, BoundArgs.Union(arguments).ToArray());
  23. }
  24. public ObjectInstance Construct(object[] arguments)
  25. {
  26. var target = TargetFunction as IConstructor;
  27. if (target == null)
  28. {
  29. throw new JavaScriptException(Engine.TypeError);
  30. }
  31. return target.Construct(BoundArgs.Union(arguments).ToArray());
  32. }
  33. public override bool HasInstance(object v)
  34. {
  35. var f = TargetFunction as FunctionInstance;
  36. if (f == null)
  37. {
  38. throw new JavaScriptException(Engine.TypeError);
  39. }
  40. return f.HasInstance(v);
  41. }
  42. }
  43. }