Skip to content

Commit a22631d

Browse files
committed
[js] Async/await stuff
ctx.die(...) calls are replace by new NQPException(...) for places where we from a unresumable exception for low level errors
1 parent 73f5eb8 commit a22631d

File tree

6 files changed

+60
-57
lines changed

6 files changed

+60
-57
lines changed

src/vm/js/Compiler.nqp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
13811381
}
13821382
else {
13831383
$loop.handle($type);
1384-
return Chunk.void("$*CTX.$type();\n");
1384+
return Chunk.void("{self.await}$*CTX.$type();\n");
13851385
}
13861386
}
13871387
}
@@ -1393,7 +1393,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
13931393

13941394
if $label {
13951395
my $compiled_label := self.as_js($label, :want($T_OBJ));
1396-
Chunk.void($compiled_label, "$*CTX.{$type}Labeled({$compiled_label.expr});\n");
1396+
Chunk.void($compiled_label, "{self.await}$*CTX.{$type}Labeled({$compiled_label.expr});\n");
13971397
}
13981398
else {
13991399
self.NYI("can't find surrounding loop for $type");

src/vm/js/Operations.nqp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,8 @@ class QAST::OperationsJS {
766766
my $*CTX := $handler_ctx;
767767
my $body := $comp.as_js($protected, :$want);
768768

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

772772
if $want != $T_VOID {
773773
$try_ret := $*BLOCK.add_tmp;
@@ -779,8 +779,8 @@ class QAST::OperationsJS {
779779
%convert{$T_NUM} := 'toNum';
780780
%convert{$T_INT} := 'toInt';
781781
if nqp::existskey(%convert, $want) {
782-
$catch_exception := 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_exception ~ ')';
783-
$catch_wrapped_exception := 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_wrapped_exception ~ ')';
782+
$catch_exception := $comp.await ~ 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_exception ~ ')';
783+
$catch_wrapped_exception := $comp.await ~ 'nqp.' ~ %convert{$want} ~ '(' ~ $catch_wrapped_exception ~ ')';
784784
}
785785

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

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

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

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

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

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

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

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

16661666
add_simple_op('decoderconfigure', $T_OBJ, [$T_OBJ, $T_STR, $T_OBJ], :ctx, :side_effects, :method_call);
1667-
add_simple_op('decodersetlineseps', $T_OBJ, [$T_OBJ, $T_OBJ], :ctx, :side_effects, :method_call);
1667+
add_simple_op('decodersetlineseps', $T_OBJ, [$T_OBJ, $T_OBJ], :ctx, :side_effects, :method_call, :await);
16681668
add_simple_op('decoderaddbytes', $T_OBJ, [$T_OBJ, $T_OBJ], :side_effects, :method_call);
16691669
add_simple_op('decodertakechars', $T_STR, [$T_OBJ, $T_INT], :side_effects, :method_call);
16701670
add_simple_op('decodertakeallchars', $T_STR, [$T_OBJ], :side_effects, :method_call);
@@ -1688,7 +1688,7 @@ class QAST::OperationsJS {
16881688
add_simple_op('asynclisten', $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR, $T_INT, $T_INT, $T_OBJ], :side_effects);
16891689
add_simple_op('asyncwritebytes', $T_OBJ, [$T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ], :side_effects);
16901690
add_simple_op('asyncreadbyte', $T_OBJ, [$T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ], :side_effects);
1691-
add_simple_op('spawnprocasync', $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR, $T_OBJ, $T_OBJ], :ctx, :side_effects);
1691+
add_simple_op('spawnprocasync', $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR, $T_OBJ, $T_OBJ], :ctx, :side_effects, :await);
16921692
add_simple_op('killprocasync', $T_OBJ, [$T_OBJ, $T_INT], :side_effects);
16931693

16941694
add_simple_op('permit', $T_OBJ, [$T_OBJ, $T_INT, $T_INT], :side_effects);

src/vm/js/nqp-runtime/core.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ class WrappedFunction extends NQPObject {
441441
this.func = func;
442442
}
443443

444-
$$apply(args) {
444+
async $$apply(args) {
445445
var converted = [];
446446
for (var i = 2; i < args.length; i++) {
447-
converted.push(toJS(args[i]));
447+
converted.push(await toJS(args[i]));
448448
}
449449
return fromJS(this.func.apply(null, converted));
450450
}
@@ -645,10 +645,10 @@ op.setparameterizer = function(ctx, type, parameterizer) {
645645
var st = type._STable;
646646
/* Ensure that the type is not already parametric or parameterized. */
647647
if (st.parameterizer) {
648-
ctx.die('This type is already parametric');
648+
throw new NQPException('This type is already parametric');
649649
return Null;
650650
} else if (st.parametricType) {
651-
ctx.die('Cannot make a parameterized type also be parametric');
651+
throw new NQPException('Cannot make a parameterized type also be parametric');
652652
return Null;
653653
}
654654

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

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

671671
var unpackedParams = params.array;
@@ -688,7 +688,7 @@ op.parameterizetype = function(ctx, type, params) {
688688
}
689689
}
690690

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

693693
var newSTable = result._STable;
694694
newSTable.parametricType = type;
@@ -739,7 +739,7 @@ op.isnanorinf = function(n) {
739739
function typeparameters(ctx, type) {
740740
var st = type._STable;
741741
if (!st.parametricType) {
742-
ctx.die('This type is not parameterized');
742+
throw new NQPException('This type is not parameterized');
743743
}
744744

745745
return st.parameters;
@@ -886,7 +886,7 @@ op.throwextype = function(ctx, category) {
886886
var exType = BOOT.Exception;
887887
let ex = exType._STable.REPR.allocate(exType._STable);
888888
ex.$$category = category;
889-
ctx.throw(ex);
889+
return ctx.throw(ex);
890890
};
891891

892892
function EvalResult(mainline, codeRefs) {

src/vm/js/nqp-runtime/ctx.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,45 @@ class Ctx extends NQPObject {
5757
}
5858

5959
last() {
60-
this.controlException(LAST);
60+
return this.controlException(LAST);
6161
}
6262

6363
lastLabeled(label) {
64-
this.controlExceptionLabeled(label, LAST);
64+
return this.controlExceptionLabeled(label, LAST);
6565
}
6666

6767
next() {
68-
this.controlException(NEXT);
68+
return this.controlException(NEXT);
6969
}
7070

7171
nextLabeled(label) {
72-
this.controlExceptionLabeled(label, NEXT);
72+
return this.controlExceptionLabeled(label, NEXT);
7373
}
7474

7575
redo() {
76-
this.controlException(REDO);
76+
return this.controlException(REDO);
7777
}
7878

7979
redoLabeled(label) {
80-
this.controlExceptionLabeled(label, REDO);
80+
return this.controlExceptionLabeled(label, REDO);
8181
}
8282

8383
controlException(category) {
8484
let exType = BOOT.Exception;
8585
let exception = exType._STable.REPR.allocate(exType._STable);
8686
exception.$$category = category;
87-
this.propagateControlException(exception);
87+
return this.propagateControlException(exception);
8888
}
8989

9090
controlExceptionLabeled(label, category) {
9191
let exType = BOOT.Exception;
9292
let exception = exType._STable.REPR.allocate(exType._STable);
9393
exception.$$category = category | LABELED;
9494
exception.$$payload = label;
95-
this.propagateControlException(exception);
95+
return this.propagateControlException(exception);
9696
}
9797

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

@@ -111,7 +111,7 @@ class Ctx extends NQPObject {
111111
if (ctx[handler]) {
112112
ctx.unwind.ret = ctx[handler]();
113113
} else {
114-
ctx.unwind.ret = ctx.$$CONTROL();
114+
ctx.unwind.ret = await ctx.$$CONTROL();
115115
}
116116
} catch (e) {
117117
if (e instanceof ResumeException && e.exception === exception) {
@@ -133,7 +133,7 @@ class Ctx extends NQPObject {
133133

134134
async propagateException(exception) {
135135
if (exception.$$category) {
136-
this.propagateControlException(exception);
136+
return this.propagateControlException(exception);
137137
return;
138138
}
139139

@@ -164,11 +164,11 @@ class Ctx extends NQPObject {
164164
throw exception;
165165
}
166166

167-
catchException(exception) {
167+
async catchException(exception) {
168168
this.exception = exception;
169169
exceptionsStack.push(exception);
170170
try {
171-
return this.$$CATCH();
171+
return await this.$$CATCH();
172172
} finally {
173173
exceptionsStack.pop();
174174
}
@@ -197,14 +197,14 @@ class Ctx extends NQPObject {
197197
ctx = ctx.$$caller;
198198
}
199199

200-
this.$$throwLexicalException(ctx, category, payload);
200+
return this.$$throwLexicalException(ctx, category, payload);
201201
}
202202

203203
throwpayloadlex(category, payload) {
204-
this.$$throwLexicalException(this, category, payload);
204+
return this.$$throwLexicalException(this, category, payload);
205205
}
206206

207-
$$throwLexicalException(lookFrom, category, payload) {
207+
async $$throwLexicalException(lookFrom, category, payload) {
208208
let exType = BOOT.Exception;
209209
let exception = exType._STable.REPR.allocate(exType._STable);
210210
exception.$$category = category;
@@ -223,7 +223,7 @@ class Ctx extends NQPObject {
223223
if (ctx[handler]) {
224224
ctx.unwind.ret = ctx[handler]();
225225
} else {
226-
ctx.unwind.ret = ctx.$$CONTROL();
226+
ctx.unwind.ret = await ctx.$$CONTROL();
227227
}
228228
} catch (e) {
229229
if (e instanceof ResumeException && e.exception === exception) {

src/vm/js/nqp-runtime/io.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ op.open = function(name, mode) {
286286

287287
op.seekfh = function(ctx, fh, offset, whence) {
288288
if (whence == 0 && offset < 0) {
289-
ctx.die(`Can't seek to position: ${offset}`);
289+
throw new NQPException(`Can't seek to position: ${offset}`);
290290
}
291291
if (!(whence == 0 || whence == 1 || whence == 2)) {
292-
ctx.die('Invalid whence passed to seekfh: ' + whence);
292+
throw new NQPException('Invalid whence passed to seekfh: ' + whence);
293293
}
294294
return fs.seekSync(fh.fd, offset, whence);
295295
};
@@ -471,38 +471,38 @@ function wrapBuffer(buffer, type) {
471471
return wrapped;
472472
}
473473

474-
function stringifyEnv(ctx, hash) {
474+
async function stringifyEnv(ctx, hash) {
475475
let stringifed = {};
476476

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

481481
return stringifed;
482482
}
483483

484-
function stringifyArray(ctx, array) {
484+
async function stringifyArray(ctx, array) {
485485
let stringified = [];
486486
for (let element of array.array) {
487-
stringified.push(nqp.toStr(element, ctx));
487+
stringified.push(await nqp.toStr(element, ctx));
488488
}
489489
return stringified;
490490
}
491491

492-
op.spawnprocasync = function(ctx, queue, args, cwd, env, config) {
492+
op.spawnprocasync = async function(ctx, queue, args, cwd, env, config) {
493493
const options = {
494494
shell: false,
495495
cwd: cwd,
496-
env: stringifyEnv(ctx, env),
496+
env: await stringifyEnv(ctx, env),
497497
stdio: [process.stdin, 'pipe', 'pipe'],
498498
};
499499

500-
const stringified = stringifyArray(ctx, args);
500+
const stringified = await stringifyArray(ctx, args);
501501

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

504504
if (config.content.get('ready')) {
505-
config.content.get('ready').$$call(ctx, null);
505+
await config.content.get('ready').$$call(ctx, null);
506506
}
507507

508508

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

513513
if (config.content.get('stdout_bytes')) {
514-
config.content.get('stdout_bytes').$$call(ctx, null, 0, wrapBuffer(result.output[1], config.content.get('buf_type')), str_box);
515-
config.content.get('stdout_bytes').$$call(ctx, null, 1, str_box, str_box);
514+
await config.content.get('stdout_bytes').$$call(ctx, null, 0, wrapBuffer(result.output[1], config.content.get('buf_type')), str_box);
515+
await config.content.get('stdout_bytes').$$call(ctx, null, 1, str_box, str_box);
516516
}
517517

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

523523
if (config.content.get('done')) {
524-
config.content.get('done').$$call(ctx, null, result.status << 8);
524+
await config.content.get('done').$$call(ctx, null, result.status << 8);
525525
}
526526
};
527527

src/vm/js/nqp-runtime/reprs.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,9 +1482,12 @@ class Decoder extends REPR {
14821482
return this;
14831483
}
14841484

1485-
$$decodersetlineseps(ctx, seps) {
1485+
async $$decodersetlineseps(ctx, seps) {
14861486
this.$$check();
1487-
this.$$seps = seps.array.map(sep => nqp.toStr(sep, ctx));
1487+
this.$$seps = [];
1488+
for (let sep of seps.array) {
1489+
this.$$seps.push(await nqp.toStr(sep, ctx));
1490+
}
14881491
}
14891492

14901493
$$decoderaddbytes(bytes) {

0 commit comments

Comments
 (0)