Browse Source

Lua: move the Array hacks to inline lua code emitted statically by the compiler

Justin Donaldson 9 years ago
parent
commit
0b5b8b8b8a
2 changed files with 20 additions and 20 deletions
  1. 18 5
      genlua.ml
  2. 2 15
      std/lua/Boot.hx

+ 18 - 5
genlua.ml

@@ -363,7 +363,7 @@ let rec gen_call ctx e el in_value =
 			| e :: _ -> gen_value ctx e)
 			| e :: _ -> gen_value ctx e)
 	| TLocal { v_name = "__resources__" }, [] ->
 	| TLocal { v_name = "__resources__" }, [] ->
 		(* TODO: Array declaration helper *)
 		(* TODO: Array declaration helper *)
-		spr ctx "lua.Boot.defArray({";
+		spr ctx "__tabArray({";
 		let count = ref 0 in
 		let count = ref 0 in
 		concat ctx "," (fun (name,data) ->
 		concat ctx "," (fun (name,data) ->
 			if (!count == 0) then spr ctx "[0]=";
 			if (!count == 0) then spr ctx "[0]=";
@@ -525,7 +525,7 @@ and gen_expr ?(local=true) ctx e = begin
 		    gen_call ctx e el false;
 		    gen_call ctx e el false;
 		end;
 		end;
 	| TArrayDecl el ->
 	| TArrayDecl el ->
-		spr ctx "lua.Boot.defArray({";
+		spr ctx "__tabArray({";
 		let count = ref 0 in
 		let count = ref 0 in
 		List.iteri (fun i e ->
 		List.iteri (fun i e ->
 		    incr count;
 		    incr count;
@@ -1509,15 +1509,14 @@ let generate_enum ctx e =
 		| TFun (args,_) ->
 		| TFun (args,_) ->
 			let count = List.length args in
 			let count = List.length args in
 			let sargs = String.concat "," (List.map (fun (n,_,_) -> ident n) args) in
 			let sargs = String.concat "," (List.map (fun (n,_,_) -> ident n) args) in
-			print ctx "function(%s)  local _x = lua.Boot.defArray({[0]=\"%s\",%d,%s,__enum__=%s}, %i);" sargs f.ef_name f.ef_index sargs p (count + 2);
+			print ctx "function(%s)  local _x = __tabArray({[0]=\"%s\",%d,%s,__enum__=%s}, %i);" sargs f.ef_name f.ef_index sargs p (count + 2);
 			if has_feature ctx "may_print_enum" then
 			if has_feature ctx "may_print_enum" then
 				(* TODO: better namespacing for _estr *)
 				(* TODO: better namespacing for _estr *)
 				spr ctx " _x.toString = _estr;";
 				spr ctx " _x.toString = _estr;";
 			spr ctx " return _x; end ";
 			spr ctx " return _x; end ";
 			ctx.separator <- true;
 			ctx.separator <- true;
 		| _ ->
 		| _ ->
-			(* TODO: Figure out how to use 1-based indexing for enum values. *)
-			print ctx "{[0]=\"%s\",%d}" f.ef_name f.ef_index;
+			print ctx "__tabArray({[0]=\"%s\",%d},2)" f.ef_name f.ef_index;
 			newline ctx;
 			newline ctx;
 			if has_feature ctx "may_print_enum" then begin
 			if has_feature ctx "may_print_enum" then begin
 				print ctx "%s%s.toString = _estr" p (field f.ef_name);
 				print ctx "%s%s.toString = _estr" p (field f.ef_name);
@@ -1696,6 +1695,20 @@ let generate com =
 	newline ctx;
 	newline ctx;
 	spr ctx "--[[end class hoists--]]"; newline ctx;
 	spr ctx "--[[end class hoists--]]"; newline ctx;
 
 
+	spr ctx "__tabArray = function(tab,length)"; newline ctx;
+	spr ctx "   tab.length = length"; newline ctx;
+	spr ctx "   setmetatable(tab, {"; newline ctx;
+	spr ctx "	__index = Array.prototype,"; newline ctx;
+	spr ctx "	__newindex = function(t,k,v)"; newline ctx;
+	spr ctx "	    if _G.type(k) == 'number' then"; newline ctx;
+	spr ctx "		t.length = k + 1"; newline ctx;
+	spr ctx "	    end"; newline ctx;
+	spr ctx "	    rawset(t,k,v)"; newline ctx;
+	spr ctx "	end"; newline ctx;
+	spr ctx "   })"; newline ctx;
+	spr ctx "   return tab"; newline ctx;
+	spr ctx "end"; newline ctx;
+
 	spr ctx "--[[begin __init__ hoist --]]"; newline ctx;
 	spr ctx "--[[begin __init__ hoist --]]"; newline ctx;
 	List.iter (gen__init__hoist ctx) (List.rev ctx.inits);
 	List.iter (gen__init__hoist ctx) (List.rev ctx.inits);
 	ctx.inits <- []; (* reset inits *)
 	ctx.inits <- []; (* reset inits *)

+ 2 - 15
std/lua/Boot.hx

@@ -137,22 +137,9 @@ class Boot {
 		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
 		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
 	}
 	}
 
 
-	@:keep
-	public static function arrayNewIndex(tab:Dynamic, key:Dynamic, value:Dynamic){
-		untyped rawset(tab, key, value);
-		if (Std.is(key,Int) && key+1 > tab.length){
-			tab.length = key + 1;
-		}
-	}
 
 
-	@:keep
-	public static function defArray(tabobj: Dynamic, length : Int) : Array<Dynamic>  untyped {
-		tabobj.length = length;
-		setmetatable(tabobj, {
-			__index : __lua__("Array.prototype"),
-			__newindex : lua.Boot.arrayNewIndex
-		});
-		return tabobj;
+	public static function defArray(tab: Dynamic, length : Int) : Array<Dynamic>  untyped {
+		return untyped __tabArray(tab, length);
 	}
 	}
 
 
 	public static function urlDecode(str:String){
 	public static function urlDecode(str:String){