diff --git a/index.js b/index.js index 054017c..1cfc61f 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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; @@ -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; @@ -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 @@ -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; } @@ -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 @@ -172,7 +172,7 @@ var zco_core = function (gen, model) { } - var next = function () { + var zco_core_next = function () { if (suspended) { return; } @@ -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]; } @@ -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); } } @@ -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); } @@ -221,7 +221,7 @@ var zco_core = function (gen, model) { } - return future; + return zco_core_future; } var all = function () { diff --git a/package.json b/package.json index c920c5a..b6dc7b9 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/track.js b/track.js index cfac68a..83e895f 100644 --- a/track.js +++ b/track.js @@ -1,6 +1,7 @@ /** * Created by yyrdl on 2017/6/1. Happy Children's Day * record and modify callstack + * */ /** @@ -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 ()"; +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 @@ -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; }