0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
Commit Graph

24 Commits

Author SHA1 Message Date
Joyee Cheung
757f3f8b2c
process: reduce the number of internal frames in async stack trace
Previously, we call the JS land `runNextTicks` implementation
immediately from JS land after evaluating the main module or the
input, so these synchronous JS call frames would show up in the stack
trace of the async errors, which can be confusing. This patch moves
those calls into C++ so that more of these internal scheduler
implementation details can be hidden and the users can see a cleaner
a cleaner async JS stack trace.

PR-URL: https://github.com/nodejs/node/pull/27392
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2019-04-28 15:36:28 +08:00
Joyee Cheung
8d669bbeb1 process: refactor global.queueMicrotask()
- Lazy load `async_hooks` in the implementation
- Rename `process/next_tick.js` to `process/task_queues.js`
  and move the implementation of `global.queueMicrotask()`
  there since these methods are conceptually related to
  each other.
- Move the bindings used by `global.queueMicrotask()` into
  `node_task_queue.cc` instead of the generic `node_util.cc`
- Use `defineOperation` to define `global.queueMicrotask()`

PR-URL: https://github.com/nodejs/node/pull/26523
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-03-11 06:11:12 +01:00
Joyee Cheung
6967f91368
process: split execution into main scripts
This patch splits the execution mode selection from the environment
setup in `lib/internal/bootstrap/node.js`, and split the entry point
of different execution mode into main scripts under
`lib/internal/main`:

- `check_syntax.js`: used when `-c`/`--check` which only checks the
  syntax of the input instead of executing it.
- `eval_stdin.js`: used when `-e` is passed without value and stdin
  is not a TTY (e.g. something is piped).
- `eval_string`: used when `-e` is passed along with a string argument
- `inspect.js`: for `node inspect`/`node debug`
- `print_bash_completion.js`: for `--completion-bash`
- `print_help.js`: for `--help`
- `prof_process.js`: for `--prof-process`
- `repl.js`: for the REPL
- `run_main_module.js`: used when a main module is passed
- `run_third_party_main.js`: for the legacy `_third_party_main.js`
  support
- `worker_thread.js`: for workers

This makes the entry points easier to navigate and paves the way
for customized v8 snapshots (that do not need to deserialize
execution mode setup) and better embedder APIs.

As an example, after this patch, for the most common case where
Node.js executes a user module as an entry point, it essentially
goes through:

- `lib/internal/per_context.js` to setup the v8 Context (which is
  also run when setting up contexts for the `vm` module)
- `lib/internal/bootstrap/loaders.js` to set up internal binding
  and builtin module loaders (that are separate from the loaders
  accessible in the user land).
- `lib/internal/bootstrap/node.js`: to set up the rest of the
  environment, including various globals and the process object
- `lib/internal/main/run_main_module.js`: which is selected from
  C++ to prepare execution of the user module.

This patch also removes `NativeModuleLoader::CompileAndCall` and
exposes `NativeModuleLoader::LookupAndCompile` directly so that
we can handle syntax errors and runtime errors of bootstrap
scripts differently.

PR-URL: https://github.com/nodejs/node/pull/25667
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2019-01-30 21:03:17 +08:00
Joyee Cheung
2c7f4f474b
process: allow StartExecution() to take a main script ID
The idea is to allow the C++ layer to run arbitrary scripts
as the main script. This paves the way for

- cctest of the execution of Node.js instances
- Earlier handling of per-process CLI options that affect
  execution modes (those usually do not make sense for the
  embedders).
- Targets like mkcodecache or mksnapshot.

Also moves the handling of `_third_party_main.js` into C++.

PR-URL: https://github.com/nodejs/node/pull/25474
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
2019-01-16 03:42:41 +08:00
Joyee Cheung
d18b0a0132
process: make tick callback and promise rejection callback more robust
- Rename `internalTickCallback` to `processTicksAndRejections`, make
  sure it does not get called if it's not set in C++.
- Rename `emitPromiseRejectionWarnings` to `processPromiseRejections`
  since it also emit events that are not warnings.
