Skip to content

Commit

Permalink
vm: fix produceCachedData
Browse files Browse the repository at this point in the history
Fix segmentation faults when compiling the same code with
`produceCachedData` option. V8 ignores the option when the code is in
its compilation cache and does not return cached data. Added
`cachedDataProduced` property to `v8.Script` to denote whether the
cached data is produced successfully.

PR-URL: #5343
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
jray319 authored and rvagg committed Feb 21, 2016
1 parent 967cf97 commit 6c8378b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
9 changes: 6 additions & 3 deletions doc/api/vm.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ The options when creating a script are:
- `cachedData`: an optional `Buffer` with V8's code cache data for the supplied
source. When supplied `cachedDataRejected` value will be set to either
`true` or `false` depending on acceptance of the data by V8.
- `produceCachedData`: if `true` and no `cachedData` is present - a `Buffer`
with V8's code cache data will be produced and stored in `cachedData` property
of the returned `vm.Script` instance.
- `produceCachedData`: if `true` and no `cachedData` is present - V8 tries to
produce code cache data for `code`. Upon success, a `Buffer` with V8's code
cache data will be produced and stored in `cachedData` property of the
returned `vm.Script` instance. `cachedDataProduced` value will be set to
either `true` or `false` depending on whether code cache data is produced
successfully.

### script.runInContext(contextifiedSandbox[, options])

Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace node {
V(bytes_string, "bytes") \
V(bytes_parsed_string, "bytesParsed") \
V(cached_data_string, "cachedData") \
V(cached_data_produced_string, "cachedDataProduced") \
V(cached_data_rejected_string, "cachedDataRejected") \
V(callback_string, "callback") \
V(change_string, "change") \
Expand Down
16 changes: 11 additions & 5 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,17 @@ class ContextifyScript : public BaseObject {
Boolean::New(env->isolate(), source.GetCachedData()->rejected));
} else if (compile_options == ScriptCompiler::kProduceCodeCache) {
const ScriptCompiler::CachedData* cached_data = source.GetCachedData();
MaybeLocal<Object> buf = Buffer::Copy(
env,
reinterpret_cast<const char*>(cached_data->data),
cached_data->length);
args.This()->Set(env->cached_data_string(), buf.ToLocalChecked());
bool cached_data_produced = cached_data != nullptr;
if (cached_data_produced) {
MaybeLocal<Object> buf = Buffer::Copy(
env,
reinterpret_cast<const char*>(cached_data->data),
cached_data->length);
args.This()->Set(env->cached_data_string(), buf.ToLocalChecked());
}
args.This()->Set(
env->cached_data_produced_string(),
Boolean::New(env->isolate(), cached_data_produced));
}
}

Expand Down
11 changes: 10 additions & 1 deletion test/parallel/test-vm-cached-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function produce(source) {
const script = new vm.Script(source, {
produceCachedData: true
});
assert(script.cachedData instanceof Buffer);
assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);

return script.cachedData;
}
Expand All @@ -31,6 +31,15 @@ function testProduceConsume() {
}
testProduceConsume();

function testProduceMultiple() {
const source = getSource('original');

produce(source);
produce(source);
produce(source);
}
testProduceMultiple();

function testRejectInvalid() {
const source = getSource('invalid');

Expand Down

0 comments on commit 6c8378b

Please sign in to comment.