|
10 | 10 | (function(process) { |
11 | 11 | let internalBinding; |
12 | 12 | const exceptionHandlerState = { captureFn: null }; |
| 13 | + const primordials = makePrimordials(); |
13 | 14 |
|
14 | 15 | function startup() { |
15 | 16 | const EventEmitter = NativeModule.require('events'); |
|
411 | 412 | addCommandLineAPI('require', makeRequireFunction(consoleAPIModule)); |
412 | 413 | const config = {}; |
413 | 414 | for (const key of Object.keys(wrappedConsole)) { |
414 | | - if (!originalConsole.hasOwnProperty(key)) |
| 415 | + if (!primordials.ObjectHasOwnProperty(originalConsole, key)) |
415 | 416 | continue; |
416 | 417 | // If global console has the same method as inspector console, |
417 | 418 | // then wrap these two methods into one. Native wrapper will preserve |
|
422 | 423 | config); |
423 | 424 | } |
424 | 425 | for (const key of Object.keys(originalConsole)) { |
425 | | - if (wrappedConsole.hasOwnProperty(key)) |
| 426 | + if (primordials.ObjectHasOwnProperty(wrappedConsole, key)) |
426 | 427 | continue; |
427 | 428 | wrappedConsole[key] = originalConsole[key]; |
428 | 429 | } |
|
601 | 602 | NativeModule.require = function(id) { |
602 | 603 | if (id === 'native_module') { |
603 | 604 | return NativeModule; |
| 605 | + } else if (id === 'primordials') { |
| 606 | + return primordials; |
604 | 607 | } |
605 | 608 |
|
606 | 609 | const cached = NativeModule.getCached(id); |
|
643 | 646 | }; |
644 | 647 |
|
645 | 648 | NativeModule.exists = function(id) { |
646 | | - return NativeModule._source.hasOwnProperty(id); |
| 649 | + return primordials.ObjectHasOwnProperty(NativeModule._source, id); |
647 | 650 | }; |
648 | 651 |
|
649 | 652 | if (config.exposeInternals) { |
|
704 | 707 | }; |
705 | 708 |
|
706 | 709 | startup(); |
| 710 | + |
| 711 | + function makePrimordials() { |
| 712 | + const ReflectApply = Reflect.apply; |
| 713 | + |
| 714 | + // This function is borrowed from the function with the same name on V8 |
| 715 | + // Extras' `utils` object. V8 implements Reflect.apply very efficiently in |
| 716 | + // conjunction with the spread syntax, such that no additional special case |
| 717 | + // is needed for function calls w/o arguments. |
| 718 | + // Refs: https://git.io/vAOyO |
| 719 | + function uncurryThis(func) { |
| 720 | + return (thisArg, ...args) => ReflectApply(func, thisArg, args); |
| 721 | + } |
| 722 | + |
| 723 | + return { |
| 724 | + uncurryThis, |
| 725 | + |
| 726 | + ArrayJoin: uncurryThis(Array.prototype.join), |
| 727 | + ArrayMap: uncurryThis(Array.prototype.map), |
| 728 | + |
| 729 | + FunctionBind: uncurryThis(Function.prototype.bind), |
| 730 | + |
| 731 | + JSONParse: JSON.parse, |
| 732 | + |
| 733 | + ObjectIsPrototypeOf: uncurryThis(Object.prototype.isPrototypeOf), |
| 734 | + ObjectHasOwnProperty: uncurryThis(Object.prototype.hasOwnProperty), |
| 735 | + ObjectKeys: Object.keys, |
| 736 | + |
| 737 | + ReflectApply, |
| 738 | + ReflectHas: Reflect.has, |
| 739 | + |
| 740 | + StringReplace: uncurryThis(String.prototype.replace), |
| 741 | + StringStartsWith: uncurryThis(String.prototype.startsWith), |
| 742 | + }; |
| 743 | + } |
707 | 744 | }); |
0 commit comments