Skip to content

Commit

Permalink
[js] Async/await stuff
Browse files Browse the repository at this point in the history
ctx.die(...) calls are replace by new NQPException(...) for places
where we from a unresumable exception for low level errors
  • Loading branch information
pmurias committed Jul 23, 2017
1 parent 73f5eb8 commit a22631d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/vm/js/Compiler.nqp
Expand Up @@ -1381,7 +1381,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
}
else {
$loop.handle($type);
return Chunk.void("$*CTX.$type();\n");
return Chunk.void("{self.await}$*CTX.$type();\n");
}
}
}
Expand All @@ -1393,7 +1393,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {

if $label {
my $compiled_label := self.as_js($label, :want($T_OBJ));
Chunk.void($compiled_label, "$*CTX.{$type}Labeled({$compiled_label.expr});\n");
Chunk.void($compiled_label, "{self.await}$*CTX.{$type}Labeled({$compiled_label.expr});\n");
}
else {
self.NYI("can't find surrounding loop for $type");
Expand Down
20 changes: 10 additions & 10 deletions src/vm/js/Operations.nqp
Expand Up @@ -766,8 +766,8 @@ class QAST::OperationsJS {
my $*CTX := $handler_ctx;
my $body := $comp.as_js($protected, :$want);

my str $catch_wrapped_exception := "$handler_ctx.catchException(nqp.wrapException(e))";
my str $catch_exception := "$handler_ctx.catchException(e)";
my str $catch_wrapped_exception := "{$comp.await}$handler_ctx.catchException(nqp.wrapException(e))";
my str $catch_exception := "{$comp.await}$handler_ctx.catchException(e)";

if $want != $T_VOID {
$try_ret := $*BLOCK.add_tmp;
Expand All @@ -779,8 +779,8 @@ class QAST::OperationsJS {
%convert{$T_NUM} := 'toNum';
%convert{$T_INT} := 'toInt';
if nqp::existskey(%convert, $want) {
$catch_exception := 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_exception ~ ')';
$catch_wrapped_exception := 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_wrapped_exception ~ ')';
$catch_exception := $comp.await ~ 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_exception ~ ')';
$catch_wrapped_exception := $comp.await ~ 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_wrapped_exception ~ ')';
}

$catch_exception := "$try_ret = $catch_exception";
Expand Down Expand Up @@ -986,7 +986,7 @@ class QAST::OperationsJS {
add_simple_op('getcomp', $T_OBJ, [$T_STR], :side_effects);

add_simple_op('setparameterizer', $T_OBJ, [$T_OBJ, $T_OBJ], :side_effects, :ctx, :decont(0,1));
add_simple_op('parameterizetype', $T_OBJ, [$T_OBJ, $T_OBJ], :side_effects, :ctx, :decont(0,1));
add_simple_op('parameterizetype', $T_OBJ, [$T_OBJ, $T_OBJ], :side_effects, :ctx, :decont(0,1), :await);
add_simple_op('typeparameterized', $T_OBJ, [$T_OBJ], :decont(0));
add_simple_op('typeparameters', $T_OBJ, [$T_OBJ], :ctx, :decont(0));
add_simple_op('typeparameterat', $T_OBJ, [$T_OBJ, $T_INT], :ctx, :decont(0));
Expand Down Expand Up @@ -1405,7 +1405,7 @@ class QAST::OperationsJS {
|| $category ~~ QAST::Op && $category.op eq 'const' && $category.name eq 'CONTROL_RETURN';
}

add_simple_op('throwpayloadlexcaller', $T_VOID, [$T_INT, $T_OBJ], :side_effects, :!inlinable,
add_simple_op('throwpayloadlexcaller', $T_VOID, [$T_INT, $T_OBJ], :side_effects, :!inlinable, :await,
sub ($category, $payload) {"$*CTX.throwpayloadlexcaller($category, $payload)"});

add_op('throwpayloadlex', :!inlinable, sub ($comp, $node, :$want) {
Expand All @@ -1421,10 +1421,10 @@ class QAST::OperationsJS {
}

my $category := $comp.as_js(:want($T_INT), $node[0]);
Chunk.void($category, $payload, "$*CTX.throwpayloadlex({$category.expr}, {$payload.expr});\n");
Chunk.void($category, $payload, "{$comp.await}$*CTX.throwpayloadlex({$category.expr}, {$payload.expr});\n");
});

add_simple_op('throwextype', $T_VOID, [$T_INT], :side_effects, :ctx);
add_simple_op('throwextype', $T_VOID, [$T_INT], :side_effects, :ctx, :await);

add_simple_op('lastexpayload', $T_OBJ, [], :!inlinable);

Expand Down Expand Up @@ -1664,7 +1664,7 @@ class QAST::OperationsJS {
add_simple_op('execname', $T_STR, []);

add_simple_op('decoderconfigure', $T_OBJ, [$T_OBJ, $T_STR, $T_OBJ], :ctx, :side_effects, :method_call);
add_simple_op('decodersetlineseps', $T_OBJ, [$T_OBJ, $T_OBJ], :ctx, :side_effects, :method_call);
add_simple_op('decodersetlineseps', $T_OBJ, [$T_OBJ, $T_OBJ], :ctx, :side_effects, :method_call, :await);
add_simple_op('decoderaddbytes', $T_OBJ, [$T_OBJ, $T_OBJ], :side_effects, :method_call);
add_simple_op('decodertakechars', $T_STR, [$T_OBJ, $T_INT], :side_effects, :method_call);
add_simple_op('decodertakeallchars', $T_STR, [$T_OBJ], :side_effects, :method_call);
Expand All @@ -1688,7 +1688,7 @@ class QAST::OperationsJS {
add_simple_op('asynclisten', $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR, $T_INT, $T_INT, $T_OBJ], :side_effects);
add_simple_op('asyncwritebytes', $T_OBJ, [$T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ], :side_effects);
add_simple_op('asyncreadbyte', $T_OBJ, [$T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ], :side_effects);
add_simple_op('spawnprocasync', $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR, $T_OBJ, $T_OBJ], :ctx, :side_effects);
add_simple_op('spawnprocasync', $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR, $T_OBJ, $T_OBJ], :ctx, :side_effects, :await);
add_simple_op('killprocasync', $T_OBJ, [$T_OBJ, $T_INT], :side_effects);

add_simple_op('permit', $T_OBJ, [$T_OBJ, $T_INT, $T_INT], :side_effects);
Expand Down
18 changes: 9 additions & 9 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -441,10 +441,10 @@ class WrappedFunction extends NQPObject {
this.func = func;
}

$$apply(args) {
async $$apply(args) {
var converted = [];
for (var i = 2; i < args.length; i++) {
converted.push(toJS(args[i]));
converted.push(await toJS(args[i]));
}
return fromJS(this.func.apply(null, converted));
}
Expand Down Expand Up @@ -645,10 +645,10 @@ op.setparameterizer = function(ctx, type, parameterizer) {
var st = type._STable;
/* Ensure that the type is not already parametric or parameterized. */
if (st.parameterizer) {
ctx.die('This type is already parametric');
throw new NQPException('This type is already parametric');
return Null;
} else if (st.parametricType) {
ctx.die('Cannot make a parameterized type also be parametric');
throw new NQPException('Cannot make a parameterized type also be parametric');
return Null;
}

Expand All @@ -661,11 +661,11 @@ op.setparameterizer = function(ctx, type, parameterizer) {
return type;
};

op.parameterizetype = function(ctx, type, params) {
op.parameterizetype = async function(ctx, type, params) {
/* Ensure we have a parametric type. */
var st = type._STable;
if (!st.parameterizer) {
ctx.die('This type is not parametric');
throw new NQPException('This type is not parametric');
}

var unpackedParams = params.array;
Expand All @@ -688,7 +688,7 @@ op.parameterizetype = function(ctx, type, params) {
}
}

var result = st.parameterizer.$$call(ctx, {}, st.WHAT, params);
var result = await st.parameterizer.$$call(ctx, {}, st.WHAT, params);

var newSTable = result._STable;
newSTable.parametricType = type;
Expand Down Expand Up @@ -739,7 +739,7 @@ op.isnanorinf = function(n) {
function typeparameters(ctx, type) {
var st = type._STable;
if (!st.parametricType) {
ctx.die('This type is not parameterized');
throw new NQPException('This type is not parameterized');
}

return st.parameters;
Expand Down Expand Up @@ -886,7 +886,7 @@ op.throwextype = function(ctx, category) {
var exType = BOOT.Exception;
let ex = exType._STable.REPR.allocate(exType._STable);
ex.$$category = category;
ctx.throw(ex);
return ctx.throw(ex);
};

function EvalResult(mainline, codeRefs) {
Expand Down
34 changes: 17 additions & 17 deletions src/vm/js/nqp-runtime/ctx.js
Expand Up @@ -57,45 +57,45 @@ class Ctx extends NQPObject {
}

last() {
this.controlException(LAST);
return this.controlException(LAST);
}

lastLabeled(label) {
this.controlExceptionLabeled(label, LAST);
return this.controlExceptionLabeled(label, LAST);
}

next() {
this.controlException(NEXT);
return this.controlException(NEXT);
}

nextLabeled(label) {
this.controlExceptionLabeled(label, NEXT);
return this.controlExceptionLabeled(label, NEXT);
}

redo() {
this.controlException(REDO);
return this.controlException(REDO);
}

redoLabeled(label) {
this.controlExceptionLabeled(label, REDO);
return this.controlExceptionLabeled(label, REDO);
}

controlException(category) {
let exType = BOOT.Exception;
let exception = exType._STable.REPR.allocate(exType._STable);
exception.$$category = category;
this.propagateControlException(exception);
return this.propagateControlException(exception);
}

controlExceptionLabeled(label, category) {
let exType = BOOT.Exception;
let exception = exType._STable.REPR.allocate(exType._STable);
exception.$$category = category | LABELED;
exception.$$payload = label;
this.propagateControlException(exception);
return this.propagateControlException(exception);
}

propagateControlException(exception) {
async propagateControlException(exception) {
let handler = '$$' + categoryToName[exception.$$category & ~LABELED];
let labeled = exception.$$category & LABELED;

Expand All @@ -111,7 +111,7 @@ class Ctx extends NQPObject {
if (ctx[handler]) {
ctx.unwind.ret = ctx[handler]();
} else {
ctx.unwind.ret = ctx.$$CONTROL();
ctx.unwind.ret = await ctx.$$CONTROL();
}
} catch (e) {
if (e instanceof ResumeException && e.exception === exception) {
Expand All @@ -133,7 +133,7 @@ class Ctx extends NQPObject {

async propagateException(exception) {
if (exception.$$category) {
this.propagateControlException(exception);
return this.propagateControlException(exception);
return;
}

Expand Down Expand Up @@ -164,11 +164,11 @@ class Ctx extends NQPObject {
throw exception;
}

catchException(exception) {
async catchException(exception) {
this.exception = exception;
exceptionsStack.push(exception);
try {
return this.$$CATCH();
return await this.$$CATCH();
} finally {
exceptionsStack.pop();
}
Expand Down Expand Up @@ -197,14 +197,14 @@ class Ctx extends NQPObject {
ctx = ctx.$$caller;
}

this.$$throwLexicalException(ctx, category, payload);
return this.$$throwLexicalException(ctx, category, payload);
}

throwpayloadlex(category, payload) {
this.$$throwLexicalException(this, category, payload);
return this.$$throwLexicalException(this, category, payload);
}

$$throwLexicalException(lookFrom, category, payload) {
async $$throwLexicalException(lookFrom, category, payload) {
let exType = BOOT.Exception;
let exception = exType._STable.REPR.allocate(exType._STable);
exception.$$category = category;
Expand All @@ -223,7 +223,7 @@ class Ctx extends NQPObject {
if (ctx[handler]) {
ctx.unwind.ret = ctx[handler]();
} else {
ctx.unwind.ret = ctx.$$CONTROL();
ctx.unwind.ret = await ctx.$$CONTROL();
}
} catch (e) {
if (e instanceof ResumeException && e.exception === exception) {
Expand Down
34 changes: 17 additions & 17 deletions src/vm/js/nqp-runtime/io.js
Expand Up @@ -286,10 +286,10 @@ op.open = function(name, mode) {

op.seekfh = function(ctx, fh, offset, whence) {
if (whence == 0 && offset < 0) {
ctx.die(`Can't seek to position: ${offset}`);
throw new NQPException(`Can't seek to position: ${offset}`);
}
if (!(whence == 0 || whence == 1 || whence == 2)) {
ctx.die('Invalid whence passed to seekfh: ' + whence);
throw new NQPException('Invalid whence passed to seekfh: ' + whence);
}
return fs.seekSync(fh.fd, offset, whence);
};
Expand Down Expand Up @@ -471,38 +471,38 @@ function wrapBuffer(buffer, type) {
return wrapped;
}

function stringifyEnv(ctx, hash) {
async function stringifyEnv(ctx, hash) {
let stringifed = {};

hash.content.forEach(function(value, key, map) {
stringifed[key] = nqp.toStr(value, ctx);
});
for (let key of hash.content.keys()) {
stringifed[key] = await nqp.toStr(hash.content.get(key), ctx);
}

return stringifed;
}

function stringifyArray(ctx, array) {
async function stringifyArray(ctx, array) {
let stringified = [];
for (let element of array.array) {
stringified.push(nqp.toStr(element, ctx));
stringified.push(await nqp.toStr(element, ctx));
}
return stringified;
}

op.spawnprocasync = function(ctx, queue, args, cwd, env, config) {
op.spawnprocasync = async function(ctx, queue, args, cwd, env, config) {
const options = {
shell: false,
cwd: cwd,
env: stringifyEnv(ctx, env),
env: await stringifyEnv(ctx, env),
stdio: [process.stdin, 'pipe', 'pipe'],
};

const stringified = stringifyArray(ctx, args);
const stringified = await stringifyArray(ctx, args);

const result = child_process.spawnSync(stringified.shift(), stringified, options);

if (config.content.get('ready')) {
config.content.get('ready').$$call(ctx, null);
await config.content.get('ready').$$call(ctx, null);
}


Expand All @@ -511,17 +511,17 @@ op.spawnprocasync = function(ctx, queue, args, cwd, env, config) {
if (str_box === undefined) str_box = Null;

if (config.content.get('stdout_bytes')) {
config.content.get('stdout_bytes').$$call(ctx, null, 0, wrapBuffer(result.output[1], config.content.get('buf_type')), str_box);
config.content.get('stdout_bytes').$$call(ctx, null, 1, str_box, str_box);
await config.content.get('stdout_bytes').$$call(ctx, null, 0, wrapBuffer(result.output[1], config.content.get('buf_type')), str_box);
await config.content.get('stdout_bytes').$$call(ctx, null, 1, str_box, str_box);
}

if (config.content.get('stderr_bytes')) {
config.content.get('stderr_bytes').$$call(ctx, null, 0, wrapBuffer(result.output[2], config.content.get('buf_type')), str_box);
config.content.get('stderr_bytes').$$call(ctx, null, 1, str_box, str_box);
await config.content.get('stderr_bytes').$$call(ctx, null, 0, wrapBuffer(result.output[2], config.content.get('buf_type')), str_box);
await config.content.get('stderr_bytes').$$call(ctx, null, 1, str_box, str_box);
}

if (config.content.get('done')) {
config.content.get('done').$$call(ctx, null, result.status << 8);
await config.content.get('done').$$call(ctx, null, result.status << 8);
}
};

Expand Down
7 changes: 5 additions & 2 deletions src/vm/js/nqp-runtime/reprs.js
Expand Up @@ -1482,9 +1482,12 @@ class Decoder extends REPR {
return this;
}

$$decodersetlineseps(ctx, seps) {
async $$decodersetlineseps(ctx, seps) {
this.$$check();
this.$$seps = seps.array.map(sep => nqp.toStr(sep, ctx));
this.$$seps = [];
for (let sep of seps.array) {
this.$$seps.push(await nqp.toStr(sep, ctx));
}
}

$$decoderaddbytes(bytes) {
Expand Down

0 comments on commit a22631d

Please sign in to comment.