// - runInThisContext()
// - runInContext(sandbox, [timeout])
// - makeContext(sandbox)
+// - isContext(sandbox)
// From this we build the entire documented API.
Script.prototype.runInNewContext = function(initSandbox, timeout, disp) {
exports.createContext = function(initSandbox) {
if (util.isUndefined(initSandbox)) {
initSandbox = {};
+ } else if (binding.isContext(initSandbox)) {
+ return initSandbox;
}
binding.makeContext(initSandbox);
-
return initSandbox;
};
var script = exports.createScript(code, filename, disp);
return script.runInThisContext(timeout, disp);
};
+
+exports.isContext = binding.isContext;
data_wrapper_ctor.Reset(node_isolate, function_template->GetFunction());
NODE_SET_METHOD(target, "makeContext", MakeContext);
+ NODE_SET_METHOD(target, "isContext", IsContext);
}
}
Local<Object> sandbox = args[0].As<Object>();
+ Local<String> hidden_name =
+ FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden");
+
+ // Don't allow contextifying a sandbox multiple times.
+ assert(sandbox->GetHiddenValue(hidden_name).IsEmpty());
+
ContextifyContext* context = new ContextifyContext(sandbox);
Local<External> hidden_context = External::New(context);
+ sandbox->SetHiddenValue(hidden_name, hidden_context);
+ }
+
+
+ static void IsContext(const FunctionCallbackInfo<Value>& args) {
+ HandleScope scope(node_isolate);
+
+ if (!args[0]->IsObject()) {
+ ThrowTypeError("sandbox must be an object");
+ return;
+ }
+ Local<Object> sandbox = args[0].As<Object>();
+
Local<String> hidden_name =
FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden");
- sandbox->SetHiddenValue(hidden_name, hidden_context);
+
+ args.GetReturnValue().Set(!sandbox->GetHiddenValue(hidden_name).IsEmpty());
}
assert.throws(function() {
var ctx = vm.createContext('string is not supported');
-});
+}, TypeError);
assert.doesNotThrow(function() {
var ctx = vm.createContext({ a: 1 });
ctx = vm.createContext([0, 1, 2, 3]);
});
+
+assert.doesNotThrow(function() {
+ var sandbox = {};
+ vm.createContext(sandbox);
+ vm.createContext(sandbox);
+});
--- /dev/null
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var vm = require('vm');
+
+assert.throws(function() {
+ vm.isContext('string is not supported');
+}, TypeError);
+
+assert.strictEqual(vm.isContext({}), false);
+assert.strictEqual(vm.isContext([]), false);
+
+assert.strictEqual(vm.isContext(vm.createContext()), true);
+assert.strictEqual(vm.isContext(vm.createContext([])), true);
+
+var sandbox = { foo: 'bar' };
+vm.createContext(sandbox);
+assert.strictEqual(vm.isContext(sandbox), true);