diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/deno_dir.rs | 20 | ||||
-rw-r--r-- | src/isolate.rs | 42 | ||||
-rw-r--r-- | src/js_errors.rs | 543 | ||||
-rw-r--r-- | src/main.rs | 17 |
4 files changed, 606 insertions, 16 deletions
diff --git a/src/deno_dir.rs b/src/deno_dir.rs index 8f13fd843..a6a741ec1 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -5,7 +5,9 @@ use errors::DenoResult; use errors::ErrorKind; use fs as deno_fs; use http_util; +use js_errors::SourceMapGetter; use msg; + use ring; use std; use std::fmt::Write; @@ -346,6 +348,24 @@ impl DenoDir { } } +impl SourceMapGetter for DenoDir { + fn get_source_map(&self, script_name: &str) -> Option<String> { + match self.code_fetch(script_name, ".") { + Err(_e) => { + return None; + } + Ok(out) => match out.maybe_source_map { + None => { + return None; + } + Some(source_map) => { + return Some(source_map); + } + }, + } + } +} + fn get_cache_filename(basedir: &Path, url: &Url) -> PathBuf { let host = url.host_str().unwrap(); let host_port = match url.port() { diff --git a/src/isolate.rs b/src/isolate.rs index 6b2814ec4..8e78f770b 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -8,6 +8,7 @@ use deno_dir; use errors::DenoError; use errors::DenoResult; use flags; +use js_errors::JSError; use libdeno; use permissions::DenoPermissions; @@ -26,8 +27,6 @@ use std::time::Instant; use tokio; use tokio_util; -type DenoException<'a> = &'a str; - // Buf represents a byte array returned from a "Op". // The message might be empty (which will be translated into a null object on // the javascript side) or it is a heap allocated opaque sequence of bytes. @@ -184,11 +183,25 @@ impl Isolate { self.timeout_due.set(inst); } + pub fn last_exception(&self) -> Option<JSError> { + let ptr = unsafe { libdeno::deno_last_exception(self.libdeno_isolate) }; + if ptr == std::ptr::null() { + None + } else { + let cstr = unsafe { CStr::from_ptr(ptr) }; + let v8_exception = cstr.to_str().unwrap(); + debug!("v8_exception\n{}\n", v8_exception); + let js_error = JSError::from_v8_exception(v8_exception).unwrap(); + let js_error_mapped = js_error.apply_source_map(&self.state.dir); + return Some(js_error_mapped); + } + } + pub fn execute( &self, js_filename: &str, js_source: &str, - ) -> Result<(), DenoException> { + ) -> Result<(), JSError> { let filename = CString::new(js_filename).unwrap(); let source = CString::new(js_source).unwrap(); let r = unsafe { @@ -200,9 +213,8 @@ impl Isolate { ) }; if r == 0 { - let ptr = unsafe { libdeno::deno_last_exception(self.libdeno_isolate) }; - let cstr = unsafe { CStr::from_ptr(ptr) }; - return Err(cstr.to_str().unwrap()); + let js_error = self.last_exception().unwrap(); + return Err(js_error); } Ok(()) } @@ -249,7 +261,7 @@ impl Isolate { // TODO Use Park abstraction? Note at time of writing Tokio default runtime // does not have new_with_park(). - pub fn event_loop(&self) { + pub fn event_loop(&self) -> Result<(), JSError> { // Main thread event loop. while !self.is_idle() { match recv_deadline(&self.rx, self.get_timeout_due()) { @@ -258,9 +270,16 @@ impl Isolate { Err(e) => panic!("recv_deadline() failed: {:?}", e), } self.check_promise_errors(); + if let Some(err) = self.last_exception() { + return Err(err); + } } // Check on done self.check_promise_errors(); + if let Some(err) = self.last_exception() { + return Err(err); + } + Ok(()) } #[inline] @@ -389,7 +408,7 @@ mod tests { } "#, ).expect("execute error"); - isolate.event_loop(); + isolate.event_loop().ok(); }); } @@ -435,8 +454,8 @@ mod tests { const data = new Uint8Array([42, 43, 44, 45, 46]); libdeno.send(control, data); "#, - ).expect("execute error"); - isolate.event_loop(); + ).expect("execute error");; + isolate.event_loop().unwrap(); let metrics = &isolate.state.metrics; assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 1); assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 1); @@ -471,6 +490,7 @@ mod tests { const control = new Uint8Array([4, 5, 6]); const data = new Uint8Array([42, 43, 44, 45, 46]); let r = libdeno.send(control, data); + libdeno.recv(() => {}); if (r != null) throw Error("expected null"); "#, ).expect("execute error"); @@ -486,7 +506,7 @@ mod tests { // with metrics_dispatch_async() to properly validate them. } - isolate.event_loop(); + isolate.event_loop().unwrap(); // Make sure relevant metrics are updated after task is executed. { diff --git a/src/js_errors.rs b/src/js_errors.rs new file mode 100644 index 000000000..63862db3f --- /dev/null +++ b/src/js_errors.rs @@ -0,0 +1,543 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. + +// Note that source_map_mappings requires 0-indexed line and column numbers but +// V8 Exceptions are 1-indexed. + +// TODO: This currently only applies to uncaught exceptions. It would be nice to +// also have source maps for situations like this: +// const err = new Error("Boo!"); +// console.log(err.stack); +// It would require calling into Rust from Error.prototype.prepareStackTrace. + +use serde_json; +use source_map_mappings::parse_mappings; +use source_map_mappings::Bias; +use source_map_mappings::Mappings; +use std::collections::HashMap; + +pub trait SourceMapGetter { + /// Returns the raw source map file. + fn get_source_map(&self, script_name: &str) -> Option<String>; +} + +struct SourceMap { + mappings: Mappings, + sources: Vec<String>, +} + +/// Cached filename lookups. The key can be None if a previous lookup failed to +/// find a SourceMap. +type CachedMaps = HashMap<String, Option<SourceMap>>; + +#[derive(Debug, PartialEq)] +pub struct StackFrame { + pub line: u32, // zero indexed + pub column: u32, // zero indexed + pub source_url: String, // TODO rename to 'script_name' + pub function_name: String, + pub is_eval: bool, + pub is_constructor: bool, + pub is_wasm: bool, +} + +#[derive(Debug, PartialEq)] +pub struct JSError { + pub message: String, + pub frames: Vec<StackFrame>, +} + +impl ToString for StackFrame { + fn to_string(&self) -> String { + // Note when we print to string, we change from 0-indexed to 1-indexed. + let (line, column) = (self.line + 1, self.column + 1); + if self.function_name.len() > 0 { + format!( + " at {} ({}:{}:{})", + self.function_name, self.source_url, line, column + ) + } else if self.is_eval { + format!(" at eval ({}:{}:{})", self.source_url, line, column) + } else { + format!(" at {}:{}:{}", self.source_url, line, column) + } + } +} + +impl ToString for JSError { + fn to_string(&self) -> String { + let mut s = self.message.clone(); + for frame in &self.frames { + s.push_str("\n"); + s.push_str(&frame.to_string()); + } + s + } +} + +impl StackFrame { + // TODO Maybe use serde_derive? + fn from_json_value(v: &serde_json::Value) -> Option<Self> { + if !v.is_object() { + return None; + } + let obj = v.as_object().unwrap(); + + let line_v = &obj["line"]; + if !line_v.is_u64() { + return None; + } + let line = line_v.as_u64().unwrap() as u32; + + let column_v = &obj["column"]; + if !column_v.is_u64() { + return None; + } + let column = column_v.as_u64().unwrap() as u32; + + let script_name_v = &obj["scriptName"]; + if !script_name_v.is_string() { + return None; + } + let script_name = String::from(script_name_v.as_str().unwrap()); + + // Optional fields. See EncodeExceptionAsJSON() in libdeno. + // Sometimes V8 doesn't provide all the frame information. + + let mut function_name = String::from(""); // default + if obj.contains_key("functionName") { + let function_name_v = &obj["functionName"]; + if function_name_v.is_string() { + function_name = String::from(function_name_v.as_str().unwrap()); + } + } + + let mut is_eval = false; // default + if obj.contains_key("isEval") { + let is_eval_v = &obj["isEval"]; + if is_eval_v.is_boolean() { + is_eval = is_eval_v.as_bool().unwrap(); + } + } + + let mut is_constructor = false; // default + if obj.contains_key("isConstructor") { + let is_constructor_v = &obj["isConstructor"]; + if is_constructor_v.is_boolean() { + is_constructor = is_constructor_v.as_bool().unwrap(); + } + } + + let mut is_wasm = false; // default + if obj.contains_key("isWasm") { + let is_wasm_v = &obj["isWasm"]; + if is_wasm_v.is_boolean() { + is_wasm = is_wasm_v.as_bool().unwrap(); + } + } + + Some(StackFrame { + line: line - 1, + column: column - 1, + source_url: script_name, + function_name, + is_eval, + is_constructor, + is_wasm, + }) + } + + fn apply_source_map( + &self, + mappings_map: &mut CachedMaps, + getter: &SourceMapGetter, + ) -> StackFrame { + let maybe_sm = get_mappings(self.source_url.as_ref(), mappings_map, getter); + let frame_pos = (self.source_url.to_owned(), self.line, self.column); + let (source_url, line, column) = match maybe_sm { + None => frame_pos, + Some(sm) => match sm.mappings.original_location_for( + self.line, + self.column, + Bias::default(), + ) { + None => frame_pos, + Some(mapping) => match &mapping.original { + None => frame_pos, + Some(original) => { + let orig_source = sm.sources[original.source as usize].clone(); + ( + orig_source, + original.original_line, + original.original_column, + ) + } + }, + }, + }; + + StackFrame { + source_url, + function_name: self.function_name.clone(), + line, + column, + is_eval: self.is_eval, + is_constructor: self.is_constructor, + is_wasm: self.is_wasm, + } + } +} + +impl SourceMap { + fn from_json(json_str: &str) -> Option<Self> { + // Ugly. Maybe use serde_derive. + match serde_json::from_str::<serde_json::Value>(json_str) { + Ok(serde_json::Value::Object(map)) => match map["mappings"].as_str() { + None => return None, + Some(mappings_str) => { + match parse_mappings::<()>(mappings_str.as_bytes()) { + Err(_) => return None, + Ok(mappings) => { + if !map["sources"].is_array() { + return None; + } + let sources_val = map["sources"].as_array().unwrap(); + let mut sources = Vec::<String>::new(); + + for source_val in sources_val { + match source_val.as_str() { + None => return None, + Some(source) => { + sources.push(source.to_string()); + } + } + } + + return Some(SourceMap { sources, mappings }); + } + } + } + }, + _ => return None, + } + } +} + +impl JSError { + /// Creates a new JSError by parsing the raw exception JSON string from V8. + pub fn from_v8_exception(json_str: &str) -> Option<Self> { + let v = serde_json::from_str::<serde_json::Value>(json_str); + if v.is_err() { + return None; + } + let v = v.unwrap(); + + if !v.is_object() { + return None; + } + let obj = v.as_object().unwrap(); + + let message_v = &obj["message"]; + if !message_v.is_string() { + return None; + } + let message = String::from(message_v.as_str().unwrap()); + + let frames_v = &obj["frames"]; + if !frames_v.is_array() { + return None; + } + let frame_values = frames_v.as_array().unwrap(); + + let mut frames = Vec::<StackFrame>::new(); + for frame_v in frame_values { + match StackFrame::from_json_value(frame_v) { + None => return None, + Some(frame) => frames.push(frame), + } + } + + Some(JSError { message, frames }) + } + + pub fn apply_source_map(&self, getter: &SourceMapGetter) -> Self { + let message = self.message.clone(); + let mut mappings_map: CachedMaps = HashMap::new(); + let mut frames = Vec::<StackFrame>::new(); + for frame in &self.frames { + let f = frame.apply_source_map(&mut mappings_map, getter); + frames.push(f); + } + JSError { message, frames } + } +} + +fn parse_map_string( + source_url: &str, + getter: &SourceMapGetter, +) -> Option<SourceMap> { + match source_url { + "gen/bundle/main.js" => { + let s = + include_str!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/main.js.map")); + SourceMap::from_json(s) + } + _ => match getter.get_source_map(source_url) { + None => None, + Some(raw_source_map) => SourceMap::from_json(&raw_source_map), + }, + } +} + +fn get_mappings<'a>( + source_url: &str, + mappings_map: &'a mut CachedMaps, + getter: &SourceMapGetter, +) -> &'a Option<SourceMap> { + mappings_map + .entry(source_url.to_string()) + .or_insert_with(|| parse_map_string(source_url, getter)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn error1() -> JSError { + JSError { + message: "Error: foo bar".to_string(), + frames: vec![ + StackFrame { + line: 4, + column: 16, + source_url: "foo_bar.ts".to_string(), + function_name: "foo".to_string(), + is_eval: false, + is_constructor: false, + is_wasm: false, + }, + StackFrame { + line: 5, + column: 20, + source_url: "bar_baz.ts".to_string(), + function_name: "qat".to_string(), + is_eval: false, + is_constructor: false, + is_wasm: false, + }, + StackFrame { + line: 1, + column: 1, + source_url: "deno_main.js".to_string(), + function_name: "".to_string(), + is_eval: false, + is_constructor: false, + is_wasm: false, + }, + ], + } + } + + struct MockSourceMapGetter {} + + impl SourceMapGetter for MockSourceMapGetter { + fn get_source_map(&self, script_name: &str) -> Option<String> { + let s = match script_name { + "foo_bar.ts" => r#"{"sources": ["foo_bar.ts"], "mappings":";;;IAIA,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC;IAC/C,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACjD,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAE3C,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC"}"#, + "bar_baz.ts" => r#"{"sources": ["bar_baz.ts"], "mappings":";;;IAEA,CAAC,KAAK,IAAI,EAAE;QACV,MAAM,GAAG,GAAG,sDAAa,OAAO,2BAAC,CAAC;QAClC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC,CAAC,EAAE,CAAC;IAEQ,QAAA,GAAG,GAAG,KAAK,CAAC;IAEzB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC"}"#, + _ => return None, + }; + Some(s.to_string()) + } + } + + #[test] + fn stack_frame_from_json_value_1() { + let v = serde_json::from_str::<serde_json::Value>( + r#"{ + "line":2, + "column":11, + "functionName":"foo", + "scriptName":"/Users/rld/src/deno/tests/error_001.ts", + "isEval":true, + "isConstructor":false, + "isWasm":false + }"#, + ).unwrap(); + let r = StackFrame::from_json_value(&v); + assert_eq!( + r, + Some(StackFrame { + line: 1, + column: 10, + source_url: "/Users/rld/src/deno/tests/error_001.ts".to_string(), + function_name: "foo".to_string(), + is_eval: true, + is_constructor: false, + is_wasm: false, + }) + ); + } + + #[test] + fn stack_frame_from_json_value_2() { + let v = serde_json::from_str::<serde_json::Value>( + r#"{ + "scriptName": "/Users/rld/src/deno/tests/error_001.ts", + "line": 2, + "column": 11 + }"#, + ).unwrap(); + let r = StackFrame::from_json_value(&v); + assert!(r.is_some()); + let f = r.unwrap(); + assert_eq!(f.line, 1); + assert_eq!(f.column, 10); + assert_eq!(f.source_url, "/Users/rld/src/deno/tests/error_001.ts"); + } + + #[test] + fn js_error_from_v8_exception() { + let r = JSError::from_v8_exception( + r#"{ + "message":"Uncaught Error: bad", + "frames":[ + { + "line":2, + "column":11, + "functionName":"foo", + "scriptName":"/Users/rld/src/deno/tests/error_001.ts", + "isEval":true, + "isConstructor":false, + "isWasm":false + }, { + "line":5, + "column":5, + "functionName":"bar", + "scriptName":"/Users/rld/src/deno/tests/error_001.ts", + "isEval":true, + "isConstructor":false, + "isWasm":false + } + ]}"#, + ); + assert!(r.is_some()); + let e = r.unwrap(); + assert_eq!(e.message, "Uncaught Error: bad"); + assert_eq!(e.frames.len(), 2); + assert_eq!( + e.frames[0], + StackFrame { + line: 1, + column: 10, + source_url: "/Users/rld/src/deno/tests/error_001.ts".to_string(), + function_name: "foo".to_string(), + is_eval: true, + is_constructor: false, + is_wasm: false, + } + ) + } + + #[test] + fn stack_frame_to_string() { + let e = error1(); + assert_eq!(" at foo (foo_bar.ts:5:17)", e.frames[0].to_string()); + assert_eq!(" at qat (bar_baz.ts:6:21)", e.frames[1].to_string()); + } + + #[test] + fn js_error_to_string() { + let e = error1(); + assert_eq!("Error: foo bar\n at foo (foo_bar.ts:5:17)\n at qat (bar_baz.ts:6:21)\n at deno_main.js:2:2", e.to_string()); + } + + #[test] + fn js_error_apply_source_map_1() { + let e = error1(); + let getter = MockSourceMapGetter {}; + let actual = e.apply_source_map(&getter); + let expected = JSError { + message: "Error: foo bar".to_string(), + frames: vec![ + StackFrame { + line: 5, + column: 12, + source_url: "foo_bar.ts".to_string(), + function_name: "foo".to_string(), + is_eval: false, + is_constructor: false, + is_wasm: false, + }, + StackFrame { + line: 4, + column: 14, + source_url: "bar_baz.ts".to_string(), + function_name: "qat".to_string(), + is_eval: false, + is_constructor: false, + is_wasm: false, + }, + StackFrame { + line: 1, + column: 1, + source_url: "deno_main.js".to_string(), + function_name: "".to_string(), + is_eval: false, + is_constructor: false, + is_wasm: false, + }, + ], + }; + assert_eq!(actual, expected); + } + + #[test] + fn js_error_apply_source_map_2() { + // Because this is accessing the live bundle, this test might be more fragile + let e = JSError { + message: "TypeError: baz".to_string(), + frames: vec![StackFrame { + line: 11, + column: 12, + source_url: "gen/bundle/main.js".to_string(), + function_name: "setLogDebug".to_string(), + is_eval: false, + is_constructor: false, + is_wasm: false, + }], + }; + let getter = MockSourceMapGetter {}; + let actual = e.apply_source_map(&getter); + assert_eq!(actual.message, "TypeError: baz"); + assert_eq!(actual.frames.len(), 1); + assert_eq!(actual.frames[0].line, 15); + assert_eq!(actual.frames[0].column, 16); + assert_eq!(actual.frames[0].source_url, "deno/js/util.ts"); + } + + #[test] + fn source_map_from_json() { + let json = r#"{"version":3,"file":"error_001.js","sourceRoot":"","sources":["file:///Users/rld/src/deno/tests/error_001.ts"],"names":[],"mappings":"AAAA,SAAS,GAAG;IACV,MAAM,KAAK,CAAC,KAAK,CAAC,CAAC;AACrB,CAAC;AAED,SAAS,GAAG;IACV,GAAG,EAAE,CAAC;AACR,CAAC;AAED,GAAG,EAAE,CAAC"}"#; + let sm = SourceMap::from_json(json).unwrap(); + assert_eq!(sm.sources.len(), 1); + assert_eq!( + sm.sources[0], + "file:///Users/rld/src/deno/tests/error_001.ts" + ); + let mapping = sm + .mappings + .original_location_for(1, 10, Bias::default()) + .unwrap(); + assert_eq!(mapping.generated_line, 1); + assert_eq!(mapping.generated_column, 10); + assert_eq!( + mapping.original, + Some(source_map_mappings::OriginalLocation { + source: 0, + original_line: 1, + original_column: 8, + name: None + }) + ); + } +} diff --git a/src/main.rs b/src/main.rs index f2e241ab1..cdb7d8dd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ extern crate rand; extern crate remove_dir_all; extern crate ring; extern crate rustyline; +extern crate serde_json; +extern crate source_map_mappings; extern crate tempfile; extern crate tokio; extern crate tokio_executor; @@ -33,6 +35,7 @@ mod fs; mod http_body; mod http_util; pub mod isolate; +pub mod js_errors; pub mod libdeno; pub mod msg; pub mod msg_util; @@ -68,6 +71,13 @@ impl log::Log for Logger { fn flush(&self) {} } +fn print_err_and_exit(err: js_errors::JSError) { + // TODO Currently tests depend on exception going to stdout. It should go + // to stderr. https://github.com/denoland/deno/issues/964 + println!("{}", err.to_string()); + std::process::exit(1); +} + fn main() { // Rust does not die on panic by default. And -Cpanic=abort is broken. // https://github.com/rust-lang/cargo/issues/2738 @@ -102,10 +112,7 @@ fn main() { tokio_util::init(|| { isolate .execute("deno_main.js", "denoMain();") - .unwrap_or_else(|err| { - error!("{}", err); - std::process::exit(1); - }); - isolate.event_loop(); + .unwrap_or_else(print_err_and_exit); + isolate.event_loop().unwrap_or_else(print_err_and_exit); }); } |