* Run the code in VM. * * @public * @param {(string|VMScript)} code - Code to run. * @param {(string|Object)} [options] - Options map or filename. * @param {string} [options.filename="vm.js"] - Filename that shows up in any stack traces produced from this script. * This is only used
(code, options)
| 499 | * @throws {*} If the script execution terminated with an exception it is propagated. |
| 500 | */ |
| 501 | run(code, options) { |
| 502 | let script; |
| 503 | let filename; |
| 504 | |
| 505 | if (typeof options === 'object') { |
| 506 | filename = options.filename; |
| 507 | } else { |
| 508 | filename = options; |
| 509 | } |
| 510 | |
| 511 | if (code instanceof VMScript) { |
| 512 | script = code._compileVM(); |
| 513 | checkAsync(this._allowAsync || !code._hasAsync); |
| 514 | } else { |
| 515 | const useFileName = filename || 'vm.js'; |
| 516 | let scriptCode = this._compiler(code, useFileName); |
| 517 | const ret = transformer(null, scriptCode, false, false, useFileName); |
| 518 | scriptCode = ret.code; |
| 519 | checkAsync(this._allowAsync || !ret.hasAsync); |
| 520 | // Compile the script here so that we don't need to create a instance of VMScript. |
| 521 | script = new Script(scriptCode, { |
| 522 | __proto__: null, |
| 523 | filename: useFileName, |
| 524 | displayErrors: false, |
| 525 | }); |
| 526 | } |
| 527 | |
| 528 | if (!this.timeout) { |
| 529 | return this._runScript(script); |
| 530 | } |
| 531 | |
| 532 | return doWithTimeout(() => { |
| 533 | return this._runScript(script); |
| 534 | }, this.timeout); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Run the code in VM. |
no test coverage detected