compile.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.compile = void 0;
  4. const util_1 = require("util");
  5. const degenerator_1 = require("./degenerator");
  6. function compile(qjs, code, returnName, options = {}) {
  7. const compiled = (0, degenerator_1.degenerator)(code, options.names ?? []);
  8. const vm = qjs.newContext();
  9. // Add functions to global
  10. if (options.sandbox) {
  11. for (const [name, value] of Object.entries(options.sandbox)) {
  12. if (typeof value !== 'function') {
  13. throw new Error(`Expected a "function" for sandbox property \`${name}\`, but got "${typeof value}"`);
  14. }
  15. const fnHandle = vm.newFunction(name, (...args) => {
  16. const result = value(...args.map((arg) => quickJSHandleToHost(vm, arg)));
  17. vm.runtime.executePendingJobs();
  18. return hostToQuickJSHandle(vm, result);
  19. });
  20. fnHandle.consume((handle) => vm.setProp(vm.global, name, handle));
  21. }
  22. }
  23. const fnResult = vm.evalCode(`${compiled};${returnName}`, options.filename);
  24. const fn = vm.unwrapResult(fnResult);
  25. const t = vm.typeof(fn);
  26. if (t !== 'function') {
  27. throw new Error(`Expected a "function" named \`${returnName}\` to be defined, but got "${t}"`);
  28. }
  29. const r = async function (...args) {
  30. let promiseHandle;
  31. let resolvedHandle;
  32. try {
  33. const result = vm.callFunction(fn, vm.undefined, ...args.map((arg) => hostToQuickJSHandle(vm, arg)));
  34. promiseHandle = vm.unwrapResult(result);
  35. const resolvedResultP = vm.resolvePromise(promiseHandle);
  36. vm.runtime.executePendingJobs();
  37. const resolvedResult = await resolvedResultP;
  38. resolvedHandle = vm.unwrapResult(resolvedResult);
  39. return quickJSHandleToHost(vm, resolvedHandle);
  40. }
  41. catch (err) {
  42. if (err && typeof err === 'object' && 'cause' in err && err.cause) {
  43. if (typeof err.cause === 'object' &&
  44. 'stack' in err.cause &&
  45. 'name' in err.cause &&
  46. 'message' in err.cause &&
  47. typeof err.cause.stack === 'string' &&
  48. typeof err.cause.name === 'string' &&
  49. typeof err.cause.message === 'string') {
  50. // QuickJS Error `stack` does not include the name +
  51. // message, so patch those in to behave more like V8
  52. err.cause.stack = `${err.cause.name}: ${err.cause.message}\n${err.cause.stack}`;
  53. }
  54. throw err.cause;
  55. }
  56. throw err;
  57. }
  58. finally {
  59. promiseHandle?.dispose();
  60. resolvedHandle?.dispose();
  61. }
  62. };
  63. Object.defineProperty(r, 'toString', {
  64. value: () => compiled,
  65. enumerable: false,
  66. });
  67. return r;
  68. }
  69. exports.compile = compile;
  70. function quickJSHandleToHost(vm, val) {
  71. return vm.dump(val);
  72. }
  73. function hostToQuickJSHandle(vm, val) {
  74. if (typeof val === 'undefined') {
  75. return vm.undefined;
  76. }
  77. else if (val === null) {
  78. return vm.null;
  79. }
  80. else if (typeof val === 'string') {
  81. return vm.newString(val);
  82. }
  83. else if (typeof val === 'number') {
  84. return vm.newNumber(val);
  85. }
  86. else if (typeof val === 'bigint') {
  87. return vm.newBigInt(val);
  88. }
  89. else if (typeof val === 'boolean') {
  90. return val ? vm.true : vm.false;
  91. }
  92. else if (util_1.types.isPromise(val)) {
  93. const promise = vm.newPromise();
  94. promise.settled.then(vm.runtime.executePendingJobs);
  95. val.then((r) => {
  96. promise.resolve(hostToQuickJSHandle(vm, r));
  97. }, (err) => {
  98. promise.reject(hostToQuickJSHandle(vm, err));
  99. });
  100. return promise.handle;
  101. }
  102. else if (util_1.types.isNativeError(val)) {
  103. return vm.newError(val);
  104. }
  105. throw new Error(`Unsupported value: ${val}`);
  106. }
  107. //# sourceMappingURL=compile.js.map