Skip to content

Commit 2331181

Browse files
BeniChenitargos
authored andcommitted
vm: allow cachedData to also be TypedArray|DataView
PR-URL: #22921 Refs: #1826 Refs: #22921 (comment) Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 84e5807 commit 2331181

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

doc/api/vm.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,10 @@ changes:
434434
in stack traces produced by this script.
435435
* `columnOffset` {number} Specifies the column number offset that is displayed
436436
in stack traces produced by this script.
437-
* `cachedData` {Buffer} Provides an optional `Buffer` with V8's code cache
438-
data for the supplied source. When supplied, the `cachedDataRejected` value
439-
will be set to either `true` or `false` depending on acceptance of the data
440-
by V8.
437+
* `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or
438+
`TypedArray`, or `DataView` with V8's code cache data for the supplied
439+
source. When supplied, the `cachedDataRejected` value will be set to
440+
either `true` or `false` depending on acceptance of the data by V8.
441441
* `produceCachedData` {boolean} When `true` and no `cachedData` is present, V8
442442
will attempt to produce code cache data for `code`. Upon success, a
443443
`Buffer` with V8's code cache data will be produced and stored in the
@@ -669,8 +669,9 @@ added: v10.10.0
669669
in stack traces produced by this script. **Default:** `0`.
670670
* `columnOffset` {number} Specifies the column number offset that is displayed
671671
in stack traces produced by this script. **Default:** `0`.
672-
* `cachedData` {Buffer} Provides an optional `Buffer` with V8's code cache
673-
data for the supplied source.
672+
* `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or
673+
`TypedArray`, or `DataView` with V8's code cache data for the supplied
674+
source.
674675
* `produceCachedData` {boolean} Specifies whether to produce new cache data.
675676
**Default:** `false`.
676677
* `parsingContext` {Object} The [contextified][] sandbox in which the said

lib/vm.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
ERR_INVALID_ARG_TYPE,
3333
ERR_VM_MODULE_NOT_MODULE,
3434
} = require('internal/errors').codes;
35-
const { isModuleNamespaceObject, isUint8Array } = require('util').types;
35+
const { isModuleNamespaceObject, isArrayBufferView } = require('util').types;
3636
const { validateInt32, validateUint32 } = require('internal/validators');
3737
const kParsingContext = Symbol('script parsing context');
3838

