GeezerVison is showing github.com — src: refactor tickInfo access · nodejs/node@abc4cc2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/next_tick.js +15-7Lines changed: 15 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,8 @@ const { | 11 | 11 | } = internalBinding('task_queue'); | 12 | 12 |
| 13 | 13 | const { | | 14 | + setHasRejectionToWarn, | | 15 | + hasRejectionToWarn, | 14 | 16 | promiseRejectHandler, | 15 | 17 | emitPromiseRejectionWarnings | 16 | 18 | } = require('internal/process/promises'); | @@ -30,15 +32,21 @@ const { ERR_INVALID_CALLBACK } = require('internal/errors').codes; | 30 | 32 | const FixedQueue = require('internal/fixed_queue'); | 31 | 33 |
| 32 | 34 | // *Must* match Environment::TickInfo::Fields in src/env.h. | 33 | | -const kHasScheduled = 0; | 34 | | -const kHasPromiseRejections = 1; | | 35 | +const kHasTickScheduled = 0; | | 36 | + | | 37 | +function hasTickScheduled() { | | 38 | + return tickInfo[kHasTickScheduled] === 1; | | 39 | +} | | 40 | +function setHasTickScheduled(value) { | | 41 | + tickInfo[kHasTickScheduled] = value ? 1 : 0; | | 42 | +} | 35 | 43 |
| 36 | 44 | const queue = new FixedQueue(); | 37 | 45 |
| 38 | 46 | function runNextTicks() { | 39 | | - if (tickInfo[kHasScheduled] === 0 && tickInfo[kHasPromiseRejections] === 0) | | 47 | + if (!hasTickScheduled() && !hasRejectionToWarn()) | 40 | 48 | runMicrotasks(); | 41 | | - if (tickInfo[kHasScheduled] === 0 && tickInfo[kHasPromiseRejections] === 0) | | 49 | + if (!hasTickScheduled() && !hasRejectionToWarn()) | 42 | 50 | return; | 43 | 51 |
| 44 | 52 | internalTickCallback(); | @@ -70,10 +78,10 @@ function internalTickCallback() { | 70 | 78 |
| 71 | 79 | emitAfter(asyncId); | 72 | 80 | } | 73 | | - tickInfo[kHasScheduled] = 0; | | 81 | + setHasTickScheduled(false); | 74 | 82 | runMicrotasks(); | 75 | 83 | } while (!queue.isEmpty() || emitPromiseRejectionWarnings()); | 76 | | - tickInfo[kHasPromiseRejections] = 0; | | 84 | + setHasRejectionToWarn(false); | 77 | 85 | } | 78 | 86 |
| 79 | 87 | class TickObject { | @@ -119,7 +127,7 @@ function nextTick(callback) { | 119 | 127 | } | 120 | 128 |
| 121 | 129 | if (queue.isEmpty()) | 122 | | - tickInfo[kHasScheduled] = 1; | | 130 | + setHasTickScheduled(true); | 123 | 131 | queue.push(new TickObject(callback, args, getDefaultTriggerAsyncId())); | 124 | 132 | } | 125 | 133 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/promises.js +36-12Lines changed: 36 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { safeToString } = internalBinding('util'); | 4 | 4 | const { | 5 | | - promiseRejectEvents | | 5 | + tickInfo, | | 6 | + promiseRejectEvents: { | | 7 | + kPromiseRejectWithNoHandler, | | 8 | + kPromiseHandlerAddedAfterReject, | | 9 | + kPromiseResolveAfterResolved, | | 10 | + kPromiseRejectAfterResolved | | 11 | + } | 6 | 12 | } = internalBinding('task_queue'); | 7 | 13 |
| | 14 | +// *Must* match Environment::TickInfo::Fields in src/env.h. | | 15 | +const kHasRejectionToWarn = 1; | | 16 | + | 8 | 17 | const maybeUnhandledPromises = new WeakMap(); | 9 | 18 | const pendingUnhandledRejections = []; | 10 | 19 | const asyncHandledRejections = []; | 11 | 20 | let lastPromiseId = 0; | 12 | 21 |
| | 22 | +function setHasRejectionToWarn(value) { | | 23 | + tickInfo[kHasRejectionToWarn] = value ? 1 : 0; | | 24 | +} | | 25 | + | | 26 | +function hasRejectionToWarn() { | | 27 | + return tickInfo[kHasRejectionToWarn] === 1; | | 28 | +} | | 29 | + | 13 | 30 | function promiseRejectHandler(type, promise, reason) { | 14 | 31 | switch (type) { | 15 | | - case promiseRejectEvents.kPromiseRejectWithNoHandler: | 16 | | - return unhandledRejection(promise, reason); | 17 | | - case promiseRejectEvents.kPromiseHandlerAddedAfterReject: | 18 | | - return handledRejection(promise); | 19 | | - case promiseRejectEvents.kPromiseResolveAfterResolved: | 20 | | - return resolveError('resolve', promise, reason); | 21 | | - case promiseRejectEvents.kPromiseRejectAfterResolved: | 22 | | - return resolveError('reject', promise, reason); | | 32 | + case kPromiseRejectWithNoHandler: | | 33 | + unhandledRejection(promise, reason); | | 34 | + break; | | 35 | + case kPromiseHandlerAddedAfterReject: | | 36 | + handledRejection(promise); | | 37 | + break; | | 38 | + case kPromiseResolveAfterResolved: | | 39 | + resolveError('resolve', promise, reason); | | 40 | + break; | | 41 | + case kPromiseRejectAfterResolved: | | 42 | + resolveError('reject', promise, reason); | | 43 | + break; | 23 | 44 | } | 24 | 45 | } | 25 | 46 |
| @@ -38,7 +59,7 @@ function unhandledRejection(promise, reason) { | 38 | 59 | warned: false | 39 | 60 | }); | 40 | 61 | pendingUnhandledRejections.push(promise); | 41 | | - return true; | | 62 | + setHasRejectionToWarn(true); | 42 | 63 | } | 43 | 64 |
| 44 | 65 | function handledRejection(promise) { | @@ -54,10 +75,11 @@ function handledRejection(promise) { | 54 | 75 | warning.name = 'PromiseRejectionHandledWarning'; | 55 | 76 | warning.id = uid; | 56 | 77 | asyncHandledRejections.push({ promise, warning }); | 57 | | - return true; | | 78 | + setHasRejectionToWarn(true); | | 79 | + return; | 58 | 80 | } | 59 | 81 | } | 60 | | - return false; | | 82 | + setHasRejectionToWarn(false); | 61 | 83 | } | 62 | 84 |
| 63 | 85 | const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning'; | @@ -121,6 +143,8 @@ function emitPromiseRejectionWarnings() { | 121 | 143 | } | 122 | 144 |
| 123 | 145 | module.exports = { | | 146 | + hasRejectionToWarn, | | 147 | + setHasRejectionToWarn, | 124 | 148 | promiseRejectHandler, | 125 | 149 | emitPromiseRejectionWarnings | 126 | 150 | }; |
Collapse file Copy file name to clipboardExpand all lines: src/callback_scope.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -95,7 +95,7 @@ void InternalCallbackScope::Close() { | 95 | 95 | Environment::TickInfo* tick_info = env_->tick_info(); | 96 | 96 |
| 97 | 97 | if (!env_->can_call_into_js()) return; | 98 | | - if (!tick_info->has_scheduled()) { | | 98 | + if (!tick_info->has_tick_scheduled()) { | 99 | 99 | env_->isolate()->RunMicrotasks(); | 100 | 100 | } | 101 | 101 |
| @@ -106,7 +106,7 @@ void InternalCallbackScope::Close() { | 106 | 106 | CHECK_EQ(env_->trigger_async_id(), 0); | 107 | 107 | } | 108 | 108 |
| 109 | | - if (!tick_info->has_scheduled() && !tick_info->has_promise_rejections()) { | | 109 | + if (!tick_info->has_tick_scheduled() && !tick_info->has_rejection_to_warn()) { | 110 | 110 | return; | 111 | 111 | } | 112 | 112 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env-inl.h +4-8Lines changed: 4 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -265,16 +265,12 @@ inline AliasedBuffer<uint8_t, v8::Uint8Array>& Environment::TickInfo::fields() { | 265 | 265 | return fields_; | 266 | 266 | } | 267 | 267 |
| 268 | | -inline bool Environment::TickInfo::has_scheduled() const { | 269 | | - return fields_[kHasScheduled] == 1; | | 268 | +inline bool Environment::TickInfo::has_tick_scheduled() const { | | 269 | + return fields_[kHasTickScheduled] == 1; | 270 | 270 | } | 271 | 271 |
| 272 | | -inline bool Environment::TickInfo::has_promise_rejections() const { | 273 | | - return fields_[kHasPromiseRejections] == 1; | 274 | | -} | 275 | | - | 276 | | -inline void Environment::TickInfo::promise_rejections_toggle_on() { | 277 | | - fields_[kHasPromiseRejections] = 1; | | 272 | +inline bool Environment::TickInfo::has_rejection_to_warn() const { | | 273 | + return fields_[kHasRejectionToWarn] == 1; | 278 | 274 | } | 279 | 275 |
| 280 | 276 | inline void Environment::AssignToContext(v8::Local<v8::Context> context, | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +4-6Lines changed: 4 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -572,18 +572,16 @@ class Environment { | 572 | 572 | class TickInfo { | 573 | 573 | public: | 574 | 574 | inline AliasedBuffer<uint8_t, v8::Uint8Array>& fields(); | 575 | | - inline bool has_scheduled() const; | 576 | | - inline bool has_promise_rejections() const; | 577 | | - | 578 | | - inline void promise_rejections_toggle_on(); | | 575 | + inline bool has_tick_scheduled() const; | | 576 | + inline bool has_rejection_to_warn() const; | 579 | 577 |
| 580 | 578 | private: | 581 | 579 | friend class Environment; // So we can call the constructor. | 582 | 580 | inline explicit TickInfo(v8::Isolate* isolate); | 583 | 581 |
| 584 | 582 | enum Fields { | 585 | | - kHasScheduled, | 586 | | - kHasPromiseRejections, | | 583 | + kHasTickScheduled = 0, | | 584 | + kHasRejectionToWarn, | 587 | 585 | kFieldsCount | 588 | 586 | }; | 589 | 587 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_task_queue.cc +2-8Lines changed: 2 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,6 @@ using v8::kPromiseRejectAfterResolved; | 17 | 17 | using v8::kPromiseRejectWithNoHandler; | 18 | 18 | using v8::kPromiseResolveAfterResolved; | 19 | 19 | using v8::Local; | 20 | | -using v8::MaybeLocal; | 21 | 20 | using v8::Number; | 22 | 21 | using v8::Object; | 23 | 22 | using v8::Promise; | @@ -80,13 +79,8 @@ static void PromiseRejectCallback(PromiseRejectMessage message) { | 80 | 79 | } | 81 | 80 |
| 82 | 81 | Local<Value> args[] = { type, promise, value }; | 83 | | - MaybeLocal<Value> ret = callback->Call(env->context(), | 84 | | - Undefined(isolate), | 85 | | - arraysize(args), | 86 | | - args); | 87 | | - | 88 | | - if (!ret.IsEmpty() && ret.ToLocalChecked()->IsTrue()) | 89 | | - env->tick_info()->promise_rejections_toggle_on(); | | 82 | + USE(callback->Call( | | 83 | + env->context(), Undefined(isolate), arraysize(args), args)); | 90 | 84 | } | 91 | 85 |
| 92 | 86 | static void InitializePromiseRejectCallback( | |
|