0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor(core): use Box<u8> for ModuleSource.code instead of a String (#14487)

This commit is contained in:
Bartek Iwańczuk 2022-05-05 13:16:25 +02:00 committed by GitHub
parent 242273e69b
commit 45a4d75296
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 30 deletions

View file

@ -592,7 +592,7 @@ impl ProcState {
} }
}; };
Ok(ModuleSource { Ok(ModuleSource {
code, code: code.into_bytes().into_boxed_slice(),
module_url_specified: specifier.to_string(), module_url_specified: specifier.to_string(),
module_url_found: found.to_string(), module_url_found: found.to_string(),
module_type: match media_type { module_type: match media_type {
@ -646,7 +646,8 @@ impl SourceMapGetter for ProcState {
let code = String::from_utf8(code).unwrap(); let code = String::from_utf8(code).unwrap();
source_map_from_code(code).or(maybe_map) source_map_from_code(code).or(maybe_map)
} else if let Ok(source) = self.load(specifier, None, false) { } 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 { } else {
None None
} }

View file

@ -168,9 +168,9 @@ impl ModuleLoader for EmbeddedModuleLoader {
.ok_or_else(|| type_error("Module not found")); .ok_or_else(|| type_error("Module not found"));
async move { async move {
if let Some((ref source, _)) = is_data_uri { if let Some((source, _)) = is_data_uri {
return Ok(deno_core::ModuleSource { return Ok(deno_core::ModuleSource {
code: source.to_owned(), code: source.into_bytes().into_boxed_slice(),
module_type: deno_core::ModuleType::JavaScript, module_type: deno_core::ModuleType::JavaScript,
module_url_specified: module_specifier.to_string(), module_url_specified: module_specifier.to_string(),
module_url_found: module_specifier.to_string(), module_url_found: module_specifier.to_string(),
@ -184,7 +184,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
.to_owned(); .to_owned();
Ok(deno_core::ModuleSource { Ok(deno_core::ModuleSource {
code, code: code.into_bytes().into_boxed_slice(),
module_type: match module.kind { module_type: match module.kind {
eszip::ModuleKind::JavaScript => deno_core::ModuleType::JavaScript, eszip::ModuleKind::JavaScript => deno_core::ModuleType::JavaScript,
eszip::ModuleKind::Json => deno_core::ModuleType::Json, eszip::ModuleKind::Json => deno_core::ModuleType::Json,

View file

@ -80,7 +80,7 @@ impl ModuleLoader for TypescriptModuleLoader {
code code
}; };
let module = ModuleSource { let module = ModuleSource {
code, code: code.into_bytes().into_boxed_slice(),
module_type, module_type,
module_url_specified: module_specifier.to_string(), module_url_specified: module_specifier.to_string(),
module_url_found: module_specifier.to_string(), module_url_found: module_specifier.to_string(),

View file

@ -26,14 +26,14 @@ use std::task::Poll;
pub type ModuleId = i32; pub type ModuleId = i32;
pub(crate) type ModuleLoadId = 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. /// Strips the byte order mark from the provided text if it exists.
fn strip_bom(text: &str) -> &str { fn strip_bom(source_code: &[u8]) -> &[u8] {
if text.starts_with(BOM_CHAR) { if source_code.starts_with(BOM_CHAR) {
&text[BOM_CHAR.len_utf8()..] &source_code[BOM_CHAR.len()..]
} else { } else {
text source_code
} }
} }
@ -190,7 +190,7 @@ impl std::fmt::Display for ModuleType {
// intermediate redirects from file loader. // intermediate redirects from file loader.
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct ModuleSource { pub struct ModuleSource {
pub code: String, pub code: Box<[u8]>,
pub module_type: ModuleType, pub module_type: ModuleType,
pub module_url_specified: String, pub module_url_specified: String,
pub module_url_found: String, pub module_url_found: String,
@ -315,9 +315,9 @@ impl ModuleLoader for FsModuleLoader {
ModuleType::JavaScript ModuleType::JavaScript
}; };
let code = std::fs::read_to_string(path)?; let code = std::fs::read(path)?;
let module = ModuleSource { let module = ModuleSource {
code, code: code.into_boxed_slice(),
module_type, module_type,
module_url_specified: module_specifier.to_string(), module_url_specified: module_specifier.to_string(),
module_url_found: module_specifier.to_string(), module_url_found: module_specifier.to_string(),
@ -781,10 +781,15 @@ impl ModuleMap {
&mut self, &mut self,
scope: &mut v8::HandleScope, scope: &mut v8::HandleScope,
name: &str, name: &str,
source: &str, source: &[u8],
) -> Result<ModuleId, ModuleError> { ) -> Result<ModuleId, ModuleError> {
let name_str = v8::String::new(scope, name).unwrap(); 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); let tc_scope = &mut v8::TryCatch::new(scope);
@ -822,10 +827,12 @@ impl ModuleMap {
scope: &mut v8::HandleScope, scope: &mut v8::HandleScope,
main: bool, main: bool,
name: &str, name: &str,
source: &str, source: &[u8],
) -> Result<ModuleId, ModuleError> { ) -> Result<ModuleId, ModuleError> {
let name_str = v8::String::new(scope, name).unwrap(); 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 origin = bindings::module_origin(scope, name_str);
let source = v8::script_compiler::Source::new(source_str, Some(&origin)); let source = v8::script_compiler::Source::new(source_str, Some(&origin));
@ -1258,7 +1265,7 @@ import "/a.js";
} }
match mock_source_code(&inner.url) { match mock_source_code(&inner.url) {
Some(src) => Poll::Ready(Ok(ModuleSource { 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_type: ModuleType::JavaScript,
module_url_specified: inner.url.clone(), module_url_specified: inner.url.clone(),
module_url_found: src.1.to_owned(), module_url_found: src.1.to_owned(),
@ -1454,7 +1461,7 @@ import "/a.js";
scope, scope,
true, true,
&specifier_a, &specifier_a,
r#" br#"
import { b } from './b.js' import { b } from './b.js'
if (b() != 'b') throw Error(); if (b() != 'b') throw Error();
let control = 42; let control = 42;
@ -1478,7 +1485,7 @@ import "/a.js";
scope, scope,
false, false,
"file:///b.js", "file:///b.js",
"export function b() { return 'b' }", b"export function b() { return 'b' }",
) )
.unwrap(); .unwrap();
let imports = module_map.get_requested_modules(mod_b).unwrap(); let imports = module_map.get_requested_modules(mod_b).unwrap();
@ -1561,7 +1568,7 @@ import "/a.js";
scope, scope,
true, true,
&specifier_a, &specifier_a,
r#" br#"
import jsonData from './b.json' assert {type: "json"}; import jsonData from './b.json' assert {type: "json"};
assert(jsonData.a == "b"); assert(jsonData.a == "b");
assert(jsonData.c.d == 10); assert(jsonData.c.d == 10);
@ -1582,7 +1589,7 @@ import "/a.js";
.new_json_module( .new_json_module(
scope, scope,
"file:///b.json", "file:///b.json",
"{\"a\": \"b\", \"c\": {\"d\": 10}}", b"{\"a\": \"b\", \"c\": {\"d\": 10}}",
) )
.unwrap(); .unwrap();
let imports = module_map.get_requested_modules(mod_b).unwrap(); let imports = module_map.get_requested_modules(mod_b).unwrap();
@ -1692,7 +1699,9 @@ import "/a.js";
let info = ModuleSource { let info = ModuleSource {
module_url_specified: specifier.to_string(), module_url_specified: specifier.to_string(),
module_url_found: 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, module_type: ModuleType::JavaScript,
}; };
async move { Ok(info) }.boxed() async move { Ok(info) }.boxed()
@ -1836,7 +1845,7 @@ import "/a.js";
let info = ModuleSource { let info = ModuleSource {
module_url_specified: specifier.to_string(), module_url_specified: specifier.to_string(),
module_url_found: 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, module_type: ModuleType::JavaScript,
}; };
async move { Ok(info) }.boxed() 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 { "file:///main_module.js" => Ok(ModuleSource {
module_url_specified: "file:///main_module.js".to_string(), module_url_specified: "file:///main_module.js".to_string(),
module_url_found: "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, module_type: ModuleType::JavaScript,
}), }),
"file:///side_module.js" => Ok(ModuleSource { "file:///side_module.js" => Ok(ModuleSource {
module_url_specified: "file:///side_module.js".to_string(), module_url_specified: "file:///side_module.js".to_string(),
module_url_found: "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, module_type: ModuleType::JavaScript,
}), }),
_ => unreachable!(), _ => unreachable!(),

View file

@ -1554,7 +1554,7 @@ impl JsRuntime {
// main module // main module
true, true,
specifier.as_str(), specifier.as_str(),
&code, code.as_bytes(),
) )
.map_err(|e| match e { .map_err(|e| match e {
ModuleError::Exception(exception) => { ModuleError::Exception(exception) => {
@ -1613,7 +1613,7 @@ impl JsRuntime {
// not main module // not main module
false, false,
specifier.as_str(), specifier.as_str(),
&code, code.as_bytes(),
) )
.map_err(|e| match e { .map_err(|e| match e {
ModuleError::Exception(exception) => { ModuleError::Exception(exception) => {
@ -3022,7 +3022,7 @@ assertEquals(1, notify_return_value);
) -> Pin<Box<ModuleSourceFuture>> { ) -> Pin<Box<ModuleSourceFuture>> {
async move { async move {
Ok(ModuleSource { 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_specified: "file:///main.js".to_string(),
module_url_found: "file:///main.js".to_string(), module_url_found: "file:///main.js".to_string(),
module_type: ModuleType::JavaScript, module_type: ModuleType::JavaScript,