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

src: bootstrap internals loaders before bootstrapping Node.js #19112

Closed
wants to merge 3 commits into from

Conversation

@joyeecheung
Copy link
Member

commented Mar 3, 2018

  • Moves the creation of process.binding(), process._linkedBinding()
    internalBinding() and NativeModule into a separate file
    lib/internal/bootstrap_loaders.js, and documents them there.
    This file will be compiled and run before bootstrap_node.js, which
    means we now bootstrap the internal module & binding system before
    actually bootstrapping Node.js.
  • Rename the special ID that can be used to require NativeModule
    as internal/bootstrap_loaders since it is setup there. Also put
    internalBinding in the object exported by NativeModule.require
    instead of putting it inside the NativeModule.wrapper
  • Names the bootstrapping functions so their names show up
    in the stack trace.
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

process, modules

@joyeecheung

This comment has been minimized.

Copy link
Member Author

commented Mar 3, 2018

};
}

// Set up internalBinding in the closure

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Mar 3, 2018

Author Member

Come to think of it, we may also put internalBinding in the returned object and rename this file inernal_loaders which seem to be more accurate..so it will become const { NativeModule, internalBinding } = require('internal/internal_loaders') (somewhat clashes with internal/loader/* though..)

src/node.cc Outdated

static bool ExecuteFactory(Environment* env, Local<Function> factory, int argc,
Local<Value> argv[], Local<Value>* out) {
bool ret = factory->Call(

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Mar 3, 2018

Author Member

This now calls into JS from C++ twice during bootstrapping..maybe passing the loader bootstrapper into the core bootstrapper and call JS from JS will lower the performance impact..are there any existing benchmarks for measuring startup time?

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 3, 2018

Member

I’d expect this to be fine, tbh. It might be nice to have a single call into JS again at some point, but that doesn’t have to be a strict rule.

@addaleax
Copy link
Member

left a comment

LGTM, great work!


'use strict';

(function(process) {
let internalBinding;
(function bootstrapNodeJSCore(process, NativeModule) {

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 3, 2018

Member

I’m wondering whether this function name showing up is more helpful or more confusing when showing up ni embedder code…

const bindingObj = Object.create(null);

const getInternalBinding = process._internalBinding;
delete process._internalBinding;

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 3, 2018

Member

Since you’re at it – how about passing this to bootstrapInternalLoaders as an argument, too?

This comment has been minimized.

Copy link
@joyeecheung

joyeecheung Mar 4, 2018

Author Member

Good idea, also getLinkedBinding and getBinding can be passed in as well so we don’t need to override the original property, which kind of hard to understand at first glance

src/node.cc Outdated

static bool ExecuteFactory(Environment* env, Local<Function> factory, int argc,
Local<Value> argv[], Local<Value>* out) {
bool ret = factory->Call(

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 3, 2018

Member

I’d expect this to be fine, tbh. It might be nice to have a single call into JS again at some point, but that doesn’t have to be a strict rule.

@apapirovski
Copy link
Member

left a comment

LGTM. I agree with @addaleax that passing in internalBinding into native_module.js would probably be a good idea, instead of declaring it on the process and then deleting it.

@joyeecheung joyeecheung force-pushed the joyeecheung:split-native-module branch from 0240c6f to 9239ca3 Mar 4, 2018

@joyeecheung

This comment has been minimized.

Copy link
Member Author

commented Mar 4, 2018

Updates:

  • Passing getBinding(), getLinkedBinding() and getInternalBinding() to the loader bootstrapper instead of putting them on process and override latter (suggestion by @addaleax )
  • Add documentation about process._linkedBinding, this fixes #12516
  • Rename native_module -> bootstrap_loaders, ExecuteFactory/GetFactory -> ExecuteBootstrapper/GetBootstrapper, so it's consistent with bootstrap_node.js and it's easier to figure out what it does just by looking at the names
  • Expose internalBinding through the object returned by NativeModule.require instead of putting it into the NativeModule.wrapper .
@apapirovski
Copy link
Member

left a comment

Still LGTM with a tiny nit.

@@ -109,9 +109,12 @@

const config = process.binding('config');

// Think of this as module.exports in this file even though it is not
// written in CommonJS style.
const exports_ = { internalBinding, NativeModule };

This comment has been minimized.

Copy link
@apapirovski

apapirovski Mar 4, 2018

Member

Not generally a huge fan of trailing _ variables in JS, esp since it looks similar enough to exports. Could this be like loaderExports or something?

@joyeecheung

This comment has been minimized.

Copy link
Member Author

commented Mar 4, 2018

@joyeecheung joyeecheung changed the title src: move internal loaders out of bootstrap_node.js src: bootstrap loaders before bootstrapping Node.js Mar 4, 2018

@joyeecheung joyeecheung changed the title src: bootstrap loaders before bootstrapping Node.js src: bootstrap internals loaders before bootstrapping Node.js Mar 4, 2018

@devsnek

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

Expose internalBinding through the object returned by NativeModule.require instead of putting it into the NativeModule.wrapper .

i'm feeling -1ish on this with this change, is there any reason it can't be kept in the module wrapper?

also we could move these two files to internal/bootstrap or internal/startup or something and remove the bootstrap_ prefix

@joyeecheung

This comment has been minimized.

Copy link
Member Author

commented Mar 4, 2018

i'm feeling -1ish on this with this change, is there any reason it can't be kept in the module wrapper?

Doesn't have to move it out, but this way we have less globals in core modules and this can now be used in bootstrap_node.js. (Also, there is not any reason it has to be in the wrapper either...right?)

@apapirovski

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

I'm +1 on having it be an export rather than a wrapper-introduced global. Given how few modules actually use it, I don't think there's a great argument for having it be a part of the wrapper.

@devsnek

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

(Also, there is not any reason it has to be in the wrapper either...right?)

this makes it so that it can't be used even if --expose-internals is used, i think that was something @addaleax explicitly did/wanted with her pr, and personally i agree with that change.

@apapirovski

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

this makes it so that it can't be used even if --expose-internals is used, i think that was something @addaleax explicitly did/wanted with her pr, and personally i agree with that change.

Given that NativeModule was already exposed, I'm almost certain it was possible — if a bit awkward — to get internalBinding before.

(Edit: Or was it not because of the different module id?)

@joyeecheung

This comment has been minimized.

Copy link
Member Author

commented Mar 4, 2018

this makes it so that it can't be used even if --expose-internals is used, i think that was something @addaleax explicitly did/wanted with her pr, and i also like how isolated it is currently

That can be fixed by changing NativeModule.isInternal though (which reminds me this PR leaks NativeModule by prefixing the file internal/). The same could be said about NativeModule - I think we should isolate them the same way, no matter through a wrapper or by tweaking NativeModule.isInternal.

@addaleax

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

I'm +1 on having it be an export rather than a wrapper-introduced global. Given how few modules actually use it, I don't think there's a great argument for having it be a part of the wrapper.

I don’t really care how it is exposed, but I think the argument that few modules use it shouldn’t have a strong weight, because we really should be and are moving towards using it in place of process.binding() (slowly).

Given that NativeModule was already exposed, I'm almost certain it was possible — if a bit awkward — to get internalBinding before.

At least I couldn’t figure out how (and thought that that was a good thing). ¯\_(ツ)_/¯

@joyeecheung

This comment has been minimized.

Copy link
Member Author

commented Mar 4, 2018

also we could move these two files to internal/bootstrap or internal/startup or something and remove the bootstrap_ prefix

I like the idea of having a file named node.js again ;) but probably should do that later since that would involve a lot of stack traces in test/message and comments to fix.

@apapirovski

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

I don’t really care how it is exposed, but I think the argument that few modules use it shouldn’t have a strong weight, because we really should be and are moving towards using it in place of process.binding() (slowly).

I just don't love globals, so the fewer, the better in my mind and the exceptions should be things that we can't really run a module without (or are just used that often). (Needless to say, I would obviously prefer if process.binding wasn't exposed either...)

But anyway, don't have a strong opinion so I'm fine with whatever direction this ends up going in.

@devsnek

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

actually its worse than i thought, internalBinding can be gotten without any flags on current release/master, so maybe this pr actually makes it harder to get? 😓

$ cat leeks.js
process.binding('natives').owo = 'module.exports = internalBinding';
console.log(require('owo'));

$ node leeks.js
[Function: internalBinding]
@addaleax

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

@devsnek Yes, that sounds pretty bad :/ I guess we could and should freeze process.binding('natives'), for a start?

@devsnek

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

ideally it shouldn't be exposed at all, perhaps we can deprecate and remove it? i'm worried that packages might be using this, mostly for reading, not writing (and it also seems that v8_prof_processor modifies several scripts by adding newlines to the ends of them, i'm not quite sure what to make of that)

@devsnek

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

it should be noted that even with that last commit by joyee, native module can still be gotten with the same tomfoolery i posted above

@addaleax

This comment has been minimized.

Copy link
Member

commented Mar 4, 2018

ideally it shouldn't be exposed at all, perhaps we can deprecate and remove it?

I’m on board with doing that, but it might be a while before we can do it – right now, the latest verions of gulp are famously still using graceful-fs@3, which reads those bindings…

(and it also seems that v8_prof_processor modifies several scripts by adding newlines to the ends of them)

I think that’s still just reading, it basically concatenates them with newlines?

FallenRiteMonk added a commit to FallenRiteMonk/node that referenced this pull request Mar 23, 2018

test: remove NODE_DEBUG in global module loading test
Otherwise the debug log output might be mixed up with
the expected errors and the assertion matching the error
message would fail.

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

FallenRiteMonk added a commit to FallenRiteMonk/node that referenced this pull request Mar 23, 2018

src: put bootstrappers in lib/internal/bootstrap/
Create `lib/internal/bootstrap/` and put bootstrappers there:

Before:

```
lib/internal
├── ...
├── bootstrap_loaders.js
└── bootstrap_node.js
```

After:

```
lib/internal
├── ...
└── bootstrap
    ├── loaders.js
    └── node.js
```

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

FallenRiteMonk added a commit to FallenRiteMonk/node that referenced this pull request Mar 23, 2018

lib: restructure cjs and esm loaders
Create `lib/internal/modules` and restructure the module loaders
to make the purpose of those files clearer.

Also make it clear in the code that the object exported by
`lib/internal/modules/cjs/loader.js` is `CJSModule` instead of the
ambiguous `Module`.

Before:

```
lib
├── ...
├── internal
│       ├── loaders
│       │     ├── CreateDynamicModule.js
│       │     ├── DefaultResolve.js
│       │     ├── Loader.js
│       │     ├── ModuleJob.js
│       │     ├── ModuleMap.js
│       │     └── Translators.js
│       └── module.js
└── module.js
```

After:

```
lib
├── ...
├── internal
│       ├── ...
│       └── modules
│              ├── cjs
│              │     ├── helpers.js
│              │     └── loader.js
│              └── esm
│                    ├── CreateDynamicModule.js
│                    ├── DefaultResolve.js
│                    ├── Loader.js
│                    ├── ModuleJob.js
│                    ├── ModuleMap.js
│                    └── Translators.js
└── module.js # deleted in this commit to work with git file mode
```

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

FallenRiteMonk added a commit to FallenRiteMonk/node that referenced this pull request Mar 23, 2018

lib: add back lib/module.js redirection
The previous commit deleted lib/module.js so that git
recognize the file move `lib/module.js` ->
`lib/internal/modules/cjs/loader.js`. This commit add the
redirection back.

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

joyeecheung added a commit to joyeecheung/node that referenced this pull request Mar 30, 2018

src: move internal loaders out of bootstrap_node.js
- Moves the creation of `process.binding()`, `process._linkedBinding()`
  `internalBinding()` and `NativeModule` into a separate file
  `lib/internal/bootstrap_loaders.js`, and documents them there.
  This file will be compiled and run before `bootstrap_node.js`, which
  means we now bootstrap the internal module & binding system before
  actually bootstrapping Node.js.
- Rename the special ID that can be used to require `NativeModule`
  as `internal/bootstrap_loaders` since it is setup there. Also put
  `internalBinding` in the object exported by `NativeModule.require`
  instead of putting it inside the `NativeModule.wrapper`
- Use the original `getBinding()` to get the source code of native
  modules instead of getting it from `process.binding('native')`
  so that users cannot fake native modules by modifying the binding
  object.
- Names the bootstrapping functions so their names show up
  in the stack trace.

PR-URL: nodejs#19112
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

joyeecheung added a commit to joyeecheung/node that referenced this pull request Mar 30, 2018

test: remove NODE_DEBUG in global module loading test
Otherwise the debug log output might be mixed up with
the expected errors and the assertion matching the error
message would fail.

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

joyeecheung added a commit to joyeecheung/node that referenced this pull request Mar 30, 2018

src: put bootstrappers in lib/internal/bootstrap/
Create `lib/internal/bootstrap/` and put bootstrappers there:

Before:

```
lib/internal
├── ...
├── bootstrap_loaders.js
└── bootstrap_node.js
```

After:

```
lib/internal
├── ...
└── bootstrap
    ├── loaders.js
    └── node.js
```

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

joyeecheung added a commit to joyeecheung/node that referenced this pull request Mar 30, 2018

lib: restructure cjs and esm loaders
Create `lib/internal/modules` and restructure the module loaders
to make the purpose of those files clearer.

Also make it clear in the code that the object exported by
`lib/internal/modules/cjs/loader.js` is `CJSModule` instead of the
ambiguous `Module`.

Before:

```
lib
├── ...
├── internal
│       ├── loaders
│       │     ├── CreateDynamicModule.js
│       │     ├── DefaultResolve.js
│       │     ├── Loader.js
│       │     ├── ModuleJob.js
│       │     ├── ModuleMap.js
│       │     ├── ModuleWrap.js
│       │     └── Translators.js
│       └── module.js
└── module.js
```

After:

```
lib
├── ...
├── internal
│       ├── ...
│       └── modules
│              ├── cjs
│              │     ├── helpers.js
│              │     └── loader.js
│              └── esm
│                    ├── CreateDynamicModule.js
│                    ├── DefaultResolve.js
│                    ├── Loader.js
│                    ├── ModuleJob.js
│                    ├── ModuleMap.js
│                    └── Translators.js
└── module.js # deleted in this commit to work with git file mode
```

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

joyeecheung added a commit to joyeecheung/node that referenced this pull request Mar 30, 2018

lib: add back lib/module.js redirection
The previous commit deleted lib/module.js so that git
recognize the file move `lib/module.js` ->
`lib/internal/modules/cjs/loader.js`. This commit add the
redirection back.

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

src: move internal loaders out of bootstrap_node.js
- Moves the creation of `process.binding()`, `process._linkedBinding()`
  `internalBinding()` and `NativeModule` into a separate file
  `lib/internal/bootstrap_loaders.js`, and documents them there.
  This file will be compiled and run before `bootstrap_node.js`, which
  means we now bootstrap the internal module & binding system before
  actually bootstrapping Node.js.
- Rename the special ID that can be used to require `NativeModule`
  as `internal/bootstrap_loaders` since it is setup there. Also put
  `internalBinding` in the object exported by `NativeModule.require`
  instead of putting it inside the `NativeModule.wrapper`
- Use the original `getBinding()` to get the source code of native
  modules instead of getting it from `process.binding('native')`
  so that users cannot fake native modules by modifying the binding
  object.
- Names the bootstrapping functions so their names show up
  in the stack trace.

PR-URL: #19112
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Apr 4, 2018

src: put bootstrappers in lib/internal/bootstrap/
Create `lib/internal/bootstrap/` and put bootstrappers there:

Before:

```
lib/internal
├── ...
├── bootstrap_loaders.js
└── bootstrap_node.js
```

After:

```
lib/internal
├── ...
└── bootstrap
    ├── loaders.js
    └── node.js
```

PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

test: remove NODE_DEBUG in global module loading test
Otherwise the debug log output might be mixed up with
the expected errors and the assertion matching the error
message would fail.

PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

lib: restructure cjs and esm loaders
Create `lib/internal/modules` and restructure the module loaders
to make the purpose of those files clearer.

Also make it clear in the code that the object exported by
`lib/internal/modules/cjs/loader.js` is `CJSModule` instead of the
ambiguous `Module`.

Before:

```
lib
├── ...
├── internal
│       ├── loaders
│       │     ├── CreateDynamicModule.js
│       │     ├── DefaultResolve.js
│       │     ├── Loader.js
│       │     ├── ModuleJob.js
│       │     ├── ModuleMap.js
│       │     ├── ModuleWrap.js
│       │     └── Translators.js
│       └── module.js
└── module.js
```

After:

```
lib
├── ...
├── internal
│       ├── ...
│       └── modules
│              ├── cjs
│              │     ├── helpers.js
│              │     └── loader.js
│              └── esm
│                    ├── CreateDynamicModule.js
│                    ├── DefaultResolve.js
│                    ├── Loader.js
│                    ├── ModuleJob.js
│                    ├── ModuleMap.js
│                    └── Translators.js
└── module.js # deleted in this commit to work with git file mode
```

PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

lib: add back lib/module.js redirection
The previous commit deleted lib/module.js so that git
recognize the file move `lib/module.js` ->
`lib/internal/modules/cjs/loader.js`. This commit add the
redirection back.

PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

src: move internal loaders out of bootstrap_node.js
- Moves the creation of `process.binding()`, `process._linkedBinding()`
  `internalBinding()` and `NativeModule` into a separate file
  `lib/internal/bootstrap_loaders.js`, and documents them there.
  This file will be compiled and run before `bootstrap_node.js`, which
  means we now bootstrap the internal module & binding system before
  actually bootstrapping Node.js.
- Rename the special ID that can be used to require `NativeModule`
  as `internal/bootstrap_loaders` since it is setup there. Also put
  `internalBinding` in the object exported by `NativeModule.require`
  instead of putting it inside the `NativeModule.wrapper`
- Use the original `getBinding()` to get the source code of native
  modules instead of getting it from `process.binding('native')`
  so that users cannot fake native modules by modifying the binding
  object.
- Names the bootstrapping functions so their names show up
  in the stack trace.

Backport-PR-URL: #19374
PR-URL: #19112
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Apr 4, 2018

src: put bootstrappers in lib/internal/bootstrap/
Create `lib/internal/bootstrap/` and put bootstrappers there:

Before:

```
lib/internal
├── ...
├── bootstrap_loaders.js
└── bootstrap_node.js
```

After:

```
lib/internal
├── ...
└── bootstrap
    ├── loaders.js
    └── node.js
```

Backport-PR-URL: #19374
PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

test: remove NODE_DEBUG in global module loading test
Otherwise the debug log output might be mixed up with
the expected errors and the assertion matching the error
message would fail.

Backport-PR-URL: #19374
PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

lib: restructure cjs and esm loaders
Create `lib/internal/modules` and restructure the module loaders
to make the purpose of those files clearer.

Also make it clear in the code that the object exported by
`lib/internal/modules/cjs/loader.js` is `CJSModule` instead of the
ambiguous `Module`.

Before:

```
lib
├── ...
├── internal
│       ├── loaders
│       │     ├── CreateDynamicModule.js
│       │     ├── DefaultResolve.js
│       │     ├── Loader.js
│       │     ├── ModuleJob.js
│       │     ├── ModuleMap.js
│       │     ├── ModuleWrap.js
│       │     └── Translators.js
│       └── module.js
└── module.js
```

After:

```
lib
├── ...
├── internal
│       ├── ...
│       └── modules
│              ├── cjs
│              │     ├── helpers.js
│              │     └── loader.js
│              └── esm
│                    ├── CreateDynamicModule.js
│                    ├── DefaultResolve.js
│                    ├── Loader.js
│                    ├── ModuleJob.js
│                    ├── ModuleMap.js
│                    └── Translators.js
└── module.js # deleted in this commit to work with git file mode
```

Backport-PR-URL: #19374
PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

targos added a commit that referenced this pull request Apr 4, 2018

lib: add back lib/module.js redirection
The previous commit deleted lib/module.js so that git
recognize the file move `lib/module.js` ->
`lib/internal/modules/cjs/loader.js`. This commit add the
redirection back.

Backport-PR-URL: #19374
PR-URL: #19177
Refs: #19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

@targos targos referenced this pull request Apr 4, 2018

Merged

v9.11.0 proposal #19799

MayaLekova added a commit to MayaLekova/node that referenced this pull request May 8, 2018

src: move internal loaders out of bootstrap_node.js
- Moves the creation of `process.binding()`, `process._linkedBinding()`
  `internalBinding()` and `NativeModule` into a separate file
  `lib/internal/bootstrap_loaders.js`, and documents them there.
  This file will be compiled and run before `bootstrap_node.js`, which
  means we now bootstrap the internal module & binding system before
  actually bootstrapping Node.js.
- Rename the special ID that can be used to require `NativeModule`
  as `internal/bootstrap_loaders` since it is setup there. Also put
  `internalBinding` in the object exported by `NativeModule.require`
  instead of putting it inside the `NativeModule.wrapper`
- Use the original `getBinding()` to get the source code of native
  modules instead of getting it from `process.binding('native')`
  so that users cannot fake native modules by modifying the binding
  object.
- Names the bootstrapping functions so their names show up
  in the stack trace.

PR-URL: nodejs#19112
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
@MylesBorins

This comment has been minimized.

Copy link
Member

commented May 23, 2018

Would very much like to see this backported to v8.x along with #18365

@joyeecheung joyeecheung self-assigned this May 26, 2018

@addaleax addaleax referenced this pull request Jun 7, 2018

Closed

tools: add option to use custom template with js2c.py #21187

2 of 2 tasks complete

joyeecheung added a commit to joyeecheung/node that referenced this pull request Jun 13, 2018

src: move internal loaders out of bootstrap_node.js
- Moves the creation of `process.binding()`, `process._linkedBinding()`
  `internalBinding()` and `NativeModule` into a separate file
  `lib/internal/bootstrap_loaders.js`, and documents them there.
  This file will be compiled and run before `bootstrap_node.js`, which
  means we now bootstrap the internal module & binding system before
  actually bootstrapping Node.js.
- Rename the special ID that can be used to require `NativeModule`
  as `internal/bootstrap_loaders` since it is setup there. Also put
  `internalBinding` in the object exported by `NativeModule.require`
  instead of putting it inside the `NativeModule.wrapper`
- Use the original `getBinding()` to get the source code of native
  modules instead of getting it from `process.binding('native')`
  so that users cannot fake native modules by modifying the binding
  object.
- Names the bootstrapping functions so their names show up
  in the stack trace.

PR-URL: nodejs#19112
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

joyeecheung added a commit to joyeecheung/node that referenced this pull request Jun 13, 2018

test: remove NODE_DEBUG in global module loading test
Otherwise the debug log output might be mixed up with
the expected errors and the assertion matching the error
message would fail.

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

joyeecheung added a commit to joyeecheung/node that referenced this pull request Jun 13, 2018

src: put bootstrappers in lib/internal/bootstrap/
Create `lib/internal/bootstrap/` and put bootstrappers there:

Before:

```
lib/internal
├── ...
├── bootstrap_loaders.js
└── bootstrap_node.js
```

After:

```
lib/internal
├── ...
└── bootstrap
    ├── loaders.js
    └── node.js
```

PR-URL: nodejs#19177
Refs: nodejs#19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.