Explorar o código

[lua] add support for -D lua-vanilla, which generates pure-lua code with reduced functionality

Justin Donaldson %!s(int64=6) %!d(string=hai) anos
pai
achega
8b1458e758
Modificáronse 3 ficheiros con 33 adicións e 21 borrados
  1. 3 1
      src/core/define.ml
  2. 11 6
      src/generators/genlua.ml
  3. 19 14
      std/lua/_std/String.hx

+ 3 - 1
src/core/define.ml

@@ -59,8 +59,9 @@ type strict_defined =
 	| SourceMap
 	| KeepOldOutput
 	| LoopUnrollMaxCost
-	| LuaVer
 	| LuaJit
+	| LuaVanilla
+	| LuaVer
 	| Macro
 	| MacroTimes
 	| NekoSource
@@ -172,6 +173,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])
+	| LuaVanilla -> "lua_vanilla",("Generate code lacking compiled extern lib support (e.g. utf8)",[Platform Lua])
 	| LuaVer -> "lua_ver",("The lua version to target",[Platform Lua])
 	| Macro -> "macro",("Defined when code is compiled in the macro context",[])
 	| MacroTimes -> "macro_times",("Display per-macro timing when used with --times",[])

+ 11 - 6
src/generators/genlua.ml

@@ -47,6 +47,7 @@ type ctx = {
     mutable separator : bool;
     mutable found_expose : bool;
     mutable lua_jit : bool;
+    mutable lua_vanilla : bool;
     mutable lua_ver : float;
 }
 
@@ -646,10 +647,15 @@ and gen_expr ?(local=true) ctx e = begin
     | TEnumIndex x ->
         gen_value ctx x;
         print ctx "[1]"
-    | TField (e, ef) when is_string_expr e && field_name ef = "length"->
-        spr ctx "__lua_lib_luautf8_Utf8.len(";
-        gen_value ctx e;
-        spr ctx ")";
+    | TField (e, ef) when is_string_expr e && field_name ef = "length" ->
+        if ctx.lua_vanilla then (
+            spr ctx "#";
+            gen_value ctx e;
+        ) else (
+            spr ctx "__lua_lib_luautf8_Utf8.len(";
+            gen_value ctx e;
+            spr ctx ")";
+        )
     | TField (e, ef) when is_possible_string_field e (field_name ef)  ->
         add_feature ctx "use._hx_wrap_if_string_field";
         add_feature ctx "use.string";
@@ -1841,6 +1847,7 @@ let alloc_ctx com =
         separator = false;
         found_expose = false;
         lua_jit = Common.defined com Define.LuaJit;
+        lua_vanilla = Common.defined com Define.LuaVanilla;
         lua_ver = try
                 float_of_string (PMap.find "lua_ver" com.defines.Define.values)
             with | Not_found -> 5.2;
@@ -2042,8 +2049,6 @@ let generate com =
         println ctx "  _hx_bit = setmetatable({}, { __index = _hx_bit_raw });";
         println ctx "  _hx_bit.bnot = function(...) return _hx_bit_clamp(_hx_bit_raw.bnot(...)) end;"; (* lua 5.2  weirdness *)
         println ctx "  _hx_bit.bxor = function(...) return _hx_bit_clamp(_hx_bit_raw.bxor(...)) end;"; (* lua 5.2  weirdness *)
-        println ctx "else";
-        println ctx "  _G.error(\"Bitop library is missing.  Please install luabitop\");";
         println ctx "end";
     end;
 

+ 19 - 14
std/lua/_std/String.hx

@@ -23,7 +23,12 @@
 import lua.Lua;
 import lua.Table;
 import lua.Boot;
-import lua.lib.luautf8.Utf8;
+
+#if lua_vanilla
+	typedef BaseString = lua.NativeStringTools;
+#else
+	typedef BaseString =  lua.lib.luautf8.Utf8;
+#end
 
 @:coreApi
 @:extern
@@ -35,7 +40,7 @@ class String {
 
 	@:keep
 	static function __index(s:Dynamic, k:Dynamic) : Dynamic {
-		if (k == "length") return Utf8.len(s);
+		if (k == "length") return BaseString.len(s);
 		else if (Reflect.hasField(untyped String.prototype, k)) return untyped String.prototype[k];
 		else if (__oldindex != null) {
 			if (Lua.type(__oldindex) == "function"){
@@ -48,12 +53,12 @@ class String {
 		else return null;
 	}
 
-	public inline function toUpperCase() : String return Utf8.upper(this);
-	public inline function toLowerCase() : String return Utf8.lower(this);
+	public inline function toUpperCase() : String return BaseString.upper(this);
+	public inline function toLowerCase() : String return BaseString.lower(this);
 	public inline function indexOf( str : String, ?startIndex : Int ) : Int {
 		if (startIndex == null) startIndex = 1;
 		else startIndex += 1;
-		var r = Utf8.find(this, str, startIndex, true).begin;
+		var r = BaseString.find(this, str, startIndex, true).begin;
 		if (r != null && r > 0) return r-1;
 		else return -1;
 	}
@@ -77,7 +82,7 @@ class String {
 		while (idx != null){
 			var newidx = 0;
 			if (delimiter.length > 0){
-				newidx = Utf8.find(this, delimiter, idx, true).begin;
+				newidx = BaseString.find(this, delimiter, idx, true).begin;
 			} else if (idx >= this.length){
 				newidx = null;
 			} else {
@@ -85,11 +90,11 @@ class String {
 			}
 
 			if (newidx != null){
-				var match = Utf8.sub(this, idx, newidx-1).match;
+				var match = BaseString.sub(this, idx, newidx-1).match;
 				ret.push(match);
 				idx = newidx + delimiter.length;
 			} else {
-				ret.push(Utf8.sub(this,idx,this.length).match);
+				ret.push(BaseString.sub(this,idx,this.length).match);
 				idx = null;
 			}
 		}
@@ -105,17 +110,17 @@ class String {
 		if (startIndex < 0) startIndex = 0;
 		if (endIndex < startIndex) {
 			// swap the index positions
-			return Utf8.sub(this, endIndex+1, startIndex).match;
+			return BaseString.sub(this, endIndex+1, startIndex).match;
 		} else {
-			return Utf8.sub(this, startIndex+1, endIndex).match;
+			return BaseString.sub(this, startIndex+1, endIndex).match;
 		}
 	}
 
 	public inline function charAt( index : Int) : String {
-		return Utf8.sub(this,index+1, index+1).match;
+		return BaseString.sub(this,index+1, index+1).match;
 	}
 	public inline function charCodeAt( index : Int) : Null<Int> {
-		return Utf8.byte(this,index+1);
+		return BaseString.byte(this,index+1);
 	}
 
 	public inline function substr( pos : Int, ?len : Int ) : String {
@@ -123,11 +128,11 @@ class String {
 		else if (len < 0) len = length + len;
 		if (pos < 0) pos = length + pos;
 		if (pos < 0) pos = 0;
-		return Utf8.sub(this, pos + 1, pos+len).match;
+		return BaseString.sub(this, pos + 1, pos+len).match;
 	}
 
 	public inline static function fromCharCode( code : Int ) : String {
-		return Utf8.char(code);
+		return BaseString.char(code);
 	}
 
 }