Function.js 1.2 KB

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