From 4b98fa5b5f35a1603740675e27f07424f53d3b82 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 16 Aug 2024 15:24:08 +0900 Subject: [PATCH 1/3] fix(ext/node): fix prismjs compatibiliy in Web Worker --- ext/node/global.rs | 4 ++-- .../npm/@denotest/check-worker-globals/1.0.0/index.js | 7 +++++++ .../npm/@denotest/check-worker-globals/1.0.0/package.json | 5 +++++ tests/specs/npm/workers/main.out | 1 + tests/specs/npm/workers/main.ts | 3 +++ tests/specs/npm/workers/worker4.ts | 4 ++++ 6 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js create mode 100644 tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json create mode 100644 tests/specs/npm/workers/worker4.ts diff --git a/ext/node/global.rs b/ext/node/global.rs index 0fc215bd4d7790..a24917068c7dfb 100644 --- a/ext/node/global.rs +++ b/ext/node/global.rs @@ -65,6 +65,7 @@ const fn str_to_utf16(s: &str) -> [u16; N] { #[rustfmt::skip] const MANAGED_GLOBALS: [&[u16]; 13] = [ &str_to_utf16::<6>("Buffer"), + &str_to_utf16::<17>("WorkerGlobalScope"), &str_to_utf16::<14>("clearImmediate"), &str_to_utf16::<13>("clearInterval"), &str_to_utf16::<12>("clearTimeout"), @@ -76,11 +77,10 @@ const MANAGED_GLOBALS: [&[u16]; 13] = [ &str_to_utf16::<12>("setImmediate"), &str_to_utf16::<11>("setInterval"), &str_to_utf16::<10>("setTimeout"), - &str_to_utf16::<6>("window"), ]; const SHORTEST_MANAGED_GLOBAL: usize = 4; -const LONGEST_MANAGED_GLOBAL: usize = 14; +const LONGEST_MANAGED_GLOBAL: usize = 17; #[derive(Debug, Clone, Copy)] enum Mode { diff --git a/tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js new file mode 100644 index 00000000000000..8da68f7914c9f4 --- /dev/null +++ b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js @@ -0,0 +1,7 @@ +if (typeof self !== "undefined") { + throw new Error("self is defined"); +} + +if (typeof WorkerGlobalScope !== "undefined") { + throw new Error("WorkerGlobalScope is defined"); +} diff --git a/tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json new file mode 100644 index 00000000000000..b78fa210a5d815 --- /dev/null +++ b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "@denotest/check-worker-globals", + "version": "1.0.0", + "main": "index.js" +} diff --git a/tests/specs/npm/workers/main.out b/tests/specs/npm/workers/main.out index 55ff51cd3d811b..f1f2d6a36f60b1 100644 --- a/tests/specs/npm/workers/main.out +++ b/tests/specs/npm/workers/main.out @@ -2,4 +2,5 @@ 1 2 3 +4 [UNORDERED_END] diff --git a/tests/specs/npm/workers/main.ts b/tests/specs/npm/workers/main.ts index f51cf4d902f8d0..5755807902d7c6 100644 --- a/tests/specs/npm/workers/main.ts +++ b/tests/specs/npm/workers/main.ts @@ -7,3 +7,6 @@ new Worker(new URL("./worker2.ts", import.meta.url), { new Worker(new URL("./worker3.ts", import.meta.url), { type: "module", }); +new Worker(new URL("./worker4.ts", import.meta.url), { + type: "module", +}); diff --git a/tests/specs/npm/workers/worker4.ts b/tests/specs/npm/workers/worker4.ts new file mode 100644 index 00000000000000..fab3f4f9ab1797 --- /dev/null +++ b/tests/specs/npm/workers/worker4.ts @@ -0,0 +1,4 @@ +import "npm:@denotest/check-worker-globals"; + +console.log(4); +self.close(); From 7a15b7a5ecfb820643304b152f8c66ce4c905207 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 16 Aug 2024 16:30:33 +0900 Subject: [PATCH 2/3] fix --- ext/node/global.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/node/global.rs b/ext/node/global.rs index a24917068c7dfb..5eaa2ff411b67b 100644 --- a/ext/node/global.rs +++ b/ext/node/global.rs @@ -63,7 +63,7 @@ const fn str_to_utf16(s: &str) -> [u16; N] { // UTF-16 encodings of the managed globals. THIS LIST MUST BE SORTED. #[rustfmt::skip] -const MANAGED_GLOBALS: [&[u16]; 13] = [ +const MANAGED_GLOBALS: [&[u16]; 14] = [ &str_to_utf16::<6>("Buffer"), &str_to_utf16::<17>("WorkerGlobalScope"), &str_to_utf16::<14>("clearImmediate"), @@ -77,6 +77,7 @@ const MANAGED_GLOBALS: [&[u16]; 13] = [ &str_to_utf16::<12>("setImmediate"), &str_to_utf16::<11>("setInterval"), &str_to_utf16::<10>("setTimeout"), + &str_to_utf16::<6>("window"), ]; const SHORTEST_MANAGED_GLOBAL: usize = 4; From 453dc1c1fa05b3bba192a54353fb9634e12334bb Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 16 Aug 2024 23:45:02 +0900 Subject: [PATCH 3/3] calc shortest and longest compile time --- ext/node/global.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/ext/node/global.rs b/ext/node/global.rs index 5eaa2ff411b67b..0b4adfc7d1c340 100644 --- a/ext/node/global.rs +++ b/ext/node/global.rs @@ -80,8 +80,25 @@ const MANAGED_GLOBALS: [&[u16]; 14] = [ &str_to_utf16::<6>("window"), ]; -const SHORTEST_MANAGED_GLOBAL: usize = 4; -const LONGEST_MANAGED_GLOBAL: usize = 17; +// Calculates the shortest & longest length of global var names +const MANAGED_GLOBALS_INFO: (usize, usize) = { + let l = MANAGED_GLOBALS[0].len(); + let (mut longest, mut shortest, mut i) = (l, l, 1); + while i < MANAGED_GLOBALS.len() { + let l = MANAGED_GLOBALS[i].len(); + if l > longest { + longest = l + } + if l < shortest { + shortest = l + } + i += 1; + } + (shortest, longest) +}; + +const SHORTEST_MANAGED_GLOBAL: usize = MANAGED_GLOBALS_INFO.0; +const LONGEST_MANAGED_GLOBAL: usize = MANAGED_GLOBALS_INFO.1; #[derive(Debug, Clone, Copy)] enum Mode {