Ver Fonte

swap sub closure object for wrapper functions (close https://github.com/HaxeFoundation/hashlink/issues/143)

Nicolas Cannasse há 7 anos atrás
pai
commit
ba58d874ab
2 ficheiros alterados com 9 adições e 1 exclusões
  1. 1 0
      std/hl/Api.hx
  2. 8 1
      std/hl/_std/Reflect.hx

+ 1 - 0
std/hl/Api.hx

@@ -30,6 +30,7 @@ extern class Api {
 	@:hlNative("std","obj_delete_field") static function deleteField( obj : Dynamic, hash : Int ) : Bool;
 	@:hlNative("std","call_method") static function callMethod( f : haxe.Constraints.Function, args : NativeArray<Dynamic> ) : Dynamic;
 	@:hlNative("std","get_closure_value") static function getClosureValue( f : haxe.Constraints.Function ) : Dynamic;
+	@:hlNative("std","make_closure") static function makeClosure( f : haxe.Constraints.Function, v : Dynamic ) : Dynamic;
 	@:hlNative("std","no_closure") static function noClosure( f : haxe.Constraints.Function ) : haxe.Constraints.Function;
 	@:hlNative("std", "value_cast") static function safeCast( v : Dynamic, t : Type ) : Dynamic;
 	@:hlNative("std", "make_var_args") static function makeVarArgs( v : NativeArray<Dynamic> -> Dynamic ) : haxe.Constraints.Function;

+ 8 - 1
std/hl/_std/Reflect.hx

@@ -63,11 +63,18 @@ class Reflect {
 		if( ft.kind != HFun )
 			throw "Invalid function " + func;
 		var need = ft.getArgsCount();
-		var cval = hl.Api.getClosureValue(func);
+		var cval : Dynamic = hl.Api.getClosureValue(func);
 		var isClosure = cval != null && need >= 0;
 		if( isClosure ) {
 			if( o == null )
 				o = cval;
+			else if( isFunction(cval) ) {
+				// swap the sub object for closure wrapping
+				// see hashlink/#143
+				var subO = hl.Api.getClosureValue(cval);
+				if( subO != null )
+					o = hl.Api.makeClosure(cval, o);
+			}
 		} else {
 			if( count == need )
 				o = null;