Skip to content

Commit

Permalink
string/utf8 modules to generator ABI, fix coroutine.resume, add corou…
Browse files Browse the repository at this point in the history
…tine.status, coroutine.wrap
  • Loading branch information
serprex committed Apr 12, 2017
1 parent 6163be8 commit 536676d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
42 changes: 36 additions & 6 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ module.exports = function () {
env.set("coroutine", coroutine);
coroutine.set("create", coroutine_create);
coroutine.set("resume", coroutine_resume);
coroutine.set("status", coroutine_status);
coroutine.set("yield", coroutine_yield);
coroutine.set("wrap", coroutine_wrap);

var packge = new Table();
env.set("package", packge);
Expand Down Expand Up @@ -93,11 +95,17 @@ const obj = require("./obj"),


function*assert(stack, base) {
if (cond) throw val;
if (base + 1 == stack.length) throw "assert #1: expected value";
let cond = util.readarg(stack, base+1)
if (cond !== null && cond !== false) {
throw base+2 >= stack.length ? "Assertion failed!" : stack[base+2];
}
stack[base] = cond;
stack.length = base + 1;
}

function*error(stack, base) {
throw val;
throw util.readarg(stack, base+1);
}

function*pairs(stack, base) {
Expand Down Expand Up @@ -488,13 +496,35 @@ function*coroutine_create(stack, base) {
}

function*coroutine_resume(stack, base) {
var thread = util.readarg(stack, base+1);
if (!(thread instanceof Thread)) throw "coroutine.resume #1: expected thread"
yield*thread.resume(stack, base);
// run vm with stack
let thread = util.readarg(stack, base+1);
if (!(thread instanceof Thread)) throw "coroutine.resume #1: expected thread";
try {
yield*thread.resume(stack, base, base+2);
stack.splice(base, 0, true);
} catch (e) {
stack.length = base + 2;
stack[base] = false;
stack[base+1] = e;
}
}

function*coroutine_status(stack, base) {
let thread = util.readarg(stack, base+1);
if (!(thread instanceof Thread)) throw "coroutine.status #1: expected thread";
stack[base] = thread.status;
stack.length = base + 1;
}

function*coroutine_yield(stack, base) {
stack.splice(base, 1);
yield;
}

function coroutine_wrap(stack, base) {
let thread = util.readarg(stack, base+1);
if (!(thread instanceof Thread)) throw "coroutine.status #1: expected thread";
stack[base] = function*(stack, base){
yield*thread.resume(stack, base, base+1);
};
stack.length = base + 1;
}
6 changes: 3 additions & 3 deletions string.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ string.set("len", string_len);
string.set("lower", string_lower);
string.set("upper", string_upper);

function string_len(vm, stack, base) {
function*string_len(vm, stack, base) {
let s = util.readarg(stack, base+1);
if (typeof s != "string") throw "string.lower #1: expected string";
stack[base] = s.length;
stack.length = base + 1;
}

function string_lower(vm, stack, base) {
function*string_lower(vm, stack, base) {
let s = util.readarg(stack, base+1);
if (typeof s != "string") throw "string.lower #1: expected string";
stack[base] = s.toLowerCase();
stack.length = base + 1;
}

function string_upper(vm, stack, base) {
function*string_upper(vm, stack, base) {
let s = util.readarg(stack, base+1);
if (typeof s != "string") throw "string.upper #1: expected string";
stack[base] = s.toUpperCase();
Expand Down
4 changes: 2 additions & 2 deletions thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function Thread(stack, base) {
this.vm = runbc._run(vm, this.stack);
}
}
Thread.prototype.resume = function*(stack, base) {
Thread.prototype.resume = function*(stack, base, pbase) {
let cbase = this.stack.length;
if (this.status == "running") {
this.stack.push(...stack.splice(base+2));
this.stack.push(...stack.splice(pbase));
}
stack.length = base;
let res = this.vm.next();
Expand Down
2 changes: 1 addition & 1 deletion utf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var utf8 = module.exports = new Table();
utf8.set("char", utf8_char);
utf8.set("charpattern", "[\0-\x7f\xc2-\xf4][\x80-\xbf]*");

function utf8_char(vm, stack, base) {
function*utf8_char(vm, stack, base) {
let ret = "";
for (var i = base+1; i<stack.length; i++) {
// TODO reject invalid character codes
Expand Down

0 comments on commit 536676d

Please sign in to comment.