Bläddra i källkod

Lua: Fix String for new OOP mechanism

I tried a new approach for OOP in a previous commit.  It works ok, but
broke strings.  It forced me to reconsider how to set up the String
class for the Haxe Lua target.

The way things work now is that the base lua String.hx class sets the
metatable for the lua "string" class to be the String.hx metatable.
I.e., field accesses will be referenced against the base lua string
instance first, and then against the Haxe String metatable methods.

The Haxe String metatable methods also include an __index method which
looks for references to a field "length", and aliases this against the
respective lua implementation.
Justin Donaldson 10 år sedan
förälder
incheckning
ee88b4d178
2 ändrade filer med 10 tillägg och 8 borttagningar
  1. 1 5
      std/lua/Boot.hx
  2. 9 3
      std/lua/_std/String.hx

+ 1 - 5
std/lua/Boot.hx

@@ -22,9 +22,6 @@
 package lua;
 
 class Boot {
-	static function __init__(){
-		untyped __lua__("setmetatable(_G.string, String.mt)");
-	}
 	public static var unpack : Dynamic->lua.Table<Int,Dynamic> = untyped __lua__("function(...) return {...} end");
 
 	static function __unhtml(s : String) {
@@ -76,10 +73,9 @@ class Boot {
 
 	@:keep
 	public static function defArray(tabobj: Dynamic, length : Int) : Array<Dynamic>  untyped {
-		tabobj.__methods = __lua__("{Array.mt}");
 		tabobj.length = length;
 		setmetatable(tabobj, {
-			__index : lua.Boot.resolveMethod,
+			__index : __lua__("Array.mt"),
 			__newindex : lua.Boot.arrayNewIndex
 		});
 		return tabobj;

+ 9 - 3
std/lua/_std/String.hx

@@ -23,10 +23,16 @@
 @:coreApi
 class String {
 	public var length(default,null) : Int;
+	public function new(string:String) untyped {}
 
-	public function new(string:String) untyped {
-		// TODO: extern this somehow
-		__lua__("self = string");
+	static function __init__() : Void{
+		untyped __lua__("setmetatable(_G.string, String.mt)");
+	}
+
+	@:keep
+	function __index(k:String) : Dynamic {
+		if (k == "length") return untyped __lua__("#self");
+		else return null;
 	}
 
 	public function toUpperCase() : String return untyped this.upper();