| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 174f7d2 commit 21e6064
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,6 +134,20 @@ Note that running untrusted code is a tricky business requiring great care. | |||
| 134 | 134 | a separate process. | |
| 135 | 135 | ||
| 136 | 136 | ||
| 137 | + ## vm.runInDebugContext(code) | ||
| 138 | + | ||
| 139 | + `vm.runInDebugContext` compiles and executes `code` inside the V8 debug context. | ||
| 140 | + The primary use case is to get access to the V8 debug object: | ||
| 141 | + | ||
| 142 | + var Debug = vm.runInDebugContext('Debug'); | ||
| 143 | + Debug.scripts().forEach(function(script) { console.log(script.name); }); | ||
| 144 | + | ||
| 145 | + Note that the debug context and object are intrinsically tied to V8's debugger | ||
| 146 | + implementation and may change (or even get removed) without prior warning. | ||
| 147 | + | ||
| 148 | + The debug object can also be exposed with the `--expose_debug_as=` switch. | ||
| 149 | + | ||
| 150 | + | ||
| 137 | 151 | ## Class: Script | |
| 138 | 152 | ||
| 139 | 153 | A class for holding precompiled scripts, and running them in specific sandboxes. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,10 @@ exports.createContext = function(sandbox) { | |||
| 55 | 55 | return sandbox; | |
| 56 | 56 | }; | |
| 57 | 57 | ||
| 58 | + exports.runInDebugContext = function(code) { | ||
| 59 | + return binding.runInDebugContext(code); | ||
| 60 | + }; | ||
| 61 | + | ||
| 58 | 62 | exports.runInContext = function(code, contextifiedSandbox, options) { | |
| 59 | 63 | var script = new Script(code, options); | |
| 60 | 64 | return script.runInContext(contextifiedSandbox, options); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,13 +28,15 @@ | |||
| 28 | 28 | #include "env-inl.h" | |
| 29 | 29 | #include "util.h" | |
| 30 | 30 | #include "util-inl.h" | |
| 31 | + #include "v8-debug.h" | ||
| 31 | 32 | ||
| 32 | 33 | namespace node { | |
| 33 | 34 | ||
| 34 | 35 | using v8::AccessType; | |
| 35 | 36 | using v8::Array; | |
| 36 | 37 | using v8::Boolean; | |
| 37 | 38 | using v8::Context; | |
| 39 | + using v8::Debug; | ||
| 38 | 40 | using v8::EscapableHandleScope; | |
| 39 | 41 | using v8::External; | |
| 40 | 42 | using v8::Function; | |
@@ -244,11 +246,25 @@ class ContextifyContext { | |||
| 244 | 246 | function_template->InstanceTemplate()->SetInternalFieldCount(1); | |
| 245 | 247 | env->set_script_data_constructor_function(function_template->GetFunction()); | |
| 246 | 248 | ||
| 249 | + NODE_SET_METHOD(target, "runInDebugContext", RunInDebugContext); | ||
| 247 | 250 | NODE_SET_METHOD(target, "makeContext", MakeContext); | |
| 248 | 251 | NODE_SET_METHOD(target, "isContext", IsContext); | |
| 249 | 252 | } | |
| 250 | 253 | ||
| 251 | 254 | ||
| 255 | + static void RunInDebugContext(const FunctionCallbackInfo<Value>& args) { | ||
| 256 | + HandleScope scope(args.GetIsolate()); | ||
| 257 | + Local<String> script_source(args[0]->ToString()); | ||
| 258 | + if (script_source.IsEmpty()) | ||
| 259 | + return; // Exception pending. | ||
| 260 | + Context::Scope context_scope(Debug::GetDebugContext()); | ||
| 261 | + Local<Script> script = Script::Compile(script_source); | ||
| 262 | + if (script.IsEmpty()) | ||
| 263 | + return; // Exception pending. | ||
| 264 | + args.GetReturnValue().Set(script->Run()); | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + | ||
| 252 | 268 | static void MakeContext(const FunctionCallbackInfo<Value>& args) { | |
| 253 | 269 | Environment* env = Environment::GetCurrent(args.GetIsolate()); | |
| 254 | 270 | HandleScope scope(env->isolate()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,48 @@ | |||
| 1 | + // Copyright Joyent, Inc. and other Node contributors. | ||
| 2 | + // | ||
| 3 | + // Permission is hereby granted, free of charge, to any person obtaining a | ||
| 4 | + // copy of this software and associated documentation files (the | ||
| 5 | + // "Software"), to deal in the Software without restriction, including | ||
| 6 | + // without limitation the rights to use, copy, modify, merge, publish, | ||
| 7 | + // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
| 8 | + // persons to whom the Software is furnished to do so, subject to the | ||
| 9 | + // following conditions: | ||
| 10 | + // | ||
| 11 | + // The above copyright notice and this permission notice shall be included | ||
| 12 | + // in all copies or substantial portions of the Software. | ||
| 13 | + // | ||
| 14 | + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
| 15 | + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 16 | + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
| 17 | + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| 18 | + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
| 19 | + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
| 20 | + // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 21 | + | ||
| 22 | + var common = require('../common'); | ||
| 23 | + var assert = require('assert'); | ||
| 24 | + var vm = require('vm'); | ||
| 25 | + | ||
| 26 | + assert.throws(function() { | ||
| 27 | + vm.runInDebugContext('*'); | ||
| 28 | + }, /SyntaxError/); | ||
| 29 | + | ||
| 30 | + assert.throws(function() { | ||
| 31 | + vm.runInDebugContext({ toString: assert.fail }); | ||
| 32 | + }, /AssertionError/); | ||
| 33 | + | ||
| 34 | + assert.throws(function() { | ||
| 35 | + vm.runInDebugContext('throw URIError("BAM")'); | ||
| 36 | + }, /URIError/); | ||
| 37 | + | ||
| 38 | + assert.throws(function() { | ||
| 39 | + vm.runInDebugContext('(function(f) { f(f) })(function(f) { f(f) })'); | ||
| 40 | + }, /RangeError/); | ||
| 41 | + | ||
| 42 | + assert.equal(typeof(vm.runInDebugContext('this')), 'object'); | ||
| 43 | + assert.equal(typeof(vm.runInDebugContext('Debug')), 'object'); | ||
| 44 | + | ||
| 45 | + assert.strictEqual(vm.runInDebugContext(), undefined); | ||
| 46 | + assert.strictEqual(vm.runInDebugContext(0), 0); | ||
| 47 | + assert.strictEqual(vm.runInDebugContext(null), null); | ||
| 48 | + assert.strictEqual(vm.runInDebugContext(undefined), undefined); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments