Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contextify: use offset/length from Uint8Array #4947

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,11 @@ class ContextifyScript : public BaseObject {

ScriptCompiler::CachedData* cached_data = nullptr;
if (!cached_data_buf.IsEmpty()) {
ArrayBuffer::Contents contents =
cached_data_buf.ToLocalChecked()->Buffer()->GetContents();
Local<Uint8Array> ui8 = cached_data_buf.ToLocalChecked();
ArrayBuffer::Contents contents = ui8->Buffer()->GetContents();
cached_data = new ScriptCompiler::CachedData(
static_cast<uint8_t*>(contents.Data()), contents.ByteLength());
static_cast<uint8_t*>(contents.Data()) + ui8->ByteOffset(),
ui8->ByteLength());
}

ScriptOrigin origin(filename, lineOffset, columnOffset);
Expand Down
66 changes: 47 additions & 19 deletions test/parallel/test-vm-cached-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,58 @@ const assert = require('assert');
const vm = require('vm');
const Buffer = require('buffer').Buffer;

const originalSource = '(function bcd() { return \'original\'; })';
function getSource(tag) {
return '(function ' + tag + '() { return \'' + tag + '\'; })';
Copy link
Member

Choose a reason for hiding this comment

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

Template string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack.

}

// It should produce code cache
const original = new vm.Script(originalSource, {
produceCachedData: true
});
assert(original.cachedData instanceof Buffer);
function produce(source) {
const script = new vm.Script(source, {
produceCachedData: true
});
assert(script.cachedData instanceof Buffer);

assert.equal(original.runInThisContext()(), 'original');
return script.cachedData;
}

// It should consume code cache
const success = new vm.Script(originalSource, {
cachedData: original.cachedData
});
assert(!success.cachedDataRejected);
function testProduceConsume() {
const source = getSource('original');

assert.equal(success.runInThisContext()(), 'original');
const data = produce(source);

// It should reject invalid code cache
const reject = new vm.Script('(function abc() { return \'invalid\'; })', {
cachedData: original.cachedData
});
assert(reject.cachedDataRejected);
assert.equal(reject.runInThisContext()(), 'invalid');
// It should consume code cache
const script = new vm.Script(source, {
cachedData: data
});
assert(!script.cachedDataRejected);
assert.equal(script.runInThisContext()(), 'original');
}
testProduceConsume();

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);
assert.equal(script.runInThisContext()(), 'invalid_1');
}
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();

// It should throw on non-Buffer cachedData
assert.throws(() => {
Expand Down