File: Closure.hx

package info (click to toggle)
haxe 1%3A4.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,556 kB
  • sloc: ml: 125,171; ansic: 2,408; makefile: 436; java: 362; cs: 323; cpp: 318; python: 316; sh: 75; objc: 64; php: 39; xml: 30; javascript: 11
file content (65 lines) | stat: -rw-r--r-- 1,611 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package jvm;

import java.NativeArray;
import java.lang.reflect.Method;

@:native("haxe.jvm.Closure")
@:nativeGen
@:keep
class Closure extends ClosureDispatch {
	public var context:Dynamic;
	public var method:Method;

	var isStatic:Bool;
	var params:NativeArray<java.lang.Class<Dynamic>>;

	public function new(context:Null<Dynamic>, method:Method) {
		super();
		this.context = context;
		this.method = method;
		isStatic = method.getModifiers() & java.lang.reflect.Modifier.STATIC != 0;
		params = method.getParameterTypes();
	}

	public function bindTo(context:Dynamic) {
		return new Closure(context, method);
	}

	override public function equals(other:java.lang.Object) {
		if (!Jvm.instanceof(other, Closure)) {
			return false;
		}
		var other:Closure = cast other;
		return context == other.context && method == other.method;
	}

	public override function invokeDynamic(args:NativeArray<Dynamic>):Dynamic {
		if (isStatic && context != null) {
			var newArgs = new NativeArray(args.length + 1);
			haxe.ds.Vector.blit(cast args, 0, cast newArgs, 1, args.length);
			newArgs[0] = context;
			args = newArgs;
		}
		var args = switch (jvm.Jvm.unifyCallArguments(args, params, true)) {
			case Some(args):
				args;
			case None:
				args;
		};
		try {
			return method.invoke(context, args);
		} catch (e:java.lang.reflect.InvocationTargetException) {
			throw e.getCause();
		}
	}
}

@:native("haxe.jvm.ClosureDispatch")
extern class ClosureDispatch extends Function {}

@:native("haxe.jvm.VarArgs")
extern class VarArgs extends Function {
	var func:Function;

	public function new(func:Function):Void;
}