Add more types and APIs for attr_container (#1841)
Add more types for attr_container, e.g. uint8, uint32, uint64 Add more APIs for attr_container for int8, int16 and int32 types Rename fields of the union 'jvalue' and refactor some files that use attr_container
This commit is contained in:
@ -33,7 +33,7 @@
|
||||
</h1>
|
||||
<p>
|
||||
1. Download a simple runtime (build for ubuntu 20.04 64 bits, other platforms please build
|
||||
from the <a href="https://github.com/intel/wasm-micro-runtime">source code</a>)
|
||||
from the <a href="https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/samples/simple">source code</a>)
|
||||
</p>
|
||||
<p>
|
||||
2. In the terminal: <code>cd ~/Download && ./simple -a 82.156.57.236</code>
|
||||
@ -44,7 +44,7 @@
|
||||
<h5>
|
||||
Notes:
|
||||
</h5> We also have a <strong>UI-enabled runtime</strong>, please <a
|
||||
href="../static/upload/simple">download here</a> and enjoy.</strong> It may require
|
||||
href="../static/upload/wasm_runtime_wgl">download here</a> and enjoy.</strong> It may require
|
||||
a few more setups.
|
||||
<p>Before running the UI-enabled runtime, please install some required softwares:</p>
|
||||
<p><code>sudo apt-get install libsdl2-2.0-0:i386</code> </p>
|
||||
|
||||
@ -16,14 +16,18 @@ import logging
|
||||
import os
|
||||
|
||||
attr_type_list = [
|
||||
"ATTR_NONE",
|
||||
"ATTR_TYPE_SHORT",
|
||||
"ATTR_TYPE_INT",
|
||||
"ATTR_TYPE_BYTE", # = ATTR_TYPE_INT8
|
||||
"ATTR_TYPE_SHORT",# = ATTR_TYPE_INT16
|
||||
"ATTR_TYPE_INT", # = ATTR_TYPE_INT32
|
||||
"ATTR_TYPE_INT64",
|
||||
"ATTR_TYPE_BYTE",
|
||||
"ATTR_TYPE_UINT8",
|
||||
"ATTR_TYPE_UINT16",
|
||||
"ATTR_TYPE_UINT32",
|
||||
"ATTR_TYPE_UINT64",
|
||||
"ATTR_TYPE_FLOAT",
|
||||
"ATTR_TYPE_DOUBLE",
|
||||
"ATTR_NONE",
|
||||
"ATTR_NONE",
|
||||
"ATTR_TYPE_BOOLEAN",
|
||||
"ATTR_TYPE_STRING",
|
||||
"ATTR_TYPE_BYTEARRAY"
|
||||
@ -140,26 +144,38 @@ def decode_attr_container(msg):
|
||||
attr_type = attr_type_list[int(type_index[0])]
|
||||
buf = buf[1 : ]
|
||||
|
||||
if attr_type == "ATTR_TYPE_SHORT":
|
||||
if attr_type == "ATTR_TYPE_BYTE": # = ATTR_TYPE_INT8
|
||||
(attr_value, ) = struct.unpack('@c', buf[0 : 1])
|
||||
buf = buf[1 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_SHORT": # = ATTR_TYPE_INT16
|
||||
(attr_value, ) = struct.unpack('@h', buf[0 : 2])
|
||||
buf = buf[2 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_INT":
|
||||
(attr_value, ) = struct.unpack('@I', buf[0 : 4])
|
||||
elif attr_type == "ATTR_TYPE_INT": # = ATTR_TYPE_INT32
|
||||
(attr_value, ) = struct.unpack('@i', buf[0 : 4])
|
||||
buf = buf[4 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_INT64":
|
||||
(attr_value, ) = struct.unpack('@q', buf[0 : 8])
|
||||
buf = buf[8 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_BYTE":
|
||||
(attr_value, ) = struct.unpack('@c', buf[0 : 1])
|
||||
elif attr_type == "ATTR_TYPE_UINT8":
|
||||
(attr_value, ) = struct.unpack('@B', buf[0 : 1])
|
||||
buf = buf[1 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_UINT16":
|
||||
(attr_value, ) = struct.unpack('@H', buf[0 : 2])
|
||||
buf = buf[2 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_UINT32":
|
||||
(attr_value, ) = struct.unpack('@I', buf[0 : 4])
|
||||
buf = buf[4 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_UINT64":
|
||||
(attr_value, ) = struct.unpack('@Q', buf[0 : 8])
|
||||
buf = buf[8 : ]
|
||||
# continue
|
||||
elif attr_type == "ATTR_TYPE_FLOAT":
|
||||
(attr_value, ) = struct.unpack('@f', buf[0 : 4])
|
||||
buf = buf[4 : ]
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -15,11 +15,14 @@
|
||||
|
||||
typedef union jvalue {
|
||||
bool z;
|
||||
int8_t b;
|
||||
uint16_t c;
|
||||
int16_t s;
|
||||
int32_t i;
|
||||
int64_t j;
|
||||
int8_t i8;
|
||||
uint8_t u8;
|
||||
int16_t i16;
|
||||
uint16_t u16;
|
||||
int32_t i32;
|
||||
uint32_t u32;
|
||||
int64_t i64;
|
||||
uint64_t u64;
|
||||
float f;
|
||||
double d;
|
||||
} jvalue;
|
||||
@ -90,43 +93,64 @@ attr2json(const attr_container_t *attr_cont)
|
||||
type = *p++;
|
||||
|
||||
switch (type) {
|
||||
case ATTR_TYPE_SHORT:
|
||||
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.s)))
|
||||
case ATTR_TYPE_BYTE: /* = ATTR_TYPE_INT8 */
|
||||
bh_memcpy_s(&value.i8, 1, p, 1);
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.i8)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
p++;
|
||||
break;
|
||||
case ATTR_TYPE_SHORT: /* = ATTR_TYPE_INT16 */
|
||||
bh_memcpy_s(&value.i16, sizeof(int16_t), p, sizeof(int16_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.i16)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
/* another approach: cJSON_AddNumberToObject(root, key, value.s)
|
||||
*/
|
||||
p += 2;
|
||||
break;
|
||||
case ATTR_TYPE_INT:
|
||||
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.i)))
|
||||
case ATTR_TYPE_INT: /* = ATTR_TYPE_INT32 */
|
||||
bh_memcpy_s(&value.i32, sizeof(int32_t), p, sizeof(int32_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.i32)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
p += 4;
|
||||
break;
|
||||
case ATTR_TYPE_INT64:
|
||||
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.j)))
|
||||
bh_memcpy_s(&value.i64, sizeof(int64_t), p, sizeof(int64_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.i64)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
p += 8;
|
||||
break;
|
||||
case ATTR_TYPE_BYTE:
|
||||
bh_memcpy_s(&value.b, 1, p, 1);
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.b)))
|
||||
case ATTR_TYPE_UINT8:
|
||||
bh_memcpy_s(&value.u8, 1, p, 1);
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.u8)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
p++;
|
||||
break;
|
||||
case ATTR_TYPE_UINT16:
|
||||
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.c)))
|
||||
bh_memcpy_s(&value.u16, sizeof(uint16_t), p, sizeof(uint16_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.u16)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
p += 2;
|
||||
break;
|
||||
case ATTR_TYPE_UINT32:
|
||||
bh_memcpy_s(&value.u32, sizeof(uint32_t), p, sizeof(uint32_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.u32)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
p += 4;
|
||||
break;
|
||||
case ATTR_TYPE_UINT64:
|
||||
bh_memcpy_s(&value.u64, sizeof(uint64_t), p, sizeof(uint64_t));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.u64)))
|
||||
goto fail;
|
||||
cJSON_AddItemToObject(root, key, obj);
|
||||
p += 8;
|
||||
break;
|
||||
case ATTR_TYPE_FLOAT:
|
||||
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
|
||||
if (NULL == (obj = cJSON_CreateNumber(value.f)))
|
||||
|
||||
Reference in New Issue
Block a user