Skip to content

Commit

Permalink
add TUninitialized to deal with uninitialized state
Browse files Browse the repository at this point in the history
closes #17
  • Loading branch information
Simn committed Mar 25, 2019
1 parent e7f6cd1 commit 736d38a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/generators/genjvm.ml
Expand Up @@ -29,7 +29,6 @@ type method_type =
| MInstance
| MConstructor
| MConstructorTop
| MMain

type access_kind =
| AKPost
Expand Down Expand Up @@ -79,7 +78,7 @@ module NativeArray = struct
| TMethod _ -> reference NativeSignatures.method_handle_path
| TTypeParameter _ -> reference NativeSignatures.object_path
| TArray _ -> assert false (* TODO: hmm... *)
| TObjectInner _ -> assert false
| TObjectInner _ | TUninitialized _ -> assert false
end;
ja
end
Expand Down Expand Up @@ -347,7 +346,7 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
| FEnum(en,ef) ->
let path = en.e_path in
let offset = pool#add_path path in
code#new_ (TObject(path,[])) offset;
code#new_ offset;
code#dup;
code#iconst (Int32.of_int ef.ef_index);
code#iconst Int32.zero;
Expand Down Expand Up @@ -905,7 +904,7 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
| TField(_,FEnum(en,ef)) ->
let path = en.e_path in
let offset = pool#add_path path in
code#new_ (TObject(path,[])) offset;
code#new_ offset;
code#dup;
code#iconst (Int32.of_int ef.ef_index);
code#iconst (Int32.of_int (List.length el));
Expand All @@ -914,7 +913,7 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
code#invokespecial offset_field (TObject(path,[])) [TInt;jasig] [];
Some (TObject(path,[]))
| TConst TSuper ->
code#aload jc#get_jsig 0;
code#aload (TUninitialized None) 0;
begin match follow e1.etype with
| TInst(c,_) ->
begin match c.cl_constructor with
Expand Down Expand Up @@ -1075,7 +1074,7 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return

method construct ret path t f =
let offset_class = pool#add_path (path_map path) in
code#new_ t offset_class;
code#new_ offset_class;
if ret <> RVoid then code#dup;
let tl,offset = f() in
code#invokespecial offset t tl []
Expand Down Expand Up @@ -1351,7 +1350,14 @@ let generate_expr gctx jc jm e is_main is_method mtype =
e,[],t_dynamic
in
let handler = new texpr_to_jvm gctx jc jm tr in
if (mtype <> MStatic) then ignore(jm#add_local "this" jc#get_jsig VarArgument);
begin match mtype with
| MStatic ->
()
| MInstance ->
ignore(jm#add_local "this" jc#get_jsig VarArgument)
| MConstructor | MConstructorTop ->
ignore(jm#add_local "this" (TUninitialized None) VarArgument)
end;
if is_main then ignore(jm#add_local "args" (TArray(string_sig,None)) VarArgument);
List.iter (fun (v,_) ->
ignore(handler#add_local v VarArgument);
Expand Down
5 changes: 3 additions & 2 deletions src/generators/jvm/jvmCode.ml
Expand Up @@ -121,6 +121,7 @@ class builder pool = object(self)
| TInt,TBool -> ()
| TDouble,TInt -> ()
| TInt,(TChar | TShort | TByte) -> ()
| (TObject _ | TTypeParameter _),TUninitialized _ -> ()
| _ ->
if js <> js' then self#stack_error opcode expect cur
) expect;
Expand Down Expand Up @@ -283,8 +284,8 @@ class builder pool = object(self)
method invokespecial offset t1 tl tr =
self#op (OpInvokespecial offset) 3 (List.rev (t1 :: tl)) tr

method new_ t offset =
self#op (OpNew offset) 3 [] [t]
method new_ offset =
self#op (OpNew offset) 3 [] [TUninitialized (Some fp)]

(* return *)

Expand Down
3 changes: 3 additions & 0 deletions src/generators/jvm/jvmSignature.ml
Expand Up @@ -25,6 +25,7 @@ and jsignature =
| TArray of jsignature * int option (* [ *)
| TMethod of jmethod_signature (* ( *)
| TTypeParameter of string (* T *)
| TUninitialized of int option

(* ( jsignature list ) ReturnDescriptor (| V | jsignature) *)
and jmethod_signature = jsignature list * jsignature option
Expand Down Expand Up @@ -94,6 +95,8 @@ and write_signature full ch jsig = match jsig with
write_byte ch (Char.code ';')
end else
write_string ch "Ljava/lang/Object;"
| TUninitialized _ ->
()

let generate_signature full jsig =
let ch = IO.output_bytes () in
Expand Down
2 changes: 2 additions & 0 deletions src/generators/jvm/jvmVerificationTypeInfo.ml
Expand Up @@ -21,6 +21,8 @@ let of_signature pool jsig = match jsig with
| TMethod _ -> VObject (pool#add_path (["java";"lang";"invoke"],"MethodHandle"))
| TArray _ -> VObject (pool#add_path ([],generate_signature false jsig))
| TTypeParameter _ -> VObject (pool#add_path (["java";"lang"],"Object"))
| TUninitialized (Some i) -> VUninitialized i
| TUninitialized None -> VUninitializedThis
| _ -> assert false

let to_string vtt = match vtt with
Expand Down
25 changes: 24 additions & 1 deletion tests/genjvm/src/test/TestChaos.hx
Expand Up @@ -2,9 +2,24 @@ package test;

import haxe.ds.StringMap;

private class ChaosConstructor {
public var value:Int;

public function new(value:Int) {
this.value = value;
}
}

private class ChaosConstructorChild extends ChaosConstructor {
public function new(value:Int) {
super(value > 0 ? 1 : 0);
}
}

@:analyzer(ignore)
class TestChaos extends BaseTest {
var intField:Int;

static var staticIntField:Int;
static var trueValue = true;
static var falseValue = false;
Expand All @@ -17,6 +32,7 @@ class TestChaos extends BaseTest {
testStringMap();
testObjectDecl();
testStringConcat();
testBranchingCtorArgs();
}

function testAssignment() {
Expand Down Expand Up @@ -259,4 +275,11 @@ class TestChaos extends BaseTest {
eq("a1.0", "a" + 1.0);
eq("1.0a", 1.0 + "a");
}
}

function testBranchingCtorArgs() {
eq(1, new ChaosConstructor(trueValue ? 1 : 0).value);

eq(1, new ChaosConstructorChild(1).value);
eq(0, new ChaosConstructorChild(-1).value);
}
}

0 comments on commit 736d38a

Please sign in to comment.