From 4f67c6f667fa98e25ba311d3459fc8e270407250 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Wed, 25 Apr 2018 12:22:20 -0500 Subject: [PATCH] vm: add Script.createCodeCache() PR-URL: https://github.com/nodejs/node/pull/20300 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: John-David Dalton Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Yang Guo --- doc/api/deprecations.md | 9 +++++++ doc/api/vm.md | 32 +++++++++++++++++++++++ src/node_contextify.cc | 26 ++++++++++++++++-- test/parallel/test-vm-createcacheddata.js | 22 ++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-vm-createcacheddata.js diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 0b4d4986e16e64..df6371056d1af2 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -1005,6 +1005,14 @@ accepted by the legacy `url.parse()` API. The mentioned APIs now use the WHATWG URL parser that requires strictly valid URLs. Passing an invalid URL is deprecated and support will be removed in the future. + +### DEP00XX: vm.Script cached data + +Type: Documentation-only + +The option `produceCachedData` has been deprecated. Use +[`script.createCachedData()`][] instead. + [`--pending-deprecation`]: cli.html#cli_pending_deprecation [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size [`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array @@ -1055,6 +1063,7 @@ deprecated and support will be removed in the future. [`process.env`]: process.html#process_process_env [`punycode`]: punycode.html [`require.extensions`]: modules.html#modules_require_extensions +[`script.createCachedData()`]: vm.html#vm_script_create_cached_data [`setInterval()`]: timers.html#timers_setinterval_callback_delay_args [`setTimeout()`]: timers.html#timers_settimeout_callback_delay_args [`tls.CryptoStream`]: tls.html#tls_class_cryptostream diff --git a/doc/api/vm.md b/doc/api/vm.md index f3b79199c1a1e9..e2ddd25e1cdeac 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -411,6 +411,10 @@ changes: pr-url: https://github.com/nodejs/node/pull/4777 description: The `cachedData` and `produceCachedData` options are supported now. + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/20300 + description: The `produceCachedData` is deprecated in favour of + `script.createCachedData()` --> * `code` {string} The JavaScript code to compile. @@ -431,11 +435,39 @@ changes: `cachedData` property of the returned `vm.Script` instance. The `cachedDataProduced` value will be set to either `true` or `false` depending on whether code cache data is produced successfully. + This option is deprecated in favor of `script.createCachedData`. Creating a new `vm.Script` object compiles `code` but does not run it. The compiled `vm.Script` can be run later multiple times. The `code` is not bound to any global object; rather, it is bound before each run, just for that run. +### script.createCachedData() + + +* Returns: {Buffer} + +Creates a code cache that can be used with the Script constructor's +`cachedData` option. Returns a Buffer. This method may be called at any +time and any number of times. + +```js +const script = new vm.Script(` +function add(a, b) { + return a + b; +} + +const x = add(1, 2); +`); + +const cacheWithoutX = script.createCachedData(); + +script.runInThisContext(); + +const cacheWithX = script.createCachedData(); +``` + ### script.runInContext(contextifiedSandbox[, options])