From 21e60643b0795fe7b6a46ff29d73df60e6a7c9f5 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 22 Aug 2014 15:09:24 +0200 Subject: 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 #7886. Reviewed-by: Trevor Norris --- doc/api/vm.markdown | 14 +++++++++++ lib/vm.js | 4 +++ src/node_contextify.cc | 16 ++++++++++++ test/simple/test-vm-debug-context.js | 48 ++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 test/simple/test-vm-debug-context.js diff --git a/doc/api/vm.markdown b/doc/api/vm.markdown index 8ad7e72d4f..0f963ca435 100644 --- a/doc/api/vm.markdown +++ b/doc/api/vm.markdown @@ -134,6 +134,20 @@ Note that running untrusted code is a tricky business requiring great care. a separate process. +## vm.runInDebugContext(code) + +`vm.runInDebugContext` compiles and executes `code` inside the V8 debug context. +The primary use case is to get access to the V8 debug object: + + var Debug = vm.runInDebugContext('Debug'); + Debug.scripts().forEach(function(script) { console.log(script.name); }); + +Note that the debug context and object are intrinsically tied to V8's debugger +implementation and may change (or even get removed) without prior warning. + +The debug object can also be exposed with the `--expose_debug_as=` switch. + + ## Class: Script A class for holding precompiled scripts, and running them in specific sandboxes. diff --git a/lib/vm.js b/lib/vm.js index 83b78b874b..7c6c59c0af 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -55,6 +55,10 @@ exports.createContext = function(sandbox) { return sandbox; }; +exports.runInDebugContext = function(code) { + return binding.runInDebugContext(code); +}; + exports.runInContext = function(code, contextifiedSandbox, options) { var script = new Script(code, options); return script.runInContext(contextifiedSandbox, options); diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 9525e7db64..bb43536d87 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -28,6 +28,7 @@ #include "env-inl.h" #include "util.h" #include "util-inl.h" +#include "v8-debug.h" namespace node { @@ -35,6 +36,7 @@ using v8::AccessType; using v8::Array; using v8::Boolean; using v8::Context; +using v8::Debug; using v8::EscapableHandleScope; using v8::External; using v8::Function; @@ -244,11 +246,25 @@ class ContextifyContext { function_template->InstanceTemplate()->SetInternalFieldCount(1); env->set_script_data_constructor_function(function_template->GetFunction()); + NODE_SET_METHOD(target, "runInDebugContext", RunInDebugContext); NODE_SET_METHOD(target, "makeContext", MakeContext); NODE_SET_METHOD(target, "isContext", IsContext); } + static void RunInDebugContext(const FunctionCallbackInfo& args) { + HandleScope scope(args.GetIsolate()); + Local script_source(args[0]->ToString()); + if (script_source.IsEmpty()) + return; // Exception pending. + Context::Scope context_scope(Debug::GetDebugContext()); + Local