support fn and class def on bytecode

update version, fix warning
This commit is contained in:
lyon 2022-11-15 14:53:57 +08:00
parent 307477f033
commit 363238987e
7 changed files with 94 additions and 4 deletions

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
"--gtest_filter=vm.default_no_kw"
// "--gtest_filter=vm.default_no_kw"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -87,7 +87,8 @@
"ctype.h": "c",
"mqttclient.h": "c",
"_mqtt__mqtt.h": "c",
"requests_response.h": "c"
"requests_response.h": "c",
"*.cfg": "c"
},
"python.formatting.provider": "autopep8",
"C_Cpp.errorSquiggles": "Disabled"

View File

@ -785,7 +785,7 @@ static int mqtt_suback_packet_handle(mqtt_client_t* c,
static int mqtt_unsuback_packet_handle(mqtt_client_t* c,
platform_timer_t* timer) {
int rc = MQTT_FAILED_ERROR;
message_handlers_t* msg_handler;
message_handlers_t* msg_handler = NULL;
uint16_t packet_id = 0;
rc = mqtt_is_connected(c);

View File

@ -2694,6 +2694,54 @@ __exit:
return res;
}
static char* _get_data_from_bytecode2(uint8_t* bytecode,
enum Instruct ins1,
enum Instruct ins2) {
ByteCodeFrame bf = {0};
char* res = NULL;
byteCodeFrame_init(&bf);
byteCodeFrame_loadByteCode(&bf, bytecode);
while (1) {
InstructUnit* ins_unit = instructArray_getNow(&bf.instruct_array);
if (NULL == ins_unit) {
goto __exit;
}
enum Instruct ins = instructUnit_getInstruct(ins_unit);
if (ins == ins1 || ins == ins2) {
res = constPool_getByOffset(&bf.const_pool,
ins_unit->const_pool_index);
goto __exit;
}
instructArray_getNext(&bf.instruct_array);
}
__exit:
byteCodeFrame_deinit(&bf);
return res;
}
static ByteCodeFrame* _cache_bcf_fn_bc(PikaObj* self, uint8_t* bytecode) {
ByteCodeFrame bytecode_frame_stack = {0};
ByteCodeFrame* res = NULL;
/* save 'def' and 'class' to heap */
if (NULL == _get_data_from_bytecode2(bytecode, DEF, CLS)) {
return NULL;
}
if (!obj_isArgExist(self, "@bcn")) {
/* @bc0 for first bc, @bc1 for REPL bc, start form @bc2*/
obj_setInt(self, "@bcn", 2);
}
int bcn = obj_getInt(self, "@bcn");
char bcn_str[] = "@bcx";
bcn_str[3] = '0' + bcn;
/* load bytecode to heap */
args_setHeapStruct(self->list, bcn_str, bytecode_frame_stack,
byteCodeFrame_deinit);
/* get bytecode_ptr from heap */
res = args_getHeapStruct(self->list, bcn_str);
obj_setInt(self, "@bcn", bcn + 1);
return res;
}
static VMParameters* __pikaVM_runPyLines(PikaObj* self, char* py_lines) {
VMParameters* globals = NULL;
ByteCodeFrame bytecode_frame_stack = {0};
@ -2746,6 +2794,9 @@ VMParameters* _do_pikaVM_runByteCode(PikaObj* self,
* 'class'
*/
bytecode_frame_p = _cache_bcf0(self);
if (NULL == bytecode_frame_p) {
bytecode_frame_p = _cache_bcf_fn_bc(self, bytecode);
}
if (NULL == bytecode_frame_p) {
is_use_heap_bytecode = 0;
/* get bytecode_ptr from stack */

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 11
#define PIKA_VERSION_MICRO 6
#define PIKA_EDIT_TIME "2022/11/15 12:22:09"
#define PIKA_EDIT_TIME "2022/11/15 14:57:30"

View File

@ -2196,4 +2196,34 @@ TEST(VM, print_None) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, bc_fn) {
PikaObj* self = newRootObj("root", New_PikaMain);
obj_run(self, "print('hello world')\n");
/* clang-format off */
PIKA_PYTHON(
def test():
print('test')
)
/* clang-format on */
const uint8_t
bytes[] =
{
0x14, 0x00, 0x00, 0x00, /* instruct array size */
0x00, 0x89, 0x01, 0x00, 0x00, 0x06, 0x08,
0x00, 0x11, 0x83, 0x0a, 0x00, 0x01, 0x02,
0x0f, 0x00, 0x01, 0x8a, 0x00, 0x00, /* instruct array */
0x15, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x74, 0x65, 0x73, 0x74, 0x28, 0x29,
0x00, 0x31, 0x00, 0x74, 0x65, 0x73, 0x74,
0x00, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x00, /* const pool */
};
pikaVM_runByteCode(self, (uint8_t*)bytes);
obj_run(self, "test()\n");
EXPECT_STREQ(log_buff[1], "hello world\r\n");
EXPECT_STREQ(log_buff[0], "test\r\n");
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST_END

View File

@ -688,4 +688,12 @@ TEST(compiler, for_print_1k) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(compiler, bc_fn) {
char* lines =
"def test():\n"
" print('test')\n";
Parser_linesToArray(lines);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST_END