summaryrefslogtreecommitdiff
path: root/core/modules.rs
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules.rs')
-rw-r--r--core/modules.rs55
1 files changed, 34 insertions, 21 deletions
diff --git a/core/modules.rs b/core/modules.rs
index 79ee9ebf5..01467b721 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<ModuleId, ModuleError> {
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<ModuleId, ModuleError> {
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!(),