Skip to content

Commit

Permalink
Add callback test
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Aug 22, 2024
1 parent 437218d commit d5a3b2b
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.xpenatan.jparser.example.app;

import com.github.xpenatan.jparser.example.testlib.CallbackClassManual;
import com.github.xpenatan.jparser.example.testlib.TestCallbackClass;
import com.github.xpenatan.jparser.example.testlib.TestConstructorClass;
import com.github.xpenatan.jparser.example.testlib.TestMethodClass;
import com.github.xpenatan.jparser.example.testlib.TestObjectClass;
Expand All @@ -17,6 +19,7 @@ public static boolean test() {
boolean methodTest = testMethodClass();
boolean staticMethodTest = testStaticMethodClass();
boolean callbackTest = testCallbackClass();
boolean callbackTestManual = testCallbackClassManual();

System.out.println("constructorTest: " + constructorTest);
System.out.println("stringConstructorTest: " + stringConstructorTest);
Expand All @@ -27,7 +30,7 @@ public static boolean test() {
System.out.println("staticMethodTest: " + staticMethodTest);
System.out.println("callbackTest: " + callbackTest);
return constructorTest && stringConstructorTest && attributeTest && staticAttributeTest
&& attributeArrayTest && methodTest && staticMethodTest && callbackTest;
&& attributeArrayTest && methodTest && staticMethodTest && callbackTest && callbackTestManual;
}

private static boolean testConstructorClass() {
Expand Down Expand Up @@ -220,8 +223,9 @@ private static boolean testMethodClass() {
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}

test.dispose();
}
{
TestMethodClass test = new TestMethodClass();
Expand All @@ -236,8 +240,9 @@ private static boolean testMethodClass() {
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}

test.dispose();
}
return true;
}
Expand All @@ -249,8 +254,121 @@ private static boolean testStaticMethodClass() {
}

private static boolean testCallbackClass() {
return true;
}

