Skip to content

Commit

Permalink
Parse cpp callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Aug 30, 2024
1 parent 5dc9000 commit e83190b
Show file tree
Hide file tree
Showing 15 changed files with 617 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void render() {
if(init) {
init = false;
testPass = TestLib.test();
color = testPass ? Color.GREEN : Color.RED;
color = testPass ? Color.LIME : Color.RED;
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.xpenatan.jparser.example.app;

import com.github.xpenatan.jparser.example.testlib.CallbackClass;
import com.github.xpenatan.jparser.example.testlib.CallbackClassManual;
import com.github.xpenatan.jparser.example.testlib.TestCallbackClass;
import com.github.xpenatan.jparser.example.testlib.TestConstructorClass;
Expand Down Expand Up @@ -254,6 +255,160 @@ private static boolean testStaticMethodClass() {
}

private static boolean testCallbackClass() {
{
TestCallbackClass test = new TestCallbackClass();
try {
boolean[] internal_onVoidCallback = { false };
CallbackClass callback = new CallbackClass() {
@Override
public void onVoidCallback(TestObjectClass refData, TestObjectClass 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 };
CallbackClass callback = new CallbackClass() {
@Override
public int 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 };
CallbackClass callback = new CallbackClass() {
@Override
public float 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 };
CallbackClass callback = new CallbackClass() {
@Override
public boolean 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];
CallbackClass callback = new CallbackClass() {
@Override
public void 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();
}
{
TestCallbackClass test = new TestCallbackClass();
try {
int[] onUnsignedIntCallback = { 0 };
CallbackClass callback = new CallbackClass() {
@Override
public int onUnsignedIntCallback(int unsignedInt) {
onUnsignedIntCallback[0] = unsignedInt;
return 2;
}
};
int i = test.callUnsignedIntCallback(callback);
if(!(onUnsignedIntCallback[0] == 13 && i == 2)) {
throw new RuntimeException("onUnsignedIntCallback[0] == 13 && i == 2");
}
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}
test.dispose();
}
{
TestCallbackClass test = new TestCallbackClass();
try {
short[] onUnsignedShortCallback = { 0 };
CallbackClass callback = new CallbackClass() {
@Override
public short onUnsignedShortCallback(short unsignedShort) {
onUnsignedShortCallback[0] = unsignedShort;
return 3;
}
};
short i = test.callUnsignedShortCallback(callback);
if(!(onUnsignedShortCallback[0] == 12 && i == 3)) {
throw new RuntimeException("onUnsignedShortCallback[0] == 12 && i == 3");
}
} catch(Throwable e) {
e.printStackTrace();
test.dispose();
return false;
}
test.dispose();
}
return true;
}

Expand All @@ -269,7 +424,7 @@ public void internal_onVoidCallback(long refData, long pointerData) {
internal_onVoidCallback[0] = true;
}
};
test.callVoidCallback(callback);
test.callManualVoidCallback(callback);
if(!(internal_onVoidCallback[0] == true)) {
throw new RuntimeException("internal_onVoidCallback[0] == true");
}
Expand All @@ -291,7 +446,7 @@ public int internal_onIntCallback(int intValue01, int intValue02) {
return 0;
}
};
test.callIntCallback(callback);
test.callManualIntCallback(callback);
if(!(internal_onIntCallback[0] == true)) {
throw new RuntimeException("internal_onIntCallback[0] == true");
}
Expand All @@ -313,7 +468,7 @@ public float internal_onFloatCallback(float floatValue01, float floatValue02) {
return 0;
}
};
test.callFloatCallback(callback);
test.callManualFloatCallback(callback);
if(!(internal_onFloatCallback[0] == true)) {
throw new RuntimeException("internal_onFloatCallback[0] == true");
}
Expand All @@ -335,7 +490,7 @@ public boolean internal_onBoolCallback(boolean boolValue01) {
return false;
}
};
test.callBoolCallback(callback);
test.callManualBoolCallback(callback);
if(!(internal_onBoolCallback[0] == true)) {
throw new RuntimeException("internal_onBoolCallback[0] == true");
}
Expand All @@ -358,7 +513,7 @@ public void internal_onStringCallback(String strValue01) {
internal_onStringCallback[0] = strValue01;
}
};
test.callStringCallback(callback);
test.callManualStringCallback(callback);
if(!(text.equals(internal_onStringCallback[0]) == true)) {
throw new RuntimeException("text.equals(internal_onStringCallback[0]) == true");
}
Expand Down
50 changes: 30 additions & 20 deletions example/lib/lib-build/src/main/cpp/TestLib.idl
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,19 @@ interface CallbackClassManual {
long addInt(long a, long b);
};

