Skip to content

Commit 6b561fa

Browse files
committed
Remove jfx & awt dependencies, upgrade jna to 5.5, add DFFIConsole util class.
1 parent c960f49 commit 6b561fa

File tree

9 files changed

+499
-416
lines changed

9 files changed

+499
-416
lines changed

package.php.yml

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: jphp-dffi-ext
2-
version: 1.0.1
2+
version: 2.0.0
33
description: DevelNext Foreign Function Interface
44

55
plugins:
@@ -9,30 +9,23 @@ plugins:
99

1010
deps:
1111
jphp-runtime: '*'
12-
jphp-zend-ext: '*'
13-
14-
devDeps:
15-
dn-bundle-plugin: '*'
16-
17-
develnext-bundle:
18-
version: 1.0.1
12+
13+
ide-bundle: # for DN 17+
1914
name: DFFI
20-
author: HackMemory
21-
icon: "develnext/bundle/dffi/icon32.png"
22-
description: "DevelNext Foreign Function Interface"
23-
group: "system"
24-
class: "develnext\\bundle\\dffi\\DFFIBundle"
15+
author: HackMemory & Dmitriy Zayceff
16+
class: develnext\bundle\dffi\DFFIBundle
17+
icon: develnext/bundle/dffi/icon32.png
18+
description: DevelNext Foreign Function Interface.
19+
platforms: [desktop]
20+
group: system
21+
#sources: ['src']
2522

2623
sources:
2724
- src
2825

29-
hub:
30-
31-
3226
gradle:
3327
deps:
34-
- net.java.dev.jna:jna:4.5.0
35-
28+
- 'net.java.dev.jna:jna-platform:5.5.0'
3629

3730
config:
3831
ignore:

sdk/system/DFFIConsole.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace system;
3+
4+
/**
5+
* System utils for os console
6+
*
7+
* Class DFFIConsole
8+
* @package system
9+
*/
10+
class DFFIConsole
11+
{
12+
/**
13+
* Enable colors in console (for Windows)
14+
* @return bool
15+
*/
16+
public function enableColors(): bool
17+
{
18+
}
19+
}

src-jvm/main/java/org/develnext/jphp/ext/system/DFFIExtension.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package org.develnext.jphp.ext.system;
22

3-
import org.develnext.jphp.ext.system.classes.DFFI;
4-
import org.develnext.jphp.ext.system.classes.DFFIType;
5-
import org.develnext.jphp.ext.system.classes.DFFIStruct;
6-
import org.develnext.jphp.ext.system.classes.DFFIReferenceValue;
3+
import org.develnext.jphp.ext.system.classes.*;
74
import php.runtime.env.CompileScope;
85
import php.runtime.ext.support.Extension;
9-
import php.runtime.ext.support.Extension.Status;
106

117
public class DFFIExtension extends Extension
128
{
@@ -30,5 +26,6 @@ public void onRegister(CompileScope scope)
3026
registerClass(scope, DFFIType.class);
3127
registerClass(scope, DFFIStruct.class);
3228
registerClass(scope, DFFIReferenceValue.class);
29+
registerClass(scope, DFFIConsole.class);
3330
}
3431
}

src-jvm/main/java/org/develnext/jphp/ext/system/classes/DFFI.java

Lines changed: 65 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,81 @@
22

33
import org.develnext.jphp.ext.system.DFFIExtension;
44
import php.runtime.Memory;
5-
import php.runtime.annotation.Reflection;
5+
import php.runtime.annotation.Reflection.Namespace;
6+
import php.runtime.annotation.Reflection.Signature;
67
import php.runtime.memory.*;
78
import php.runtime.env.Environment;
89
import php.runtime.env.TraceInfo;
910
import php.runtime.lang.BaseObject;
1011
import php.runtime.reflection.ClassEntity;
1112

12-
import javafx.stage.Stage;
13-
import com.sun.javafx.tk.TKStage;
14-
import java.lang.reflect.Method;
15-
1613
import java.awt.*;
14+
import java.util.LinkedHashMap;
15+
import java.util.Map;
16+
1717
import com.sun.jna.*;
1818

