Sfoglia il codice sorgente

properly handle identifiers with leading underscores like '__b', but don't apply transformation for leading and trailing underscores like '__iter__', remove dirty hack for '__b'

frabbit 11 anni fa
parent
commit
332bddd1e9
3 ha cambiato i file con 26 aggiunte e 11 eliminazioni
  1. 10 8
      genpy.ml
  2. 5 3
      std/python/Boot.hx
  3. 11 0
      tests/unit/TestPython.hx

+ 10 - 8
genpy.ml

@@ -50,18 +50,20 @@ module KeywordHandler = struct
 			"and"; "as"; "assert"; "break"; "class"; "continue"; "def"; "del"; "elif"; "else"; "except"; "exec"; "finally"; "for";
 			"from"; "global"; "if"; "import"; "in"; "is"; "lambda"; "not"; "or"; "pass"; "print";" raise"; "return"; "try"; "while";
 			"with"; "yield"; "float"; "None"; "list"; "True"; "False"
-			;"__b" (* TODO: hack to deal with haxe.Utf8 error *)
 		];
 		h
 
 	let handle_keywords s =
-		if Hashtbl.mem kwds s then "_hx_" ^ s else s
-
-	let unhandle_keywords s =
-		if String.length s > 4 && String.sub s 0 4 = "_hx_" then
-			String.sub s 4 (String.length s - 4)
-		else
-			s
+		let l = String.length s in
+		if Hashtbl.mem kwds s then
+			"_hx_" ^ s
+		(*
+			handle special __ underscore behaviour (creates private fields for objects) for fields but only if the field doesn't
+			end with at least one underscores like __iter__ because these are special fields
+		*)
+		else if l > 2 && String.sub s 0 2 = "__" && String.sub s (l - 1) 1 <> "_" then
+			"_hx_" ^ s
+		else s
 end
 
 module Transformer = struct

+ 5 - 3
std/python/Boot.hx

@@ -426,10 +426,12 @@ _hx_c._hx_AnonObject = _hx_AnonObject
 
 
 	static inline function handleKeywords(name:String):String {
-		if (keywords.has(name)) {
-			return Internal.getPrefixed(name);
+		return if (keywords.has(name)) {
+			Internal.getPrefixed(name);
+		} else if (name.length > 2 && name.substr(0,2) == "__" && name.charAt(name.length-1) != "_") {
+			Internal.getPrefixed(name);
 		}
-		return name;
+		else name;
 	}
 
 	static var prefixLength = Internal.prefix().length;

+ 11 - 0
tests/unit/TestPython.hx

@@ -133,6 +133,17 @@ class TestPython extends Test {
 	}
 	*/
 
+	function testUnderscoreAndReflection () {
+		var x = { __v : 5 };
+		eq(5, Reflect.field(x, "__v"));
+
+		var x = { ___b : 5 };
+		eq(5, Reflect.field(x, "___b"));
+
+		var x = { __iter__ : 5 };
+		eq(5, Reflect.field(x, "__iter__"));
+	}
+
 
 	function testMakeVarArgs () {
 		var f = function (a:Array<Dynamic>) {