Skip to content

Commit 45a4d75

Browse files
authored
refactor(core): use Box<u8> for ModuleSource.code instead of a String (#14487)
1 parent 242273e commit 45a4d75

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

cli/proc_state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ impl ProcState {
592592
}
593593
};
594594
Ok(ModuleSource {
595-
code,
595+
code: code.into_bytes().into_boxed_slice(),
596596
module_url_specified: specifier.to_string(),
597597
module_url_found: found.to_string(),
598598
module_type: match media_type {
@@ -646,7 +646,8 @@ impl SourceMapGetter for ProcState {
646646
let code = String::from_utf8(code).unwrap();
647647
source_map_from_code(code).or(maybe_map)
648648
} else if let Ok(source) = self.load(specifier, None, false) {
649-
source_map_from_code(source.code)
649+
let code = String::from_utf8(source.code.to_vec()).unwrap();
650+
source_map_from_code(code)
650651
} else {
651652
None
652653
}

cli/standalone.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ impl ModuleLoader for EmbeddedModuleLoader {
168168
.ok_or_else(|| type_error("Module not found"));
169169

170170
async move {
171-
if let Some((ref source, _)) = is_data_uri {
171+
if let Some((source, _)) = is_data_uri {
172172
return Ok(deno_core::ModuleSource {
173-
code: source.to_owned(),
173+
code: source.into_bytes().into_boxed_slice(),
174174
module_type: deno_core::ModuleType::JavaScript,
175175
module_url_specified: module_specifier.to_string(),
176176
module_url_found: module_specifier.to_string(),
@@ -184,7 +184,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
184184
.to_owned();
185185

186186
Ok(deno_core::ModuleSource {
187-
code,
187+
code: code.into_bytes().into_boxed_slice(),
188188
module_type: match module.kind {
189189
eszip::ModuleKind::JavaScript => deno_core::ModuleType::JavaScript,
190190
eszip::ModuleKind::Json => deno_core::ModuleType::Json,

core/examples/ts_module_loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl ModuleLoader for TypescriptModuleLoader {
8080
code
8181
};
8282
let module = ModuleSource {
83-
code,
83+
code: code.into_bytes().into_boxed_slice(),
8484
module_type,
8585
module_url_specified: module_specifier.to_string(),
8686
module_url_found: module_specifier.to_string(),

core/modules.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ use std::task::Poll;
2626
pub type ModuleId = i32;
2727
pub(crate) type ModuleLoadId = i32;
2828

29-
pub const BOM_CHAR: char = '\u{FEFF}';
29+
pub const BOM_CHAR: &[u8] = &[0xef, 0xbb, 0xbf];
3030

3131
/// Strips the byte order mark from the provided text if it exists.
32-
fn strip_bom(text: &str) -> &str {
33-
if text.starts_with(BOM_CHAR) {
34-
&text[BOM_CHAR.len_utf8()..]
32+
fn strip_bom(source_code: &[u8]) -> &[u8] {
33+
if source_code.starts_with(BOM_CHAR) {
34+
&source_code[BOM_CHAR.len()..]
3535
} else {
36-
text
36+
source_code
3737
}
3838
}
3939

@@ -190,7 +190,7 @@ impl std::fmt::Display for ModuleType {
190190
// intermediate redirects from file loader.
191191
#[derive(Debug, Clone, Eq, PartialEq)]
192192
pub struct ModuleSource {
193-
pub code: String,
193+
pub code: Box<[u8]>,
194194
pub module_type: ModuleType,
195195
pub module_url_specified: String,
196196
pub module_url_found: String,
@@ -315,9 +315,9 @@ impl ModuleLoader for FsModuleLoader {
315315
ModuleType::JavaScript
316316
};
317317

318-
let code = std::fs::read_to_string(path)?;
318+
let code = std::fs::read(path)?;
319319
let module = ModuleSource {
320-
code,
320+
code: code.into_boxed_slice(),
321321
module_type,
322322
module_url_specified: module_specifier.to_string(),
323323
module_url_found: module_specifier.to_string(),
@@ -781,10 +781,15 @@ impl ModuleMap {
781781
&mut self,
782782
scope: &mut v8::HandleScope,
783783
name: &str,
784-
source: &str,
784+
source: &[u8],
785785
) -> Result<ModuleId, ModuleError> {
786786
let name_str = v8::String::new(scope, name).unwrap();
787-
let source_str = v8::String::new(scope, strip_bom(source)).unwrap();
787+
let source_str = v8::String::new_from_utf8(
788+
scope,
789+
strip_bom(source),
790+
v8::NewStringType::Normal,
791+
)
792+
.unwrap();
788793

789794
let tc_scope = &mut v8::TryCatch::new(scope);
790795

@@ -822,10 +827,12 @@ impl ModuleMap {
822827
scope: &mut v8::HandleScope,
823828
main: bool,
824829
name: &str,
825-
source: &str,
830+
source: &[u8],
826831
) -> Result<ModuleId, ModuleError> {
827832
let name_str = v8::String::new(scope, name).unwrap();
828-
let source_str = v8::String::new(scope, source).unwrap();
833+
let source_str =
834+
v8::String::new_from_utf8(scope, source, v8::NewStringType::Normal)
835+
.unwrap();
829836

830837
let origin = bindings::module_origin(scope, name_str);
831838
let source = v8::script_compiler::Source::new(source_str, Some(&origin));
@@ -1258,7 +1265,7 @@ import "/a.js";
12581265
}
12591266
match mock_source_code(&inner.url) {
12601267
Some(src) => Poll::Ready(Ok(ModuleSource {
1261-
code: src.0.to_owned(),
1268+
code: src.0.as_bytes().to_vec().into_boxed_slice(),
12621269
module_type: ModuleType::JavaScript,
12631270
module_url_specified: inner.url.clone(),
12641271
module_url_found: src.1.to_owned(),
@@ -1454,7 +1461,7 @@ import "/a.js";
14541461
scope,
14551462
true,
14561463
&specifier_a,
1457-
r#"
1464+
br#"
14581465
import { b } from './b.js'
14591466
if (b() != 'b') throw Error();
14601467
let control = 42;
@@ -1478,7 +1485,7 @@ import "/a.js";
14781485
scope,
14791486
false,
14801487
"file:///b.js",
1481-
"export function b() { return 'b' }",
1488+
b"export function b() { return 'b' }",
14821489
)
14831490
.unwrap();
14841491
let imports = module_map.get_requested_modules(mod_b).unwrap();
@@ -1561,7 +1568,7 @@ import "/a.js";
15611568
scope,
15621569
true,
15631570
&specifier_a,
1564-
r#"
1571+
br#"
15651572
import jsonData from './b.json' assert {type: "json"};
15661573
assert(jsonData.a == "b");
15671574
assert(jsonData.c.d == 10);
@@ -1582,7 +1589,7 @@ import "/a.js";
15821589
.new_json_module(
15831590
scope,
15841591
"file:///b.json",
1585-
"{\"a\": \"b\", \"c\": {\"d\": 10}}",
1592+
b"{\"a\": \"b\", \"c\": {\"d\": 10}}",
15861593
)
15871594
.unwrap();
15881595
let imports = module_map.get_requested_modules(mod_b).unwrap();
@@ -1692,7 +1699,9 @@ import "/a.js";
16921699
let info = ModuleSource {
16931700
module_url_specified: specifier.to_string(),
16941701
module_url_found: specifier.to_string(),
1695-
code: "export function b() { return 'b' }".to_owned(),
1702+
code: b"export function b() { return 'b' }"
1703+
.to_vec()
1704+
.into_boxed_slice(),
16961705
module_type: ModuleType::JavaScript,
16971706
};
16981707
async move { Ok(info) }.boxed()
@@ -1836,7 +1845,7 @@ import "/a.js";
18361845
let info = ModuleSource {
18371846
module_url_specified: specifier.to_string(),
18381847
module_url_found: specifier.to_string(),
1839-
code: code.to_owned(),
1848+
code: code.as_bytes().to_vec().into_boxed_slice(),
18401849
module_type: ModuleType::JavaScript,
18411850
};
18421851
async move { Ok(info) }.boxed()
@@ -2184,13 +2193,17 @@ if (import.meta.url != 'file:///main_with_code.js') throw Error();
21842193
"file:///main_module.js" => Ok(ModuleSource {
21852194
module_url_specified: "file:///main_module.js".to_string(),
21862195
module_url_found: "file:///main_module.js".to_string(),
2187-
code: "if (!import.meta.main) throw Error();".to_owned(),
2196+
code: b"if (!import.meta.main) throw Error();"
2197+
.to_vec()
2198+
.into_boxed_slice(),
21882199
module_type: ModuleType::JavaScript,
21892200
}),
21902201
"file:///side_module.js" => Ok(ModuleSource {
21912202
module_url_specified: "file:///side_module.js".to_string(),
21922203
module_url_found: "file:///side_module.js".to_string(),
2193-
code: "if (import.meta.main) throw Error();".to_owned(),
2204+
code: b"if (import.meta.main) throw Error();"
2205+
.to_vec()
2206+
.into_boxed_slice(),
21942207
module_type: ModuleType::JavaScript,
21952208
}),
21962209
_ => unreachable!(),

core/runtime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ impl JsRuntime {
15541554
// main module
15551555
true,
15561556
specifier.as_str(),
1557-
&code,
1557+
code.as_bytes(),
15581558
)
15591559
.map_err(|e| match e {
15601560
ModuleError::Exception(exception) => {
@@ -1613,7 +1613,7 @@ impl JsRuntime {
16131613
// not main module
16141614
false,
16151615
specifier.as_str(),
1616-
&code,
1616+
code.as_bytes(),
16171617
)
16181618
.map_err(|e| match e {
16191619
ModuleError::Exception(exception) => {
@@ -3022,7 +3022,7 @@ assertEquals(1, notify_return_value);
30223022
) -> Pin<Box<ModuleSourceFuture>> {
30233023
async move {
30243024
Ok(ModuleSource {
3025-
code: "console.log('hello world');".to_string(),
3025+
code: b"console.log('hello world');".to_vec().into_boxed_slice(),
30263026
module_url_specified: "file:///main.js".to_string(),
30273027
module_url_found: "file:///main.js".to_string(),
30283028
module_type: ModuleType::JavaScript,

0 commit comments

Comments
 (0)