diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2022-04-15 15:08:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-15 16:08:09 +0200 |
commit | 8b31fc23cd80de9baa62535e95367da7a21c9cfd (patch) | |
tree | 994748bd06ed5b4953929392107b6beaa1c1c337 /core/bindings.rs | |
parent | b4af648c1515a8e79d7a5d1b14d8a4ba9d966a72 (diff) |
refactor: Move source map lookups to core (#14274)
The following transformations gradually faced by "JsError" have all been
moved up front to "JsError::from_v8_exception()":
- finding the first non-"deno:" source line;
- moving "JsError::script_resource_name" etc. into the first error stack
in case of syntax errors;
- source mapping "JsError::script_resource_name" etc. when wrapping
the error even though the frame locations are source mapped earlier;
- removing "JsError::{script_resource_name,line_number,start_column,end_column}"
entirely in favour of "js_error.frames.get(0)".
We also no longer pass a js-side callback to "core/02_error.js" from cli.
I avoided doing this on previous occasions because the source map lookups
were in an awkward place.
Diffstat (limited to 'core/bindings.rs')
-rw-r--r-- | core/bindings.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/core/bindings.rs b/core/bindings.rs index 46dcefe38..889229bb5 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -10,6 +10,7 @@ use crate::modules::ModuleMap; use crate::ops::OpCtx; use crate::ops_builtin::WasmStreamingResource; use crate::resolve_url_or_path; +use crate::source_map::apply_source_map as apply_source_map_; use crate::JsRuntime; use crate::PromiseId; use crate::ResourceId; @@ -21,6 +22,7 @@ use serde::Deserialize; use serde::Serialize; use serde_v8::to_v8; use std::cell::RefCell; +use std::collections::HashMap; use std::option::Option; use std::os::raw::c_void; use url::Url; @@ -109,6 +111,9 @@ pub static EXTERNAL_REFERENCES: Lazy<v8::ExternalReferences> = v8::ExternalReference { function: terminate.map_fn_to(), }, + v8::ExternalReference { + function: apply_source_map.map_fn_to(), + }, ]) }); @@ -237,6 +242,7 @@ pub fn initialize_context<'s>( set_func(scope, core_val, "abortWasmStreaming", abort_wasm_streaming); set_func(scope, core_val, "destructureError", destructure_error); set_func(scope, core_val, "terminate", terminate); + set_func(scope, core_val, "applySourceMap", apply_source_map); // Direct bindings on `window`. set_func(scope, global, "queueMicrotask", queue_microtask); @@ -1324,6 +1330,41 @@ fn terminate( scope.terminate_execution(); } +fn apply_source_map( + scope: &mut v8::HandleScope, + args: v8::FunctionCallbackArguments, + mut rv: v8::ReturnValue, +) { + #[derive(Deserialize, Serialize)] + #[serde(rename_all = "camelCase")] + struct Location { + file_name: String, + line_number: u32, + column_number: u32, + } + let state_rc = JsRuntime::state(scope); + let state = state_rc.borrow(); + if let Some(source_map_getter) = &state.source_map_getter { + let mut location = match serde_v8::from_v8::<Location>(scope, args.get(0)) { + Ok(location) => location, + Err(error) => return throw_type_error(scope, error.to_string()), + }; + let (f, l, c, _) = apply_source_map_( + location.file_name, + location.line_number.into(), + location.column_number.into(), + &mut HashMap::new(), + source_map_getter.as_ref(), + ); + location.file_name = f; + location.line_number = l as u32; + location.column_number = c as u32; + rv.set(serde_v8::to_v8(scope, location).unwrap()); + } else { + rv.set(args.get(0)); + } +} + fn create_host_object( scope: &mut v8::HandleScope, _args: v8::FunctionCallbackArguments, |