22
33import org .develnext .jphp .ext .system .DFFIExtension ;
44import php .runtime .Memory ;
5- import php .runtime .annotation .Reflection ;
5+ import php .runtime .annotation .Reflection .Namespace ;
6+ import php .runtime .annotation .Reflection .Signature ;
67import php .runtime .memory .*;
78import php .runtime .env .Environment ;
89import php .runtime .env .TraceInfo ;
910import php .runtime .lang .BaseObject ;
1011import php .runtime .reflection .ClassEntity ;
1112
12- import javafx .stage .Stage ;
13- import com .sun .javafx .tk .TKStage ;
14- import java .lang .reflect .Method ;
15-
1613import java .awt .*;
14+ import java .util .LinkedHashMap ;
15+ import java .util .Map ;
16+
1717import com .sun .jna .*;
1818
19- @ Reflection .Name ("DFFI" )
20- @ Reflection .Namespace (DFFIExtension .NS )
19+ @ Namespace (DFFIExtension .NS )
2120public class DFFI extends BaseObject {
21+ public String libName ;
22+ public static Map <String , ArrayMemory > functions = new LinkedHashMap <>();
23+ public Map <String , ArrayMemory > pfunctions = new LinkedHashMap <>();
24+
25+ public DFFI (Environment env , ClassEntity clazz ) {
26+ super (env , clazz );
27+ }
28+
29+ @ Signature
30+ public void __construct (String lib ) {
31+ this .libName = lib ;
32+ }
33+
34+ @ Signature
35+ public void bind (String functionName , String returnType , ArrayMemory _types ) {
36+ ArrayMemory array = new ArrayMemory ();
37+
38+ array .refOfIndex ("lib" ).assign (libName );
39+ array .refOfIndex ("returnType" ).assign (returnType );
40+ array .refOfIndex ("types" ).assign (_types .toImmutable ());
41+
42+ functions .put (functionName , array );
43+ pfunctions .put (functionName , array );
44+ }
45+
46+ @ Signature
47+ public static Memory __callStatic (Environment env , TraceInfo trace , String functionName , Memory ... args ) throws ClassNotFoundException , AWTException {
48+ Memory returnValue = Memory .NULL ;
49+ ArrayMemory function = functions .get (functionName );
2250
23- public String libName ;
24- public static ArrayMemory functions = new ArrayMemory ();
25- public ArrayMemory pfunctions = new ArrayMemory ();
26-
27- public DFFI (Environment env , ClassEntity clazz )
28- {
29- super (env , clazz );
30- }
31-
32- @ Reflection .Signature
33- public void __construct (String lib )
34- {
35- this .libName = lib ;
36- }
37-
38- @ Reflection .Signature
39- public void bind (String functionName , String returnType , Memory _types )
40- {
41- ArrayMemory types = (ArrayMemory )_types .toValue (ArrayMemory .class );
42-
43- ArrayMemory array = new ArrayMemory ();
44- array .refOfIndex ("lib" ).assign (libName );
45- array .refOfIndex ("returnType" ).assign (returnType );
46- array .refOfIndex ("types" ).assign (types );
47-
48- functions .refOfIndex (functionName ).assign (array );
49- pfunctions .refOfIndex (functionName ).assign (array );
50- }
51-
52- @ Reflection .Signature
53- public static Memory __callStatic (Environment env , TraceInfo trace , String functionName , Memory ... args ) throws AWTException , Exception
54- {
55- Memory returnValue = Memory .NULL ;
56- Memory _function = functions .valueOfIndex (functionName );
57- if (_function != Memory .UNDEFINED ){
58- ArrayMemory function = _function .toValue (ArrayMemory .class );
59- String lib = function .valueOfIndex ("lib" ).toString ();
60- String returnType = function .valueOfIndex ("returnType" ).toString ();
61- Memory types = function .valueOfIndex ("types" );
62-
63- returnValue = Helper .callFunction (env , lib , returnType , functionName , types , args );
64- }
65-
66- return returnValue ;
67- }
68-
69- @ Reflection .Signature
70- public Memory __call (Environment env , TraceInfo trace , String functionName , Memory ... args ) throws AWTException , Exception
71- {
72- Memory returnValue = Memory .NULL ;
73- Memory _function = functions .valueOfIndex (functionName );
74- if (_function != Memory .UNDEFINED ){
75- ArrayMemory pfunction = _function .toValue (ArrayMemory .class );
76- String lib = pfunction .valueOfIndex ("lib" ).toString ();
77- String returnType = pfunction .valueOfIndex ("returnType" ).toString ();
78- Memory types = pfunction .valueOfIndex ("types" );
79-
80- returnValue = Helper .callFunction (env , lib , returnType , functionName , types , args );
81- }
82-
83- return returnValue ;
84- }
85-
86- @ Reflection .Signature
87- public static void addSearchPath (String lib , String path ) throws AWTException
88- {
89- NativeLibrary .addSearchPath (lib , path );
90- }
91-
92- @ Reflection .Signature
93- public static Long getJFXHandle (Object window )
94- {
95- try {
96- Stage stage = (Stage ) window ;
97-
98- TKStage tkStage = stage .impl_getPeer ();
99- Method getPlatformWindow = tkStage .getClass ().getDeclaredMethod ("getPlatformWindow" );
100- getPlatformWindow .setAccessible (true );
101- Object platformWindow = getPlatformWindow .invoke (tkStage );
102- Method getNativeHandle = platformWindow .getClass ().getMethod ( "getNativeHandle" );
103- getNativeHandle .setAccessible (true );
104- Object nativeHandle = getNativeHandle .invoke (platformWindow );
105- return (Long ) nativeHandle ;
106- } catch (Throwable e ) {
107- return null ;
51+ if (function != null ) {
52+ String lib = function .valueOfIndex ("lib" ).toString ();
53+ String returnType = function .valueOfIndex ("returnType" ).toString ();
54+ Memory types = function .valueOfIndex ("types" );
55+
56+ returnValue = Helper .callFunction (env , lib , returnType , functionName , types , args );
57+ }
58+
59+ return returnValue ;
60+ }
61+
62+ @ Signature
63+ public Memory __call (Environment env , TraceInfo trace , String functionName , Memory ... args ) throws ClassNotFoundException , AWTException {
64+ Memory returnValue = Memory .NULL ;
65+ ArrayMemory pfunction = pfunctions .get (functionName );
66+
67+ if (pfunction != Memory .UNDEFINED ) {
68+ String lib = pfunction .valueOfIndex ("lib" ).toString ();
69+ String returnType = pfunction .valueOfIndex ("returnType" ).toString ();
70+ Memory types = pfunction .valueOfIndex ("types" );
71+
72+ returnValue = Helper .callFunction (env , lib , returnType , functionName , types , args );
10873 }
109- }
110-
74+
75+ return returnValue ;
76+ }
77+
78+ @ Signature
79+ public static void addSearchPath (String lib , String path ) {
80+ NativeLibrary .addSearchPath (lib , path );
81+ }
11182}
0 commit comments