//[JSImplementation="CallbackClass"]
//interface CallbackClassImpl {
// void CallbackClassImpl();
//
// [Const] void onVoidCallback([Ref] TestObjectClass refData, TestObjectClass pointerData);
// [Const] long onIntCallback(long intValue01, long intValue02);
// [Const] float onFloatCallback(float floatValue01, float floatValue02);
// [Const] boolean onBoolCallback(boolean boolValue01);
// [Const] void onStringCallback([Const] DOMString strValue01);
//};
//CallbackClassImpl implements CallbackClass;
[JSImplementation="CallbackClass"]
interface CallbackClassImpl {
void CallbackClassImpl();

[Const] void onVoidCallback([Ref] TestObjectClass refData, TestObjectClass pointerData);
[Const] long onIntCallback(long intValue01, long intValue02);
[Const] float onFloatCallback(float floatValue01, float floatValue02);
[Const] boolean onBoolCallback(boolean boolValue01);
[Const] void onStringCallback([Const] DOMString strValue01);
unsigned long onUnsignedIntCallback(unsigned long unsignedInt);
[Const] unsigned short onUnsignedShortCallback(unsigned short unsignedShort);
};
CallbackClassImpl implements CallbackClass;

[JSImplementation="CallbackClassManual"]
interface CallbackClassManualImpl {
Expand All @@ -162,10 +164,10 @@ interface CallbackClassManualImpl {
};
CallbackClassManualImpl implements CallbackClassManual;

interface DefaultCallbackClass {
void DefaultCallbackClass();
};
DefaultCallbackClass implements CallbackClass;
//interface DefaultCallbackClass {
// void DefaultCallbackClass();
//};
//DefaultCallbackClass implements CallbackClass;

//[JSImplementation="DefaultCallbackClass"]
//interface DefaultCallbackClassImpl {
Expand All @@ -187,11 +189,19 @@ interface TestCallbackClass {
[Value] attribute TestObjectClass valueObject;
attribute TestObjectClass pointerObject;

void callVoidCallback(CallbackClassManual callback);
long callIntCallback(CallbackClassManual callback);
float callFloatCallback(CallbackClassManual callback);
boolean callBoolCallback(CallbackClassManual callback);
void callStringCallback(CallbackClassManual callback);
void callVoidCallback(CallbackClass callback);
long callIntCallback(CallbackClass callback);
float callFloatCallback(CallbackClass callback);
boolean callBoolCallback(CallbackClass callback);
void callStringCallback(CallbackClass callback);
unsigned long callUnsignedIntCallback(CallbackClass callback);
unsigned short callUnsignedShortCallback(CallbackClass callback);

void callManualVoidCallback(CallbackClassManual callback);
long callManualIntCallback(CallbackClassManual callback);
float callManualFloatCallback(CallbackClassManual callback);
boolean callManualBoolCallback(CallbackClassManual callback);
void callManualStringCallback(CallbackClassManual callback);
};

enum TestEnumWithinClass {
Expand Down
26 changes: 21 additions & 5 deletions example/lib/lib-build/src/main/cpp/source/TestLib/src/TestLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ class CallbackClass
virtual float onFloatCallback(float floatValue01, float Value02) const = 0;
virtual bool onBoolCallback(bool boolValue01) const = 0;
virtual void onStringCallback(const char* strValue01) const = 0;
virtual unsigned int onUnsignedIntCallback(unsigned int unsignedInt) = 0;
virtual unsigned short onUnsignedShortCallback(unsigned short unsignedShort) const = 0;

int addInt(int a, int b)
{
Expand Down Expand Up @@ -310,6 +312,12 @@ class DefaultCallbackClass : public CallbackClass
}
virtual void onStringCallback(const char* strValue01) const {
}
virtual unsigned int onUnsignedIntCallback(unsigned int unsignedInt) {
return 10;
}
virtual unsigned short onUnsignedShortCallback(unsigned short unsignedShort) const {
return 20;
}
};

class TestCallbackClass {
Expand Down Expand Up @@ -340,20 +348,28 @@ class TestCallbackClass {
const char* text = strValue01.c_str();
callback->onStringCallback(text);
};
unsigned int callUnsignedIntCallback(CallbackClass* callback) {
unsigned int value = 13;
return callback->onUnsignedIntCallback(value);
};
unsigned short callUnsignedShortCallback(CallbackClass* callback) {
unsigned short value = 12;
return callback->onUnsignedShortCallback(value);
};

void callVoidCallback(CallbackClassManual* callback) {
void callManualVoidCallback(CallbackClassManual* callback) {
callback->onVoidCallback(valueObject, pointerObject);
};
int callIntCallback(CallbackClassManual* callback) {
int callManualIntCallback(CallbackClassManual* callback) {
return callback->onIntCallback(intValue01, intValue02);
};
float callFloatCallback(CallbackClassManual* callback) {
float callManualFloatCallback(CallbackClassManual* callback) {
return callback->onFloatCallback(floatValue01, floatValue02);
};
bool callBoolCallback(CallbackClassManual* callback) {
bool callManualBoolCallback(CallbackClassManual* callback) {
return callback->onBoolCallback(boolValue01);
};
void callStringCallback(CallbackClassManual* callback) {
void callManualStringCallback(CallbackClassManual* callback) {
const char* text = strValue01.c_str();
callback->onStringCallback(text);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public static boolean isInt(Type type) {
return JParserHelper.isType(type, "int");
}

public static boolean isShort(Type type) {
return JParserHelper.isType(type, "short");
}

public static boolean isFloat(Type type) {
return JParserHelper.isType(type, "float");
}
Expand Down
Loading

0 comments on commit e83190b

Please sign in to comment.