- Sets `SetPromiseRejectCallback` in the `Environment` constructor
  to make sure it only gets called once per-isolate, and make
  sure it does not get called if it's not set in C++.
- Wrap promise rejection callback initialization into
  `listenForRejections()`.
- Add comments.

PR-URL: https://github.com/nodejs/node/pull/25200
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-01-06 10:28:47 +08:00
Anna Henningsen
f6a1d88c5d
src: split LoadEnvironment() at startExecution()
This makes it easier to cater to embedders which wish to skip
the `startExecution()` part.

PR-URL: https://github.com/nodejs/node/pull/25320
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
2019-01-06 02:15:47 +01:00
Joyee Cheung
457603e961
src: move process.nextTick and promise setup into node_task_queue.cc
This patch:

- Moves the process.nextTick and promise setup C++ code into
  node_task_queue.cc which is exposed as
  `internalBinding('task_queue')`
- Makes `lib/internal/process/promises.js` and
  `lib/internal/process/next_tick.js` as side-effect-free
  as possible
- Removes the bootstrapper object being passed into
  `bootstrap/node.js`, let `next_tick.js` and `promises.js`
  load whatever they need from `internalBinding('task_queue')`
  instead.
- Rename `process._tickCallback` to `runNextTicks` internally
  for clarity but still expose it as `process._tickCallback`.

PR-URL: https://github.com/nodejs/node/pull/25163
Refs: https://github.com/nodejs/node/issues/24961
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
2018-12-24 07:57:15 +08:00
Joyee Cheung
edcb950090
src: use NativeModuleLoader to compile all the bootstrappers
This patch moves all the bootstrapper compilation to use
NativeModuleLoader::CompileAndCall(). With this we no longer
need to mess with the error decoration and handling any more -
there is no point in handling the JS error occurred during bootstrapping
by ourselves, we should just crash or let the VM handle it.

PR-URL: https://github.com/nodejs/node/pull/24775
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-12-04 05:54:49 +08:00
Joyee Cheung
7b8058a39e process: refactor the bootstrap mode branching for readability
This patch refactors the branches for choosing the mode to run
Node.js in `internal/bootstrap/node.js`. Instead of inlining the
decision making all in `startup`, we create a `startExecution()`
function which either detects and start the non-user-code mode,
or prepares for user code execution (worker setup, preloading modules)
and starts user code execution.
We use early returns when we decide the mode to run Node.js in for fewer
indentations and better readability.

This patch also adds a few comments about the command-line switches
and a few TODOs to remove underscore properties on `process` that
are mainly used for bootstrap mode branching. It also includes
a few other refactoring such as inlining functions/variables
that are not reused and removing the default argument of
`evalScript` for better clarity.

PR-URL: https://github.com/nodejs/node/pull/24673
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2018-11-29 12:55:56 -08:00
Anatoli Papirovski
e7af9830e9 timers: run nextTicks after each immediate and timer
In order to better match the browser behaviour, run nextTicks (and
subsequently the microtask queue) after each individual Timer and
Immediate, rather than after the whole list is processed. The
current behaviour is somewhat of a performance micro-optimization
and also partly dictated by how timer handles were implemented.

PR-URL: https://github.com/nodejs/node/pull/22842
Fixes: https://github.com/nodejs/node/issues/22257
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2018-10-17 20:38:07 -07:00
Joyee Cheung
c2b01881dc
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: https://github.com/nodejs/node/pull/19177
Refs: https://github.com/nodejs/node/pull/19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2018-03-15 20:50:35 +08:00
Joyee Cheung
8484b40b3d
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: https://github.com/nodejs/node/pull/19177
Refs: https://github.com/nodejs/node/pull/19112
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2018-03-15 20:50:34 +08:00
Joyee Cheung
2a9eb316a1
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: https://github.com/nodejs/node/pull/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>
2018-03-06 22:12:46 +08:00
Anatoli Papirovski
cbaf59c5b9
process: slightly simplify next tick execution
Get rid of separate function to call callback from _tickCallback as
it no longer yields worthwhile performance improvement.

