Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ harness = false
name = "typescript_transform_latency"
harness = false

[[test]]
name = "esm_module_load_phases"
harness = false

[[test]]
name = "migrate_config_split"
harness = false
Expand Down
32 changes: 32 additions & 0 deletions crates/wasm-rquickjs/skeleton/src/internal/module_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,10 @@ fn collect_declared_cjs_globals_in_esm(source: &str) -> Vec<String> {
let mut i = 0usize;
let mut declared = Vec::<String>::new();
while i < bytes.len() {
if bytes[i].is_ascii_whitespace() {
i = skip_ascii_whitespace(source, i);
continue;
}
if let Some(next) = skip_esm_cjs_global_scanner_span(source, i) {
i = next;
continue;
Expand Down Expand Up @@ -2241,6 +2245,10 @@ fn find_bare_cjs_global_in_esm_among(
scopes.pop();
}

if bytes[i].is_ascii_whitespace() {
i = skip_ascii_whitespace(source, i);
continue;
}
if let Some(next) = skip_esm_cjs_global_scanner_span(source, i) {
i = next;
continue;
Expand Down Expand Up @@ -7207,6 +7215,14 @@ fn skip_ws_comments(source: &str, pos: usize) -> usize {
skip_ws_comments_impl::<false>(source, pos).0
}

fn skip_ascii_whitespace(source: &str, mut pos: usize) -> usize {
let bytes = source.as_bytes();
while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
pos
}

fn skip_ws_comments_with_line_terminator(source: &str, pos: usize) -> (usize, bool) {
skip_ws_comments_impl::<true>(source, pos)
}
Expand Down Expand Up @@ -11848,6 +11864,9 @@ pub(crate) async fn initialize_module_loading(rt: &AsyncRuntime, ctx: &AsyncCont
}

fn rewrite_import_meta_main(source: &str, replacement: &str) -> String {
if !source.contains("import") {
return source.to_string();
}
let mut spans = Vec::new();
let _ = scan_code_positions(source, true, |i, _| {
if let Some(end) = parse_import_meta_main_span(source, i) {
Expand Down Expand Up @@ -12707,6 +12726,19 @@ import "./dep.js" withあ;
);
}

#[test]
fn dense_whitespace_keeps_esm_scanner_results() {
let padding = " \n\t\r".repeat(16_384);
let source = format!("{padding}export default 42;");

assert_cjs_global(&source, None);
assert!(collect_declared_cjs_globals_in_esm(&source).is_empty());
assert_eq!(rewrite_import_meta_main(&source, "false"), source);

let with_require = format!("{padding}export default require;");
assert_cjs_global(&with_require, Some("require"));
}

#[test]
fn package_type_diagnostics_ignore_local_exports_binding() {
assert!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import fs from 'node:fs';
import { stripTypeScriptTypes } from 'node:module';
import { runJavaScript } from 'wasm-rquickjs:execution';

const ROOT = '/esm-module-load-phases';

function typedPrefix(sourceBytes) {
const lines = [];
let length = 0;
for (let index = 0; length < sourceBytes; index++) {
const line = `type Padding${index} = { value: number; next?: Padding${index} };\n`;
lines.push(line);
length += line.length;
}
return lines.join('');
}

export async function measureCase(sourceBytes, sample) {
fs.mkdirSync(ROOT, { recursive: true });
const path = `${ROOT}/prepared-${sourceBytes}-${sample}.mjs`;
const typedSource = `${typedPrefix(Number(sourceBytes))}
globalThis.__esmPhaseMarks.evaluationStart = performance.now();
export default function run(): number { return 42; }
globalThis.__esmPhaseMarks.evaluationEnd = performance.now();`;
const preparedSource = stripTypeScriptTypes(typedSource, { mode: 'strip' });
fs.writeFileSync(path, preparedSource);

const started = performance.now();
const execution = await runJavaScript({ source: `
globalThis.__esmPhaseMarks = {};
globalThis.__esmPhaseMarks.importStart = performance.now();
const loaded = await import(${JSON.stringify(path)});
globalThis.__esmPhaseMarks.importResolved = performance.now();
return {
value: loaded.default(),
marks: globalThis.__esmPhaseMarks,
};
` });
return JSON.stringify({
requestedSourceBytes: Number(sourceBytes),
actualSourceBytes: typedSource.length,
preparedSourceBytes: preparedSource.length,
elapsedMs: performance.now() - started,
value: execution.value.value,
marks: execution.value.marks,
overflowed: execution.overflowed,
profile: execution.profile,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package quickjs:esm-module-load-phases;

world esm-module-load-phases {
export measure-case: func(source-bytes: u64, sample: u64) -> string;
}
Loading
Loading