Browse Source

Lua: Back to the drawing board with strings.

It was clear after running some tests that simply inlining the
implementation for strings is not going to cut it for the tests.  There
are situations involving type unification for structural types that
require an accessible field.  So, I've taken a different tack for
String.  This time, I'm adding the haxe method metatable to the base
string metatable.  Luckily, there's no conflicts.  The primary lua string
methods will still be reached, and then anything not in there will be
looked up against the haxe implementation. Modifications in Boot and
String take care of this.  There's another tweak to Std that I needed to
make, for some reason the cast type needed to be made explicit.
Justin Donaldson 10 years ago
parent
commit
d4c84f62c1
3 changed files with 24 additions and 17 deletions
  1. 3 0
      std/lua/Boot.hx
  2. 1 1
      std/lua/_std/Std.hx
  3. 20 16
      std/lua/_std/String.hx

+ 3 - 0
std/lua/Boot.hx

@@ -22,6 +22,9 @@
 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");
 
 	private static function __unhtml(s : String) {

+ 1 - 1
std/lua/_std/Std.hx

@@ -37,7 +37,7 @@ import lua.Boot;
 	}
 
 	public static inline function int( x : Float ) : Int {
-		return cast(x) | 0;
+		return cast(x, Int) | 0;
 	}
 
 	public static function parseInt( x : String ) : Null<Int> {

+ 20 - 16
std/lua/_std/String.hx

@@ -20,21 +20,23 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-extern class String {
-	var length(get,null) : Int;
+@:coreApi
+class String {
+	public var length(default,null) : Int;
 
-	function new(string:String) {
-		this = string;
+	public function new(string:String) untyped {
+		// TODO: extern this somehow
+		__lua__("self = str");
 	}
 
-	inline function toUpperCase() : String return untyped this.upper();
-	inline function toLowerCase() : String return untyped this.lower();
-	inline function indexOf( str : String, ?startIndex : Int ) : Int {
+	public function toUpperCase() : String return untyped this.upper();
+	public function toLowerCase() : String return untyped this.lower();
+	public function indexOf( str : String, ?startIndex : Int ) : Int {
 		if (startIndex == null) startIndex = 1;
 		else startIndex += 1;
 		return lua.StringTools.find(this, str, startIndex, str.length, true);
 	}
-	inline function lastIndexOf( str : String, ?startIndex : Int ) : Int {
+	public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
 		var i = 0;
 		var ret = 0;
 		while(i != null){
@@ -43,32 +45,34 @@ extern class String {
 		}
 		return ret-1;
 	}
-	inline function split( delimiter : String ) : Array<String> {
+	public function split( delimiter : String ) : Array<String> {
 		return [];
 	}
-	inline function toString() : String {
+	public function toString() : String {
 		return this;
 	}
-	inline function substring( startIndex : Int, ?endIndex : Int ) : String {
+	public function substring( startIndex : Int, ?endIndex : Int ) : String {
 		if (endIndex == null) endIndex = this.length;
 		return untyped lua.StringTools.sub(this, startIndex + 1,endIndex + 1);
 	}
 
-	inline function get_length() : Int {
+	function get_length() : Int {
 		return lua.StringTools.len(this);
 	}
-	inline function charAt( index : Int) : String {
+	public function charAt( index : Int) : String {
 		return lua.StringTools.sub(this,index+1, index+1);
 	}
-	inline function charCodeAt( index : Int) : Null<Int> {
+	public function charCodeAt( index : Int) : Null<Int> {
 		return lua.StringTools.byte(this,index+1);
 	}
 
-	inline function substr( pos : Int, ?len : Int ) : String {
+	public function substr( pos : Int, ?len : Int ) : String {
 		if (len == null || len > pos + this.length) len = this.length;
 		return lua.StringTools.sub(this, pos + 1, pos+len + 1);
 	}
 
-	static function fromCharCode( code : Int ) : String;
+	public static function fromCharCode( code : Int ) : String {
+		return untyped String.char(code);
+	}
 }