Procházet zdrojové kódy

[lua] Array metatable filled after Array prototype

peyty před 9 roky
rodič
revize
a80551ed22
1 změnil soubory, kde provedl 8 přidání a 7 odebrání
  1. 8 7
      genlua.ml

+ 8 - 7
genlua.ml

@@ -1709,20 +1709,18 @@ let generate com =
 
 	List.iter (generate_type_forward ctx) com.types; newline ctx;
 
-	(* delay mt construction and cache, coz Array.prototype printed just after this function *)
-	sprln ctx "local _hx_tabArray_mt = nil";
-	sprln ctx "local function _hx_tabArray_mt_init() _hx_tabArray_mt = {";
-	sprln ctx "   __index = Array.prototype,";
+	sprln ctx "local _hx_array_mt = {";
 	sprln ctx "   __newindex = function(t,k,v)";
 	sprln ctx "       if type(k) == 'number' and k >= t.length then";
 	sprln ctx "          t.length = k + 1";
 	sprln ctx "       end";
 	sprln ctx "       rawset(t,k,v)";
 	sprln ctx "   end";
-	sprln ctx "} return _hx_tabArray_mt end";
-	sprln ctx "local _hx_tabArray = function(tab,length)";
+	sprln ctx "}";
+
+	sprln ctx "local function _hx_tabArray(tab,length)";
 	sprln ctx "   tab.length = length";
-	sprln ctx "   return setmetatable(tab, _hx_tabArray_mt or _hx_tabArray_mt_init())";
+	sprln ctx "   return setmetatable(tab, _hx_array_mt)";
 	sprln ctx "end";
 
 	List.iter (gen__init__hoist ctx) (List.rev ctx.inits); newline ctx;
@@ -1731,6 +1729,9 @@ let generate com =
 	sprln ctx "local _hx_bind = {}";
 
 	List.iter (generate_type ctx) com.types;
+
+	sprln ctx "_hx_array_mt.__index = Array.prototype";
+
 	let rec chk_features e =
 		if is_dynamic_iterator ctx e then add_feature ctx "use._iterator";
 		match e.eexpr with