Move some code from nextTick & internalNextTick into TickObject
constructor to minimize duplication.

PR-URL: https://github.com/nodejs/node/pull/16888
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
2017-11-28 08:48:30 -05:00
Ben Noordhuis
90476ac6ee lib: remove _debugger.js
The file no longer works after the removal of the --debug/--debug-brk
switches in commit 47f8f74 ("src: remove support for --debug".)

This commit also removes several tests that still referenced the
old debugger but were either unit-testing its internals or passing
for the wrong reason (like expecting an operation to fail, which
it did because the debugger is gone.)

PR-URL: https://github.com/nodejs/node/pull/12495
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-04-21 10:35:49 +02:00
Daniel Bevenius
81b6882e51 src: rename "node" script to "bootstrap_node"
This commit updates the node.js script name to reflect its
actual name, which is now bootstrap_node.js. This commit also
fixes the requisite message tests, and relocates a comment
which seems to have drifted.

PR-URL: https://github.com/nodejs/node/pull/7277
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
2016-06-17 14:14:27 -04:00
Rich Trott
1a21524b69 debugger: fix --debug-brk interaction with -e
The command line flag `--debug-brk` was ignored when the `-e` flag was
also present. This change allows the flags to both be honored when they
are used in a single command line.

PR-URL: https://github.com/nodejs/node/pull/7089
Fixes: https://github.com/nodejs/node/issues/3589
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2016-06-03 21:40:30 -07:00
Jeremiah Senkpiel
015cef25eb lib,src: refactor src/node.js into internal files
PR-URL: https://github.com/nodejs/node/pull/5103
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-03-22 19:20:01 -04:00
Ruben Bridgewater
88307974e6 node: improve process.nextTick performance
Prevent deoptimization of process.nextTick by removing the try finally
block. This is not necessary as the next tick queue will be reset
anyway, no matter if the callback throws or not.

Use a predefined array size prevents resizing the array and is therefor
faster.

PR-URL: https://github.com/nodejs/node/pull/5092
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-02-09 02:46:43 -07:00
Rod Vagg
d2c8ba5248 node: s/doNTCallbackX/nextTickCallbackWithXArgs/
Rename doNTCallback functions for clarity when profiling, these make
sense internally but the "NT" in particular is a bit obtuse to be
immediately understandable by non-core developers.

PR-URL: https://github.com/nodejs/node/pull/4167
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
2015-12-07 23:44:26 +09:00
Brian White
c7782c0af8 node: improve nextTick performance
This commit uses separate functions to isolate deopts caused by
try-catches and avoids fn.apply() for callbacks with small numbers
of arguments.

These changes improve performance by ~1-40% in the various
nextTick benchmarks.

PR-URL: https://github.com/iojs/io.js/pull/1571
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
2015-05-01 21:27:34 -04:00
isaacs
875e4a0c59 core: Remove the nextTick for running the main file
Not necessary, since we can handle the error properly on the first tick
now, even if there are event listeners, etc.

Additionally, this removes the unnecessary "_needTickCallback" from
startup, since Module.loadMain() will kick off a nextTick callback right
after it runs the main module.

Fix #4856
2013-02-27 16:29:36 -08:00
Trevor Norris
86c0745a5e process: streamlining tick callback logic
* Callbacks from spinner now calls its own function, separate from the
  tickCallback logic
* MakeCallback will call a domain specific function if a domain is
  detected
* _tickCallback assumes no domains, until nextTick receives a callback
  with a domain. After that _tickCallback is overridden with the domain
  specific implementation.
* _needTickCallback runs in startup() instead of nextTick (isaacs)
* Fix bug in _fatalException where exit would be called twice (isaacs)
* Process.domain has a default value of null
* Manually track nextTickQueue.length (will be useful later)
* Update tests to reflect internal api changes
2013-02-15 18:13:01 -08:00
isaacs
1c2910d94c test-message: Add setTimeout and nextTick message tests 2012-12-29 10:37:31 -08:00