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 /runtime | |
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 'runtime')
-rw-r--r-- | runtime/examples/hello_runtime.rs | 2 | ||||
-rw-r--r-- | runtime/js/40_error_stack.js | 7 | ||||
-rw-r--r-- | runtime/js/99_main.js | 4 | ||||
-rw-r--r-- | runtime/web_worker.rs | 18 | ||||
-rw-r--r-- | runtime/worker.rs | 5 | ||||
-rw-r--r-- | runtime/worker_bootstrap.rs | 2 |
6 files changed, 18 insertions, 20 deletions
diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs index 0a0d6fe23..b4716076e 100644 --- a/runtime/examples/hello_runtime.rs +++ b/runtime/examples/hello_runtime.rs @@ -28,7 +28,6 @@ async fn main() -> Result<(), AnyError> { let options = WorkerOptions { bootstrap: BootstrapOptions { - apply_source_maps: false, args: vec![], cpu_count: 1, debug_flag: false, @@ -45,6 +44,7 @@ async fn main() -> Result<(), AnyError> { root_cert_store: None, user_agent: "hello_runtime".to_string(), seed: None, + source_map_getter: None, js_error_create_fn: None, web_worker_preload_module_cb, create_web_worker_cb, diff --git a/runtime/js/40_error_stack.js b/runtime/js/40_error_stack.js index 7ed463273..1ecf0f6bb 100644 --- a/runtime/js/40_error_stack.js +++ b/runtime/js/40_error_stack.js @@ -13,12 +13,7 @@ } function opApplySourceMap(location) { - const res = core.opSync("op_apply_source_map", location); - return { - fileName: res.fileName, - lineNumber: res.lineNumber, - columnNumber: res.columnNumber, - }; + return core.applySourceMap(location); } window.__bootstrap.errorStack = { diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 44d457e5c..0b83846ca 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -222,10 +222,6 @@ delete Object.prototype.__proto__; build.setBuildInfo(runtimeOptions.target); util.setLogDebug(runtimeOptions.debugFlag, source); const prepareStackTrace = core.createPrepareStackTrace( - // TODO(bartlomieju): a very crude way to disable - // source mapping of errors. This condition is true - // only for compiled standalone binaries. - runtimeOptions.applySourceMaps ? errorStack.opApplySourceMap : undefined, errorStack.opFormatFileName, ); // deno-lint-ignore prefer-primordials diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 5d416b935..40b222417 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -29,6 +29,7 @@ use deno_core::ModuleLoader; use deno_core::ModuleSpecifier; use deno_core::RuntimeOptions; use deno_core::SharedArrayBufferStore; +use deno_core::SourceMapGetter; use deno_tls::rustls::RootCertStore; use deno_web::create_entangled_message_port; use deno_web::BlobStore; @@ -91,12 +92,15 @@ impl Serialize for WorkerControlEvent { WorkerControlEvent::TerminalError(error) | WorkerControlEvent::Error(error) => { let value = match error.downcast_ref::<JsError>() { - Some(js_error) => json!({ - "message": js_error.exception_message, - "fileName": js_error.script_resource_name, - "lineNumber": js_error.line_number, - "columnNumber": js_error.start_column, - }), + Some(js_error) => { + let frame = js_error.frames.first(); + json!({ + "message": js_error.exception_message, + "fileName": frame.map(|f| f.file_name.as_ref()), + "lineNumber": frame.map(|f| f.line_number.as_ref()), + "columnNumber": frame.map(|f| f.column_number.as_ref()), + }) + } None => json!({ "message": error.to_string(), }), @@ -320,6 +324,7 @@ pub struct WebWorkerOptions { pub module_loader: Rc<dyn ModuleLoader>, pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>, pub preload_module_cb: Arc<ops::worker_host::PreloadModuleCb>, + pub source_map_getter: Option<Box<dyn SourceMapGetter>>, pub js_error_create_fn: Option<Rc<JsErrorCreateFn>>, pub use_deno_namespace: bool, pub worker_type: WebWorkerType, @@ -436,6 +441,7 @@ impl WebWorker { let mut js_runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(options.module_loader.clone()), startup_snapshot: Some(js::deno_isolate_init()), + source_map_getter: options.source_map_getter, js_error_create_fn: options.js_error_create_fn.clone(), get_error_class_fn: options.get_error_class_fn, shared_array_buffer_store: options.shared_array_buffer_store.clone(), diff --git a/runtime/worker.rs b/runtime/worker.rs index 983f174aa..fa147a7e6 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -20,6 +20,7 @@ use deno_core::ModuleLoader; use deno_core::ModuleSpecifier; use deno_core::RuntimeOptions; use deno_core::SharedArrayBufferStore; +use deno_core::SourceMapGetter; use deno_tls::rustls::RootCertStore; use deno_web::BlobStore; use log::debug; @@ -54,6 +55,7 @@ pub struct WorkerOptions { // Callbacks invoked when creating new instance of WebWorker pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>, pub web_worker_preload_module_cb: Arc<ops::worker_host::PreloadModuleCb>, + pub source_map_getter: Option<Box<dyn SourceMapGetter>>, pub js_error_create_fn: Option<Rc<JsErrorCreateFn>>, pub maybe_inspector_server: Option<Arc<InspectorServer>>, pub should_break_on_first_statement: bool, @@ -155,6 +157,7 @@ impl MainWorker { let mut js_runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(options.module_loader.clone()), startup_snapshot: Some(js::deno_isolate_init()), + source_map_getter: options.source_map_getter, js_error_create_fn: options.js_error_create_fn.clone(), get_error_class_fn: options.get_error_class_fn, shared_array_buffer_store: options.shared_array_buffer_store.clone(), @@ -357,7 +360,6 @@ mod tests { let options = WorkerOptions { bootstrap: BootstrapOptions { - apply_source_maps: false, args: vec![], cpu_count: 1, debug_flag: false, @@ -374,6 +376,7 @@ mod tests { unsafely_ignore_certificate_errors: None, root_cert_store: None, seed: None, + source_map_getter: None, js_error_create_fn: None, web_worker_preload_module_cb: Arc::new(|_| unreachable!()), create_web_worker_cb: Arc::new(|_| unreachable!()), diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index 05bde731f..f1ffd1b3d 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -8,7 +8,6 @@ use deno_core::ModuleSpecifier; pub struct BootstrapOptions { /// Sets `Deno.args` in JS runtime. pub args: Vec<String>, - pub apply_source_maps: bool, pub cpu_count: usize, pub debug_flag: bool, pub enable_testing_features: bool, @@ -28,7 +27,6 @@ impl BootstrapOptions { let payload = json!({ // Shared bootstrap args "args": self.args, - "applySourceMaps": self.apply_source_maps, "cpuCount": self.cpu_count, "debugFlag": self.debug_flag, "denoVersion": self.runtime_version, |