Skip to content

Commit 3bfea70

Browse files
committed
[js] Fix coercions with int8
1 parent d1b7d23 commit 3bfea70

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/vm/js/Compiler.nqp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
597597
return Chunk.new($desired, "{$chunk.expr}.\$\$decont($*CTX).\$\$toBool($*CTX)", $chunk);
598598
}
599599

600-
return QAST::OperationsJS.unbox($*HLL, $desired, $chunk);
600+
return QAST::OperationsJS.unbox(self, $*HLL, $desired, $chunk);
601601
}
602602

603603
if $desired == $T_STR {
@@ -637,6 +637,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
637637
%convert{$T_INT16} := 'intToObj';
638638
%convert{$T_NUM} := 'numToObj';
639639
%convert{$T_STR} := 'strToObj';
640+
nqp::die("Can't coerce $got to OBJ") unless nqp::existskey(%convert, $got);
640641
return Chunk.new($T_OBJ, "nqp.{%convert{$got}}(HLL, {$chunk.expr})", $chunk);
641642
}
642643
}
@@ -653,7 +654,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
653654

654655
if $desired == $T_INT8 || $desired == $T_INT16 {
655656
my $int_chunk := $got == $T_INT ?? $chunk !! self.coerce($chunk, $T_INT);
656-
return Chunk.new($T_INT16, self.int_to_fancy_int($desired, $int_chunk.expr) , $int_chunk);
657+
return Chunk.new($desired, self.int_to_fancy_int($desired, $int_chunk.expr) , $int_chunk);
657658
}
658659

659660
return Chunk.new($desired, "nqp.coercion($got, $desired, {$chunk.expr})") #TODO

src/vm/js/Operations.nqp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,15 +1799,24 @@ class QAST::OperationsJS {
17991799
%hll_unbox{$hll}{$type} := $method_name;
18001800
}
18011801

1802-
method unbox($hll, $desired, $chunk) {
1802+
method unbox($comp, $hll, $desired, $chunk) {
18031803
if nqp::existskey(%hll_unbox, $hll) && nqp::existskey(%hll_unbox{$hll}, $desired) {
18041804
return Chunk.new($desired, '(' ~ $chunk.expr ~ ").\$\${%hll_unbox{$hll}{$desired}}()", $chunk);
18051805
}
18061806
my %convert;
18071807
%convert{$T_STR} := 'toStr';
18081808
%convert{$T_NUM} := 'toNum';
18091809
%convert{$T_INT} := 'toInt';
1810-
return Chunk.new($desired, 'nqp.' ~ %convert{$desired} ~ '(' ~ $chunk.expr ~ ", {$*CTX})", $chunk);
1810+
my int $is_fancy_int := $desired == $T_INT8 || $desired == $T_INT16;
1811+
1812+
nqp::die("Can't coerce OBJ to $desired") unless nqp::existskey(%convert, $desired) || $is_fancy_int;
1813+
1814+
my str $rough_convert := 'nqp.' ~ %convert{$is_fancy_int ?? $T_INT !! $desired} ~ '(' ~ $chunk.expr ~ ", {$*CTX})";
1815+
my str $convert := $is_fancy_int
1816+
?? $comp.int_to_fancy_int($desired, $rough_convert)
1817+
!! $rough_convert;
1818+
1819+
return Chunk.new($desired, $convert, $chunk);
18111820
}
18121821
}
18131822

0 commit comments

Comments
 (0)