浏览代码

[lua] keep string fields for reflection, iterate string methods properly. close #6276

Justin Donaldson 8 年之前
父节点
当前提交
80b0d79374
共有 4 个文件被更改,包括 19 次插入2 次删除
  1. 4 0
      src/generators/genlua.ml
  2. 0 1
      src/optimization/dce.ml
  3. 5 1
      std/lua/_std/Reflect.hx
  4. 10 0
      tests/unit/src/unit/issues/Issue6276.hx

+ 4 - 0
src/generators/genlua.ml

@@ -508,6 +508,10 @@ and gen_expr ?(local=true) ctx e = begin
 		print ctx "_iterator(";
 		print ctx "_iterator(";
 		gen_value ctx x;
 		gen_value ctx x;
 		print ctx ")";
 		print ctx ")";
+	| TField (e, f) when is_string_expr e ->
+                spr ctx "(";
+                gen_value ctx e;
+                print ctx ").%s" (field_name f);
 	| TField (x,FClosure (_,f)) ->
 	| TField (x,FClosure (_,f)) ->
 		add_feature ctx "use._hx_bind";
 		add_feature ctx "use._hx_bind";
 		(match x.eexpr with
 		(match x.eexpr with

+ 0 - 1
src/optimization/dce.ml

@@ -56,7 +56,6 @@ let keep_whole_class dce c =
 	|| super_forces_keep c
 	|| super_forces_keep c
 	|| (match c with
 	|| (match c with
 		| { cl_path = ([],("Math"|"Array"))} when dce.com.platform = Js -> false
 		| { cl_path = ([],("Math"|"Array"))} when dce.com.platform = Js -> false
-		| { cl_path = ([],("Math"|"Array"|"String"))} when dce.com.platform = Lua -> false
 		| { cl_extern = true }
 		| { cl_extern = true }
 		| { cl_path = ["flash";"_Boot"],"RealBoot" } -> true
 		| { cl_path = ["flash";"_Boot"],"RealBoot" } -> true
 		| { cl_path = [],"String" }
 		| { cl_path = [],"String" }

+ 5 - 1
std/lua/_std/Reflect.hx

@@ -90,7 +90,11 @@ import lua.Boot;
 	}
 	}
 
 
 	public static function fields( o : Dynamic ) : Array<String> {
 	public static function fields( o : Dynamic ) : Array<String> {
-		return [for (f in lua.Boot.fieldIterator(o)) f];
+		if (lua.Lua.type(o) == "string"){
+			return Reflect.fields(untyped String.prototype);
+		} else {
+			return [for (f in lua.Boot.fieldIterator(o)) f];
+		}
 	}
 	}
 
 
 	public static function isFunction( f : Dynamic ) : Bool {
 	public static function isFunction( f : Dynamic ) : Bool {

+ 10 - 0
tests/unit/src/unit/issues/Issue6276.hx

@@ -0,0 +1,10 @@
+package unit.issues;
+
+class Issue6276 extends unit.Test {
+	function test(){
+		var s = "foo";
+		var indexOf = Reflect.field(s, "indexOf");
+		var pos = Reflect.callMethod(s, indexOf, ["o"]);
+		eq(pos, 1);
+	}
+}