private static boolean testCallbackClassManual() {

{
TestCallbackClass test = new TestCallbackClass();
try {
boolean[] internal_onVoidCallback = { false };
CallbackClassManual callback = new CallbackClassManual() {
@Override
public void internal_onVoidCallback(long refData, long pointerData) {
internal_onVoidCallback[0] = true;
}
};
test.callVoidCallback(callback);
if(!(internal_onVoidCallback[0] == true)) {
throw new RuntimeException("internal_onVoidCallback[0] == true");
}
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}
test.dispose();
}
{
TestCallbackClass test = new TestCallbackClass();
try {
boolean[] internal_onIntCallback = { false };
CallbackClassManual callback = new CallbackClassManual() {
@Override
public int internal_onIntCallback(int intValue01, int intValue02) {
internal_onIntCallback[0] = true;
return 0;
}
};
test.callIntCallback(callback);
if(!(internal_onIntCallback[0] == true)) {
throw new RuntimeException("internal_onIntCallback[0] == true");
}
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}
test.dispose();
}
{
TestCallbackClass test = new TestCallbackClass();
try {
boolean[] internal_onFloatCallback = { false };
CallbackClassManual callback = new CallbackClassManual() {
@Override
public float internal_onFloatCallback(float floatValue01, float floatValue02) {
internal_onFloatCallback[0] = true;
return 0;
}
};
test.callFloatCallback(callback);
if(!(internal_onFloatCallback[0] == true)) {
throw new RuntimeException("internal_onFloatCallback[0] == true");
}
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}
test.dispose();
}
{
TestCallbackClass test = new TestCallbackClass();
try {
boolean[] internal_onBoolCallback = { false };
CallbackClassManual callback = new CallbackClassManual() {
@Override
public boolean internal_onBoolCallback(boolean boolValue01) {
internal_onBoolCallback[0] = true;
return false;
}
};
test.callBoolCallback(callback);
if(!(internal_onBoolCallback[0] == true)) {
throw new RuntimeException("internal_onBoolCallback[0] == true");
}
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}
test.dispose();
}
{
TestCallbackClass test = new TestCallbackClass();
try {
String text = "HELLO_WORLD";
test.get_strValue01().append(text);
final String[] internal_onStringCallback = new String[1];
CallbackClassManual callback = new CallbackClassManual() {
@Override
public void internal_onStringCallback(String strValue01) {
internal_onStringCallback[0] = strValue01;
}
};
test.callStringCallback(callback);
if(!(text.equals(internal_onStringCallback[0]) == true)) {
throw new RuntimeException("text.equals(internal_onStringCallback[0]) == true");
}
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}
test.dispose();
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.xpenatan.jparser.example.testlib;

import idl.IDLBase;

public class CallbackClass extends IDLBase {

public CallbackClass(byte b, char c) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.github.xpenatan.jparser.example.testlib;
import idl.IDLBase;

public class CallbackClassManual extends CallbackClass {

/*[-JNI;-NATIVE]
static jclass CallbackClassManual_CLASS = 0;
static jmethodID onVoidCallback_ID = 0;
static jmethodID onIntCallback_ID = 0;
static jmethodID onFloatCallback_ID = 0;
static jmethodID onBoolCallback_ID = 0;
static jmethodID onStringCallback_ID = 0;
class CallbackClassManual : public CallbackClass {
private:
JNIEnv* env;
jobject obj;
public:
CallbackClassManual( JNIEnv* env, jobject obj ) {
this->env = env;
this->obj = obj;
}
virtual void onVoidCallback(TestObjectClass& refData, TestObjectClass* pointerData) const {
env->CallVoidMethod(obj, onVoidCallback_ID, (jlong)&refData, (jlong)pointerData);
}
virtual int onIntCallback(int intValue01, int intValue02) const {
return env->CallIntMethod(obj, onIntCallback_ID, intValue01, intValue02);
}
virtual float onFloatCallback(float floatValue01, float floatValue02) const {
return env->CallFloatMethod(obj, onFloatCallback_ID, floatValue01, floatValue02);
}
virtual bool onBoolCallback(bool boolValue01) const {
return env->CallBooleanMethod(obj, onBoolCallback_ID, boolValue01);
}
virtual void onStringCallback(const char* strValue01) const {
jstring jstrBuf = env->NewStringUTF(strValue01);
env->CallVoidMethod(obj, onStringCallback_ID, jstrBuf);
}
};
*/

/*[-TEAVM;-ADD]
@org.teavm.jso.JSFunctor
public interface onVoidCallback extends org.teavm.jso.JSObject {
void onVoidCallback(int refData, int pointerData);
}
*/
/*[-TEAVM;-ADD]
@org.teavm.jso.JSFunctor
public interface onIntCallback extends org.teavm.jso.JSObject {
int onIntCallback(int intValue01, int intValue02);
}
*/
/*[-TEAVM;-ADD]
@org.teavm.jso.JSFunctor
public interface onFloatCallback extends org.teavm.jso.JSObject {
float onFloatCallback(float floatValue01, float floatValue02);
}
*/
/*[-TEAVM;-ADD]
@org.teavm.jso.JSFunctor
public interface onBoolCallback extends org.teavm.jso.JSObject {
boolean onBoolCallback(boolean boolValue01);
}
*/
/*[-TEAVM;-ADD]
@org.teavm.jso.JSFunctor
public interface onStringCallback extends org.teavm.jso.JSObject {
void onStringCallback(int strValue01);
}
*/

/*[-TEAVM;-REPLACE]
public CallbackClassManual() {
super((byte)0, (char)0);
onVoidCallback onVoidCallback = new onVoidCallback() {
@Override
public void onVoidCallback(int refData, int pointerData) {
internal_onVoidCallback(refData, pointerData);
}
};
onIntCallback onIntCallback = new onIntCallback() {
@Override
public int onIntCallback(int intValue01, int intValue02) {
return internal_onIntCallback(intValue01, intValue02);
}
};
onFloatCallback onFloatCallback = new onFloatCallback() {
@Override
public float onFloatCallback(float floatValue01, float floatValue02) {
return internal_onFloatCallback(floatValue01, floatValue02);
}
};
onBoolCallback onBoolCallback = new onBoolCallback() {
@Override
public boolean onBoolCallback(boolean boolValue01) {
return internal_onBoolCallback(boolValue01);
}
};
onStringCallback onStringCallback = new onStringCallback() {
@Override
public void onStringCallback(int strValue01) {
internal_onStringCallback(IDLBase.getJSString(strValue01));
}
};
int pointer = createNative(onVoidCallback, onIntCallback, onFloatCallback, onBoolCallback, onStringCallback);
initNative(pointer, true);
}
*/
public CallbackClassManual() {
super((byte)0, (char)0);
long addr = createNATIVE();
initNative(addr, true);
}

/*[-JNI;-NATIVE]
if(CallbackClassManual_CLASS == 0) {
CallbackClassManual_CLASS = (jclass)env->NewGlobalRef(env->GetObjectClass(object));
onVoidCallback_ID = env->GetMethodID(CallbackClassManual_CLASS, "internal_onVoidCallback", "(JJ)V");
onIntCallback_ID = env->GetMethodID(CallbackClassManual_CLASS, "internal_onIntCallback", "(II)I");
onFloatCallback_ID = env->GetMethodID(CallbackClassManual_CLASS, "internal_onFloatCallback", "(FF)F");
onBoolCallback_ID = env->GetMethodID(CallbackClassManual_CLASS, "internal_onBoolCallback", "(Z)Z");
onStringCallback_ID = env->GetMethodID(CallbackClassManual_CLASS, "internal_onStringCallback", "(Ljava/lang/String;)V");
}
return (jlong)new CallbackClassManual(env, env->NewGlobalRef(object));
*/
/*[-TEAVM;-REPLACE]
@org.teavm.jso.JSBody(params = { "onVoidCallback", "onIntCallback", "onFloatCallback", "onBoolCallback", "onStringCallback" }, script = "var CallbackClassImpl = new [MODULE].CallbackClassImpl(); CallbackClassImpl.onVoidCallback = onVoidCallback; CallbackClassImpl.onIntCallback = onIntCallback; CallbackClassImpl.onFloatCallback = onFloatCallback; CallbackClassImpl.onBoolCallback = onBoolCallback; CallbackClassImpl.onStringCallback = onStringCallback; return [MODULE].getPointer(CallbackClassImpl);")
private static native int createNative(onVoidCallback onVoidCallback, onIntCallback onIntCallback, onFloatCallback onFloatCallback, onBoolCallback onBoolCallback, onStringCallback onStringCallback);
*/
private native long createNATIVE();

public void internal_onVoidCallback(long refData, long pointerData) {
}

public int internal_onIntCallback(int intValue01, int intValue02) {
return 0;
}

public float internal_onFloatCallback(float floatValue01, float floatValue02) {
return 0;
}

public boolean internal_onBoolCallback(boolean boolValue01) {
return false;
}

public void internal_onStringCallback(String strValue01) {
}

// private int onCall(long luaState) {
// LuaState.TMP.setPointer(luaState);
// return onCall(LuaState.TMP);
// }
//
// public int onCall(LuaState luaState) {
// return 0;
// }
}
6 changes: 3 additions & 3 deletions example/lib/lib-build/src/main/cpp/TestLib.idl
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ interface TestCallbackClass {
attribute TestObjectClass pointerObject;

void callVoidCallback(CallbackClass callback);
void callIntCallback(CallbackClass callback);
void callFloatCallback(CallbackClass callback);
void callBoolCallback(CallbackClass callback);
long callIntCallback(CallbackClass callback);
float callFloatCallback(CallbackClass callback);
boolean callBoolCallback(CallbackClass callback);
void callStringCallback(CallbackClass callback);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ class DefaultCallbackClass : public CallbackClass
return !boolValue01;
}
virtual void onStringCallback(const char* strValue01) const {
cout << "strValue01: " << strValue01 << endl;
}
};

Expand All @@ -314,14 +313,15 @@ class TestCallbackClass {
int callIntCallback(CallbackClass* callback) {
return callback->onIntCallback(intValue01, intValue02);
};
int callFloatCallback(CallbackClass* callback) {
float callFloatCallback(CallbackClass* callback) {
return callback->onFloatCallback(floatValue01, floatValue02);
};
bool callBoolCallback(CallbackClass* callback) {
return callback->onBoolCallback(boolValue01);
};
void callStringCallback(CallbackClass* callback) {
callback->onStringCallback(strValue01.c_str());
const char* text = strValue01.c_str();
callback->onStringCallback(text);
};
};

Expand Down
8 changes: 8 additions & 0 deletions jParser/base/src/main/java/idl/IDLBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ public void dispose() {
*/
protected void deleteNative() {
}

/*[-TEAVM;-REPLACE]
@org.teavm.jso.JSBody(params = { "addr" }, script = "return [MODULE].UTF8ToString(addr);")
public static native String getJSString(int addr);
*/
public static String getJSString(long addr) {
return null;
}
}

0 comments on commit d5a3b2b

Please sign in to comment.