Browse Source

Merge pull request #3487 from nadako/js_regexp

[js] provide native RegExp extern and use it in EReg
Dan Korostelev 11 years ago
parent
commit
a26958dee4
3 changed files with 57 additions and 6 deletions
  1. 45 0
      std/js/RegExp.hx
  2. 8 2
      std/js/_std/EReg.hx
  3. 4 4
      typeload.ml

+ 45 - 0
std/js/RegExp.hx

@@ -0,0 +1,45 @@
+/*
+ * Copyright (C)2005-2014 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+ package js;
+
+/**
+    Native JavaScript regular expressions.
+
+    For cross-platform regular expressions, use haxe `EReg` class or regexp literals.
+**/
+@:native("RegExp")
+extern class RegExp {
+    var global(default,null):Bool;
+    var ignoreCase(default,null):Bool;
+    var multiline(default,null):Bool;
+    var source(default,null):String;
+    var lastIndex:Int;
+    function new(pattern:String, ?flags:String);
+    function exec(str:String):Null<RegExpMatch>;
+    function test(str:String):Bool;
+    function toString():String;
+}
+
+extern class RegExpMatch extends Array<String> {
+    var index:Int;
+    var input:String;
+}

+ 8 - 2
std/js/_std/EReg.hx

@@ -21,11 +21,11 @@
  */
  */
 @:coreApi class EReg {
 @:coreApi class EReg {
 
 
-	var r : Dynamic;
+	var r : HaxeRegExp;
 
 
 	public function new( r : String, opt : String ) : Void {
 	public function new( r : String, opt : String ) : Void {
 		opt = opt.split("u").join(""); // 'u' (utf8) depends on page encoding
 		opt = opt.split("u").join(""); // 'u' (utf8) depends on page encoding
-		this.r = untyped __new__("RegExp",r,opt);
+		this.r = new HaxeRegExp(r, opt);
 	}
 	}
 
 
 	public function match( s : String ) : Bool {
 	public function match( s : String ) : Bool {
@@ -110,3 +110,9 @@
 		return buf.toString();
 		return buf.toString();
 	}
 	}
 }
 }
+
+@:native("RegExp")
+private extern class HaxeRegExp extends js.RegExp {
+	var m:js.RegExp.RegExpMatch;
+	var s:String;
+}

+ 4 - 4
typeload.ml

@@ -1009,10 +1009,10 @@ let is_generic_parameter ctx c =
 		false
 		false
 
 
 let check_extends ctx c t p = match follow t with
 let check_extends ctx c t p = match follow t with
-	| TInst ({ cl_path = [],"Array" },_)
-	| TInst ({ cl_path = [],"String" },_)
-	| TInst ({ cl_path = [],"Date" },_)
-	| TInst ({ cl_path = [],"Xml" },_) ->
+	| TInst ({ cl_path = [],"Array"; cl_extern = basic_extern },_)
+	| TInst ({ cl_path = [],"String"; cl_extern = basic_extern },_)
+	| TInst ({ cl_path = [],"Date"; cl_extern = basic_extern },_)
+	| TInst ({ cl_path = [],"Xml"; cl_extern = basic_extern },_) when not (c.cl_extern && basic_extern) ->
 		error "Cannot extend basic class" p;
 		error "Cannot extend basic class" p;
 	| TInst (csup,params) ->
 	| TInst (csup,params) ->
 		if is_parent c csup then error "Recursive class" p;
 		if is_parent c csup then error "Recursive class" p;