From 00320a5b3a264ec3c6733c134c72a7c2b83cd20c Mon Sep 17 00:00:00 2001 From: Lyon Date: Fri, 15 Sep 2023 01:28:44 +0800 Subject: [PATCH] add pika_runFunctionX api() --- port/linux/test/compile-test.cpp | 6 +++++ src/PikaObj.c | 45 ++++++++++++++++++-------------- src/PikaObj.h | 1 + src/PikaVersion.h | 2 +- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/port/linux/test/compile-test.cpp b/port/linux/test/compile-test.cpp index dd824d1d4..2a2055970 100644 --- a/port/linux/test/compile-test.cpp +++ b/port/linux/test/compile-test.cpp @@ -797,4 +797,10 @@ TEST(compiler, contains) { EXPECT_EQ(pikaMemNow(), 0); } +TEST(compiler, fn1) { + char* lines = "@r = @f(@d)\n"; + pika_lines2Array(lines); + EXPECT_EQ(pikaMemNow(), 0); +} + TEST_END diff --git a/src/PikaObj.c b/src/PikaObj.c index 3970c07f5..4e63cdbf0 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -2767,6 +2767,30 @@ void pika_eventListener_deinit(PikaEventListener** p_self) { } } +Arg* pika_runFunction1(Arg* functionArg, Arg* arg1) { + PikaObj* locals = New_TinyObj(NULL); + obj_setArg(locals, "@d", arg1); + obj_setArg(locals, "@f", functionArg); + /* clang-format off */ + PIKA_PYTHON( + @r = @f(@d) + ) + /* clang-format on */ + const uint8_t bytes[] = { + 0x0c, 0x00, 0x00, 0x00, /* instruct array size */ + 0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x04, 0x00, 0x00, 0x04, 0x07, 0x00, + /* instruct array */ + 0x0a, 0x00, 0x00, 0x00, /* const pool size */ + 0x00, 0x40, 0x64, 0x00, 0x40, 0x66, 0x00, 0x40, 0x72, + 0x00, /* const pool */ + }; + pikaVM_runByteCode(locals, (uint8_t*)bytes); + Arg* res = obj_getArg(locals, "@r"); + res = arg_copy(res); + obj_deinit(locals); + return res; +} + Arg* __eventListener_runEvent(PikaEventListener* lisener, uint32_t eventId, Arg* eventData) { @@ -2777,26 +2801,9 @@ Arg* __eventListener_runEvent(PikaEventListener* lisener, "Error: can not find event handler by id: [0x%02x]\r\n", eventId); return NULL; } - obj_setArg(handler, "eventData", eventData); - /* clang-format off */ - PIKA_PYTHON( - _res = eventCallBack(eventData) - ) - /* clang-format on */ - const uint8_t bytes[] = { - 0x0c, 0x00, 0x00, 0x00, /* instruct array size */ - 0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x00, 0x04, 0x19, 0x00, - /* instruct array */ - 0x1e, 0x00, 0x00, 0x00, /* const pool size */ - 0x00, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x44, 0x61, 0x74, 0x61, 0x00, 0x65, - 0x76, 0x65, 0x6e, 0x74, 0x43, 0x61, 0x6c, 0x6c, 0x42, 0x61, 0x63, 0x6b, - 0x00, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */ - }; + Arg* eventCallBack = obj_getArg(handler, "eventCallBack"); pika_debug("run event handler: %p", handler); - pikaVM_runByteCode(handler, (uint8_t*)bytes); - Arg* res = obj_getArg(handler, "_res"); - res = arg_copy(res); - obj_removeArg(handler, "_res"); + Arg* res = pika_runFunction1(eventCallBack, eventData); return res; } diff --git a/src/PikaObj.h b/src/PikaObj.h index b06942331..fffaed792 100644 --- a/src/PikaObj.h +++ b/src/PikaObj.h @@ -331,6 +331,7 @@ ByteCodeFrame* methodArg_getBytecodeFrame(Arg* method_arg); Method methodArg_getPtr(Arg* method_arg); VMParameters* obj_run(PikaObj* self, char* cmd); +Arg* pika_runFunction1(Arg* methodArg, Arg* arg1); PikaObj* New_PikaObj(void); PikaObj* New_PikaObj_noGC(void); diff --git a/src/PikaVersion.h b/src/PikaVersion.h index 35b297649..55a1a7417 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 12 #define PIKA_VERSION_MICRO 6 -#define PIKA_EDIT_TIME "2023/09/13 22:07:55" +#define PIKA_EDIT_TIME "2023/09/15 01:28:35"