Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

process: execute nextTick callback after microtask #8376

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,6 @@ Module._extensions['.node'] = process.dlopen;
Module.runMain = function() {
// Load the main module--the command line argument.
Module._load(process.argv[1], null, true);
// Handle any nextTicks added in the first tick of the program
process._tickCallback();
};

Module._initPaths = function() {
Expand Down
83 changes: 39 additions & 44 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,41 @@ void SetupNextTick(const FunctionCallbackInfo<Value>& args) {
}


Handle<Boolean> ExecuteNextTickCallback(Environment* env) {
Environment::TickInfo* tick_info = env->tick_info();

TryCatch try_catch;
try_catch.SetVerbose(true);

if (tick_info->last_threw() == 1) {
tick_info->set_last_threw(0);
return True(env->isolate());
}

if (tick_info->in_tick()) {
return True(env->isolate());
}

if (tick_info->length() == 0) {
tick_info->set_index(0);
return True(env->isolate());
}

tick_info->set_in_tick(true);

env->tick_callback_function()->Call(env->process_object(), 0, NULL);

tick_info->set_in_tick(false);

if (try_catch.HasCaught()) {
tick_info->set_last_threw(true);
return False(env->isolate());
}

return True(env->isolate());
}


Handle<Value> MakeDomainCallback(Environment* env,
Handle<Value> recv,
const Handle<Function> callback,
Expand Down Expand Up @@ -1063,30 +1098,7 @@ Handle<Value> MakeDomainCallback(Environment* env,
return Undefined(env->isolate());
}

Environment::TickInfo* tick_info = env->tick_info();

if (tick_info->last_threw() == 1) {
tick_info->set_last_threw(0);
return ret;
}

if (tick_info->in_tick()) {
return ret;
}

if (tick_info->length() == 0) {
tick_info->set_index(0);
return ret;
}

tick_info->set_in_tick(true);

env->tick_callback_function()->Call(process, 0, NULL);

tick_info->set_in_tick(false);

if (try_catch.HasCaught()) {
tick_info->set_last_threw(true);
if (ExecuteNextTickCallback(env)->IsFalse()) {
return Undefined(env->isolate());
}

Expand Down Expand Up @@ -1132,26 +1144,7 @@ Handle<Value> MakeCallback(Environment* env,
return Undefined(env->isolate());
}

Environment::TickInfo* tick_info = env->tick_info();

if (tick_info->in_tick()) {
return ret;
}

if (tick_info->length() == 0) {
tick_info->set_index(0);
return ret;
}

tick_info->set_in_tick(true);

// process nextTicks after call
env->tick_callback_function()->Call(process, 0, NULL);

tick_info->set_in_tick(false);

if (try_catch.HasCaught()) {
tick_info->set_last_threw(true);
if (ExecuteNextTickCallback(env)->IsFalse()) {
return Undefined(env->isolate());
}

Expand Down Expand Up @@ -2893,6 +2886,8 @@ void Load(Environment* env) {

Local<Value> arg = env->process_object();
f->Call(global, 1, &arg);
// Handle any nextTicks added in the first tick of the program
ExecuteNextTickCallback(env);
}

static void PrintHelp();
Expand Down
3 changes: 0 additions & 3 deletions test/message/nexttick_throw.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@
ReferenceError: undefined_reference_error_maker is not defined
at *test*message*nexttick_throw.js:*:*
at process._tickCallback (node.js:*:*)
at Function.Module.runMain (module.js:*:*)
at startup (node.js:*:*)
at node.js:*:*
109 changes: 109 additions & 0 deletions test/simple/test-next-tick-ordering3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var util = require('util');

if (!util.isFunction(Object.observe) && !util.isFunction(Promise)) {
console.error('Skipping because node does not support Object.observe and Promise');
process.exit(0);
}

var results = {};


function Test1() {
var order = results['Test1'] = [];
var obj = {};
Object.observe(obj, function(changeRecord) {
order.push('Object.observe');
});
process.nextTick(function() {
order.push('nextTick');
});
obj.a = 1;
}


function Test2() {
var order = results['Test2'] = [];
var obj = {};
Object.observe(obj, function(changeRecord) {
order.push('Object.observe');
});
setTimeout(function() {
process.nextTick(function() {
order.push('nextTick');
});
obj.a = 1;
}, 0);
}


function Test3() {
var order = results['Test3'] = [];
var obj = {};

var promise = new Promise(function(onFulfilled, onRejected) {
onFulfilled();
});

process.nextTick(function() {
order.push('nextTick');
});

promise.then(function() {
order.push('Promise.onFullfilled');
}, null);
}


function Test4() {
var order = results['Test4'] = [];
var obj = {};

setTimeout(function() {
var promise = new Promise(function(onFulfilled, onRejected) {
onFulfilled();
});

process.nextTick(function() {
order.push('nextTick');
});

promise.then(function() {
order.push('Promise.onFullfilled');
}, null);
}, 0);
}

Test1();
Test2();
Test3();
Test4();

process.on('exit', function() {
assert.deepEqual(results.Test1, ['Object.observe', 'nextTick']);
assert.deepEqual(results.Test2, ['Object.observe', 'nextTick']);
assert.deepEqual(results.Test3, ['Promise.onFullfilled', 'nextTick']);
assert.deepEqual(results.Test4, ['Promise.onFullfilled', 'nextTick']);
});