diff --git a/cli/proc_state.rs b/cli/proc_state.rs index d4173ee2e1d818..8df68d15bf89bb 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -592,7 +592,7 @@ impl ProcState { } }; Ok(ModuleSource { - code, + code: code.into_bytes().into_boxed_slice(), module_url_specified: specifier.to_string(), module_url_found: found.to_string(), module_type: match media_type { @@ -646,7 +646,8 @@ impl SourceMapGetter for ProcState { let code = String::from_utf8(code).unwrap(); source_map_from_code(code).or(maybe_map) } else if let Ok(source) = self.load(specifier, None, false) { - source_map_from_code(source.code) + let code = String::from_utf8(source.code.to_vec()).unwrap(); + source_map_from_code(code) } else { None } diff --git a/cli/standalone.rs b/cli/standalone.rs index 1d75734be974c9..13bc3e70ff455d 100644 --- a/cli/standalone.rs +++ b/cli/standalone.rs @@ -168,9 +168,9 @@ impl ModuleLoader for EmbeddedModuleLoader { .ok_or_else(|| type_error("Module not found")); async move { - if let Some((ref source, _)) = is_data_uri { + if let Some((source, _)) = is_data_uri { return Ok(deno_core::ModuleSource { - code: source.to_owned(), + code: source.into_bytes().into_boxed_slice(), module_type: deno_core::ModuleType::JavaScript, module_url_specified: module_specifier.to_string(), module_url_found: module_specifier.to_string(), @@ -184,7 +184,7 @@ impl ModuleLoader for EmbeddedModuleLoader { .to_owned(); Ok(deno_core::ModuleSource { - code, + code: code.into_bytes().into_boxed_slice(), module_type: match module.kind { eszip::ModuleKind::JavaScript => deno_core::ModuleType::JavaScript, eszip::ModuleKind::Json => deno_core::ModuleType::Json, diff --git a/core/examples/ts_module_loader.rs b/core/examples/ts_module_loader.rs index 963533ffc65d68..68ee06e60eff0e 100644 --- a/core/examples/ts_module_loader.rs +++ b/core/examples/ts_module_loader.rs @@ -80,7 +80,7 @@ impl ModuleLoader for TypescriptModuleLoader { code }; let module = ModuleSource { - code, + code: code.into_bytes().into_boxed_slice(), module_type, module_url_specified: module_specifier.to_string(), module_url_found: module_specifier.to_string(), diff --git a/core/modules.rs b/core/modules.rs index 79ee9ebf5e6d77..01467b721ead77 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -26,14 +26,14 @@ use std::task::Poll; pub type ModuleId = i32; pub(crate) type ModuleLoadId = i32; -pub const BOM_CHAR: char = '\u{FEFF}'; +pub const BOM_CHAR: &[u8] = &[0xef, 0xbb, 0xbf]; /// Strips the byte order mark from the provided text if it exists. -fn strip_bom(text: &str) -> &str { - if text.starts_with(BOM_CHAR) { - &text[BOM_CHAR.len_utf8()..] +fn strip_bom(source_code: &[u8]) -> &[u8] { + if source_code.starts_with(BOM_CHAR) { + &source_code[BOM_CHAR.len()..] } else { - text + source_code } } @@ -190,7 +190,7 @@ impl std::fmt::Display for ModuleType { // intermediate redirects from file loader. #[derive(Debug, Clone, Eq, PartialEq)] pub struct ModuleSource { - pub code: String, + pub code: Box<[u8]>, pub module_type: ModuleType, pub module_url_specified: String, pub module_url_found: String, @@ -315,9 +315,9 @@ impl ModuleLoader for FsModuleLoader { ModuleType::JavaScript }; - let code = std::fs::read_to_string(path)?; + let code = std::fs::read(path)?; let module = ModuleSource { - code, + code: code.into_boxed_slice(), module_type, module_url_specified: module_specifier.to_string(), module_url_found: module_specifier.to_string(), @@ -781,10 +781,15 @@ impl ModuleMap { &mut self, scope: &mut v8::HandleScope, name: &str, - source: &str, + source: &[u8], ) -> Result { let name_str = v8::String::new(scope, name).unwrap(); - let source_str = v8::String::new(scope, strip_bom(source)).unwrap(); + let source_str = v8::String::new_from_utf8( + scope, + strip_bom(source), + v8::NewStringType::Normal, + ) + .unwrap(); let tc_scope = &mut v8::TryCatch::new(scope); @@ -822,10 +827,12 @@ impl ModuleMap { scope: &mut v8::HandleScope, main: bool, name: &str, - source: &str, + source: &[u8], ) -> Result { let name_str = v8::String::new(scope, name).unwrap(); - let source_str = v8::String::new(scope, source).unwrap(); + let source_str = + v8::String::new_from_utf8(scope, source, v8::NewStringType::Normal) + .unwrap(); let origin = bindings::module_origin(scope, name_str); let source = v8::script_compiler::Source::new(source_str, Some(&origin)); @@ -1258,7 +1265,7 @@ import "/a.js"; } match mock_source_code(&inner.url) { Some(src) => Poll::Ready(Ok(ModuleSource { - code: src.0.to_owned(), + code: src.0.as_bytes().to_vec().into_boxed_slice(), module_type: ModuleType::JavaScript, module_url_specified: inner.url.clone(), module_url_found: src.1.to_owned(), @@ -1454,7 +1461,7 @@ import "/a.js"; scope, true, &specifier_a, - r#" + br#" import { b } from './b.js' if (b() != 'b') throw Error(); let control = 42; @@ -1478,7 +1485,7 @@ import "/a.js"; scope, false, "file:///b.js", - "export function b() { return 'b' }", + b"export function b() { return 'b' }", ) .unwrap(); let imports = module_map.get_requested_modules(mod_b).unwrap(); @@ -1561,7 +1568,7 @@ import "/a.js"; scope, true, &specifier_a, - r#" + br#" import jsonData from './b.json' assert {type: "json"}; assert(jsonData.a == "b"); assert(jsonData.c.d == 10); @@ -1582,7 +1589,7 @@ import "/a.js"; .new_json_module( scope, "file:///b.json", - "{\"a\": \"b\", \"c\": {\"d\": 10}}", + b"{\"a\": \"b\", \"c\": {\"d\": 10}}", ) .unwrap(); let imports = module_map.get_requested_modules(mod_b).unwrap(); @@ -1692,7 +1699,9 @@ import "/a.js"; let info = ModuleSource { module_url_specified: specifier.to_string(), module_url_found: specifier.to_string(), - code: "export function b() { return 'b' }".to_owned(), + code: b"export function b() { return 'b' }" + .to_vec() + .into_boxed_slice(), module_type: ModuleType::JavaScript, }; async move { Ok(info) }.boxed() @@ -1836,7 +1845,7 @@ import "/a.js"; let info = ModuleSource { module_url_specified: specifier.to_string(), module_url_found: specifier.to_string(), - code: code.to_owned(), + code: code.as_bytes().to_vec().into_boxed_slice(), module_type: ModuleType::JavaScript, }; async move { Ok(info) }.boxed() @@ -2184,13 +2193,17 @@ if (import.meta.url != 'file:///main_with_code.js') throw Error(); "file:///main_module.js" => Ok(ModuleSource { module_url_specified: "file:///main_module.js".to_string(), module_url_found: "file:///main_module.js".to_string(), - code: "if (!import.meta.main) throw Error();".to_owned(), + code: b"if (!import.meta.main) throw Error();" + .to_vec() + .into_boxed_slice(), module_type: ModuleType::JavaScript, }), "file:///side_module.js" => Ok(ModuleSource { module_url_specified: "file:///side_module.js".to_string(), module_url_found: "file:///side_module.js".to_string(), - code: "if (import.meta.main) throw Error();".to_owned(), + code: b"if (import.meta.main) throw Error();" + .to_vec() + .into_boxed_slice(), module_type: ModuleType::JavaScript, }), _ => unreachable!(), diff --git a/core/runtime.rs b/core/runtime.rs index 865995f749056c..f589ad25b8ff4e 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1554,7 +1554,7 @@ impl JsRuntime { // main module true, specifier.as_str(), - &code, + code.as_bytes(), ) .map_err(|e| match e { ModuleError::Exception(exception) => { @@ -1613,7 +1613,7 @@ impl JsRuntime { // not main module false, specifier.as_str(), - &code, + code.as_bytes(), ) .map_err(|e| match e { ModuleError::Exception(exception) => { @@ -3022,7 +3022,7 @@ assertEquals(1, notify_return_value); ) -> Pin> { async move { Ok(ModuleSource { - code: "console.log('hello world');".to_string(), + code: b"console.log('hello world');".to_vec().into_boxed_slice(), module_url_specified: "file:///main.js".to_string(), module_url_found: "file:///main.js".to_string(), module_type: ModuleType::JavaScript,