19-
@Reflection.Name("DFFI")
20-
@Reflection.Namespace(DFFIExtension.NS)
19+
@Namespace(DFFIExtension.NS)
2120
public 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
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.develnext.jphp.ext.system.classes;
2+
3+
import com.sun.jna.Function;
4+
import com.sun.jna.platform.win32.WinDef;
5+
import com.sun.jna.platform.win32.WinNT;
6+
import org.develnext.jphp.ext.system.DFFIExtension;
7+
import php.runtime.annotation.Reflection;
8+
import php.runtime.annotation.Reflection.Signature;
9+
import php.runtime.env.Environment;
10+
import php.runtime.lang.BaseObject;
11+
import php.runtime.reflection.ClassEntity;
12+
13+
@Reflection.Namespace(DFFIExtension.NS)
14+
public class DFFIConsole extends BaseObject {
15+
public DFFIConsole(Environment env) {
16+
super(env);
17+
}
18+
19+
protected DFFIConsole(ClassEntity entity) {
20+
super(entity);
21+
}
22+
23+
public DFFIConsole(Environment env, ClassEntity clazz) {
24+
super(env, clazz);
25+
}
26+
27+
@Signature
28+
public boolean enableColors() {
29+
String osName = System.getProperty("os.name");
30+
31+
if (osName.equalsIgnoreCase("windows")) {
32+
try {
33+
float osVersion = Float.parseFloat(System.getProperty("os.version"));
34+
if (osVersion >= 10.0f) {
35+
enableColorsForWindows();
36+
return true;
37+
}
38+
39+
return false;
40+
} catch (NumberFormatException e) {
41+
return false;
42+
}
43+
}
44+
45+
return true;
46+
}
47+
48+
protected void enableColorsForWindows() {
49+
// Set output mode to handle virtual terminal sequences
50+
Function GetStdHandleFunc = Function.getFunction("kernel32", "GetStdHandle");
51+
WinDef.DWORD STD_OUTPUT_HANDLE = new WinDef.DWORD(-11);
52+
WinNT.HANDLE hOut = (WinNT.HANDLE) GetStdHandleFunc.invoke(WinNT.HANDLE.class, new Object[]{STD_OUTPUT_HANDLE});
53+
54+
WinDef.DWORDByReference p_dwMode = new WinDef.DWORDByReference(new WinDef.DWORD(0));
55+
Function GetConsoleModeFunc = Function.getFunction("kernel32", "GetConsoleMode");
56+
GetConsoleModeFunc.invoke(WinDef.BOOL.class, new Object[]{hOut, p_dwMode});
57+
58+
int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
59+
WinDef.DWORD dwMode = p_dwMode.getValue();
60+
dwMode.setValue(dwMode.intValue() | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
61+
Function SetConsoleModeFunc = Function.getFunction("kernel32", "SetConsoleMode");
62+
SetConsoleModeFunc.invoke(WinDef.BOOL.class, new Object[]{hOut, dwMode});
63+
}
64+
}

src-jvm/main/java/org/develnext/jphp/ext/system/classes/DFFIReferenceValue.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@
55
import php.runtime.annotation.Reflection;
66
import php.runtime.annotation.Reflection.Signature;
77
import php.runtime.env.Environment;
8-
import php.runtime.env.TraceInfo;
98
import php.runtime.lang.BaseObject;
10-
import php.runtime.lang.BaseWrapper;
119
import php.runtime.reflection.ClassEntity;
12-
import php.runtime.memory.ArrayMemory;
1310

1411
import com.sun.jna.ptr.*;
1512
import com.sun.jna.*;
16-
import java.awt.*;
17-
import java.util.*;
18-
import java.lang.reflect.Field;
1913

2014
@Reflection.Name("DFFIReferenceValue")
2115
@Reflection.Namespace(DFFIExtension.NS)
@@ -48,12 +42,12 @@ public void __construct(Environment env, String _type, Memory value) {
4842
}*/
4943

5044
@Signature
51-
public void setValue(Environment env, Memory value) throws AWTException {
45+
public void setValue(Environment env, Memory value) {
5246
this.refval = Helper.SetValueToReference(this.refval, Memory.unwrap(env, value));
5347
}
5448

5549
@Signature
56-
public Memory getValue() throws AWTException, ClassNotFoundException {
50+
public Memory getValue() throws ClassNotFoundException {
5751
if(this.refval != null){
5852
return Helper.ConvertObjectToMemory(this.type, Helper.ConvertReferenceToObject(this.refval));
5953
}

0 commit comments

Comments
 (0)