Ver código fonte

[java] added synchronized in java Lib

Caue Waneck 13 anos atrás
pai
commit
f4dc17606e
2 arquivos alterados com 21 adições e 12 exclusões
  1. 7 0
      genjava.ml
  2. 14 12
      std/java/Lib.hx

+ 7 - 0
genjava.ml

@@ -622,6 +622,7 @@ let rec get_fun_modifiers meta access modifiers =
     (*| (":readonly",[],_) :: meta -> get_fun_modifiers meta access ("readonly" :: modifiers)*)
     (*| (":unsafe",[],_) :: meta -> get_fun_modifiers meta access ("unsafe" :: modifiers)*)
     | (":volatile",[],_) :: meta -> get_fun_modifiers meta access ("volatile" :: modifiers)
+    | (":transient",[],_) :: meta -> get_fun_modifiers meta access ("transient" :: modifiers)
     | _ :: meta -> get_fun_modifiers meta access modifiers
     
 (* this was the way I found to pass the generator context to be accessible across all functions here *)
@@ -974,6 +975,11 @@ let configure gen =
           write w " )"
         | TCall ({ eexpr = TLocal( { v_name = "__java__" } ) }, [ { eexpr = TConst(TString(s)) } ] ) ->
           write w s
+        | TCall ({ eexpr = TLocal( { v_name = "__lock__" } ) }, [ eobj; eblock ] ) ->
+          write w "synchronized(";
+          expr_s w eobj;
+          write w ")";
+          expr_s w (mk_block eblock)
         | TCall ({ eexpr = TLocal( { v_name = "__goto__" } ) }, [ { eexpr = TConst(TInt v) } ] ) ->
           print w "break label%ld" v
         | TCall ({ eexpr = TLocal( { v_name = "__label__" } ) }, [ { eexpr = TConst(TInt v) } ] ) ->
@@ -1456,6 +1462,7 @@ let configure gen =
   Hashtbl.add gen.gspecial_vars "__is__" true;
   Hashtbl.add gen.gspecial_vars "__typeof__" true;
   Hashtbl.add gen.gspecial_vars "__java__" true;
+  Hashtbl.add gen.gspecial_vars "__lock__" true;
   
   gen.greal_type <- real_type;
   gen.greal_type_param <- change_param_type;

+ 14 - 12
std/java/Lib.hx

@@ -14,7 +14,7 @@ package java;
 		
 		If equalLengthRequired is true, the result might be a copy of an array with the correct size.
 	**/
-	public static function toNativeReadOnlyArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
+	public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
 	{
 		var native:NativeArray<T> = untyped arr.__a;
 		if (native.length == arr.length)
@@ -25,17 +25,6 @@ package java;
 		}
 	}
 	
-	/**
-		Returns a System.Type equivalent to the Haxe Class<> type.
-		
-		Currently Haxe's Class<> is equivalent to System.Type, but this is an implementation detail.
-		This may change in the future, so use this function whenever you need to perform such conversion.
-	**/
-	public static function toNativeType<T>(cl:Class<T>):java.lang.Class<T>
-	{
-		return untyped cl;
-	}	
-	
 	/**
 		Gets the native System.Type from the supplied object. Will throw an exception in case of null being passed.
 	**/
@@ -64,4 +53,17 @@ package java;
 	{
 		return untyped Array.alloc(size);
 	}
+	
+	/**
+		Ensures that one thread does not enter a critical section of code while another thread
+		is in the critical section. If another thread attempts to enter a locked code, it 
+		will wait, block, until the object is released.
+		This is the equivalent to "synchronized" in java code.
+		
+		This method only exists at compile-time, so it can't be called via reflection.
+	**/
+	@:extern public static inline function lock(obj:Dynamic, block:Dynamic):Void
+	{
+		untyped __lock__(obj, block);
+	}
 }