forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm: introduce
cachedData
/produceCachedData
Introduce `cachedData`/`produceCachedData` options for `v8.Script`. Could be used to consume/produce V8's code cache for speeding up compilation of known code. PR-URL: nodejs#4777 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
Showing
4 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
const Buffer = require('buffer').Buffer; | ||
|
||
const originalSource = '(function bcd() { return \'original\'; })'; | ||
|
||
// It should produce code cache | ||
const original = new vm.Script(originalSource, { | ||
produceCachedData: true | ||
}); | ||
assert(original.cachedData instanceof Buffer); | ||
|
||
assert.equal(original.runInThisContext()(), 'original'); | ||
|
||
// It should consume code cache | ||
const success = new vm.Script(originalSource, { | ||
cachedData: original.cachedData | ||
}); | ||
assert(!success.cachedDataRejected); | ||
|
||
assert.equal(success.runInThisContext()(), 'original'); | ||
|
||
// 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 throw on non-Buffer cachedData | ||
assert.throws(() => { | ||
new vm.Script('function abc() {}', { | ||
cachedData: 'ohai' | ||
}); | ||
}); |