Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): fix prismjs compatibiliy in Web Worker #25062

Merged
merged 3 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions ext/node/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ const fn str_to_utf16<const N: usize>(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"),
&str_to_utf16::<13>("clearInterval"),
&str_to_utf16::<12>("clearTimeout"),
Expand All @@ -80,7 +81,7 @@ const MANAGED_GLOBALS: [&[u16]; 13] = [
];

const SHORTEST_MANAGED_GLOBAL: usize = 4;
const LONGEST_MANAGED_GLOBAL: usize = 14;
const LONGEST_MANAGED_GLOBAL: usize = 17;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these could be calculated in a const expr so they can't fall out of date

Copy link
Member Author

@kt3k kt3k Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could that be easily/reasonably done? Looked into const fn feature, but it seems very limited..

Anyway this value is indirectly tested via the test cases

Copy link
Member

@littledivy littledivy Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const MANAGED_GLOBAL_INFO: (usize, usize) = {
  let mut longest = MANAGED_GLOBALS[0].len();
  let mut shortest = MANAGED_GLOBALS[0].len();
  let mut i = 0;
  while i < MANAGED_GLOBALS.len() {
      let g = MANAGED_GLOBALS[i];
      if g.len() > longest {
        longest = g.len();
      }
      if g.len() < shortest {
        shortest = g.len();
      }
      
      i += 1;
  }
  
  (shortest, longest)
};

const SHORTEST_MANAGED_GLOBAL: usize = MANAGED_GLOBAL_INFO.0;
const LONGEST_MANAGED_GLOBAL: usize = MANAGED_GLOBAL_INFO.1;

Playground


#[derive(Debug, Clone, Copy)]
enum Mode {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if (typeof self !== "undefined") {
throw new Error("self is defined");
}

if (typeof WorkerGlobalScope !== "undefined") {
throw new Error("WorkerGlobalScope is defined");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@denotest/check-worker-globals",
"version": "1.0.0",
"main": "index.js"
}
1 change: 1 addition & 0 deletions tests/specs/npm/workers/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
1
2
3
4
[UNORDERED_END]
3 changes: 3 additions & 0 deletions tests/specs/npm/workers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
4 changes: 4 additions & 0 deletions tests/specs/npm/workers/worker4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "npm:@denotest/check-worker-globals";

console.log(4);
self.close();