@@ -64,9 +64,12 @@ class Script extends ContextifyScript {
6464
}
6565
validateInt32(lineOffset, 'options.lineOffset');
6666
validateInt32(columnOffset, 'options.columnOffset');
67-
if (cachedData !== undefined && !isUint8Array(cachedData)) {
68-
throw new ERR_INVALID_ARG_TYPE('options.cachedData',
69-
['Buffer', 'Uint8Array'], cachedData);
67+
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
68+
throw new ERR_INVALID_ARG_TYPE(
69+
'options.cachedData',
70+
['Buffer', 'TypedArray', 'DataView'],
71+
cachedData
72+
);
7073
}
7174
if (typeof produceCachedData !== 'boolean') {
7275
throw new ERR_INVALID_ARG_TYPE('options.produceCachedData', 'boolean',
@@ -346,10 +349,10 @@ function compileFunction(code, params, options = {}) {
346349
}
347350
validateUint32(columnOffset, 'options.columnOffset');
348351
validateUint32(lineOffset, 'options.lineOffset');
349-
if (cachedData !== undefined && !isUint8Array(cachedData)) {
352+
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
350353
throw new ERR_INVALID_ARG_TYPE(
351354
'options.cachedData',
352-
'Uint8Array',
355+
['Buffer', 'TypedArray', 'DataView'],
353356
cachedData
354357
);
355358
}

src/node_contextify.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace contextify {
3333

3434
using v8::Array;
3535
using v8::ArrayBuffer;
36+
using v8::ArrayBufferView;
3637
using v8::Boolean;
3738
using v8::Context;
3839
using v8::EscapableHandleScope;
@@ -64,7 +65,6 @@ using v8::String;
6465
using v8::Symbol;
6566
using v8::TryCatch;
6667
using v8::Uint32;
67-
using v8::Uint8Array;
6868
using v8::UnboundScript;
6969
using v8::Value;
7070
using v8::WeakCallbackInfo;
@@ -629,7 +629,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
629629

630630
Local<Integer> line_offset;
631631
Local<Integer> column_offset;
632-
Local<Uint8Array> cached_data_buf;
632+
Local<ArrayBufferView> cached_data_buf;
633633
bool produce_cached_data = false;
634634
Local<Context> parsing_context = context;
635635

@@ -642,8 +642,8 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
642642
CHECK(args[3]->IsNumber());
643643
column_offset = args[3].As<Integer>();
644644
if (!args[4]->IsUndefined()) {
645-
CHECK(args[4]->IsUint8Array());
646-
cached_data_buf = args[4].As<Uint8Array>();
645+
CHECK(args[4]->IsArrayBufferView());
646+
cached_data_buf = args[4].As<ArrayBufferView>();
647647
}
648648
CHECK(args[5]->IsBoolean());
649649
produce_cached_data = args[5]->IsTrue();
@@ -994,10 +994,10 @@ void ContextifyContext::CompileFunction(
994994
Local<Integer> column_offset = args[3].As<Integer>();
995995

996996
// Argument 5: cached data (optional)
997-
Local<Uint8Array> cached_data_buf;
997+
Local<ArrayBufferView> cached_data_buf;
998998
if (!args[4]->IsUndefined()) {
999-
CHECK(args[4]->IsUint8Array());
1000-
cached_data_buf = args[4].As<Uint8Array>();
999+
CHECK(args[4]->IsArrayBufferView());
1000+
cached_data_buf = args[4].As<ArrayBufferView>();
10011001
}
10021002

10031003
// Argument 6: produce cache data

test/parallel/test-vm-basic.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,20 @@ const vm = require('vm');
178178
'filename': 'string',
179179
'columnOffset': 'number',
180180
'lineOffset': 'number',
181-
'cachedData': 'Uint8Array',
181+
'cachedData': 'Buffer, TypedArray, or DataView',
182182
'produceCachedData': 'boolean',
183183
};
184184

185185
for (const option in optionTypes) {
186+
const typeErrorMessage = `The "options.${option}" property must be ` +
187+
`${option === 'cachedData' ? 'one of' : 'of'} type`;
186188
common.expectsError(() => {
187189
vm.compileFunction('', undefined, { [option]: null });
188190
}, {
189191
type: TypeError,
190192
code: 'ERR_INVALID_ARG_TYPE',
191-
message: `The "options.${option}" property must be of type ` +
192-
`${optionTypes[option]}. Received type object`
193+
message: typeErrorMessage +
194+
` ${optionTypes[option]}. Received type object`
193195
});
194196
}
195197

test/parallel/test-vm-cached-data.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ function testProduceConsume() {
4141

4242
const data = produce(source);
4343

44-
// It should consume code cache
45-
const script = new vm.Script(source, {
46-
cachedData: data
47-
});
48-
assert(!script.cachedDataRejected);
49-
assert.strictEqual(script.runInThisContext()(), 'original');
44+
for (const cachedData of common.getArrayBufferViews(data)) {
45+
// It should consume code cache
46+
const script = new vm.Script(source, {
47+
cachedData
48+
});
49+
assert(!script.cachedDataRejected);
50+
assert.strictEqual(script.runInThisContext()(), 'original');
51+
}
5052
}
5153
testProduceConsume();
5254

@@ -91,5 +93,5 @@ common.expectsError(() => {
9193
}, {
9294
code: 'ERR_INVALID_ARG_TYPE',
9395
type: TypeError,
94-
message: /must be one of type Buffer or Uint8Array/
96+
message: /must be one of type Buffer, TypedArray, or DataView/
9597
});

0 commit comments

Comments
 (0)