Browse Source

Use plain method closure comparison for JS. (#10553)

* Make example js generator guarantee method equality.

* Method closures are equal on JS.
Juraj Kirchheim 3 years ago
parent
commit
5825e121d0
2 changed files with 9 additions and 7 deletions
  1. 7 1
      std/haxe/macro/ExampleJSGenerator.hx
  2. 2 6
      std/js/_std/Reflect.hx

+ 7 - 1
std/haxe/macro/ExampleJSGenerator.hx

@@ -231,7 +231,13 @@ class ExampleJSGenerator {
 	public function generate() {
 		print("var $_, $hxClasses = $hxClasses || {}, $estr = function() { return js.Boot.__string_rec(this,''); }");
 		newline();
-		print("function $bind(o,m) { var f = function(){ return f.method.apply(f.scope, arguments); }; f.scope = o; f.method = m; return f; };");
+		print(
+			#if (js_es < 5)
+				"function $bind(o,m) { if( m == null ) return null; if( m.__id__ == null ) m.__id__ = $global.$haxeUID++; var f; if( o.hx__closures__ == null ) o.hx__closures__ = {}; else f = o.hx__closures__[m.__id__]; if( f == null ) { f = function(){ return f.method.apply(f.scope, arguments); }; f.scope = o; f.method = m; o.hx__closures__[m.__id__] = f; } return f; }"
+			#else
+				"function $bind(o,m) { if( m == null ) return null; if( m.__id__ == null ) m.__id__ = $global.$haxeUID++; var f; if( o.hx__closures__ == null ) o.hx__closures__ = {}; else f = o.hx__closures__[m.__id__]; if( f == null ) { f = m.bind(o); o.hx__closures__[m.__id__] = f; } return f; }"
+			#end
+		);
 		newline();
 		for (t in api.types)
 			genType(t);

+ 2 - 6
std/js/_std/Reflect.hx

@@ -79,12 +79,8 @@
 		return (a == b) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
 	}
 
-	public static function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
-		if (f1 == f2)
-			return true;
-		if (!isFunction(f1) || !isFunction(f2))
-			return false;
-		return f1.scope == f2.scope && f1.method == f2.method && f1.method != null;
+	public static inline function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
+		return f1 == f2;
 	}
 
 	@:access(js.Boot)