Skip to content

Add setImmediate, setTimeout and other Timer functions #176

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -5,3 +5,4 @@ node_modules
npm-debug.log
deps/minifier/bin/minify
run-all-tests-output.log
package-lock.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
"url": "http://github.com/audreyt/node-webworker-threads.git"
},
"scripts": {
"js": "env PATH=./node_modules/.bin:\"$PATH\" lsc -cj package.ls;\ngcc deps/minifier/src/minify.c -o deps/minifier/bin/minify;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/worker.ls > src/worker.js;\n./deps/minifier/bin/minify kWorker_js < src/worker.js > src/worker.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/events.ls > src/events.js;\n./deps/minifier/bin/minify kEvents_js < src/events.js > src/events.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/createPool.ls > src/createPool.js;\n./deps/minifier/bin/minify kCreatePool_js < src/createPool.js > src/createPool.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/thread_nextTick.ls > src/thread_nextTick.js;\n./deps/minifier/bin/minify kThread_nextTick_js 1 < src/thread_nextTick.js > src/thread_nextTick.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/load.ls > src/load.js;\n./deps/minifier/bin/minify kLoad_js 1 1 < src/load.js > src/load.js.c;",
"js": "env PATH=./node_modules/.bin:\"$PATH\" lsc -cj package.ls;\ngcc deps/minifier/src/minify.c -o deps/minifier/bin/minify;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/worker.ls > src/worker.js;\n./deps/minifier/bin/minify kWorker_js < src/worker.js > src/worker.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/events.ls > src/events.js;\n./deps/minifier/bin/minify kEvents_js < src/events.js > src/events.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/createPool.ls > src/createPool.js;\n./deps/minifier/bin/minify kCreatePool_js < src/createPool.js > src/createPool.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/thread_nextTick.ls > src/thread_nextTick.js;\n./deps/minifier/bin/minify kThread_nextTick_js 1 < src/thread_nextTick.js > src/thread_nextTick.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" ./deps/minifier/bin/minify kThread_timer_js 1 < src/thread_timer.js > src/thread_timer.js.c;\nenv PATH=./node_modules/.bin:\"$PATH\" lsc -cbp src/load.ls > src/load.js;\n./deps/minifier/bin/minify kLoad_js 1 1 < src/load.js > src/load.js.c;",
"pretest": "npm run js && node-gyp rebuild",
"test": "./run-all-tests.sh"
},
1 change: 1 addition & 0 deletions package.ls
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ scripts:
./deps/minifier/bin/minify kCreatePool_js < src/createPool.js > src/createPool.js.c;
env PATH=./node_modules/.bin:"$PATH" lsc -cbp src/thread_nextTick.ls > src/thread_nextTick.js;
./deps/minifier/bin/minify kThread_nextTick_js 1 < src/thread_nextTick.js > src/thread_nextTick.js.c;
env PATH=./node_modules/.bin:"$PATH" ./deps/minifier/bin/minify kThread_timer_js 1 < src/thread_timer.js > src/thread_timer.js.c;
env PATH=./node_modules/.bin:"$PATH" lsc -cbp src/load.ls > src/load.js;
./deps/minifier/bin/minify kLoad_js 1 1 < src/load.js > src/load.js.c;
"""
277 changes: 106 additions & 171 deletions src/WebWorkerThreads.cc

Large diffs are not rendered by default.

118 changes: 68 additions & 50 deletions src/jslib.cc
Original file line number Diff line number Diff line change
@@ -17,20 +17,24 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// Modified By LostAbaddon
// 2018.02.08
// Changed the output format of console.

#include <errno.h>

static const PropertyAttribute attribute_ro_dd = (PropertyAttribute)(ReadOnly | DontDelete);
static const PropertyAttribute attribute_ro_de_dd = (PropertyAttribute)(ReadOnly | DontEnum | DontDelete);

// 'obj.name = fnname' with PropertyAttribute attribute_ro_dd.
#define JSObjFn(obj, name, fnname) \
Nan::DefineOwnProperty(obj, Nan::New<String>(name).ToLocalChecked(), Nan::New<FunctionTemplate>(fnname)->GetFunction(), (PropertyAttribute)(DontDelete));
Nan::DefineOwnProperty(obj, Nan::New<String>(name).ToLocalChecked(), Nan::New<FunctionTemplate>(fnname)->GetFunction(), (PropertyAttribute)(DontDelete));

static void ReportException(Nan::TryCatch* try_catch) {
Nan::HandleScope scope;
Nan::HandleScope scope;

String::Utf8Value exception(try_catch->Exception());
Local<Message> message = try_catch->Message();
Local<Message> message = try_catch->Message();

if (message.IsEmpty()) {
printf("%s\n", *exception);
@@ -72,13 +76,13 @@ static void ReportException(Nan::TryCatch* try_catch) {

//static Local<Value> readFileSync_(const Arguments &info) {
NAN_METHOD(readFileSync_) {
Nan::HandleScope scope;
Nan::HandleScope scope;

FILE *f = fopen(*String::Utf8Value(Local<String>::Cast(info[0])), "rb");
FILE *f = fopen(*String::Utf8Value(Local<String>::Cast(info[0])), "rb");
if (f == NULL) {
char str[256];
sprintf(str, "Error: readfile open failed. %d %s\n", errno, strerror(errno));
return Nan::ThrowError(Nan::New<String>(str).ToLocalChecked());
return Nan::ThrowError(Nan::New<String>(str).ToLocalChecked());
}
fseek(f, 0, SEEK_END);
size_t s = ftell(f);
@@ -91,98 +95,112 @@ NAN_METHOD(readFileSync_) {
sprintf(str, "Error: readfile read failed. %d %s\n", ferror(f), strerror(ferror(f)));
delete[] buf;
fclose(f);
Nan::ThrowError(str);
Nan::ThrowError(str);
}
buf[s] = 0;
Local<String> str = Nan::New<String>(buf).ToLocalChecked();
Local<String> str = Nan::New<String>(buf).ToLocalChecked();
free(buf);
fclose(f);

info.GetReturnValue().Set(str);
info.GetReturnValue().Set(str);
}



// console section
static inline void console_common_1(const Local<Value> &v, FILE* fd, const int deep) {
static inline void console_common_1(const Local<Value> &v, FILE* fd, const int deep, const bool inside) {
char indent[36] = {};
char tab = 0x20;
int i, n;
int mark = 0;
for (i=0; i<deep; ++i) {
indent[mark++] = 0x20;
indent[mark++] = 0x20;
indent[mark++] = tab;
indent[mark++] = tab;
}

Local<Value> lv;
Local<Value> lv;
if (v->IsFunction()) {
fprintf(fd, "%s[Function]\n", indent);
} else if (v->IsObject()) {
Local<Object> obj = Local<Object>::Cast(v);
Local<Array> ar = obj->GetPropertyNames();
fprintf(fd, "%s{Object}\n", indent);
for (i=0, n=ar->Length(); i<n; ++i) {
lv = obj->Get(ar->Get(i));
fprintf(fd, "%s%s: ", indent, *(String::Utf8Value(Local<String>::Cast(ar->Get(i)))));
fprintf(fd, "[Function]\n");
} else if (v->IsArray()) {
Local<Array> obj = Local<Array>::Cast(v);
if (inside) fprintf(fd, "[\n");
else fprintf(fd, "%s[\n", indent);
for (i=0, n=obj->Length(); i<n; ++i) {
lv = obj->Get(i);
if (lv->IsFunction()) {
fprintf(fd, "%s[Function]\n", indent);
fprintf(fd, "%s%c%c[Function]", indent, tab, tab);
} else if (lv->IsObject() || lv->IsArray()) {
//fprintf(fd, "\n");
console_common_1(lv, fd, deep+1);
fprintf(fd, "%s%c%c", indent, tab, tab);
console_common_1(lv, fd, deep+1, true);
} else {
fprintf(fd, "%s%s\n", indent, *(String::Utf8Value(Local<String>::Cast(lv))));
fprintf(fd, "%s%c%c%s", indent, tab, tab, *(String::Utf8Value(Local<String>::Cast(lv))));
}
if (i<n-1) {
fprintf(fd, ",\n");
}
else {
fprintf(fd, "\n");
}
}
fprintf(fd, "%s{/Object}\n", indent);

} else if (v->IsArray()) {
Local<Array> obj = Local<Array>::Cast(v);
fprintf(fd, "%s[Array]\n", indent);
for (i=0, n=obj->Length(); i<n; ++i) {
lv = obj->Get(i);
fprintf(fd, "%s%d: ", indent, i);
if (inside) fprintf(fd, "%s]", indent);
else fprintf(fd, "%s]\n", indent);
} else if (v->IsObject()) {
Local<Object> obj = Local<Object>::Cast(v);
Local<Array> ar = obj->GetPropertyNames();
if (inside) fprintf(fd, "{\n");
else fprintf(fd, "%s{\n", indent);
for (i=0, n=ar->Length(); i<n; ++i) {
lv = obj->Get(ar->Get(i));
// Print Key
fprintf(fd, "%s%c%c%s: ", indent, tab, tab, *(String::Utf8Value(Local<String>::Cast(ar->Get(i)))));
// Pring Value
if (lv->IsFunction()) {
fprintf(fd, "%s[Function]\n", indent);
fprintf(fd, "[Function]");
} else if (lv->IsObject() || lv->IsArray()) {
fprintf(fd, "\n");
console_common_1(lv, fd, deep+1);
console_common_1(lv, fd, deep+1, true);
} else {
fprintf(fd, "%s%s\n", indent, *(String::Utf8Value(Local<String>::Cast(lv))));
fprintf(fd, "%s", *(String::Utf8Value(Local<String>::Cast(lv))));
}
if (i<n-1) {
fprintf(fd, ",\n");
}
else {
fprintf(fd, "\n");
}
}
fprintf(fd, "%s[/Array]\n", indent);
if (inside) fprintf(fd, "%s}", indent);
else fprintf(fd, "%s}\n", indent);
} else {
fprintf(fd, "%s%s\n", indent, *(String::Utf8Value(Local<String>::Cast(v))));
fprintf(fd, "%s\n", *(String::Utf8Value(Local<String>::Cast(v))));
}
}

NAN_METHOD(console_log) {
Nan::HandleScope scope;
Nan::HandleScope scope;

Nan::TryCatch trycatch;

for (int i=0, n=info.Length(); i<n; ++i) {
console_common_1(info[i], stdout, 0);
for (int i=0, n=info.Length(); i<n; ++i) {
console_common_1(info[i], stdout, 0, false);
}

if (trycatch.HasCaught()) {
ReportException(&trycatch);
}

info.GetReturnValue().SetUndefined();
info.GetReturnValue().SetUndefined();
}

NAN_METHOD(console_error) {
Nan::HandleScope scope;
Nan::HandleScope scope;

Nan::TryCatch trycatch;

for (int i=0, n=info.Length(); i<n; ++i) {
console_common_1(info[i], stderr, 0);
for (int i=0, n=info.Length(); i<n; ++i) {
console_common_1(info[i], stderr, 0, false);
}

if (trycatch.HasCaught()) {
ReportException(&trycatch);
}

info.GetReturnValue().SetUndefined();
}
info.GetReturnValue().SetUndefined();
}
1 change: 1 addition & 0 deletions src/thread_nextTick.js
2 changes: 1 addition & 1 deletion src/thread_nextTick.js.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
static const char* kThread_nextTick_js= "(\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x54\x68\x72\x65\x61\x64\x4e\x65\x78\x74\x54\x69\x63\x6b\x28\x29\x7b\x66\x75\x6e\x63\x74\x69\x6f\x6e \x6e\x65\x78\x74\x54\x69\x63\x6b\x28\x63\x62\x29\x7b\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x2e\x70\x75\x73\x68\x28\x63\x62\x29\x3b\x72\x65\x74\x75\x72\x6e \x74\x68\x69\x73\x3b\x7d\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x64\x69\x73\x70\x61\x74\x63\x68\x4e\x65\x78\x74\x54\x69\x63\x6b\x73\x28\x6c\x2c\x70\x2c\x65\x72\x72\x2c\x5f\x6e\x74\x71\x29\x7b\x76\x61\x72 \x65\x3b\x69\x66\x28\x6c\x3d\x28\x5f\x6e\x74\x71\x3d\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x29\x2e\x6c\x65\x6e\x67\x74\x68\x29\x7b\x70\x3d\x65\x72\x72\x3d\x30\x3b\x74\x72\x79\x7b\x66\x6f\x72\x28\x3b\x3b\x29\x7b\x5f\x6e\x74\x71\x5b\x70\x5d\x28\x29\x3b\x69\x66\x28\x21\x28\x2b\x2b\x70\x3c\x6c\x29\x29\x7b\x62\x72\x65\x61\x6b\x3b\x7d\x7d\x7d\x63\x61\x74\x63\x68\x28\x65\x24\x29\x7b\x65\x3d\x65\x24\x3b\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x3d\x5f\x6e\x74\x71\x2e\x73\x6c\x69\x63\x65\x28\x2b\x2b\x70\x29\x3b\x74\x68\x72\x6f\x77 \x65\x3b\x7d\n\x72\x65\x74\x75\x72\x6e\x28\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x3d\x5f\x6e\x74\x71\x2e\x73\x6c\x69\x63\x65\x28\x70\x29\x29\x2e\x6c\x65\x6e\x67\x74\x68\x3b\x7d\n\x72\x65\x74\x75\x72\x6e \x30\x3b\x7d\n\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x3d\x5b\x5d\x3b\x74\x68\x72\x65\x61\x64\x2e\x6e\x65\x78\x74\x54\x69\x63\x6b\x3d\x6e\x65\x78\x74\x54\x69\x63\x6b\x3b\x72\x65\x74\x75\x72\x6e \x64\x69\x73\x70\x61\x74\x63\x68\x4e\x65\x78\x74\x54\x69\x63\x6b\x73\x3b\x7d)()";
static const char* kThread_nextTick_js= "(\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x54\x68\x72\x65\x61\x64\x4e\x65\x78\x74\x54\x69\x63\x6b\x28\x29\x7b\x66\x75\x6e\x63\x74\x69\x6f\x6e \x6e\x65\x78\x74\x54\x69\x63\x6b\x28\x63\x62\x29\x7b\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x2e\x70\x75\x73\x68\x28\x63\x62\x29\x3b\x72\x65\x74\x75\x72\x6e \x74\x68\x69\x73\x3b\x7d\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x64\x69\x73\x70\x61\x74\x63\x68\x4e\x65\x78\x74\x54\x69\x63\x6b\x73\x28\x6c\x2c\x70\x2c\x65\x72\x72\x2c\x5f\x6e\x74\x71\x29\x7b\x76\x61\x72 \x65\x3b\x69\x66\x28\x6c\x3d\x28\x5f\x6e\x74\x71\x3d\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x29\x2e\x6c\x65\x6e\x67\x74\x68\x29\x7b\x70\x3d\x65\x72\x72\x3d\x30\x3b\x74\x72\x79\x7b\x66\x6f\x72\x28\x3b\x3b\x29\x7b\x5f\x6e\x74\x71\x5b\x70\x5d\x28\x29\x3b\x6c\x3d\x5f\x6e\x74\x71\x2e\x6c\x65\x6e\x67\x74\x68\x3b\x69\x66\x28\x21\x28\x2b\x2b\x70\x3c\x6c\x29\x29\x7b\x62\x72\x65\x61\x6b\x3b\x7d\x7d\x7d\x63\x61\x74\x63\x68\x28\x65\x24\x29\x7b\x65\x3d\x65\x24\x3b\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x3d\x5f\x6e\x74\x71\x2e\x73\x6c\x69\x63\x65\x28\x2b\x2b\x70\x29\x3b\x74\x68\x72\x6f\x77 \x65\x3b\x7d\n\x72\x65\x74\x75\x72\x6e\x28\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x3d\x5f\x6e\x74\x71\x2e\x73\x6c\x69\x63\x65\x28\x70\x29\x29\x2e\x6c\x65\x6e\x67\x74\x68\x3b\x7d\n\x72\x65\x74\x75\x72\x6e \x30\x3b\x7d\n\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x74\x71\x3d\x5b\x5d\x3b\x74\x68\x72\x65\x61\x64\x2e\x6e\x65\x78\x74\x54\x69\x63\x6b\x3d\x6e\x65\x78\x74\x54\x69\x63\x6b\x3b\x72\x65\x74\x75\x72\x6e \x64\x69\x73\x70\x61\x74\x63\x68\x4e\x65\x78\x74\x54\x69\x63\x6b\x73\x3b\x7d)()";
1 change: 1 addition & 0 deletions src/thread_nextTick.ls
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ function ThreadNextTick
try
while true
_ntq[p]!
l = _ntq.length
break unless ++p < l
catch e
thread._ntq = _ntq.slice ++p
124 changes: 124 additions & 0 deletions src/thread_timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
function ThreadSetImmediate(){
function setImmediate(cb){
thread._nlp.push(cb);
return this;
}
function clearImmediate(cb){
var i = thread._nlp.indexOf(cb);
if (i >= 0) thread._nlp.splice(i, 1);
return this;
}
function dispatchSetImmediate(l, p, err, _nlp){
_nlp = thread._nlp.map(i => i);
thread._nlp.splice(0, thread._nlp.length);
if (l = _nlp.length) {
err = 0;
try {
for (p = 0; p < l; p ++) {
_nlp[p]();
}
} catch (e) {
_nlp = _nlp.slice(++p);
_nlp.map(i => thread._nlp.push(i));
throw e;
}
return _nlp.length;
}
return 0;
}
thread._nlp = [];
thread.setImmediate = setImmediate;
thread.clearImmediate = clearImmediate;

thread.AllTimerTag = Symbol('AllTimerTag');
var timerPool = [];
const getStamp = () => new Date().getTime();
const invokeTask = () => {
var pending = [], now = getStamp();
for (var i = 0, l = timerPool.length; i < l; i ++) {
let task = timerPool[i];
if (task.expire <= now) {
pending.push(task);
if (!task.keep) {
timerPool.splice(i, 1);
l --;
i --;
}
}
else break;
}
if (timerPool.length > 0) setImmediate(invokeTask);
pending.map(task => {
var t = 0;
if (task.tag === 2 && task.delay !== 2) {
t = task.delay === 0 ? now : getStamp();
}
task.func();
if (task.tag === 2 && task.delay == 2) {
t = getStamp();
}
if (task.tag === 2) {
task.expire = t + task.duration;
}
});
};
const dispatchTask = (task) => {
timerPool.push(task);
timerPool.sort((ta, tb) => ta.expire - tb.expire);
setImmediate(invokeTask);
};
const removeTask = (tag, func) => {
var index = -1;
timerPool.some((task, i) => {
if (task.tag === tag && task.func === func) {
index = i;
return true;
}
});
if (index < 0) return;
timerPool.splice(index, 1);
};
const removeAllTask = (tag) => {
var l = timerPool.length, i;
for (i = l - 1; i >= 0; i --) {
let task = timerPool[i];
if (task.tag === tag) {
timerPool.splice(i, 1);
}
}
};
thread.setTimeout = (cb, delay) => {
delay = (delay * 1) || 0;
var data = {
tag: 1,
func: cb,
expire: getStamp() + delay,
duration: delay,
keep: false,
delay: 0
};
dispatchTask(data);
};
thread.clearTimeout = cb => {
if (cb === thread.AllTimerTag) removeAllTask(1);
else removeTask(1, cb);
};
thread.setInterval = (cb, delay, delayMode) => {
delay = (delay * 1) || 0;
var data = {
tag: 2,
func: cb,
expire: getStamp() + delay,
duration: delay,
keep: true,
delay: delayMode || 0
};
dispatchTask(data);
};
thread.clearInterval = cb => {
if (cb === thread.AllTimerTag) removeAllTask(2);
else removeTask(2, cb);
};

return dispatchSetImmediate;
}
1 change: 1 addition & 0 deletions src/thread_timer.js.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static const char* kThread_timer_js= "(\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x54\x68\x72\x65\x61\x64\x53\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x28\x29\x7b\x66\x75\x6e\x63\x74\x69\x6f\x6e \x73\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x28\x63\x62\x29\x7b\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x2e\x70\x75\x73\x68\x28\x63\x62\x29\x3b\x72\x65\x74\x75\x72\x6e \x74\x68\x69\x73\x3b\x7d\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x63\x6c\x65\x61\x72\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x28\x63\x62\x29\x7b\x76\x61\x72 \x69\x3d\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x2e\x69\x6e\x64\x65\x78\x4f\x66\x28\x63\x62\x29\x3b\x69\x66\x28\x69\x3e\x3d\x30\x29\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x2e\x73\x70\x6c\x69\x63\x65\x28\x69\x2c\x31\x29\x3b\x72\x65\x74\x75\x72\x6e \x74\x68\x69\x73\x3b\x7d\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x64\x69\x73\x70\x61\x74\x63\x68\x53\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x28\x6c\x2c\x70\x2c\x65\x72\x72\x2c\x5f\x6e\x6c\x70\x29\x7b\x5f\x6e\x6c\x70\x3d\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x2e\x6d\x61\x70\x28\x69\x3d\x3e\x69\x29\x3b\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x2e\x73\x70\x6c\x69\x63\x65\x28\x30\x2c\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x2e\x6c\x65\x6e\x67\x74\x68\x29\x3b\x69\x66\x28\x6c\x3d\x5f\x6e\x6c\x70\x2e\x6c\x65\x6e\x67\x74\x68\x29\x7b\x65\x72\x72\x3d\x30\x3b\x74\x72\x79\x7b\x66\x6f\x72\x28\x70\x3d\x30\x3b\x70\x3c\x6c\x3b\x70\x2b\x2b\x29\x7b\x5f\x6e\x6c\x70\x5b\x70\x5d\x28\x29\x3b\x7d\x7d\x63\x61\x74\x63\x68\x28\x65\x29\x7b\x5f\x6e\x6c\x70\x3d\x5f\x6e\x6c\x70\x2e\x73\x6c\x69\x63\x65\x28\x2b\x2b\x70\x29\x3b\x5f\x6e\x6c\x70\x2e\x6d\x61\x70\x28\x69\x3d\x3e\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x2e\x70\x75\x73\x68\x28\x69\x29\x29\x3b\x74\x68\x72\x6f\x77 \x65\x3b\x7d\n\x72\x65\x74\x75\x72\x6e \x5f\x6e\x6c\x70\x2e\x6c\x65\x6e\x67\x74\x68\x3b\x7d\n\x72\x65\x74\x75\x72\x6e \x30\x3b\x7d\n\x74\x68\x72\x65\x61\x64\x2e\x5f\x6e\x6c\x70\x3d\x5b\x5d\x3b\x74\x68\x72\x65\x61\x64\x2e\x73\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x3d\x73\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x3b\x74\x68\x72\x65\x61\x64\x2e\x63\x6c\x65\x61\x72\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x3d\x63\x6c\x65\x61\x72\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x3b\x74\x68\x72\x65\x61\x64\x2e\x41\x6c\x6c\x54\x69\x6d\x65\x72\x54\x61\x67\x3d\x53\x79\x6d\x62\x6f\x6c\x28\x27\x41\x6c\x6c\x54\x69\x6d\x65\x72\x54\x61\x67\x27\x29\x3b\x76\x61\x72 \x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x3d\x5b\x5d\x3b\x63\x6f\x6e\x73\x74 \x67\x65\x74\x53\x74\x61\x6d\x70\x3d\x28\x29\x3d\x3e\x6e\x65\x77 \x44\x61\x74\x65\x28\x29\x2e\x67\x65\x74\x54\x69\x6d\x65\x28\x29\x3b\x63\x6f\x6e\x73\x74 \x69\x6e\x76\x6f\x6b\x65\x54\x61\x73\x6b\x3d\x28\x29\x3d\x3e\x7b\x76\x61\x72 \x70\x65\x6e\x64\x69\x6e\x67\x3d\x5b\x5d\x2c\x6e\x6f\x77\x3d\x67\x65\x74\x53\x74\x61\x6d\x70\x28\x29\x3b\x66\x6f\x72\x28\x76\x61\x72 \x69\x3d\x30\x2c\x6c\x3d\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x6c\x65\x6e\x67\x74\x68\x3b\x69\x3c\x6c\x3b\x69\x2b\x2b\x29\x7b\x6c\x65\x74 \x74\x61\x73\x6b\x3d\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x5b\x69\x5d\x3b\x69\x66\x28\x74\x61\x73\x6b\x2e\x65\x78\x70\x69\x72\x65\x3c\x3d\x6e\x6f\x77\x29\x7b\x70\x65\x6e\x64\x69\x6e\x67\x2e\x70\x75\x73\x68\x28\x74\x61\x73\x6b\x29\x3b\x69\x66\x28\x21\x74\x61\x73\x6b\x2e\x6b\x65\x65\x70\x29\x7b\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x73\x70\x6c\x69\x63\x65\x28\x69\x2c\x31\x29\x3b\x6c\x2d\x2d\x3b\x69\x2d\x2d\x3b\x7d\x7d\n\x65\x6c\x73\x65 \x62\x72\x65\x61\x6b\x3b\x7d\n\x69\x66\x28\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x6c\x65\x6e\x67\x74\x68\x3e\x30\x29\x73\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x28\x69\x6e\x76\x6f\x6b\x65\x54\x61\x73\x6b\x29\x3b\x70\x65\x6e\x64\x69\x6e\x67\x2e\x6d\x61\x70\x28\x74\x61\x73\x6b\x3d\x3e\x7b\x76\x61\x72 \x74\x3d\x30\x3b\x69\x66\x28\x74\x61\x73\x6b\x2e\x74\x61\x67\x3d\x3d\x3d\x32\x26\x26\x74\x61\x73\x6b\x2e\x64\x65\x6c\x61\x79\x21\x3d\x3d\x32\x29\x7b\x74\x3d\x74\x61\x73\x6b\x2e\x64\x65\x6c\x61\x79\x3d\x3d\x3d\x30\x3f\x6e\x6f\x77\x3a\x67\x65\x74\x53\x74\x61\x6d\x70\x28\x29\x3b\x7d\n\x74\x61\x73\x6b\x2e\x66\x75\x6e\x63\x28\x29\x3b\x69\x66\x28\x74\x61\x73\x6b\x2e\x74\x61\x67\x3d\x3d\x3d\x32\x26\x26\x74\x61\x73\x6b\x2e\x64\x65\x6c\x61\x79\x3d\x3d\x32\x29\x7b\x74\x3d\x67\x65\x74\x53\x74\x61\x6d\x70\x28\x29\x3b\x7d\n\x69\x66\x28\x74\x61\x73\x6b\x2e\x74\x61\x67\x3d\x3d\x3d\x32\x29\x7b\x74\x61\x73\x6b\x2e\x65\x78\x70\x69\x72\x65\x3d\x74\x2b\x74\x61\x73\x6b\x2e\x64\x75\x72\x61\x74\x69\x6f\x6e\x3b\x7d\x7d\x29\x3b\x7d\x3b\x63\x6f\x6e\x73\x74 \x64\x69\x73\x70\x61\x74\x63\x68\x54\x61\x73\x6b\x3d\x28\x74\x61\x73\x6b\x29\x3d\x3e\x7b\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x70\x75\x73\x68\x28\x74\x61\x73\x6b\x29\x3b\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x73\x6f\x72\x74\x28\x28\x74\x61\x2c\x74\x62\x29\x3d\x3e\x74\x61\x2e\x65\x78\x70\x69\x72\x65\x2d\x74\x62\x2e\x65\x78\x70\x69\x72\x65\x29\x3b\x73\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x28\x69\x6e\x76\x6f\x6b\x65\x54\x61\x73\x6b\x29\x3b\x7d\x3b\x63\x6f\x6e\x73\x74 \x72\x65\x6d\x6f\x76\x65\x54\x61\x73\x6b\x3d\x28\x74\x61\x67\x2c\x66\x75\x6e\x63\x29\x3d\x3e\x7b\x76\x61\x72 \x69\x6e\x64\x65\x78\x3d\x2d\x31\x3b\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x73\x6f\x6d\x65\x28\x28\x74\x61\x73\x6b\x2c\x69\x29\x3d\x3e\x7b\x69\x66\x28\x74\x61\x73\x6b\x2e\x74\x61\x67\x3d\x3d\x3d\x74\x61\x67\x26\x26\x74\x61\x73\x6b\x2e\x66\x75\x6e\x63\x3d\x3d\x3d\x66\x75\x6e\x63\x29\x7b\x69\x6e\x64\x65\x78\x3d\x69\x3b\x72\x65\x74\x75\x72\x6e \x74\x72\x75\x65\x3b\x7d\x7d\x29\x3b\x69\x66\x28\x69\x6e\x64\x65\x78\x3c\x30\x29\x72\x65\x74\x75\x72\x6e\x3b\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x73\x70\x6c\x69\x63\x65\x28\x69\x6e\x64\x65\x78\x2c\x31\x29\x3b\x7d\x3b\x63\x6f\x6e\x73\x74 \x72\x65\x6d\x6f\x76\x65\x41\x6c\x6c\x54\x61\x73\x6b\x3d\x28\x74\x61\x67\x29\x3d\x3e\x7b\x76\x61\x72 \x6c\x3d\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x6c\x65\x6e\x67\x74\x68\x2c\x69\x3b\x66\x6f\x72\x28\x69\x3d\x6c\x2d\x31\x3b\x69\x3e\x3d\x30\x3b\x69\x2d\x2d\x29\x7b\x6c\x65\x74 \x74\x61\x73\x6b\x3d\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x5b\x69\x5d\x3b\x69\x66\x28\x74\x61\x73\x6b\x2e\x74\x61\x67\x3d\x3d\x3d\x74\x61\x67\x29\x7b\x74\x69\x6d\x65\x72\x50\x6f\x6f\x6c\x2e\x73\x70\x6c\x69\x63\x65\x28\x69\x2c\x31\x29\x3b\x7d\x7d\x7d\x3b\x74\x68\x72\x65\x61\x64\x2e\x73\x65\x74\x54\x69\x6d\x65\x6f\x75\x74\x3d\x28\x63\x62\x2c\x64\x65\x6c\x61\x79\x29\x3d\x3e\x7b\x64\x65\x6c\x61\x79\x3d\x28\x64\x65\x6c\x61\x79\x2a\x31\x29\x7c\x7c\x30\x3b\x76\x61\x72 \x64\x61\x74\x61\x3d\x7b\x74\x61\x67\x3a\x31\x2c\x66\x75\x6e\x63\x3a\x63\x62\x2c\x65\x78\x70\x69\x72\x65\x3a\x67\x65\x74\x53\x74\x61\x6d\x70\x28\x29\x2b\x64\x65\x6c\x61\x79\x2c\x64\x75\x72\x61\x74\x69\x6f\x6e\x3a\x64\x65\x6c\x61\x79\x2c\x6b\x65\x65\x70\x3a\x66\x61\x6c\x73\x65\x2c\x64\x65\x6c\x61\x79\x3a\x30\x7d\x3b\x64\x69\x73\x70\x61\x74\x63\x68\x54\x61\x73\x6b\x28\x64\x61\x74\x61\x29\x3b\x7d\x3b\x74\x68\x72\x65\x61\x64\x2e\x63\x6c\x65\x61\x72\x54\x69\x6d\x65\x6f\x75\x74\x3d\x63\x62\x3d\x3e\x7b\x69\x66\x28\x63\x62\x3d\x3d\x3d\x74\x68\x72\x65\x61\x64\x2e\x41\x6c\x6c\x54\x69\x6d\x65\x72\x54\x61\x67\x29\x72\x65\x6d\x6f\x76\x65\x41\x6c\x6c\x54\x61\x73\x6b\x28\x31\x29\x3b\x65\x6c\x73\x65 \x72\x65\x6d\x6f\x76\x65\x54\x61\x73\x6b\x28\x31\x2c\x63\x62\x29\x3b\x7d\x3b\x74\x68\x72\x65\x61\x64\x2e\x73\x65\x74\x49\x6e\x74\x65\x72\x76\x61\x6c\x3d\x28\x63\x62\x2c\x64\x65\x6c\x61\x79\x2c\x64\x65\x6c\x61\x79\x4d\x6f\x64\x65\x29\x3d\x3e\x7b\x64\x65\x6c\x61\x79\x3d\x28\x64\x65\x6c\x61\x79\x2a\x31\x29\x7c\x7c\x30\x3b\x76\x61\x72 \x64\x61\x74\x61\x3d\x7b\x74\x61\x67\x3a\x32\x2c\x66\x75\x6e\x63\x3a\x63\x62\x2c\x65\x78\x70\x69\x72\x65\x3a\x67\x65\x74\x53\x74\x61\x6d\x70\x28\x29\x2b\x64\x65\x6c\x61\x79\x2c\x64\x75\x72\x61\x74\x69\x6f\x6e\x3a\x64\x65\x6c\x61\x79\x2c\x6b\x65\x65\x70\x3a\x74\x72\x75\x65\x2c\x64\x65\x6c\x61\x79\x3a\x64\x65\x6c\x61\x79\x4d\x6f\x64\x65\x7c\x7c\x30\x7d\x3b\x64\x69\x73\x70\x61\x74\x63\x68\x54\x61\x73\x6b\x28\x64\x61\x74\x61\x29\x3b\x7d\x3b\x74\x68\x72\x65\x61\x64\x2e\x63\x6c\x65\x61\x72\x49\x6e\x74\x65\x72\x76\x61\x6c\x3d\x63\x62\x3d\x3e\x7b\x69\x66\x28\x63\x62\x3d\x3d\x3d\x74\x68\x72\x65\x61\x64\x2e\x41\x6c\x6c\x54\x69\x6d\x65\x72\x54\x61\x67\x29\x72\x65\x6d\x6f\x76\x65\x41\x6c\x6c\x54\x61\x73\x6b\x28\x32\x29\x3b\x65\x6c\x73\x65 \x72\x65\x6d\x6f\x76\x65\x54\x61\x73\x6b\x28\x32\x2c\x63\x62\x29\x3b\x7d\x3b\x72\x65\x74\x75\x72\x6e \x64\x69\x73\x70\x61\x74\x63\x68\x53\x65\x74\x49\x6d\x6d\x65\x64\x69\x61\x74\x65\x3b\x7d)()";
5 changes: 5 additions & 0 deletions test/nextloop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Threads = require('../');
var worker = new Threads.Worker(__dirname + '/nextloop_worker.js');
worker.addEventListener('close', () => {
worker.terminate();
});
41 changes: 41 additions & 0 deletions test/nextloop_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
console.log('Welcome To Kosmos!!!');

var go = true;
var time = 0;
var limit = 1;

const ticker = () => {
console.log('tick');
if (time < limit) {
thread.nextTick(ticker);
time ++;
}
else {
time = 0;
go = true;
}
};

const looper = () => {
console.log('loop');
thread.setImmediate(looper);
if (go) {
go = false;
thread.nextTick(ticker);
}
};

var test = () => {
console.log('>>>>>>>> FUCK!!!');
};
try {
// thread.setImmediate(looper);
thread.setInterval(test, 100);
thread.setTimeout(() => {
thread.clearInterval(test);
thread.emit('close');
}, 100);
}
catch (err) {
console.log(err.message);
}