Browse Source

js core api classes extraction done

Nicolas Cannasse 15 years ago
parent
commit
f00b39fd95
7 changed files with 740 additions and 31 deletions
  1. 90 0
      std/js/_std/EReg.hx
  2. 101 0
      std/js/_std/Hash.hx
  3. 89 0
      std/js/_std/IntHash.hx
  4. 120 0
      std/js/_std/Reflect.hx
  5. 71 0
      std/js/_std/Std.hx
  6. 219 0
      std/js/_std/Type.hx
  7. 50 31
      std/js/_std/Xml.hx

+ 90 - 0
std/js/_std/EReg.hx

@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class EReg {
+
+	var r : Dynamic;
+
+	public function new( r : String, opt : String ) : Void {
+		opt = opt.split("u").join(""); // 'u' (utf8) depends on page encoding
+		this.r = untyped __new__("RegExp",r,opt);
+	}
+
+	public function match( s : String ) : Bool {
+		r.m = r.exec(s);
+		r.s = s;
+		r.l = untyped __js__("RegExp.leftContext");
+		r.r = untyped __js__("RegExp.rightContext");
+		return (r.m != null);
+	}
+
+	public function matched( n : Int ) : String {
+		return if( r.m != null && n >= 0 && n < r.m.length ) r.m[n] else throw "EReg::matched";
+	}
+
+	public function matchedLeft() : String {
+		if( r.m == null ) throw "No string matched";
+		if( r.l == null ) return r.s.substr(0,r.m.index);
+		return r.l;
+	}
+
+	public function matchedRight() : String {
+		if( r.m == null ) throw "No string matched";
+		if( r.r == null ) {
+			var sz = r.m.index+r.m[0].length;
+			return r.s.substr(sz,r.s.length-sz);
+		}
+		return r.r;
+	}
+
+	public function matchedPos() : { pos : Int, len : Int } {
+		if( r.m == null ) throw "No string matched";
+		return { pos : r.m.index, len : r.m[0].length };
+	}
+
+	public function split( s : String ) : Array<String> {
+		// we can't use directly s.split because it's ignoring the 'g' flag
+		var d = "#__delim__#";
+		return untyped s.replace(r,d).split(d);
+	}
+
+	public function replace( s : String, by : String ) : String {
+		return untyped s.replace(r,by);
+	}
+
+	public function customReplace( s : String, f : EReg -> String ) : String {
+		var buf = new StringBuf();
+		while( true ) {
+			if( !match(s) )
+				break;
+			buf.add(matchedLeft());
+			buf.add(f(this));
+			s = matchedRight();
+		}
+		buf.add(s);
+		return buf.toString();
+	}
+
+}

+ 101 - 0
std/js/_std/Hash.hx

