Skip to content

Commit

Permalink
fix some TUnop vs. Null<T> situations
Browse files Browse the repository at this point in the history
see #29
  • Loading branch information
Simn committed Apr 1, 2019
1 parent 7813a19 commit 582ad46
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/generators/genjvm.ml
Expand Up @@ -1134,29 +1134,34 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
| TByte | TShort | TInt ->
jm#cast TInt;
code#iconst Int32.one;
if op = Increment then code#iadd else code#isub
if op = Increment then code#iadd else code#isub;
if is_null then self#expect_reference_type;
| _ ->
Error.error (Printf.sprintf "Unuspported unop on %s" (generate_signature false t)) e.epos;
end
in
self#read_write ret (if flag = Prefix then AKPre else AKPost) e f e.etype;
if is_null then self#expect_reference_type;
| Neg,_ ->
self#texpr rvalue_any e;
begin match jsignature_of_type (follow e.etype) with
let jsig = jsignature_of_type (follow e.etype) in
jm#cast jsig;
begin match jsig with
| TLong -> code#lneg;
| TDouble -> code#dneg;
| TByte | TShort | TInt -> code#ineg;
| _ -> jm#invokestatic haxe_jvm_path "neg" (method_sig [object_sig] (Some object_sig))
end;
self#cast e.etype;
| Not,_ ->
jm#if_then_else
(self#condition e)
(fun () -> code#bconst false)
(fun () -> code#bconst true)
| NegBits,_ ->
let jsig = jsignature_of_type (follow e.etype) in
self#texpr rvalue_any e;
begin match jsignature_of_type (follow e.etype) with
jm#cast jsig;
begin match jsig with
| TByte | TShort | TInt ->
code#iconst Int32.minus_one;
code#ixor;
Expand All @@ -1165,7 +1170,8 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
code#lxor_;
| _ ->
jm#invokestatic haxe_jvm_path "~" (method_sig [object_sig] (Some object_sig))
end
end;
self#cast e.etype;

(* calls *)

Expand Down
60 changes: 60 additions & 0 deletions tests/genjvm/src/test/TestChaos.hx
Expand Up @@ -29,6 +29,7 @@ class TestChaos extends BaseTest {
super();
testAssignment();
testOps();
testNullOps();
testDynamicOps();
testIntArray();
testDynamicArray();
Expand Down Expand Up @@ -132,6 +133,65 @@ class TestChaos extends BaseTest {
eq(false, 0 == minusA);
}

function testNullOps() {
var a:Null<Int> = 10;
// arithmetic
eq(9, a - 1);
eq(20, a * 2);
eq(5., a / 2); // careful with Float comparison...
eq(1, a % 3);

// bit
eq(20, a << 1);
eq(5, a >> 1);
eq(5, a >>> 1);
eq(10, a & 15);
eq(15, a | 15);
eq(2, a ^ 8);

// unary
eq(-10, -a);
eq(-11, ~a);

// boolean
var b:Null<Bool> = true;
eq(false, !b);
eq(false, b && falseValue);
eq(true, b && trueValue);
eq(true, b || falseValue);
eq(true, b || trueValue);

b = false;
eq(true, !b);
eq(false, b && falseValue);
eq(false, b && trueValue);
eq(false, b || falseValue);
eq(true, b || trueValue);

eq(true, a > 5);
eq(true, a >= 5);
eq(false, a < 5);
eq(false, a <= 5);
eq(true, a != 5);
eq(false, a != 10);

eq(false, 0 > a);
eq(false, 0 >= a);
eq(true, 0 < a);
eq(true, 0 <= a);
eq(true, 0 != a);
eq(false, 0 == a);

var minusA:Null<Int> = -10;
eq(true, 0 > minusA);
eq(true, 0 >= minusA);
eq(false, 0 < minusA);
eq(false, 0 <= minusA);
eq(true, 0 != minusA);
eq(false, 0 == minusA);
}


function testDynamicOps() {
var a:Dynamic = 10;
// arithmetic
Expand Down

0 comments on commit 582ad46

Please sign in to comment.