Skip to content

Commit 058d21b

Browse files
committed
Use new "convertToDataObject()" in the "ExampleModule" class
1 parent ec01caa commit 058d21b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/main/java/me/jddev0/ExampleModule.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,43 @@ public DataObject load(List<DataObject> args, final int SCOPE_ID) {
102102
}
103103
}
104104

105+
exportFunction("testConvertToDataObject", (argumentList, INNER_SCOPE_ID) -> {
106+
List<DataObject> combinedArgumentList = LangUtils.combineArgumentsWithoutArgumentSeparators(argumentList);
107+
if(combinedArgumentList.size() > 1)
108+
return new LangInterpreterInterface(interpreter).setErrnoErrorObject(LangInterpreter.InterpretingError.INVALID_ARG_COUNT, "0 ore 1 argument(s) are needed", INNER_SCOPE_ID);
109+
110+
if(combinedArgumentList.size() == 0) {
111+
callPredefinedFunction("println", Arrays.asList(
112+
createDataObject("Call \"func.testConvertToDataObject()\" with 0 for normal output or with 1 for debug output using \"func.printDebug()\" [1 Will only work in the LangShell]")
113+
), INNER_SCOPE_ID);
114+
115+
return null;
116+
}
117+
118+
boolean useLangShellsPrintDebug = combinedArgumentList.get(0).getBoolean();
119+
120+
printDataObjectInformation(convertToDataObject(null), useLangShellsPrintDebug, INNER_SCOPE_ID);
121+
printDataObjectInformation(convertToDataObject("text"), useLangShellsPrintDebug, INNER_SCOPE_ID);
122+
printDataObjectInformation(convertToDataObject(new Object[] {
123+
2, "test", null, Character.class
124+
}), useLangShellsPrintDebug, INNER_SCOPE_ID);
125+
printDataObjectInformation(convertToDataObject(42), useLangShellsPrintDebug, INNER_SCOPE_ID);
126+
printDataObjectInformation(convertToDataObject(true), useLangShellsPrintDebug, INNER_SCOPE_ID);
127+
printDataObjectInformation(convertToDataObject(42.2f), useLangShellsPrintDebug, INNER_SCOPE_ID);
128+
printDataObjectInformation(convertToDataObject(42.2), useLangShellsPrintDebug, INNER_SCOPE_ID);
129+
printDataObjectInformation(convertToDataObject('a'), useLangShellsPrintDebug, INNER_SCOPE_ID);
130+
printDataObjectInformation(convertToDataObject(new RuntimeException("A custom error")), useLangShellsPrintDebug, INNER_SCOPE_ID);
131+
printDataObjectInformation(convertToDataObject(new IllegalStateException("Another error")), useLangShellsPrintDebug, INNER_SCOPE_ID);
132+
printDataObjectInformation(convertToDataObject(Integer.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
133+
printDataObjectInformation(convertToDataObject(Boolean.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
134+
printDataObjectInformation(convertToDataObject(Long.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
135+
printDataObjectInformation(convertToDataObject(Class.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
136+
137+
return null;
138+
});
139+
140+
System.out.println("Test convertToDataObject() by calling \"func.testConvertToDataObject()\"");
141+
105142
System.out.println("Test calling fn.exampleFunction!");
106143

107144
return createDataObject(new DataObject[] {
@@ -136,4 +173,28 @@ private void printModuleInformation(LangInterpreterInterface lii, final int SCOP
136173
System.out.println("Module Path: " + lii.getData(SCOPE_ID).var.get("$LANG_MODULE_PATH"));
137174
System.out.println("Module File: " + lii.getData(SCOPE_ID).var.get("$LANG_MODULE_FILE"));
138175
}
176+
177+
private void printDataObjectInformation(DataObject dataObject, boolean useLangShellsPrintDebug, final int SCOPE_ID) {
178+
if(useLangShellsPrintDebug) {
179+
DataObject printDebugFunc = getPredefinedFunctionAsDataObject("printDebug");
180+
if(printDebugFunc == null) {
181+
LangInterpreterInterface lii = new LangInterpreterInterface(interpreter);
182+
lii.setErrno(LangInterpreter.InterpretingError.INVALID_ARGUMENTS, "func.printDebug() can only be used inside the LangShell", SCOPE_ID);
183+
184+
return;
185+
}
186+
187+
callFunctionPointer(printDebugFunc, Arrays.asList(
188+
dataObject
189+
), SCOPE_ID);
190+
callPredefinedFunction("println", new LinkedList<>(), SCOPE_ID);
191+
192+
return;
193+
}
194+
195+
System.out.println("DataObject:");
196+
System.out.println(" Type: " + dataObject.getType());
197+
System.out.println(" String representation: " + dataObject);
198+
System.out.println();
199+
}
139200
}

0 commit comments

Comments
 (0)