2016-01-20 19:34:19 -05:00
|
|
|
'use strict';
|
2018-03-16 15:23:39 +01:00
|
|
|
const common = require('../common');
|
2016-01-20 19:34:19 -05:00
|
|
|
const assert = require('assert');
|
|
|
|
const vm = require('vm');
|
2016-04-19 11:42:26 -04:00
|
|
|
const spawnSync = require('child_process').spawnSync;
|
2016-01-20 19:34:19 -05:00
|
|
|
|
2016-01-29 03:38:14 -05:00
|
|
|
function getSource(tag) {
|
2016-10-15 12:16:42 -07:00
|
|
|
return `(function ${tag}() { return '${tag}'; })`;
|
2016-01-29 03:38:14 -05:00
|
|
|
}
|
2016-01-20 19:34:19 -05:00
|
|
|
|
2016-04-19 11:42:26 -04:00
|
|
|
function produce(source, count) {
|
|
|
|
if (!count)
|
|
|
|
count = 1;
|
|
|
|
|
|
|
|
const out = spawnSync(process.execPath, [ '-e', `
|
2016-04-20 16:09:04 -07:00
|
|
|
'use strict';
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
|
|
|
const vm = require('vm');
|
2016-04-19 11:42:26 -04:00
|
|
|
|
2016-04-20 16:09:04 -07:00
|
|
|
var data;
|
2016-04-19 11:42:26 -04:00
|
|
|
for (var i = 0; i < ${count}; i++) {
|
|
|
|
var script = new vm.Script(process.argv[1], {
|
|
|
|
produceCachedData: true
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);
|
|
|
|
|
|
|
|
if (script.cachedDataProduced)
|
|
|
|
data = script.cachedData.toString('base64');
|
|
|
|
}
|
|
|
|
console.log(data);
|
|
|
|
`, source]);
|
|
|
|
|
2017-04-28 04:06:42 +03:00
|
|
|
assert.strictEqual(out.status, 0, String(out.stderr));
|
2016-01-20 19:34:19 -05:00
|
|
|
|
2016-04-19 11:42:26 -04:00
|
|
|
return Buffer.from(out.stdout.toString(), 'base64');
|
2016-01-29 03:38:14 -05:00
|
|
|
}
|
2016-01-20 19:34:19 -05:00
|
|
|
|
2016-01-29 03:38:14 -05:00
|
|
|
function testProduceConsume() {
|
|
|
|
const source = getSource('original');
|
2016-01-20 19:34:19 -05:00
|
|
|
|
2016-01-29 03:38:14 -05:00
|
|
|
const data = produce(source);
|
2016-01-20 19:34:19 -05:00
|
|
|
|
2018-09-18 01:28:41 -04:00
|
|
|
for (const cachedData of common.getArrayBufferViews(data)) {
|
|
|
|
// It should consume code cache
|
|
|
|
const script = new vm.Script(source, {
|
|
|
|
cachedData
|
|
|
|
});
|
|
|
|
assert(!script.cachedDataRejected);
|
|
|
|
assert.strictEqual(script.runInThisContext()(), 'original');
|
|
|
|
}
|
2016-01-29 03:38:14 -05:00
|
|
|
}
|
|
|
|
testProduceConsume();
|
|
|
|
|
2016-02-20 20:44:06 -06:00
|
|
|
function testProduceMultiple() {
|
|
|
|
const source = getSource('original');
|
|
|
|
|
2016-04-19 11:42:26 -04:00
|
|
|
produce(source, 3);
|
2016-02-20 20:44:06 -06:00
|
|
|
}
|
|
|
|
testProduceMultiple();
|
|
|
|
|
2016-01-29 03:38:14 -05:00
|
|
|
function testRejectInvalid() {
|
|
|
|
const source = getSource('invalid');
|
|
|
|
|
|
|
|
const data = produce(source);
|
|
|
|
|
|
|
|
// It should reject invalid code cache
|
|
|
|
const script = new vm.Script(getSource('invalid_1'), {
|
|
|
|
cachedData: data
|
|
|
|
});
|
|
|
|
assert(script.cachedDataRejected);
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(script.runInThisContext()(), 'invalid_1');
|
2016-01-29 03:38:14 -05:00
|
|
|
}
|
|
|
|
testRejectInvalid();
|
|
|
|
|
|
|
|
function testRejectSlice() {
|
|
|
|
const source = getSource('slice');
|
|
|
|
|
|
|
|
const data = produce(source).slice(4);
|
|
|
|
|
|
|
|
const script = new vm.Script(source, {
|
|
|
|
cachedData: data
|
|
|
|
});
|
|
|
|
assert(script.cachedDataRejected);
|
|
|
|
}
|
|
|
|
testRejectSlice();
|
2016-01-20 19:34:19 -05:00
|
|
|
|
|
|
|
// It should throw on non-Buffer cachedData
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2016-01-20 19:34:19 -05:00
|
|
|
new vm.Script('function abc() {}', {
|
|
|
|
cachedData: 'ohai'
|
|
|
|
});
|
2018-03-16 15:23:39 +01:00
|
|
|
}, {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2019-09-23 08:17:25 +02:00
|
|
|
message: /must be an instance of Buffer, TypedArray, or DataView/
|
2018-03-16 15:23:39 +01:00
|
|
|
});
|