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

Conversation

kt3k
Copy link
Member

@kt3k kt3k commented Aug 16, 2024

PrismJS uses WorkerGlobalScope and self for detecting browser's Web Worker context:

https://github.com/PrismJS/prism/blob/59e5a3471377057de1f401ba38337aca27b80e03/prism.js#L11

Now the detection logic above is broken when it's imported from Deno's Web Worker context because we only hide self (Prism assumes when WorkerGlobalScope is available, self is also available).

This PR fixes the above by also hiding WorkerGlobalScope global in Node compat mode.

closes #25008

@kt3k kt3k requested review from devsnek, lucacasonato and bartlomieju and removed request for devsnek, lucacasonato and bartlomieju August 16, 2024 06:55
@@ -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

Copy link
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

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

LGTM

@kt3k kt3k merged commit b6cdb31 into denoland:main Aug 17, 2024
17 checks passed
@kt3k kt3k deleted the fix-prismjs-compat-in-web-worker branch August 17, 2024 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

self is not defined in NPM packages imported in a Worker
3 participants