Browse Source

[lua] put the _hx_bit library behind a use._bit feature, and inline it

Justin Donaldson 9 years ago
parent
commit
37769a3c18
3 changed files with 17 additions and 16 deletions
  1. 17 1
      src/generators/genlua.ml
  2. 0 3
      std/lua/Boot.hx
  3. 0 12
      std/lua/_lua/_hx_bit.lua

+ 17 - 1
src/generators/genlua.ml

@@ -1248,6 +1248,7 @@ and gen_wrap_tbinop ctx e=
 	    gen_value ctx e
 
 and gen_bitop ctx op e1 e2 =
+    add_feature ctx "use._bitop";
     print ctx "_hx_bit.%s(" (match op with
 	| Ast.OpXor  ->  "bxor"
 	| Ast.OpAnd  ->  "band"
@@ -1808,12 +1809,26 @@ let generate com =
 
 	List.iter (generate_type_forward ctx) com.types; newline ctx;
 
-	spr ctx "local _hx_bind";
+	(* Generate some dummy placeholders for bind/bit behavior that may be *)
+	(* generated later *)
+	spr ctx "local _hx_bind,_hx_bit";
 	List.iter (gen__init__hoist ctx) (List.rev ctx.inits); newline ctx;
 	ctx.inits <- []; (* reset inits *)
 
 	List.iter (generate_type ctx) com.types;
 
+	if has_feature ctx "use._bitop" then begin
+	    sprln ctx "pcall(require, 'bit32') pcall(require, 'bit')";
+	    sprln ctx "local _hx_bit_raw = bit or bit32";
+	    sprln ctx "local function _hx_bit_clamp(v) return _hx_bit_raw.band(v, 2147483647 ) - _hx_bit_raw.band(v, 2147483648) end";
+	    sprln ctx "if type(jit) == 'table' then";
+	    sprln ctx "_hx_bit = setmetatable({},{__index = function(t,k) return function(...) return _hx_bit_clamp(rawget(_hx_bit_raw,k)(...)) end end})";
+	    sprln ctx "else";
+	    sprln ctx "_hx_bit = setmetatable({}, { __index = _hx_bit_raw })";
+	    sprln ctx "_hx_bit.bnot = function(...) return _hx_bit_clamp(_hx_bit_raw.bnot(...)) end";
+	    sprln ctx "end";
+	end;
+
 	(* If we use haxe Strings, patch Lua's string *)
 	if has_feature ctx "use.string" then begin
 	    sprln ctx "local _hx_string_mt = _G.getmetatable('');";
@@ -1855,6 +1870,7 @@ let generate com =
 	end;
 	if has_feature ctx "use._hx_bind" then println ctx "_hx_bind = lua.Boot.bind";
 
+
 	List.iter (generate_enumMeta_fields ctx) com.types;
 
 	(match com.main with

+ 0 - 3
std/lua/Boot.hx

@@ -24,9 +24,6 @@ package lua;
 
 import haxe.Constraints.Function;
 
-// TODO: seperate bit helper method from extern so operators can use it.
-import lua.Bit;
-
 @:dox(hide)
 class Boot {
 

+ 0 - 12
std/lua/_lua/_hx_bit.lua

@@ -1,12 +0,0 @@
-local _hx_bit
-pcall(require, 'bit32') pcall(require, 'bit')
-local _hx_bit_raw = bit or bit32
-
-local function _hx_bit_clamp(v) return _hx_bit_raw.band(v, 2147483647 ) - _hx_bit_raw.band(v, 2147483648) end
-
-if type(jit) == 'table' then
-  _hx_bit = setmetatable({},{__index = function(t,k) return function(...) return _hx_bit_clamp(rawget(_hx_bit_raw,k)(...)) end end})
-else
-  _hx_bit = setmetatable({}, { __index = _hx_bit_raw })
-  _hx_bit.bnot = function(...) return _hx_bit_clamp(_hx_bit_raw.bnot(...)) end
-end