6
6
import at .jddev0 .lang .DataObject .FunctionPointerObject ;
7
7
import at .jddev0 .lang .LangInterpreter .LangInterpreterInterface ;
8
8
9
+ import static at .jddev0 .lang .LangFunction .*;
10
+ import static at .jddev0 .lang .LangFunction .LangParameter .*;
11
+
9
12
public class ExampleModule extends LangNativeModule {
10
13
@ Override
11
14
public DataObject load (List <DataObject > args , final int SCOPE_ID ) {
@@ -35,13 +38,15 @@ public DataObject load(List<DataObject> args, final int SCOPE_ID) {
35
38
callPredefinedFunction ("println" , Arrays .asList (
36
39
createDataObject ("Another print statement." ) //createDataObject can be used instead of new DataObject().set...()
37
40
), SCOPE_ID );
38
-
39
- exportFunction ("exampleFunction" , (argumentList , INNER_SCOPE_ID ) -> {
40
- List <DataObject > innerCombinedArgs = LangUtils .combineArgumentsWithoutArgumentSeparators (argumentList );
41
41
42
- System .out .println ("exampleFunction was called with " + innerCombinedArgs );
42
+ exportNativeFunction (new Object () {
43
+ @ LangFunction ("exampleFunction" )
44
+ public DataObject exampleFunctionFunction (int SCOPE_ID ,
45
+ @ LangParameter ("&args" ) @ LangParameter .VarArgs List <DataObject > args ) {
46
+ System .out .println ("exampleFunction was called with " + args );
43
47
44
- return createDataObject (innerCombinedArgs .size ());
48
+ return createDataObject (args .size ());
49
+ }
45
50
});
46
51
47
52
exportNormalVariable ("testVar" , createDataObject ("This is a test variable provided by the \" " + lmc .getName () + "\" module!" ));
@@ -56,30 +61,24 @@ public DataObject load(List<DataObject> args, final int SCOPE_ID) {
56
61
exportCompositeVariableFinal ("finalValues" , new DataObject ().setArray (new DataObject [] {
57
62
createDataObject ('a' ), createDataObject (false )
58
63
}));
59
- exportFunctionPointerVariable ("calc" , new DataObject ().setFunctionPointer (new FunctionPointerObject ((interpreter , argumentList , INNER_SCOPE_ID ) -> {
60
- List <DataObject > combinedArgumentList = LangUtils .combineArgumentsWithoutArgumentSeparators (argumentList );
61
- if (combinedArgumentList .size () != 3 )
62
- return new LangInterpreterInterface (interpreter ).setErrnoErrorObject (LangInterpreter .InterpretingError .INVALID_ARG_COUNT , "3 arguments are needed" , INNER_SCOPE_ID );
63
-
64
- DataObject aObj = combinedArgumentList .get (0 );
65
- DataObject bObj = combinedArgumentList .get (1 );
66
- DataObject cObj = combinedArgumentList .get (2 );
67
-
68
- Number aNum = aObj .toNumber ();
69
- if (aNum == null )
70
- return new LangInterpreterInterface (interpreter ).setErrnoErrorObject (LangInterpreter .InterpretingError .INVALID_ARGUMENTS , "Argument 1 must be a number" , INNER_SCOPE_ID );
71
- Number bNum = bObj .toNumber ();
72
- if (bNum == null )
73
- return new LangInterpreterInterface (interpreter ).setErrnoErrorObject (LangInterpreter .InterpretingError .INVALID_ARGUMENTS , "Argument 2 must be a number" , INNER_SCOPE_ID );
74
- Number cNum = cObj .toNumber ();
75
- if (cNum == null )
76
- return new LangInterpreterInterface (interpreter ).setErrnoErrorObject (LangInterpreter .InterpretingError .INVALID_ARGUMENTS , "Argument 2 must be a number" , INNER_SCOPE_ID );
77
-
78
- return createDataObject (aNum .intValue () * bNum .intValue () + cNum .intValue () * cNum .intValue ());
79
- })));
80
- exportFunctionPointerVariableFinal ("finalFunc" , new DataObject ().setFunctionPointer (new FunctionPointerObject ((interpreter , argumentList , INNER_SCOPE_ID ) -> {
81
- return createDataObject (-42 );
82
- })));
64
+ exportFunctionPointerVariable ("calc" , new DataObject ().setFunctionPointer (new FunctionPointerObject (
65
+ LangNativeFunction .getSingleLangFunctionFromObject (interpreter , new Object () {
66
+ @ LangFunction ("calc" )
67
+ public DataObject calcFunction (int SCOPE_ID ,
68
+ @ LangParameter ("$a" ) @ NumberValue Number aNum ,
69
+ @ LangParameter ("$b" ) @ NumberValue Number bNum ,
70
+ @ LangParameter ("$c" ) @ NumberValue Number cNum ) {
71
+ return createDataObject (aNum .intValue () * bNum .intValue () + cNum .intValue () * cNum .intValue ());
72
+ }
73
+ }))));
74
+ exportFunctionPointerVariableFinal ("finalFunc" , new DataObject ().setFunctionPointer (new FunctionPointerObject (
75
+ LangNativeFunction .getSingleLangFunctionFromObject (interpreter , new Object () {
76
+ @ LangFunction ("finalFunc" )
77
+ public DataObject finalFuncFunction (LangInterpreter interpreter , int SCOPE_ID ,
78
+ @ LangParameter ("&args" ) @ RawVarArgs List <DataObject > ignore ) {
79
+ return createDataObject (-42 );
80
+ }
81
+ }))));
83
82
84
83
DataObject ret = callPredefinedFunction ("include" , LangUtils .separateArgumentsWithArgumentSeparators (
85
84
Arrays .asList (
@@ -146,46 +145,46 @@ public DataObject load(List<DataObject> args, final int SCOPE_ID) {
146
145
}
147
146
}
148
147
149
- exportFunction ("testConvertToDataObject" , (argumentList , INNER_SCOPE_ID ) -> {
150
- List <DataObject > combinedArgumentList = LangUtils .combineArgumentsWithoutArgumentSeparators (argumentList );
151
- if (combinedArgumentList .size () > 1 )
152
- return new LangInterpreterInterface (interpreter ).setErrnoErrorObject (LangInterpreter .InterpretingError .INVALID_ARG_COUNT , "0 ore 1 argument(s) are needed" , INNER_SCOPE_ID );
153
-
154
- if (combinedArgumentList .size () == 0 ) {
148
+ exportNativeFunction (new Object () {
149
+ @ LangFunction (value ="testConvertToDataObject" , hasInfo =true )
150
+ public DataObject testConvertToDataObjectFunction (int SCOPE_ID ) {
155
151
callPredefinedFunction ("println" , Arrays .asList (
156
- 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]" )
157
- ), INNER_SCOPE_ID );
152
+ createDataObject ("Call \" func.testConvertToDataObject()\" with 0 for normal output " +
153
+ "or with 1 for debug output using \" func.printDebug()\" [1 Will only work in the LangShell]" )
154
+ ), SCOPE_ID );
158
155
159
156
return null ;
160
157
}
161
158
162
- boolean useLangShellsPrintDebug = combinedArgumentList .get (0 ).getBoolean ();
163
-
164
- printDataObjectInformation (convertToDataObject (null ), useLangShellsPrintDebug , INNER_SCOPE_ID );
165
- printDataObjectInformation (convertToDataObject ("text" ), useLangShellsPrintDebug , INNER_SCOPE_ID );
166
- printDataObjectInformation (convertToDataObject (new Object [] {
167
- 2 , "test" , null , Character .class
168
- }), useLangShellsPrintDebug , INNER_SCOPE_ID );
169
- printDataObjectInformation (convertToDataObject (new byte [] {
170
- 2 , -1 , (byte )255 , 127 , -128 , 0 , (byte )'A' , (byte )'B' , (byte )'µ' , (byte )1555
171
- }), useLangShellsPrintDebug , INNER_SCOPE_ID );
172
- printDataObjectInformation (convertToDataObject (42 ), useLangShellsPrintDebug , INNER_SCOPE_ID );
173
- printDataObjectInformation (convertToDataObject (true ), useLangShellsPrintDebug , INNER_SCOPE_ID );
174
- printDataObjectInformation (convertToDataObject (42.2f ), useLangShellsPrintDebug , INNER_SCOPE_ID );
175
- printDataObjectInformation (convertToDataObject (42.2 ), useLangShellsPrintDebug , INNER_SCOPE_ID );
176
- printDataObjectInformation (convertToDataObject ('a' ), useLangShellsPrintDebug , INNER_SCOPE_ID );
177
- printDataObjectInformation (convertToDataObject (new RuntimeException ("A custom error" )), useLangShellsPrintDebug , INNER_SCOPE_ID );
178
- printDataObjectInformation (convertToDataObject (new IllegalStateException ("Another error" )), useLangShellsPrintDebug , INNER_SCOPE_ID );
179
- printDataObjectInformation (convertToDataObject (Integer .class ), useLangShellsPrintDebug , INNER_SCOPE_ID );
180
- printDataObjectInformation (convertToDataObject (Boolean .class ), useLangShellsPrintDebug , INNER_SCOPE_ID );
181
- printDataObjectInformation (convertToDataObject (Long .class ), useLangShellsPrintDebug , INNER_SCOPE_ID );
182
- printDataObjectInformation (convertToDataObject (Class .class ), useLangShellsPrintDebug , INNER_SCOPE_ID );
183
- printDataObjectInformation (convertToDataObject (Object [].class ), useLangShellsPrintDebug , INNER_SCOPE_ID ); //DataType.ARRAY
184
- printDataObjectInformation (convertToDataObject (DataObject [].class ), useLangShellsPrintDebug , INNER_SCOPE_ID ); //DataType.ARRAY
185
- printDataObjectInformation (convertToDataObject (Byte [].class ), useLangShellsPrintDebug , INNER_SCOPE_ID ); //DataType.ARRAY
186
- printDataObjectInformation (convertToDataObject (byte [].class ), useLangShellsPrintDebug , INNER_SCOPE_ID ); //ByteBuffer.ARRAY
187
-
188
- return null ;
159
+ @ LangFunction ("testConvertToDataObject" )
160
+ public DataObject testConvertToDataObjectFunction (int SCOPE_ID ,
161
+ @ LangParameter ("usePrintDebug" ) @ BooleanValue boolean usePrintDebug ) {
162
+ printDataObjectInformation (convertToDataObject (null ), usePrintDebug , SCOPE_ID );
163
+ printDataObjectInformation (convertToDataObject ("text" ), usePrintDebug , SCOPE_ID );
164
+ printDataObjectInformation (convertToDataObject (new Object [] {
165
+ 2 , "test" , null , Character .class
166
+ }), usePrintDebug , SCOPE_ID );
167
+ printDataObjectInformation (convertToDataObject (new byte [] {
168
+ 2 , -1 , (byte )255 , 127 , -128 , 0 , (byte )'A' , (byte )'B' , (byte )'µ' , (byte )1555
169
+ }), usePrintDebug , SCOPE_ID );
170
+ printDataObjectInformation (convertToDataObject (42 ), usePrintDebug , SCOPE_ID );
171
+ printDataObjectInformation (convertToDataObject (true ), usePrintDebug , SCOPE_ID );
172
+ printDataObjectInformation (convertToDataObject (42.2f ), usePrintDebug , SCOPE_ID );
173
+ printDataObjectInformation (convertToDataObject (42.2 ), usePrintDebug , SCOPE_ID );
174
+ printDataObjectInformation (convertToDataObject ('a' ), usePrintDebug , SCOPE_ID );
175
+ printDataObjectInformation (convertToDataObject (new RuntimeException ("A custom error" )), usePrintDebug , SCOPE_ID );
176
+ printDataObjectInformation (convertToDataObject (new IllegalStateException ("Another error" )), usePrintDebug , SCOPE_ID );
177
+ printDataObjectInformation (convertToDataObject (Integer .class ), usePrintDebug , SCOPE_ID );
178
+ printDataObjectInformation (convertToDataObject (Boolean .class ), usePrintDebug , SCOPE_ID );
179
+ printDataObjectInformation (convertToDataObject (Long .class ), usePrintDebug , SCOPE_ID );
180
+ printDataObjectInformation (convertToDataObject (Class .class ), usePrintDebug , SCOPE_ID );
181
+ printDataObjectInformation (convertToDataObject (Object [].class ), usePrintDebug , SCOPE_ID ); //DataType.ARRAY
182
+ printDataObjectInformation (convertToDataObject (DataObject [].class ), usePrintDebug , SCOPE_ID ); //DataType.ARRAY
183
+ printDataObjectInformation (convertToDataObject (Byte [].class ), usePrintDebug , SCOPE_ID ); //DataType.ARRAY
184
+ printDataObjectInformation (convertToDataObject (byte [].class ), usePrintDebug , SCOPE_ID ); //ByteBuffer.ARRAY
185
+
186
+ return null ;
187
+ }
189
188
});
190
189
191
190
System .out .println ("Test convertToDataObject() by calling \" func.testConvertToDataObject()\" " );
0 commit comments