Skip to content

Commit 682c859

Browse files
committed
Merge branch 'master' into async-await-continuations
2 parents 9a378ee + 43902be commit 682c859

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

src/vm/js/HLL/Backend.nqp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ my sub run_command($command, :$stdout) {
3737
$read_all := 1;
3838
}
3939

40-
nqp::spawnprocasync($queue, $command, nqp::cwd(), nqp::getenvhash(), $config);
40+
my $task := nqp::spawnprocasync($queue, $command, nqp::cwd(), nqp::getenvhash(), $config);
41+
42+
if $stdout {
43+
nqp::permit($task, 1, -1);
44+
}
4145

4246
while !$done || !$read_all {
4347
if nqp::shift($queue) -> $task {

src/vm/js/Operations.nqp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,8 @@ class QAST::OperationsJS {
16901690
add_simple_op('spawnprocasync', $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR, $T_OBJ, $T_OBJ], :ctx, :side_effects);
16911691
add_simple_op('killprocasync', $T_OBJ, [$T_OBJ, $T_INT], :side_effects);
16921692

1693+
add_simple_op('permit', $T_OBJ, [$T_OBJ, $T_INT, $T_INT], :side_effects);
1694+
16931695
add_simple_op('semacquire', $T_OBJ, [$T_OBJ], :side_effects);
16941696
add_simple_op('semtryacquire', $T_INT, [$T_OBJ], :side_effects);
16951697
add_simple_op('semrelease', $T_OBJ, [$T_OBJ], :side_effects);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,8 @@ op.spawnprocasync = function(ctx, queue, args, cwd, env, config) {
524524
config.content.get('done').$$call(ctx, null, result.status << 8);
525525
}
526526
};
527+
528+
op.permit = function(handle, channel, permits) {
529+
// TODO Implement permit handling properly
530+
return handle;
531+
};

t/nqp/111-spawnprocasync.t

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
plan(2);
2+
3+
my $tmp-file := "tmp";
4+
spew($tmp-file, "\n");
5+
my $is-windows := nqp::stat($tmp-file, nqp::const::STAT_FILESIZE) == 2;
6+
nqp::unlink($tmp-file);
7+
8+
my $args := $is-windows ?? nqp::list(nqp::getenvhash()<CompSpec>, '/c', 'echo aardvarks') !! nqp::list('/bin/sh', '-c', 'echo aardvarks');
9+
10+
my sub create_buf($type) {
11+
my $buf := nqp::newtype(nqp::null(), 'VMArray');
12+
nqp::composetype($buf, nqp::hash('array', nqp::hash('type', $type)));
13+
nqp::setmethcache($buf, nqp::hash('new', method () {nqp::create($buf)}));
14+
$buf;
15+
}
16+
17+
my $done := 0;
18+
19+
my class Queue is repr('ConcBlockingQueue') { }
20+
my $queue := nqp::create(Queue);
21+
22+
my @stdout_bytes;
23+
my $read_all := 0;
24+
25+
my $called_ready := 0;
26+
27+
28+
my $config := nqp::hash(
29+
'done', -> $status {
30+
$done := $done + 1;
31+
},
32+
'ready', -> $stdin?, $stdout? {
33+
$called_ready := $called_ready + 1;
34+
},
35+
'stdout_bytes', -> $seq, $data, $err {
36+
if nqp::isconcrete($data) {
37+
@stdout_bytes[$seq] := $data;
38+
}
39+
else {
40+
$read_all := 1;
41+
}
42+
},
43+
'buf_type', create_buf(uint8)
44+
);
45+
46+
47+
my $task := nqp::spawnprocasync($queue, $args, nqp::cwd(), nqp::getenvhash(), $config);
48+
49+
nqp::permit($task, 1, -1);
50+
51+
while !$done || !$read_all {
52+
if nqp::shift($queue) -> $task {
53+
if nqp::list($task) {
54+
my $code := nqp::shift($task);
55+
$code(|$task);
56+
} else {
57+
$task();
58+
}
59+
}
60+
}
61+
62+
my class VMDecoder is repr('Decoder') {}
63+
my $dec := nqp::create(VMDecoder);
64+
nqp::decoderconfigure($dec, 'utf8', nqp::hash());
65+
66+
is($called_ready, 1, 'called the ready callback once');
67+
68+
for @stdout_bytes -> $bytes {
69+
nqp::decoderaddbytes($dec, $bytes);
70+
}
71+
ok(nqp::decodertakeallchars($dec) ~~ /^aardvarks\s*$/, 'got the correct output');

0 commit comments

Comments
 (0)