Skip to content

Commit

Permalink
adapt callstack to different version of node
Browse files Browse the repository at this point in the history
  • Loading branch information
yyrdl committed Jun 26, 2017
1 parent a5f42bc commit a7cc3b4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
46 changes: 23 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var zco_core = function (gen, model) {
frame = track.callStackFrame(4)
}

var _run_callback = function (error, value) {
var zco_core_run_callback = function (error, value) {

if (callback != null) {
return callback(error, value);
Expand All @@ -45,11 +45,11 @@ var zco_core = function (gen, model) {
}
}

var _make_defer_func = function (error) {
var zco_core_make_defer_func = function (error) {
return zco_core(deferFunc, WRAP_DEFER_MODEL, error); //build defer func,and delivery error
}

var _return = function (e, v) {
var zco_core_return = function (e, v) {

if (hasRunCallback) {
return;
Expand All @@ -64,23 +64,23 @@ var zco_core = function (gen, model) {

if (deferFunc != null) {

var _func = _make_defer_func(e);
var _func = zco_core_make_defer_func(e);

return _func(function (ee) {
if (ee != null && callback === null) {
throw ee; //error occurred in defer,throw out if no handler provided
} else {
_run_callback(e || ee, v)
zco_core_run_callback(e || ee, v)
}
});

}

return _run_callback(e, v);
return zco_core_run_callback(e, v);

}

var run = function (arg) {
var zco_core_run = function (arg) {

var v = null,
error = null;
Expand All @@ -93,24 +93,24 @@ var zco_core = function (gen, model) {
}

if (error != null) {
return _return(error);
return zco_core_return(error);
}

if (v.done) {
return _return(null, v.value);
return zco_core_return(null, v.value);
}

if (isZcoFuture(v.value)) {
current_child_future = v.value;
internal = true;
return v.value(next);
return v.value(zco_core_next);
}

}

var nextSlave = function (arg) {
var zco_core_nextSlave = function (arg) {
hasReturn = false;
return run(arg);
return zco_core_run(arg);
}
/**
* define defer
Expand All @@ -131,17 +131,17 @@ var zco_core = function (gen, model) {
/**
* define zco future
* */
var future = function (cb) {
var zco_core_future = function (cb) {
if ("function" == typeof cb) {
callback = cb;
}
run();
zco_core_run();
}

/**
* define 'suspend' method
* */
future.__suspend__ = function () {
zco_core_future.__suspend__ = function () {
if (hasRunCallback || suspended) {
return;
}
Expand All @@ -152,7 +152,7 @@ var zco_core = function (gen, model) {

if (deferFunc != null) {

var _func = _make_defer_func(new Error("coroutine is suspended,maybe because of timeout."));
var _func = zco_core_make_defer_func(new Error("coroutine is suspended,maybe because of timeout."));

/**
* run defer ,ignore the error occurred in defer
Expand All @@ -172,7 +172,7 @@ var zco_core = function (gen, model) {

}

var next = function () {
var zco_core_next = function () {
if (suspended) {
return;
}
Expand All @@ -181,7 +181,7 @@ var zco_core = function (gen, model) {

if (model === BRIEF_MODEl && true === internal) {
if (arg[0] !== null) {
return _return(arg[0]);
return zco_core_return(arg[0]);
}
arg = arg[1];
}
Expand All @@ -192,13 +192,13 @@ var zco_core = function (gen, model) {

setTimeout(function () {

nextSlave(arg);
zco_core_nextSlave(arg);

}, 0);

} else {

nextSlave(arg);
zco_core_nextSlave(arg);

}
}
Expand All @@ -207,11 +207,11 @@ var zco_core = function (gen, model) {

if (model === WRAP_DEFER_MODEL) {

iterator = gen(next, arguments[2]);
iterator = gen(zco_core_next, arguments[2]);

} else {

iterator = gen(next, defer);
iterator = gen(zco_core_next, defer);

}

Expand All @@ -221,7 +221,7 @@ var zco_core = function (gen, model) {

}

return future;
return zco_core_future;
}

var all = function () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zco",
"version": "1.3.3",
"version": "1.3.4",
"description": "co_routine code style in js ,work with callback,and no promise",
"main": "index.js",
"scripts": {
Expand Down
45 changes: 32 additions & 13 deletions track.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Created by yyrdl on 2017/6/1. Happy Children's Day
* record and modify callstack
*
*/

/**
Expand Down Expand Up @@ -33,20 +34,26 @@ var BLANK_LENGTH = 5;
index = stack.indexOf(STACK_LINE_SEPARATOR, index + 1);
var end = stack.indexOf(STACK_LINE_SEPARATOR, index + 1);
END_LINE = stack.substring(index, end);

})()

/**
* at zco_code where we catch the error
* */
var GENERATOR_LINE = STACK_LINE_SEPARATOR + " Generator.next (<anonymous>)";
var tail = function (stack) {
var index = stack.indexOf("zco_core_run");
if (index > -1) {
var next_index = stack.lastIndexOf("next", index);
if (next_index != -1 && stack.lastIndexOf(STACK_LINE_SEPARATOR, index) === stack.indexOf(STACK_LINE_SEPARATOR, next_index)) {
index = stack.lastIndexOf(STACK_LINE_SEPARATOR, next_index);
} else {
index = -1;
}
}
return index;
}

var appendStackFrame = function (error, frame) {
if (error && "string" === typeof error.stack) {
var index = error.stack.indexOf(GENERATOR_LINE);
if (index !== -1) {

error.stack = error.stack.substring(0, index) + "\n " + frame;
var index = tail(error.stack);
if (index != -1) {
error.stack = error.stack.slice(0, index) + "\n " + frame;
} else {
/**
* some times the error is not been caught by zco
Expand All @@ -73,17 +80,29 @@ var callStackFrame = function (deep) {
}
stack = stack.slice(start_index + BLANK_LENGTH, end_index);
/**
* remove zco internal call-stack,zco core is robust enough ,and not going to modify anymore
* remove zco internal call-stack
* */
if (stack.indexOf("at Generator.next") > -1 && stack.indexOf("at run") > -1 && stack.indexOf("at future") > -1) {
var head = stack.slice(0, stack.lastIndexOf(STACK_LINE_SEPARATOR, stack.indexOf("at Generator.next")));
end_index = stack.indexOf(STACK_LINE_SEPARATOR, stack.indexOf("at future"));
var next_index = stack.indexOf("next");

if (next_index > -1 && stack.indexOf(STACK_LINE_SEPARATOR + " zco_core", next_index) === stack.indexOf(STACK_LINE_SEPARATOR, next_index)) {
var head = stack.slice(0, stack.lastIndexOf(STACK_LINE_SEPARATOR, next_index));
end_index = next_index;
for (; ; ) {
start_index = stack.indexOf("zco_core", end_index + 1);
if (start_index > -1) {
end_index = start_index;
} else {
break;
}
}
end_index = stack.indexOf(STACK_LINE_SEPARATOR, end_index + 1);
if (end_index < 0 || (end_index + STACK_LINE_SEPARATOR.length) == stack.length) {
stack = head;
} else {
stack = head + stack.slice(end_index, stack.length);
}
}

return stack;
}

Expand Down

1 comment on commit a7cc3b4

@yyrdl
Copy link
Owner Author

@yyrdl yyrdl commented on a7cc3b4 Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the callstack of node v6.x is differen from node v8.x , rename function name ,add "zco_core" prefix ,make it easier to modify callstack

Please sign in to comment.