summaryrefslogtreecommitdiff
path: root/lib/vm.js
diff options
context:
space:
mode:
authorTimothy Gu <timothygu99@gmail.com>2017-07-17 13:06:23 +0800
committerTimothy Gu <timothygu99@gmail.com>2017-09-05 10:47:42 +0800
commitd932e802317f9f61bd10988189fa43ed03ad0f61 (patch)
tree60ced59b8a4a75ef4b2d670ee7cf5ddf834a5799 /lib/vm.js
parent86e7c61a070d056daec9a756b86aece9ec30c2a8 (diff)
downloadandroid-node-v8-d932e802317f9f61bd10988189fa43ed03ad0f61.tar.gz
android-node-v8-d932e802317f9f61bd10988189fa43ed03ad0f61.tar.bz2
android-node-v8-d932e802317f9f61bd10988189fa43ed03ad0f61.zip
vm: support parsing a script in a specific context
PR-URL: https://github.com/nodejs/node/pull/14888 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Diffstat (limited to 'lib/vm.js')
-rw-r--r--lib/vm.js41
1 files changed, 32 insertions, 9 deletions
diff --git a/lib/vm.js b/lib/vm.js
index 5bee450bec..e7fccc9749 100644
--- a/lib/vm.js
+++ b/lib/vm.js
@@ -21,8 +21,14 @@
'use strict';
-const binding = process.binding('contextify');
-const Script = binding.ContextifyScript;
+const {
+ ContextifyScript: Script,
+ kParsingContext,
+
+ makeContext,
+ isContext,
+ runInDebugContext
+} = process.binding('contextify');
// The binding provides a few useful primitives:
// - Script(code, { filename = "evalmachine.anonymous",
@@ -62,11 +68,11 @@ Script.prototype.runInNewContext = function(sandbox, options) {
function createContext(sandbox) {
if (sandbox === undefined) {
sandbox = {};
- } else if (binding.isContext(sandbox)) {
+ } else if (isContext(sandbox)) {
return sandbox;
}
- binding.makeContext(sandbox);
+ makeContext(sandbox);
return sandbox;
}
@@ -99,16 +105,33 @@ function sigintHandlersWrap(fn, thisArg, argsArray) {
}
}
-function runInDebugContext(code) {
- return binding.runInDebugContext(code);
-}
-
function runInContext(code, contextifiedSandbox, options) {
+ if (typeof options === 'string') {
+ options = {
+ filename: options,
+ [kParsingContext]: contextifiedSandbox
+ };
+ } else {
+ options = Object.assign({}, options, {
+ [kParsingContext]: contextifiedSandbox
+ });
+ }
return createScript(code, options)
.runInContext(contextifiedSandbox, options);
}
function runInNewContext(code, sandbox, options) {
+ sandbox = createContext(sandbox);
+ if (typeof options === 'string') {
+ options = {
+ filename: options,
+ [kParsingContext]: sandbox
+ };
+ } else {
+ options = Object.assign({}, options, {
+ [kParsingContext]: sandbox
+ });
+ }
return createScript(code, options).runInNewContext(sandbox, options);
}
@@ -124,5 +147,5 @@ module.exports = {
runInContext,
runInNewContext,
runInThisContext,
- isContext: binding.isContext
+ isContext
};