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

Why does set.prototype.difference need to test regex? #2

Closed
loynoir opened this issue Apr 8, 2023 · 5 comments
Closed

Why does set.prototype.difference need to test regex? #2

loynoir opened this issue Apr 8, 2023 · 5 comments

Comments

@loynoir
Copy link

loynoir commented Apr 8, 2023

Reproduce

import setDifference1 from "set.prototype.difference"

const sa = new Set([1, 2, 3])
const sb = new Set([3, 4, 5])

console.log(setDifference1(sa, sb))

Expected

$ node reproduce.mjs 
Set(2) { 1, 2 }

Unexpected

$ node --frozen-intrinsics reproduce.mjs 
(node:225431) ExperimentalWarning: The --frozen-intrinsics flag is experimental
(Use `node --trace-warnings ...` to show where the warning was created)
/path/to/node_modules/safe-regex-test/index.js:12
                throw new $TypeError('`regex` must be a RegExp');
                      ^

TypeError <Object <Object <[Object: null prototype] {}>>>: `regex` must be a RegExp
    at regexTester /path/to/node_modules/safe-regex-test/index.js:12:9)
    at Object.<anonymous> /path/to/node_modules/es-abstract/2022/StringToNumber.js:14:16)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> /path/to/node_modules/es-abstract/2022/ToNumber.js:10:22)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)

Node.js v18.15.0

Related

inspect-js/is-regex#34

@ljharb
Copy link
Member

ljharb commented Apr 8, 2023

That’s just an implementation detail (it’s detecting binary/octal/hex prefixes in StringToNumber), but the frozen intrinsics flag isn’t default precisely because it breaks things.

I’ll leave this open for now, but it probably will be closed in favor of inspect-js/is-regex#34

@loynoir
Copy link
Author

loynoir commented Apr 8, 2023

🤔

But why need StringToNumber?

What are the logical differences compared with https://stackoverflow.com/questions/1723168/what-is-the-fastest-or-most-elegant-way-to-compute-a-set-difference-using-javasc/36504668#36504668

console.clear();

let a = new Set([1, 2, 3, 4]);
let b = new Set([5, 4, 3, 2]);

let a_minus_b = new Set([...a].filter(x => !b.has(x)));
let b_minus_a = new Set([...b].filter(x => !a.has(x)));
let a_intersect_b = new Set([...a].filter(x => b.has(x))); 
let a_union_b = new Set([...a, ...b]); 

console.log(...a_minus_b);     // {1}
console.log(...b_minus_a);     // {5}
console.log(...a_intersect_b); // {2,3,4}
console.log(...a_union_b);     // {1,2,3,4,5}

@ljharb
Copy link
Member

ljharb commented Apr 8, 2023

Oh, I’m this case it’s not actually invoked by this package, it’s just something that’s evaluated as part of loading the module. That’s what the flag is affecting - you should be able to repro it with nothing except a require of the package.

@ljharb
Copy link
Member

ljharb commented Apr 8, 2023

(it would be needed if a setlike provided a string size, fwiw)

@ljharb
Copy link
Member

ljharb commented Apr 8, 2023

I'm going to close this in favor of ljharb/call-bind#4, which is where it would be handled, if at all.

In the meantime, I strongly suggest not using the frozen-intrinsics flag.

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

No branches or pull requests

2 participants