From 7b9737b9f4c25e1d25bfb352198cf24a50ceb2de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 27 Jun 2021 02:27:50 +0200 Subject: 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. --- core/bindings.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'core/bindings.rs') 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::::try_from(args.get(0)).unwrap(); + let deno_console_method = + v8::Local::::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, -- cgit v1.2.3