summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2021-06-27 02:27:50 +0200
committerGitHub <noreply@github.com>2021-06-27 02:27:50 +0200
commit7b9737b9f4c25e1d25bfb352198cf24a50ceb2de (patch)
tree4f853a8218f5d0607304db33e2b02d87976678a7 /core
parent015f252066c21bd6d36e5b4d58a34dd40e4c0db0 (diff)
feat(inspector): pipe console messages between terminal and inspector (#11134)
This commit adds support for piping console messages to inspector. This is done by "wrapping" Deno's console implementation with default console provided by V8 by the means of "Deno.core.callConsole" binding. Effectively each call to "console.*" methods calls a method on Deno's console and V8's console.
Diffstat (limited to 'core')
-rw-r--r--core/bindings.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/core/bindings.rs b/core/bindings.rs
index c96a8559c..fd683b3ba 100644
--- a/core/bindings.rs
+++ b/core/bindings.rs
@@ -59,6 +59,9 @@ lazy_static::lazy_static! {
v8::ExternalReference {
function: memory_usage.map_fn_to(),
},
+ v8::ExternalReference {
+ function: call_console.map_fn_to(),
+ },
]);
}
@@ -134,6 +137,7 @@ pub fn initialize_context<'s>(
set_func(scope, core_val, "getPromiseDetails", get_promise_details);
set_func(scope, core_val, "getProxyDetails", get_proxy_details);
set_func(scope, core_val, "memoryUsage", memory_usage);
+ set_func(scope, core_val, "callConsole", call_console);
set_func(scope, core_val, "createHostObject", create_host_object);
// Direct bindings on `window`.
@@ -460,6 +464,54 @@ fn eval_context(
rv.set(to_v8(tc_scope, output).unwrap());
}
+/// This binding should be used if there's a custom console implementation
+/// available. Using it will make sure that proper stack frames are displayed
+/// in the inspector console.
+///
+/// Each method on console object should be bound to this function, eg:
+/// ```ignore
+/// function wrapConsole(consoleFromDeno, consoleFromV8) {
+/// const callConsole = core.callConsole;
+///
+/// for (const key of Object.keys(consoleFromV8)) {
+/// if (consoleFromDeno.hasOwnProperty(key)) {
+/// consoleFromDeno[key] = callConsole.bind(
+/// consoleFromDeno,
+/// consoleFromV8[key],
+/// consoleFromDeno[key],
+/// );
+/// }
+/// }
+/// }
+/// ```
+///
+/// Inspired by:
+/// https://github.com/nodejs/node/blob/1317252dfe8824fd9cfee125d2aaa94004db2f3b/src/inspector_js_api.cc#L194-L222
+fn call_console(
+ scope: &mut v8::HandleScope,
+ args: v8::FunctionCallbackArguments,
+ _rv: v8::ReturnValue,
+) {
+ assert!(args.length() >= 2);
+
+ assert!(args.get(0).is_function());
+ assert!(args.get(1).is_function());
+
+ let mut call_args = vec![];
+ for i in 2..args.length() {
+ call_args.push(args.get(i));
+ }
+
+ let receiver = args.this();
+ let inspector_console_method =
+ v8::Local::<v8::Function>::try_from(args.get(0)).unwrap();
+ let deno_console_method =
+ v8::Local::<v8::Function>::try_from(args.get(1)).unwrap();
+
+ inspector_console_method.call(scope, receiver.into(), &call_args);
+ deno_console_method.call(scope, receiver.into(), &call_args);
+}
+
fn encode(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,