From 6726169ad9497b43c356e7c5cc69685fbef2f8a3 Mon Sep 17 00:00:00 2001 From: pikastech Date: Sun, 28 Aug 2022 17:53:22 +0800 Subject: [PATCH] serilized to api use mask in arg for serilized support isKeyword flag for arg --- examples/socket/server_client.py | 2 +- port/linux/.vscode/launch.json | 2 +- .../linux/test/python/socket/server_client.py | 8 ++--- src/PikaPlatform.h | 8 ++--- src/dataArg.c | 10 ++++--- src/dataArg.h | 30 ++++++++++++++----- src/dataArgs.c | 4 +-- src/dataStack.c | 6 ++-- 8 files changed, 44 insertions(+), 26 deletions(-) diff --git a/examples/socket/server_client.py b/examples/socket/server_client.py index 3e8927176..51afcbc31 100644 --- a/examples/socket/server_client.py +++ b/examples/socket/server_client.py @@ -5,7 +5,7 @@ from PikaStdData import ByteArray server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "127.0.0.1" -port = 9999 + random.randint(0, 100) +port = 9999 + random.randint(0, 1000) print("port:", port) server.bind((host, port)) server.listen(5) diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index f5e80e88c..5265ab6f7 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - // "--gtest_filter=parser.issues_I5MIFO" + "--gtest_filter=object_test.test6" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/test/python/socket/server_client.py b/port/linux/test/python/socket/server_client.py index 601005c1c..51afcbc31 100644 --- a/port/linux/test/python/socket/server_client.py +++ b/port/linux/test/python/socket/server_client.py @@ -5,7 +5,7 @@ from PikaStdData import ByteArray server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "127.0.0.1" -port = 9999 + random.randint(0, 100) +port = 9999 + random.randint(0, 1000) print("port:", port) server.bind((host, port)) server.listen(5) @@ -17,13 +17,13 @@ res = server.accept() accept = res[0] addr = res[1] -print("recv from client: %s" % str(addr)) +print("recv from client: %s" % str(addr)) client.send(String("send test from client").encode()) -print("server recv:", ByteArray(accept.recv(1024)).decode()) +print("server recv:", ByteArray(accept.recv(1024)).decode()) accept.send(String("send test from server").encode()) -print("client recv:", ByteArray(client.recv(1024)).decode()) +print("client recv:", ByteArray(client.recv(1024)).decode()) accept.close() client.close() diff --git a/src/PikaPlatform.h b/src/PikaPlatform.h index 12129061b..c23106616 100644 --- a/src/PikaPlatform.h +++ b/src/PikaPlatform.h @@ -100,10 +100,10 @@ typedef enum { /* clang-format off */ /* pikascript bool type */ -typedef enum { - PIKA_TRUE = 1, - PIKA_FALSE = 0, -} PIKA_BOOL; +typedef enum{ + PIKA_TRUE = 1, + PIKA_FALSE = 0, +}PIKA_BOOL; /* clang-format on */ diff --git a/src/dataArg.c b/src/dataArg.c index 600ceb539..b0dc62841 100644 --- a/src/dataArg.c +++ b/src/dataArg.c @@ -57,7 +57,8 @@ static Arg* arg_init_hash(Hash nameHash, self->size = size; self->name_hash = nameHash; self->type = type; - self->serialized = PIKA_TRUE; + arg_setSerialized(self, PIKA_TRUE); + arg_setIsKeyword(self, PIKA_FALSE); __platform_memset(arg_getContent(self), 0, aline_by(size, sizeof(uint32_t))); if (NULL != content) { @@ -81,7 +82,8 @@ void arg_init_stack(Arg* self, uint8_t* buffer, uint32_t size) { self->size = size; self->type = ARG_TYPE_UNDEF; self->name_hash = 0; - self->serialized = PIKA_FALSE; + arg_setSerialized(self, PIKA_FALSE); + arg_setIsKeyword(self, PIKA_FALSE); } uint32_t arg_totleSize(Arg* self) { @@ -339,7 +341,7 @@ Arg* arg_copy_noalloc(Arg* arg_src, Arg* arg_dict) { if (ARG_TYPE_OBJECT == arg_type) { obj_refcntInc((PikaObj*)arg_getPtr(arg_src)); } - arg_dict->serialized = PIKA_FALSE; + arg_setSerialized(arg_dict, PIKA_FALSE); arg_dict = arg_setContent(arg_dict, arg_getContent(arg_src), arg_getContentSize(arg_src)); arg_dict = arg_setNameHash(arg_dict, arg_getNameHash(arg_src)); @@ -423,7 +425,7 @@ void arg_deinit(Arg* self) { } /* deinit arg pointed heap */ arg_deinitHeap(self); - if (!self->serialized) { + if (!arg_getSerialized(self)) { return; } /* free the ref */ diff --git a/src/dataArg.h b/src/dataArg.h index dc5f57407..26ae7fe4b 100644 --- a/src/dataArg.h +++ b/src/dataArg.h @@ -56,17 +56,19 @@ typedef enum { typedef void (*StructDeinitFun)(void* struct_); typedef struct Arg Arg; + typedef union { Arg* next; uint8_t* buffer; } _arg_union; + struct Arg { - _arg_union _; - uint32_t size; - uint8_t type; - PIKA_BOOL serialized; - Hash name_hash; - uint8_t content[]; + _arg_union _; // 32/64 bit + uint32_t size; // 32 bit + uint8_t type; // 8 bit + PIKA_BOOL flag; // + Hash name_hash; // 32bit + uint8_t content[]; // n bit }; Arg* arg_getNext(Arg* self); @@ -131,8 +133,22 @@ uint8_t argType_isObject(ArgType type); #define arg_getNext(self) ((self)->_.next) #define arg_getSize(self) ((self)->size) + +#define ARG_FLAG_MASK_SERIALIZED 0x01 +#define ARG_FLAG_MASK_ISKEYWORD 0x02 + +#define arg_getSerialized(self) ((self)->flag & ARG_FLAG_MASK_SERIALIZED) +#define arg_setSerialized(self, _serialized) \ + ((self)->flag = ((self)->flag & ~ARG_FLAG_MASK_SERIALIZED) | \ + ((_serialized) ? ARG_FLAG_MASK_SERIALIZED : 0)) +#define arg_getIsKeyword(self) ((self)->flag & ARG_FLAG_MASK_ISKEYWORD) + +#define arg_setIsKeyword(self, _isKeyword) \ + ((self)->flag = ((self)->flag & ~ARG_FLAG_MASK_ISKEYWORD) | \ + ((_isKeyword) ? ARG_FLAG_MASK_ISKEYWORD : 0)) + #define arg_getContent(self) \ - ((self)->serialized ? (self)->content : ((self)->_.buffer)) + (arg_getSerialized(self) ? (self)->content : ((self)->_.buffer)) #define arg_getNext(self) ((self)->_.next) #define arg_setNext(self, __next) ((self)->_.next = (__next)) diff --git a/src/dataArgs.c b/src/dataArgs.c index c1ab5fe93..01aaf86e6 100644 --- a/src/dataArgs.c +++ b/src/dataArgs.c @@ -88,7 +88,7 @@ PIKA_RES args_setStr(Args* self, char* name, char* strIn) { PIKA_RES args_pushArg(Args* self, Arg* arg) { Arg* new_arg = NULL; - if (!arg->serialized) { + if (!arg_getSerialized(arg)) { new_arg = arg_copy(arg); arg_deinit(arg); } else { @@ -306,7 +306,7 @@ PIKA_RES __updateArg(Args* self, Arg* argNew) { arg_setNext((Arg*)priorNode, (Arg*)nodeToUpdate); goto exit; exit: - if (!argNew->serialized) { + if (!arg_getSerialized(argNew)) { return PIKA_RES_OK; } arg_freeContent(argNew); diff --git a/src/dataStack.c b/src/dataStack.c index 45bd8852b..8c6ec876e 100644 --- a/src/dataStack.c +++ b/src/dataStack.c @@ -74,14 +74,14 @@ void stack_pushPyload(Stack* stack, Arg* content, size_t size) { while (1) { } } - if (content->serialized) { + if (arg_getSerialized(content)) { __platform_memcpy(stack->sp, content, size); } else { __platform_memcpy(stack->sp, content, sizeof(Arg)); __platform_memcpy(stack->sp + sizeof(Arg), content->_.buffer, size - sizeof(Arg)); /* transfer to serialized form */ - ((Arg*)stack->sp)->serialized = PIKA_TRUE; + arg_setSerialized((Arg*)stack->sp, PIKA_TRUE); } stack->sp += size; } @@ -116,7 +116,7 @@ static int32_t _stack_pushArg(Stack* stack, Arg* arg, PIKA_BOOL is_alloc) { int32_t stack_pushArg(Stack* stack, Arg* arg) { pika_assert(arg != NULL); - if (arg->serialized) { + if (arg_getSerialized(arg)) { return _stack_pushArg(stack, arg, PIKA_TRUE); } return _stack_pushArg(stack, arg, PIKA_FALSE);