-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
lib: use safe methods from primordials #27096
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,19 @@ | |
| // `primordials.Object` where `primordials` is a lexical variable passed | ||
| // by the native module compiler. | ||
|
|
||
| const ReflectApply = Reflect.apply; | ||
|
|
||
| // This function is borrowed from the function with the same name on V8 Extras' | ||
| // `utils` object. V8 implements Reflect.apply very efficiently in conjunction | ||
| // with the spread syntax, such that no additional special case is needed for | ||
| // function calls w/o arguments. | ||
| // Refs: https://github.com/v8/v8/blob/d6ead37d265d7215cf9c5f768f279e21bd170212/src/js/prologue.js#L152-L156 | ||
| function uncurryThis(func) { | ||
| return (thisArg, ...args) => ReflectApply(func, thisArg, args); | ||
| } | ||
|
|
||
| primordials.uncurryThis = uncurryThis; | ||
|
|
||
| function copyProps(src, dest) { | ||
| for (const key of Reflect.ownKeys(src)) { | ||
| if (!Reflect.getOwnPropertyDescriptor(dest, key)) { | ||
|
|
@@ -23,6 +36,18 @@ function copyProps(src, dest) { | |
| } | ||
| } | ||
|
|
||
| function copyPrototype(src, dest) { | ||
| for (const key of Reflect.ownKeys(src)) { | ||
| if (!Reflect.getOwnPropertyDescriptor(dest, key)) { | ||
| const desc = Reflect.getOwnPropertyDescriptor(src, key); | ||
| if (typeof desc.value === 'function') { | ||
| desc.value = uncurryThis(desc.value); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was talking about this, as this affect all prototype methods, not just the Function ones.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is already what was done manually in all the files I changed (either with call.bind or uncurryThis). This is centralizing the uncurrying to this file. If we want to use the methods safely, we have to call them without going through the prototype. |
||
| } | ||
| Reflect.defineProperty(dest, key, desc); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function makeSafe(unsafe, safe) { | ||
| copyProps(unsafe.prototype, safe.prototype); | ||
| copyProps(unsafe, safe); | ||
|
|
@@ -64,17 +89,23 @@ primordials.SafePromise = makeSafe( | |
| // Create copies of intrinsic objects | ||
| [ | ||
| 'Array', | ||
| 'BigInt', | ||
| 'Boolean', | ||
| 'Date', | ||
| 'Error', | ||
| 'Function', | ||
| 'Map', | ||
| 'Number', | ||
| 'Object', | ||
| 'RegExp', | ||
| 'Set', | ||
| 'String', | ||
| 'Symbol', | ||
| ].forEach((name) => { | ||
| const target = primordials[name] = Object.create(null); | ||
| copyProps(global[name], target); | ||
| const proto = primordials[name + 'Prototype'] = Object.create(null); | ||
| copyProps(global[name].prototype, proto); | ||
| copyPrototype(global[name].prototype, proto); | ||
| }); | ||
|
|
||
| Object.setPrototypeOf(primordials, null); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.