Przeglądaj źródła

[lua] experimental global table specification (incomplete)

Justin Donaldson 7 lat temu
rodzic
commit
3f961a4170
2 zmienionych plików z 15 dodań i 4 usunięć
  1. 3 1
      src/core/define.ml
  2. 12 3
      src/generators/genlua.ml

+ 3 - 1
src/core/define.ml

@@ -60,8 +60,9 @@ type strict_defined =
 	| SourceMap
 	| KeepOldOutput
 	| LoopUnrollMaxCost
-	| LuaVer
+	| LuaGlobal
 	| LuaJit
+	| LuaVer
 	| Macro
 	| MacroDebug
 	| MacroTimes
@@ -175,6 +176,7 @@ let infos = function
 	| KeepOldOutput -> "keep_old_output",("Keep old source files in the output directory (for C#/Java)",[Platforms [Cs;Java]])
 	| LoopUnrollMaxCost -> "loop_unroll_max_cost",("Maximum cost (number of expressions * iterations) before loop unrolling is canceled (default 250)",[])
 	| LuaJit -> "lua_jit",("Enable the jit compiler for lua (version 5.2 only)",[Platform Lua])
+	| LuaGlobal -> "lua_global",("The name of the global table to reference (default _G)",[Platform Lua])
 	| LuaVer -> "lua_ver",("The lua version to target",[Platform Lua])
 	| Macro -> "macro",("Defined when code is compiled in the macro context",[])
 	| MacroDebug -> "macro_debug",("Show warnings for potential macro problems (e.g. macro-in-macro calls)",[])

+ 12 - 3
src/generators/genlua.ml

@@ -48,6 +48,7 @@ type ctx = {
     mutable found_expose : bool;
     mutable lua_jit : bool;
     mutable lua_ver : float;
+    mutable lua_global : string;
 }
 
 type object_store = {
@@ -1029,8 +1030,9 @@ and gen_expr ?(local=true) ctx e = begin
         spr ctx ")"
     | TCast (e1,None) ->
         gen_value ctx e1;
-    | TIdent s ->
+    | TIdent s ->  begin
         spr ctx s
+      end
 end;
 
     (* gen_block_element handles expressions that map to "statements" in lua. *)
@@ -1840,9 +1842,12 @@ let alloc_ctx com =
         separator = false;
         found_expose = false;
         lua_jit = Common.defined com Define.LuaJit;
-        lua_ver = try
+        lua_ver = (try
                 float_of_string (PMap.find "lua_ver" com.defines.Define.values)
-            with | Not_found -> 5.2;
+            with | Not_found -> 5.2);
+        lua_global = (try
+                (PMap.find "lua_global" com.defines.Define.values)
+            with | Not_found -> "_G");
     } in
     ctx.type_accessor <- (fun t ->
         let p = t_path t in
@@ -1929,6 +1934,8 @@ let generate com =
     let ctx = alloc_ctx com in
 
     Codegen.map_source_header com (fun s -> print ctx "-- %s\n" s);
+    println ctx "local _HXG = %s;" ctx.lua_global;
+
 
     if has_feature ctx "Class" || has_feature ctx "Type.getClassName" then add_feature ctx "lua.Boot.isClass";
     if has_feature ctx "Enum" || has_feature ctx "Type.getEnumName" then add_feature ctx "lua.Boot.isEnum";
@@ -2190,3 +2197,5 @@ let generate com =
     output_string ch (Buffer.contents ctx.buf);
     close_out ch
 
+
+