2021-10-01 00:21:50 +08:00
|
|
|
/* this demo shows the usage of input and return of method */
|
|
|
|
|
|
|
|
#include "BaseObj.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void addMethod(PikaObj *self, Args* args)
|
|
|
|
{
|
|
|
|
int32_t val1 = args_getInt(args, "val1");
|
|
|
|
int32_t val2 = args_getInt(args, "val2");
|
|
|
|
int32_t res = val1 + val2;
|
|
|
|
method_returnInt(args, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
PikaObj *New_TEST(Args *args)
|
|
|
|
{
|
|
|
|
/* Derive from the tiny object class.
|
|
|
|
Tiny object can not import sub object.
|
|
|
|
Tiny object is the smallest object. */
|
2021-10-18 17:33:55 +08:00
|
|
|
PikaObj *self = New_TinyObj(args);
|
2021-10-01 00:21:50 +08:00
|
|
|
|
|
|
|
/* bind the method */
|
|
|
|
class_defineMethod(self, "add(val1:int, val2:int)->int", addMethod);
|
|
|
|
|
|
|
|
/* return the object */
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
PikaObj *New_MYROOT(Args *args)
|
|
|
|
{
|
|
|
|
/* Derive from the base object class .
|
|
|
|
BaseObj is the smallest object that can
|
|
|
|
import sub object. */
|
2021-10-18 17:33:55 +08:00
|
|
|
PikaObj *self = New_BaseObj(args);
|
2021-10-01 00:21:50 +08:00
|
|
|
|
|
|
|
/* new led object bellow root object */
|
2021-10-18 15:09:30 +08:00
|
|
|
obj_newObj(self, "test", "TEST", New_TEST);
|
2021-10-01 00:21:50 +08:00
|
|
|
|
|
|
|
/* return the object */
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t main()
|
|
|
|
{
|
|
|
|
/* new root object */
|
|
|
|
PikaObj *root = newRootObj("root", New_MYROOT);
|
|
|
|
/* user input buff */
|
|
|
|
char inputBuff[256] = {0};
|
|
|
|
/* run the script with check*/
|
|
|
|
obj_run(root, "res = test.add(1, 2)");
|
|
|
|
int32_t res = obj_getInt(root, "res");
|
|
|
|
printf("the res of 'test.add(1, 2)' is %d \r\n", res);
|
|
|
|
printf("memory used max = %0.2f kB\r\n", pikaMemMax() / 1024.0);
|
|
|
|
printf("memory used now = %0.2f kB\r\n", pikaMemNow() / 1024.0);
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* get user input */
|
|
|
|
fgets(inputBuff, sizeof(inputBuff), stdin);
|
|
|
|
|
|
|
|
/* run PikaScript and get res */
|
|
|
|
Args *resArgs = obj_runDirect(root, inputBuff);
|
|
|
|
|
|
|
|
/* get system output of PikaScript*/
|
|
|
|
char *sysOut = args_getSysOut(resArgs);;
|
|
|
|
|
|
|
|
if (NULL != sysOut)
|
|
|
|
{
|
|
|
|
/* print32_t out the system output */
|
|
|
|
printf("%s\r\n", sysOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deinit the res */
|
|
|
|
args_deinit(resArgs);
|
|
|
|
}
|
|
|
|
}
|