Browse Source

Optimize some places by using Object.prototype.hasOwnProperty, rather than
calling an unknown object's hasOwnProperty and having to provide a fallback
if things go wrong with it.

Feel free to revert if I'm wrong or the previous behavior is intended.

Bruno Garcia 13 years ago
parent
commit
3d90efb7b7
2 changed files with 13 additions and 30 deletions
  1. 4 10
      std/js/_std/Hash.hx
  2. 9 20
      std/js/_std/Reflect.hx

+ 4 - 10
std/js/_std/Hash.hx

@@ -40,19 +40,13 @@
 	}
 
 	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;
-		}
+		return untyped Object.prototype.hasOwnProperty.call(h,"$"+key);
 	}
 
 	public function remove( key : String ) : Bool {
-		if( !exists(key) )
-			return false;
-		untyped __js__("delete")(h["$"+key]);
+		key = "$"+key;
+		if( untyped !Object.prototype.hasOwnProperty.call(h,key) ) return false;
+		untyped __js__("delete")(h[key]);
 		return true;
 	}
 

+ 9 - 20
std/js/_std/Reflect.hx

@@ -25,13 +25,8 @@
 
 @: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 static function hasField( o : Dynamic, field : String ) : Bool {
+		return untyped Object.prototype.hasOwnProperty.call(o, field);
 	}
 
 	public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
@@ -61,19 +56,13 @@
 		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;
+	public static function fields( o : Dynamic ) : Array<String> {
+		var a = [];
+		if (o != null) untyped {
+			var hasOwnProperty = Object.prototype.hasOwnProperty;
+			__js__("for( var f in o ) {");
+			if( hasOwnProperty.call(o, f) ) a.push(f);
+			__js__("}");
 		}
 		return a;
 	}