|
| 1 | +use tracing::{debug, info, warn}; |
| 2 | + |
| 3 | +/// Convert a V8 value to a string, using JSON.stringify for objects |
| 4 | +fn v8_value_to_string(scope: &mut v8::HandleScope, value: v8::Local<v8::Value>) -> String { |
| 5 | + // For objects and arrays, try JSON.stringify |
| 6 | + if value.is_object() && !value.is_null() && !value.is_undefined() { |
| 7 | + if let Some(json_str) = try_json_stringify(scope, value) { |
| 8 | + return json_str; |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | + // Fallback to toString() for all types |
| 13 | + value |
| 14 | + .to_string(scope) |
| 15 | + .map(|s| s.to_rust_string_lossy(scope)) |
| 16 | + .unwrap_or_else(|| "[value]".to_string()) |
| 17 | +} |
| 18 | + |
| 19 | +/// Try to JSON.stringify a value, returning None if it fails |
| 20 | +fn try_json_stringify(scope: &mut v8::HandleScope, value: v8::Local<v8::Value>) -> Option<String> { |
| 21 | + let global = scope.get_current_context().global(scope); |
| 22 | + let json_key = v8::String::new(scope, "JSON")?; |
| 23 | + let stringify_key = v8::String::new(scope, "stringify")?; |
| 24 | + |
| 25 | + let json_obj = global.get(scope, json_key.into())?.to_object(scope)?; |
| 26 | + let stringify_fn = json_obj.get(scope, stringify_key.into())?; |
| 27 | + let stringify_fn = v8::Local::<v8::Function>::try_from(stringify_fn).ok()?; |
| 28 | + |
| 29 | + let result = stringify_fn.call(scope, json_obj.into(), &[value])?; |
| 30 | + result |
| 31 | + .to_string(scope) |
| 32 | + .map(|s| s.to_rust_string_lossy(scope)) |
| 33 | +} |
| 34 | + |
| 35 | +/// Format console arguments into a single string |
| 36 | +fn format_console_args(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments) -> String { |
| 37 | + let mut log_parts = Vec::new(); |
| 38 | + for i in 0..args.length() { |
| 39 | + let arg = args.get(i); |
| 40 | + log_parts.push(v8_value_to_string(scope, arg)); |
| 41 | + } |
| 42 | + log_parts.join(" ") |
| 43 | +} |
| 44 | + |
| 45 | +/// Generic console callback that logs at a specific level |
| 46 | +fn console_callback( |
| 47 | + scope: &mut v8::HandleScope, |
| 48 | + args: v8::FunctionCallbackArguments, |
| 49 | + _retval: v8::ReturnValue, |
| 50 | + log_fn: fn(&str), |
| 51 | +) { |
| 52 | + let message = format_console_args(scope, args); |
| 53 | + log_fn(&message); |
| 54 | +} |
| 55 | + |
| 56 | +/// Macro to generate console method callbacks for each log level |
| 57 | +macro_rules! console_method { |
| 58 | + ($name:ident, $log_macro:path) => { |
| 59 | + fn $name( |
| 60 | + scope: &mut v8::HandleScope, |
| 61 | + args: v8::FunctionCallbackArguments, |
| 62 | + retval: v8::ReturnValue, |
| 63 | + ) { |
| 64 | + console_callback(scope, args, retval, |msg| { |
| 65 | + $log_macro!(target: "httpjail::rules::js", "{}", msg) |
| 66 | + }); |
| 67 | + } |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +// Generate console.debug, console.log, console.info, console.warn, console.error |
| 72 | +console_method!(console_debug, debug); |
| 73 | +console_method!(console_log, info); |
| 74 | +console_method!(console_info, info); |
| 75 | +console_method!(console_warn, warn); |
| 76 | +console_method!(console_error, tracing::error); |
| 77 | + |
| 78 | +/// Set up console object with debug, log, info, warn, error methods |
| 79 | +pub fn setup_console(context_scope: &mut v8::ContextScope<v8::HandleScope>) { |
| 80 | + let global = context_scope.get_current_context().global(context_scope); |
| 81 | + let console_obj = v8::Object::new(context_scope); |
| 82 | + |
| 83 | + // Register each console method |
| 84 | + macro_rules! add_console_method { |
| 85 | + ($name:expr, $callback:expr) => { |
| 86 | + let key = v8::String::new(context_scope, $name).unwrap(); |
| 87 | + let func = v8::Function::new(context_scope, $callback).unwrap(); |
| 88 | + console_obj.set(context_scope, key.into(), func.into()); |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + add_console_method!("debug", console_debug); |
| 93 | + add_console_method!("log", console_log); |
| 94 | + add_console_method!("info", console_info); |
| 95 | + add_console_method!("warn", console_warn); |
| 96 | + add_console_method!("error", console_error); |
| 97 | + |
| 98 | + let console_key = v8::String::new(context_scope, "console").unwrap(); |
| 99 | + global.set(context_scope, console_key.into(), console_obj.into()); |
| 100 | +} |
0 commit comments