From 4fc05ac7e1a8bf373ef3f0cc30eab4d48571f868 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 aeb4a6a4475d42..40b48e8bef2dbc 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -993,6 +993,14 @@ because it also made sense to interpret the value as the number of bytes read by the engine, but is inconsistent with other streams in Node.js that expose values under these names. + +### 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 @@ -1039,6 +1047,7 @@ expose values under these names. [`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])