Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions test/parallel/test-vm-cached-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@
require('../common');
const assert = require('assert');
const vm = require('vm');
const spawnSync = require('child_process').spawnSync;
const Buffer = require('buffer').Buffer;

function getSource(tag) {
return `(function ${tag}() { return \'${tag}\'; })`;
}

function produce(source) {
const script = new vm.Script(source, {
produceCachedData: true
});
assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);
function produce(source, count) {
if (!count)
count = 1;

const out = spawnSync(process.execPath, [ '-p', `
var assert = require('assert');
var vm = require('vm');

for (var i = 0; i < ${count}; i++) {
var script = new vm.Script(process.argv[1], {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understanding, process.argv[1] === undefined here, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not, see source on line 32.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ node -e 'console.log(process.argv[1])' hello
hello

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bet you didn't know about this 😉

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed the third argument. Never mind.

produceCachedData: true
});

assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);

if (script.cachedDataProduced)
script.cachedData.toString('base64');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value isn't used. I think you intended to console.log or process.stdout.write it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is JS, the last statement will be printed when using -p argv.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, very subtle. I guess it's not wrong but it's not very obviously correct, either. I'd use -e and explicitly print it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, I agree.

}
`, source]);

assert.equal(out.status, 0, out.stderr + '');

return script.cachedData;
return new Buffer(out.stdout.toString(), 'base64');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer.from(out.stdout.toString(), 'base64')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

}

function testProduceConsume() {
Expand All @@ -34,9 +51,7 @@ testProduceConsume();
function testProduceMultiple() {
const source = getSource('original');

produce(source);
produce(source);
produce(source);
produce(source, 3);
}
testProduceMultiple();

Expand Down