@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class Hash<T> {
+
+	private var h : Dynamic;
+
+	public function new() : Void {
+		untyped {
+			h = __js__("{}");
+			if( h.__proto__ != null ) {
+				h.__proto__ = null;
+				__js__("delete")(h.__proto__);
+			}
+		}
+	}
+
+	public function set( key : String, value : T ) : Void {
+		untyped h["$"+key] = value;
+	}
+
+	public function get( key : String ) : Null<T> {
+		return untyped h["$"+key];
+	}
+
+	public function exists( key : String ) : Bool {
+		try {
+			key = "$"+key;
+			return untyped this.hasOwnProperty.call(h,key);
+		}catch(e:Dynamic){
+			untyped __js__("
+				for(var i in this.h)
+					if( i == key ) return true;
+			");
+			return false;
+		}
+	}
+
+	public function remove( key : String ) : Bool {
+		if( !exists(key) )
+			return false;
+		untyped __js__("delete")(h["$"+key]);
+		return true;
+	}
+
+	public function keys() : Iterator<String> {
+		var a = new Array<String>();
+		untyped __js__("
+			for(var i in this.h)
+				a.push(i.substr(1));
+		");
+		return a.iterator();
+	}
+
+	public function iterator() : Iterator<T> {
+		return untyped {
+			ref : h,
+			it : keys(),
+			hasNext : function() { return this.it.hasNext(); },
+			next : function() { var i = this.it.next(); return this.ref["$"+i]; }
+		};
+	}
+
+	public function toString() : String {
+		var s = new StringBuf();
+		s.add("{");
+		var it = keys();
+		for( i in it ) {
+			s.add(i);
+			s.add(" => ");
+			s.add(Std.string(get(i)));
+			if( it.hasNext() )
+				s.add(", ");
+		}
+		s.add("}");
+		return s.toString();
+	}
+
+}

+ 89 - 0
std/js/_std/IntHash.hx

@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class IntHash<T> {
+
+	private var h : Dynamic;
+
+	public function new() : Void {
+		h = untyped __js__("{}");
+		untyped if( h.__proto__ != null ) {
+			h.__proto__ = null;
+			__js__("delete")(h.__proto__);
+		};
+	}
+
+	public function set( key : Int, value : T ) : Void {
+		untyped h[key] = value;
+	}
+
+	public function get( key : Int ) : Null<T> {
+		return untyped h[key];
+	}
+
+	public function exists( key : Int ) : Bool {
+		return untyped h[key] != null;
+	}
+
+	public function remove( key : Int ) : Bool {
+		if( untyped h[key] == null ) return false;
+		untyped  __js__("delete")(h[key]);
+		return true;
+	}
+
+	public function keys() : Iterator<Int> {
+		var a = new Array();
+		untyped __js__("
+			for( x in this.h )
+				a.push(x);
+		");
+		return a.iterator();
+	}
+
+	public function iterator() : Iterator<T> {
+		return untyped {
+			ref : h,
+			it : keys(),
+			hasNext : function() { return this.it.hasNext(); },
+			next : function() { var i = this.it.next(); return this.ref[i]; }
+		};
+	}
+
+	public function toString() : String {
+		var s = new StringBuf();
+		s.add("{");
+		var it = keys();
+		for( i in it ) {
+			s.add(i);
+			s.add(" => ");
+			s.add(Std.string(get(i)));
+			if( it.hasNext() )
+				s.add(", ");
+		}
+		s.add("}");
+		return s.toString();
+	}
+
+}

+ 120 - 0
std/js/_std/Reflect.hx

@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class Reflect {
+
+	public static function hasField( o : Dynamic, field : String ) : Bool untyped {
+		if( o.hasOwnProperty != null )
+			return o.hasOwnProperty(field);
+		var arr = fields(o);
+		for( t in arr.iterator() )
+			if( t == field ) return true;
+		return false;
+	}
+
+	public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
+		var v = null;
+		try {
+			v = o[field];
+		} catch( e : Dynamic ) {
+		}
+		return v;
+	}
+
+	public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
+		o[field] = value;
+	}
+
+	public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
+		return func.apply(o,args);
+	}
+
+	public static function fields( o : Dynamic ) : Array<String> untyped {
+		if( o == null ) return new Array();
+		var a = new Array();
+		if( o.hasOwnProperty ) {
+			__js__("
+				for(var i in o)
+					if( o.hasOwnProperty(i) )
+						a.push(i);
+			");
+		} else {
+			var t;
+			try{ t = o.__proto__; } catch( e : Dynamic ) { t = null; }
+			if( t != null )
+				o.__proto__ = null;
+			__js__("
+				for(var i in o)
+					if( i != \"__proto__\" )
+						a.push(i);
+			");
+			if( t != null )
+				o.__proto__ = t;
+		}
+		return a;
+	}
+
+	public static function isFunction( f : Dynamic ) : Bool untyped {
+		return __js__("typeof(f)") == "function" && f.__name__ == null;
+	}
+
+	public static function compare<T>( a : T, b : T ) : Int {
+		return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
+	}
+
+	public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
+		return f1.scope == f2.scope && f1.method == f2.method && f1.method != null;
+	}
+
+	public static function isObject( v : Dynamic ) : Bool untyped {
+		if( v == null )
+			return false;
+		var t = __js__("typeof(v)");
+		return (t == "string" || (t == "object" && !v.__enum__) || (t == "function" && v.__name__ != null));
+	}
+
+	public static function deleteField( o : Dynamic, f : String ) : Bool untyped {
+		if( !hasField(o,f) ) return false;
+		__js__("delete")(o[f]);
+		return true;
+	}
+
+	public static function copy<T>( o : T ) : T {
+		var o2 : Dynamic = {};
+		for( f in Reflect.fields(o) )
+			Reflect.setField(o2,f,Reflect.field(o,f));
+		return o2;
+	}
+
+	public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
+		return function() untyped {
+			var a = new Array();
+			for( i in 0...arguments.length )
+				a.push(arguments[i]);
+			return f(a);
+		};
+	}
+
+}

+ 71 - 0
std/js/_std/Std.hx

@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class Std {
+
+	public static function is( v : Dynamic, t : Dynamic ) : Bool {
+		return untyped js.Boot.__instanceof(v,t);
+	}
+
+	public static function string( s : Dynamic ) : String {
+		return untyped js.Boot.__string_rec(s,"");
+	}
+
+	public static function int( x : Float ) : Int {
+		if( x < 0 ) return Math.ceil(x);
+		return Math.floor(x);
+	}
+
+	public static function parseInt( x : String ) : Null<Int> {
+		var v = untyped __js__("parseInt")(x);
+		if( Math.isNaN(v) )
+			return null;
+		return cast v;
+	}
+
+	public static function parseFloat( x : String ) : Float {
+		return untyped __js__("parseFloat")(x);
+	}
+
+	public static function random( x : Int ) : Int {
+		return untyped Math.floor(Math.random()*x);
+	}
+
+	static function __init__() : Void untyped {
+		String.prototype.__class__ = String;
+		String.__name__ = ["String"];
+		Array.prototype.__class__ = Array;
+		Array.__name__ = ["Array"];
+		Int = { __name__ : ["Int"] };
+		Dynamic = { __name__ : ["Dynamic"] };
+		Float = __js__("Number");
+		Float.__name__ = ["Float"];
+		Bool = { __ename__ : ["Bool"] };
+		Class = { __name__ : ["Class"] };
+		Enum = {};
+		Void = { __ename__ : ["Void"] };
+	}
+
+}

+ 219 - 0
std/js/_std/Type.hx

@@ -0,0 +1,219 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+enum ValueType {
+	TNull;
+	TInt;
+	TFloat;
+	TBool;
+	TObject;
+	TFunction;
+	TClass( c : Class<Dynamic> );
+	TEnum( e : Enum<Dynamic> );
+	TUnknown;
+}
+
+@:core_api class Type {
+
+	public static function getClass<T>( o : T ) : Class<T> untyped {
+		if( o == null )
+			return null;
+		if( o.__enum__ != null )
+			return null;
+		return o.__class__;
+	}
+
+	public static function getEnum( o : Dynamic ) : Enum<Dynamic> untyped {
+		if( o == null )
+			return null;
+		return o.__enum__;
+	}
+
+	public static function getSuperClass( c : Class<Dynamic> ) : Class<Dynamic> untyped {
+		return c.__super__;
+	}
+
+
+	public static function getClassName( c : Class<Dynamic> ) : String {
+		var a : Array<String> = untyped c.__name__;
+		return a.join(".");
+	}
+
+	public static function getEnumName( e : Enum<Dynamic> ) : String {
+		var a : Array<String> = untyped e.__ename__;
+		return a.join(".");
+	}
+
+	public static function resolveClass( name : String ) : Class<Dynamic> untyped {
+		var cl : Class<Dynamic>;
+		try {
+			#if js_namespace
+			if (name.indexOf('.') < 0)
+				cl = eval(js.Boot.__ns + '.' + name);
+			else
+				cl = eval(name);
+			#else
+			cl = eval(name);
+			#end
+		} catch( e : Dynamic ) {
+			cl = null;
+		}
+		// ensure that this is a class
+		if( cl == null || cl.__name__ == null )
+			return null;
+		return cl;
+	}
+
+	public static function resolveEnum( name : String ) : Enum<Dynamic> untyped {
+		var e : Dynamic;
+		try {
+			#if js_namespace
+			if (name.indexOf('.') < 0)
+				e = eval(js.Boot.__ns + '.' + name);
+			else
+				e = eval(name);
+			#else
+			e = eval(name);
+			#end
+		} catch( err : Dynamic ) {
+			e = null;
+		}
+		// ensure that this is an enum
+		if( e == null || e.__ename__ == null )
+			return null;
+		return e;
+	}
+
+	public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T untyped {
+		if( args.length <= 3 )
+			return __new__(cl,args[0],args[1],args[2]);
+		if( args.length > 8 )
+			throw "Too many arguments";
+		return __new__(cl,args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
+	}
+
+	public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
+		return __new__(cl,__js__("$_"));
+	}
+
+	public static function createEnum<T>( e : Enum<T>, constr : String, ?params : Array<Dynamic> ) : T {
+		var f = Reflect.field(e,constr);
+		if( f == null ) throw "No such constructor "+constr;
+		if( Reflect.isFunction(f) ) {
+			if( params == null ) throw "Constructor "+constr+" need parameters";
+			return Reflect.callMethod(e,f,params);
+		}
+		if( params != null && params.length != 0 )
+			throw "Constructor "+constr+" does not need parameters";
+		return f;
+	}
+
+	public static function createEnumIndex<T>( e : Enum<T>, index : Int, ?params : Array<Dynamic> ) : T {
+		var c = Type.getEnumConstructs(e)[index];
+		if( c == null ) throw index+" is not a valid enum constructor index";
+		return createEnum(e,c,params);
+	}
+
+	public static function getInstanceFields( c : Class<Dynamic> ) : Array<String> {
+		var a = Reflect.fields(untyped c.prototype);
+		a.remove("__class__");
+		return a;
+	}
+
+	public static function getClassFields( c : Class<Dynamic> ) : Array<String> {
+		var a = Reflect.fields(c);
+		a.remove(__unprotect__("__name__"));
+		a.remove(__unprotect__("__interfaces__"));
+		a.remove(__unprotect__("__super__"));
+		a.remove("prototype");
+		return a;
+	}
+
+	public static function getEnumConstructs( e : Enum<Dynamic> ) : Array<String> untyped {
+		return untyped e.__constructs__;
+	}
+
+	public static function typeof( v : Dynamic ) : ValueType untyped {
+		switch( __js__("typeof")(v) ) {
+		case "boolean": return TBool;
+		case "string": return TClass(String);
+		case "number":
+			// this should handle all cases : NaN, +/-Inf and Floats outside range
+			if( Math.ceil(v) == v%2147483648.0 )
+				return TInt;
+			return TFloat;
+		case "object":
+			if( v == null )
+				return TNull;
+			var e = v.__enum__;
+			if( e != null )
+				return TEnum(e);
+			var c = v.__class__;
+			if( c != null )
+				return TClass(c);
+			return TObject;
+		case "function":
+			if( v.__name__ != null )
+				return TObject;
+			return TFunction;
+		case "undefined":
+			return TNull;
+		default:
+			return TUnknown;
+		}
+	}
+
+	public static function enumEq<T>( a : T, b : T ) : Bool untyped {
+		if( a == b )
+			return true;
+		try {
+			if( a[0] != b[0] )
+				return false;
+			for( i in 2...a.length )
+				if( !enumEq(a[i],b[i]) )
+					return false;
+			var e = a.__enum__;
+			if( e != b.__enum__ || e == null )
+				return false;
+		} catch( e : Dynamic ) {
+			return false;
+		}
+		return true;
+	}
+
+	public inline static function enumConstructor( e : Dynamic ) : String {
+		return e[0];
+	}
+
+	public inline static function enumParameters( e : Dynamic ) : Array<Dynamic> {
+		return e.slice(2);
+	}
+
+	public inline static function enumIndex( e : Dynamic ) : Int {
+		return e[1];
+	}
+
+}
+

+ 50 - 31
std/js/JsXml__.hx → std/js/_std/Xml.hx

@@ -22,10 +22,19 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  * DAMAGE.
  */
  */
-package js;
-import Xml;
 
 
-class JsXml__ {
+enum XmlType {
+}
+
+@:core_api class Xml {
+
+	public static var Element(default,null) : XmlType;
+	public static var PCData(default,null) : XmlType;
+	public static var CData(default,null) : XmlType;
+	public static var Comment(default,null) : XmlType;
+	public static var DocType(default,null) : XmlType;
+	public static var Prolog(default,null) : XmlType;
+	public static var Document(default,null) : XmlType;
 
 
 	static var enode = ~/^<([a-zA-Z0-9:_-]+)/;
 	static var enode = ~/^<([a-zA-Z0-9:_-]+)/;
 	static var ecdata = ~/^<!\[CDATA\[/i;
 	static var ecdata = ~/^<!\[CDATA\[/i;
@@ -44,13 +53,13 @@ class JsXml__ {
 	public var nodeType(default,null) : XmlType;
 	public var nodeType(default,null) : XmlType;
 	public var nodeName(getNodeName,setNodeName) : String;
 	public var nodeName(getNodeName,setNodeName) : String;
 	public var nodeValue(getNodeValue,setNodeValue) : String;
 	public var nodeValue(getNodeValue,setNodeValue) : String;
-	public var parent(getParent,null) : JsXml__;
+	public var parent(getParent,null) : Xml;
 
 
 	var _nodeName : String;
 	var _nodeName : String;
 	var _nodeValue : String;
 	var _nodeValue : String;
 	var _attributes : Hash<String>;
 	var _attributes : Hash<String>;
-	var _children : Array<JsXml__>;
-	var _parent : JsXml__;
+	var _children : Array<Xml>;
+	var _parent : Xml;
 
 
 	public static function parse( str : String ) : Xml {
 	public static function parse( str : String ) : Xml {
 		var rules = [enode,epcdata,eend,ecdata,edoctype,ecomment,eprolog];
 		var rules = [enode,epcdata,eend,ecdata,edoctype,ecomment,eprolog];
@@ -152,11 +161,11 @@ class JsXml__ {
 		untyped return current;
 		untyped return current;
 	}
 	}
 
 
-	private function new(){
+	private function new() : Void {
 	}
 	}
 
 
-	static function createElement( name : String ) : JsXml__ {
-		var r = new JsXml__();
+	public static function createElement( name : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Element;
 		r.nodeType = Xml.Element;
 		r._children = new Array();
 		r._children = new Array();
 		r._attributes = new Hash();
 		r._attributes = new Hash();
@@ -164,43 +173,43 @@ class JsXml__ {
 		return r;
 		return r;
 	}
 	}
 
 
-	static function createPCData( data : String ) : JsXml__ {
-		var r = new JsXml__();
+	public static function createPCData( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.PCData;
 		r.nodeType = Xml.PCData;
 		r.setNodeValue( data );
 		r.setNodeValue( data );
 		return r;
 		return r;
 	}
 	}
 
 
-	static function createCData( data : String ) : JsXml__ {
-		var r = new JsXml__();
+	public static function createCData( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.CData;
 		r.nodeType = Xml.CData;
 		r.setNodeValue( data );
 		r.setNodeValue( data );
 		return r;
 		return r;
 	}
 	}
 
 
-	static function createComment( data : String ) : JsXml__ {
-		var r = new JsXml__();
+	public static function createComment( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Comment;
 		r.nodeType = Xml.Comment;
 		r.setNodeValue( data );
 		r.setNodeValue( data );
 		return r;
 		return r;
 	}
 	}
 
 
-	static function createDocType( data : String ) : JsXml__ {
-		var r = new JsXml__();
+	public static function createDocType( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.DocType;
 		r.nodeType = Xml.DocType;
 		r.setNodeValue( data );
 		r.setNodeValue( data );
 		return r;
 		return r;
 	}
 	}
 
 
-	static function createProlog( data : String ) : JsXml__ {
-		var r = new JsXml__();
+	public static function createProlog( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Prolog;
 		r.nodeType = Xml.Prolog;
 		r.setNodeValue( data );
 		r.setNodeValue( data );
 		return r;
 		return r;
 	}
 	}
 
 
-	static function createDocument() : JsXml__ {
-		var r = new JsXml__();
+	public static function createDocument() : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Document;
 		r.nodeType = Xml.Document;
 		r._children = new Array();
 		r._children = new Array();
 		return r;
 		return r;
@@ -230,7 +239,7 @@ class JsXml__ {
 		return _nodeValue = v;
 		return _nodeValue = v;
 	}
 	}
 
 
-	private function getParent() {
+	private function getParent() : Xml {
 		return _parent;
 		return _parent;
 	}
 	}
 
 
@@ -264,7 +273,7 @@ class JsXml__ {
 		return _attributes.keys();
 		return _attributes.keys();
 	}
 	}
 
 
-	public function iterator() : Iterator<JsXml__> {
+	public function iterator() : Iterator<Xml> {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		return untyped {
 		return untyped {
 			cur: 0,
 			cur: 0,
@@ -278,7 +287,7 @@ class JsXml__ {
 		}
 		}
 	}
 	}
 
 
-	public function elements(){
+	public function elements() : Iterator<Xml> {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		return untyped {
 		return untyped {
 			cur: 0,
 			cur: 0,
@@ -310,7 +319,7 @@ class JsXml__ {
 		}
 		}
 	}
 	}
 
 
-	public function elementsNamed( name : String ) {
+	public function elementsNamed( name : String ) : Iterator<Xml> {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		return untyped {
 		return untyped {
 			cur: 0,
 			cur: 0,
@@ -343,12 +352,12 @@ class JsXml__ {
 		}
 		}
 	}
 	}
 
 
-	public function firstChild() : JsXml__ {
+	public function firstChild() : Xml {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		return _children[0];
 		return _children[0];
 	}
 	}
 
 
-	public function firstElement() : JsXml__ {
+	public function firstElement() : Xml {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		var cur = 0;
 		var cur = 0;
 		var l = _children.length;
 		var l = _children.length;
@@ -361,14 +370,14 @@ class JsXml__ {
 		return null;
 		return null;
 	}
 	}
 
 
-	public function addChild( x : JsXml__ ) : Void {
+	public function addChild( x : Xml ) : Void {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		if( x._parent != null ) x._parent._children.remove(x);
 		if( x._parent != null ) x._parent._children.remove(x);
 		x._parent = this;
 		x._parent = this;
 		_children.push( x );
 		_children.push( x );
 	}
 	}
 
 
-	public function removeChild( x : JsXml__ ) : Bool {
+	public function removeChild( x : Xml ) : Bool {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		var b = _children.remove( x );
 		var b = _children.remove( x );
 		if( b )
 		if( b )
@@ -376,14 +385,14 @@ class JsXml__ {
 		return b;
 		return b;
 	}
 	}
 
 
-	public function insertChild( x : JsXml__, pos : Int ) : Void {
+	public function insertChild( x : Xml, pos : Int ) : Void {
 		if( _children == null ) throw "bad nodetype";
 		if( _children == null ) throw "bad nodetype";
 		if( x._parent != null ) x._parent._children.remove(x);
 		if( x._parent != null ) x._parent._children.remove(x);
 		x._parent = this;
 		x._parent = this;
 		_children.insert( pos, x );
 		_children.insert( pos, x );
 	}
 	}
 
 
-	public function toString() {
+	public function toString() : String {
 		if( nodeType == Xml.PCData )
 		if( nodeType == Xml.PCData )
 			return _nodeValue;
 			return _nodeValue;
 		if( nodeType == Xml.CData )
 		if( nodeType == Xml.CData )
@@ -424,4 +433,14 @@ class JsXml__ {
 		return s.toString();
 		return s.toString();
 	}
 	}
 
 
+	static function __init__() : Void untyped {
+		Xml.Element = "element";
+		Xml.PCData = "pcdata";
+		Xml.CData = "cdata";
+		Xml.Comment = "comment";
+		Xml.DocType = "doctype";
+		Xml.Prolog = "prolog";
+		Xml.Document = "document";
+	}
+
 }
 }