浏览代码

some core classes extractions for "flash" platform

Nicolas Cannasse 15 年之前
父节点
当前提交
7c37bf15ad
共有 5 个文件被更改,包括 527 次插入0 次删除
  1. 81 0
      std/flash/_std/Hash.hx
  2. 83 0
      std/flash/_std/IntHash.hx
  3. 91 0
      std/flash/_std/Reflect.hx
  4. 73 0
      std/flash/_std/Std.hx
  5. 199 0
      std/flash/_std/Type.hx

+ 81 - 0
std/flash/_std/Hash.hx

@@ -0,0 +1,81 @@
+/*
+ * 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 {
+		h = untyped __new__(_global["Object"]);
+	}
+
+	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 {
+		return untyped h["hasOwnProperty"]("$"+key);
+	}
+
+	public function remove( key : String ) : Bool {
+		key = "$"+key;
+		if( untyped !h["hasOwnProperty"](key) ) return false;
+		untyped __delete__(h,key);
+		return true;
+	}
+
+	public function keys() : Iterator<String> {
+		return untyped (__hkeys__(h))["iterator"]();
+	}
+
+	public function iterator() : Iterator<T> {
+		return untyped {
+			ref : h,
+			it : __keys__(h)["iterator"](),
+			hasNext : function() { return this.it[__unprotect__("hasNext")](); },
+			next : function() { var i = this.it[__unprotect__("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();
+	}
+
+}

+ 83 - 0
std/flash/_std/IntHash.hx

@@ -0,0 +1,83 @@
+/*
+ * 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 __new__(_global["Object"]);
+	}
+
+	public function set( key : Int, value : T ) : Void {
+		h[key] = value;
+	}
+
+	public function get( key : Int ) : Null<T> {
+		return h[key];
+	}
+
+	public function exists( key : Int ) : Bool {
+		return untyped h["hasOwnProperty"](key);
+	}
+
+	public function remove( key : Int ) : Bool {
+		if( untyped !h["hasOwnProperty"](key) ) return false;
+		untyped __delete__(h,key);
+		return true;
+	}
+
+	public function keys() : Iterator<Int> {
+		var l : Array<Int> = untyped __keys__(h);
+		for( x in 0...l.length )
+			l[x] = Std.int(l[x]);
+		return l.iterator();
+	}
+
+	public function iterator() : Iterator<T> {
+		return untyped {
+			ref : h,
+			it : keys(),
+			hasNext : function() { return this.it[__unprotect__("hasNext")](); },
+			next : function() { var i = this.it[__unprotect__("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();
+	}
+
+}

+ 91 - 0
std/flash/_std/Reflect.hx

@@ -0,0 +1,91 @@
+/*
+ * 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 inline static function hasField( o : Dynamic, field : String ) : Bool untyped {
+		return this["hasOwnProperty"]["call"](o,field);
+	}
+
+	public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
+		return o[field];
+	}
+
+	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 : Array<String> = __keys__(o);
+		var i = 0;
+		while( i < a.length ) {
+			if( !a["hasOwnProperty"]["call"](o,a[i]) )
+				a.splice(i,1);
+			else
+				++i;
+		}
+		return a;
+	}
+
+	public static function isFunction( f : Dynamic ) : Bool untyped {
+		return __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 untyped f1["f"] == f2["f"] && f1["o"] == f2["o"] && f1["f"] != null;
+	}
+
+	public static function isObject( v : Dynamic ) : Bool untyped {
+		var t = __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( this["hasOwnProperty"]["call"](o,f) != true ) return false;
+		__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() { return f(untyped __arguments__); };
+	}
+
+}

+ 73 - 0
std/flash/_std/Std.hx

@@ -0,0 +1,73 @@
+/*
+ * 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 flash.Boot.__instanceof(v,t);
+	}
+
+	public static function string( s : Dynamic ) : String {
+		return untyped flash.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> untyped {
+		var v = _global["parseInt"](x);
+		if( Math.isNaN(v) )
+			return null;
+		return v;
+	}
+
+	public static function parseFloat( x : String ) : Float {
+		return untyped _global["parseFloat"](x);
+	}
+
+	public static function random( x : Int ) : Int {
+		return untyped __random__(x);
+	}
+
+	static function __init__() : Void untyped {
+		var g : Dynamic = _global;
+		g["Int"] = { __name__ : ["Int"] };
+		g["Bool"] = { __ename__ : ["Bool"] };
+		g.Dynamic = { __name__ : [__unprotect__("Dynamic")] };
+		g.Class = { __name__ : [__unprotect__("Class")] };
+		g.Enum = {};
+		g.Void = { __ename__ : [__unprotect__("Void")] };
+		g["Float"] = _global["Number"];
+		g["Float"][__unprotect__("__name__")] = ["Float"];
+		Array.prototype[__unprotect__("__class__")] = Array;
+		Array[__unprotect__("__name__")] = ["Array"];
+		String.prototype[__unprotect__("__class__")] = String;
+		String[__unprotect__("__name__")] = ["String"];
+		g["ASSetPropFlags"](Array.prototype,null,7);
+	}
+
+}

+ 199 - 0
std/flash/_std/Type.hx

@@ -0,0 +1,199 @@
+/*
+ * 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.__enum__ != null )
+			return null;
+		return o.__class__;
+	}
+
+	public static function getEnum( o : Dynamic ) : Enum<Dynamic> untyped {
+		return o.__enum__;
+	}
+
+	public static function getSuperClass( c : Class<Dynamic> ) : Class<Dynamic> untyped {
+		return c.__super__;
+	}
+
+
+	public static function getClassName( c : Class<Dynamic> ) : String {
+		if( c == null )
+			return null;
+		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> = untyped __eval__(name);
+		// 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 : Enum<Dynamic> = untyped __eval__(name);
+		// 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( cl == Array ) return new Array();
+		var o = { __constructor__ : cl, __proto__ : cl.prototype };
+		cl["apply"](o,args);
+		return o;
+	}
+
+	public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
+		if( cl == Array ) return new Array();
+		var o : Dynamic = __new__(_global["Object"]);
+		o.__proto__ = cl.prototype;
+		return o;
+	}
+
+	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__");
+		c = untyped c.__super__;
+		while( c != null ) {
+			for( f in Reflect.fields(untyped c.prototype) ) {
+				a.remove(f);
+				a.push(f);
+			}
+			c = untyped c.__super__;
+		}
+		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__"));
+		return a;
+	}
+
+	public static function getEnumConstructs( e : Enum<Dynamic> ) : Array<String> untyped {
+		return untyped e.__constructs__;
+	}
+
+	public static function typeof( v : Dynamic ) : ValueType {
+		switch( untyped __typeof__(v) ) {
+		case "null": return TNull;
+		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":
+			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;
+		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;
+		return true;
+	}
+
+	public static function enumConstructor( e : Dynamic ) : String {
+		return e[0];
+	}
+
+	public static function enumParameters( e : Dynamic ) : Array<Dynamic> {
+		return e.slice(2);
+	}
+
+	public inline static function enumIndex( e : Dynamic ) : Int {
+		return e[1];
+	}
+
+}
+