Przeglądaj źródła

Lua : De-simplify defArray

It turns out that the simplified approach won't work... it doesn't handle cases
like :

```haxe3
var x = {null, null, null};
```

Lua just has problems with handling nulls in tables.
Justin Donaldson 10 lat temu
rodzic
commit
9da2aa7c23
3 zmienionych plików z 6 dodań i 4 usunięć
  1. 3 1
      genlua.ml
  2. 2 2
      std/lua/Boot.hx
  3. 1 1
      std/lua/_std/Array.hx

+ 3 - 1
genlua.ml

@@ -492,11 +492,13 @@ and gen_expr ctx e =
 		gen_call ctx e el false
 	| TArrayDecl el ->
 		spr ctx "lua.Boot.defArray({";
+		let count = ref 0 in
 		List.iteri (fun i e ->
+		    incr count;
 		    if (i == 0) then spr ctx "[0]="
 		    else spr ctx ", ";
 		    gen_value ctx e) el;
-		print ctx " })";
+		print ctx " }, %i)" !count;
 	| TThrow e ->
 		spr ctx "error(";
 		gen_value ctx e;

+ 2 - 2
std/lua/Boot.hx

@@ -96,8 +96,8 @@ class Boot {
 	}
 
 	@:keep
-	public static function defArray(tabobj: Dynamic) : Array<Dynamic>  untyped {
-		tabobj.length = tabobj[0] == null ? 0 : lua.TableTools.maxn(tabobj) + 1;
+	public static function defArray(tabobj: Dynamic, length : Int) : Array<Dynamic>  untyped {
+		tabobj.length = length; 
 		setmetatable(tabobj, {
 			__index : __lua__("Array.prototype"),
 			__newindex : lua.Boot.arrayNewIndex

+ 1 - 1
std/lua/_std/Array.hx

@@ -25,7 +25,7 @@ class Array<T> {
 	public var length(default,null) : Int;
 
 	public function new() : Void  {
-		lua.Boot.defArray(this);
+		lua.Boot.defArray(this,0);
 	}
 	public function concat( a : Array<T> ) : Array<T> {
 		var ret = this.copy();