Browse Source

Lua : Misc changes to make new object prototyping work.

I have to follow suit on my oop protocol with some of my other classes.

Unfortunately, this makes externing constructors more difficult, since
they are unlikely to follow the same protocol of passing "self" as the
first argument.  However, there's really no standard there on the lua
side, and it's an easy workaround here.
Justin Donaldson 10 years ago
parent
commit
c99fc2d0d2
4 changed files with 17 additions and 8 deletions
  1. 1 1
      genlua.ml
  2. 4 1
      std/lua/Rex.hx
  3. 1 1
      std/lua/_std/EReg.hx
  4. 11 5
      std/lua/_std/String.hx

+ 1 - 1
genlua.ml

@@ -1073,7 +1073,7 @@ let gen_class_field ctx c f =
 		    ctx.in_value <- None;
 		    ctx.in_value <- None;
 		    ctx.in_loop <- false;
 		    ctx.in_loop <- false;
 		    print ctx "%s = function" (anon_field f.cf_name);
 		    print ctx "%s = function" (anon_field f.cf_name);
-		    print ctx "(%s) " (String.concat "," (List.map ident (List.map arg_name f2.tf_args)));
+		    print ctx "(%s) " (String.concat "," ("self" :: List.map ident (List.map arg_name f2.tf_args)));
 		    newline ctx;
 		    newline ctx;
 		    let fblock = fun_block ctx f2 e.epos in
 		    let fblock = fun_block ctx f2 e.epos in
 		    (match fblock.eexpr with
 		    (match fblock.eexpr with

+ 4 - 1
std/lua/Rex.hx

@@ -1,7 +1,10 @@
 package lua;
 package lua;
 @:luaRequire("rex_pcre")
 @:luaRequire("rex_pcre")
 extern class Rex {
 extern class Rex {
-	public function new(expr : String, flag : String);
+
+	inline public static function create(expr : String, flag : String) : Rex{
+		return untyped Rex['new'](expr, flag);
+	}
 
 
 	/**
 	/**
 	  The function searches for the first match of the regexp patt in the
 	  The function searches for the first match of the regexp patt in the

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

@@ -41,7 +41,7 @@ class EReg {
 			}
 			}
 		}
 		}
 		if (global == null) global = false;
 		if (global == null) global = false;
-		this.r = new Rex(r, ropt.toString());
+		this.r = Rex.create(r, ropt.toString());
 	}
 	}
 
 
 	public function match( str : String ) : Bool {
 	public function match( str : String ) : Bool {

+ 11 - 5
std/lua/_std/String.hx

@@ -23,18 +23,24 @@
 @:coreApi
 @:coreApi
 class String {
 class String {
 	public var length(default,null) : Int;
 	public var length(default,null) : Int;
-	public function new(string:String) untyped {}
 
 
-	static function __init__() : Void{
-		untyped __lua__("setmetatable(_G.string, String.prototype)");
+
+	public function new(string:String) untyped {
+		if (string != null) __lua__("self = string");
+		else __lua__("self = ''");
+	}
+
+	static function __init__() : Void untyped{
+		__lua__("getmetatable('').__index = String.__index");
 	}
 	}
 
 
 	@:keep
 	@:keep
-	function __index(k:String) : Dynamic {
-		if (k == "length") return untyped __lua__("#self");
+	static function __index(s:Dynamic, k:Dynamic) : Dynamic {
+		if (k == "length") return untyped __lua__("#s");
 		else return null;
 		else return null;
 	}
 	}
 
 
+
 	public function toUpperCase() : String return untyped this.upper();
 	public function toUpperCase() : String return untyped this.upper();
 	public function toLowerCase() : String return untyped this.lower();
 	public function toLowerCase() : String return untyped this.lower();
 	public function indexOf( str : String, ?startIndex : Int ) : Int {
 	public function indexOf( str : String, ?startIndex : Int ) : Int {