FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

lib, src: add vm.runInDebugContext() · bderagon/node@21e6064 · GitHub

/ node Public
forked from nodejs/node

Commit 21e6064

Browse files
authored andcommitted
lib, src: add vm.runInDebugContext()
Compiles and executes source code in V8's debugger context. Provides a programmatic way to get access to the debug object by executing: var Debug = vm.runInDebugContext('Debug'); Fixes nodejs#7886. Reviewed-by: Trevor Norris <trev.norris@gmail.com>
1 parent 174f7d2 commit 21e6064

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎doc/api/vm.markdown‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ Note that running untrusted code is a tricky business requiring great care.
134134
a separate process.
135135

136136

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+
137151
## Class: Script
138152

139153
A class for holding precompiled scripts, and running them in specific sandboxes.

‎lib/vm.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ exports.createContext = function(sandbox) {
5555
return sandbox;
5656
};
5757

58+
exports.runInDebugContext = function(code) {
59+
return binding.runInDebugContext(code);
60+
};
61+
5862
exports.runInContext = function(code, contextifiedSandbox, options) {
5963
var script = new Script(code, options);
6064
return script.runInContext(contextifiedSandbox, options);

‎src/node_contextify.cc‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
#include "env-inl.h"
2929
#include "util.h"
3030
#include "util-inl.h"
31+
#include "v8-debug.h"
3132

3233
namespace node {
3334

3435
using v8::AccessType;
3536
using v8::Array;
3637
using v8::Boolean;
3738
using v8::Context;
39+
using v8::Debug;
3840
using v8::EscapableHandleScope;
3941
using v8::External;
4042
using v8::Function;
@@ -244,11 +246,25 @@ class ContextifyContext {
244246
function_template->InstanceTemplate()->SetInternalFieldCount(1);
245247
env->set_script_data_constructor_function(function_template->GetFunction());
246248

249+
NODE_SET_METHOD(target, "runInDebugContext", RunInDebugContext);
247250
NODE_SET_METHOD(target, "makeContext", MakeContext);
248251
NODE_SET_METHOD(target, "isContext", IsContext);
249252
}
250253

251254

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+
252268
static void MakeContext(const FunctionCallbackInfo<Value>& args) {
253269
Environment* env = Environment::GetCurrent(args.GetIsolate());
254270
HandleScope scope(env->isolate());
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL