You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. exports.convert = (value, { context = "The provided value" } = {}) => {
  5. if (typeof value !== "function") {
  6. throw new TypeError(context + " is not a function");
  7. }
  8. function invokeTheCallbackFunction(...args) {
  9. if (new.target !== undefined) {
  10. throw new Error("Internal error: invokeTheCallbackFunction is not a constructor");
  11. }
  12. const thisArg = utils.tryWrapperForImpl(this);
  13. let callResult;
  14. for (let i = 0; i < args.length; i++) {
  15. args[i] = utils.tryWrapperForImpl(args[i]);
  16. }
  17. callResult = Reflect.apply(value, thisArg, args);
  18. callResult = conversions["any"](callResult, { context: context });
  19. return callResult;
  20. }
  21. invokeTheCallbackFunction.construct = (...args) => {
  22. for (let i = 0; i < args.length; i++) {
  23. args[i] = utils.tryWrapperForImpl(args[i]);
  24. }
  25. let callResult = Reflect.construct(value, args);
  26. callResult = conversions["any"](callResult, { context: context });
  27. return callResult;
  28. };
  29. invokeTheCallbackFunction[utils.wrapperSymbol] = value;
  30. invokeTheCallbackFunction.objectReference = value;
  31. return invokeTheCallbackFunction;
  32. };