Quellcode durchsuchen

Refine wgl native functions call (#225)

Weining vor 6 Jahren
Ursprung
Commit
374e687938

+ 91 - 68
core/app-framework/wgl/native/wgl_btn_wrapper.c

@@ -11,115 +11,139 @@
 /* -------------------------------------------------------------------------
  * Button widget native function wrappers
  * -------------------------------------------------------------------------*/
-static int32
-lv_btn_create_wrapper(wasm_module_inst_t module_inst,
-               lv_obj_t *par, lv_obj_t *copy)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_create_wrapper)
 {
-    return wgl_native_wigdet_create(WIDGET_TYPE_BTN, par, copy, module_inst);
+    int32 res;
+    wgl_native_return_type(int32);
+    wgl_native_get_arg(uint32, par_obj_id);
+    wgl_native_get_arg(uint32, copy_obj_id);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    res = wgl_native_wigdet_create(WIDGET_TYPE_BTN, par_obj_id, copy_obj_id, module_inst);
+    wgl_native_set_return(res);
 }
 
-static void
-lv_btn_set_toggle_wrapper(wasm_module_inst_t module_inst,
-                          lv_obj_t * btn, bool tgl)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_set_toggle_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, btn);
+    wgl_native_get_arg(bool, tgl);
+
+    (void)exec_env;
     lv_btn_set_toggle(btn, tgl);
 }
 
-static void
-lv_btn_set_state_wrapper(wasm_module_inst_t module_inst,
-                         lv_obj_t * btn, lv_btn_state_t state)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_set_state_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, btn);
+    wgl_native_get_arg(lv_btn_state_t, state);
+
+    (void)exec_env;
     lv_btn_set_state(btn, state);
 }
 
-static void
-lv_btn_set_ink_in_time_wrapper(wasm_module_inst_t module_inst,
-                               lv_obj_t * btn, uint16_t time)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_set_ink_in_time_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, btn);
+    wgl_native_get_arg(uint16_t, time);
+
+    (void)exec_env;
     lv_btn_set_ink_in_time(btn, time);
 }
 
-static void
-lv_btn_set_ink_out_time_wrapper(wasm_module_inst_t module_inst,
-                                lv_obj_t * btn, uint16_t time)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_set_ink_out_time_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, btn);
+    wgl_native_get_arg(uint16_t, time);
+
+    (void)exec_env;
     lv_btn_set_ink_out_time(btn, time);
 }
 
-static void
-lv_btn_set_ink_wait_time_wrapper(wasm_module_inst_t module_inst,
-                                 lv_obj_t * btn, uint16_t time)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_set_ink_wait_time_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, btn);
+    wgl_native_get_arg(uint16_t, time);
+
+    (void)exec_env;
     lv_btn_set_ink_wait_time(btn, time);
 }
 
-static uint16_t
-lv_btn_get_ink_in_time_wrapper(wasm_module_inst_t module_inst,
-                               lv_obj_t * btn)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_get_ink_in_time_wrapper)
 {
-    (void)module_inst;
-    return lv_btn_get_ink_in_time(btn);
+    uint16_t res;
+    wgl_native_return_type(uint16_t);
+    wgl_native_get_arg(lv_obj_t *, btn);
+
+    (void)exec_env;
+    res = lv_btn_get_ink_in_time(btn);
+    wgl_native_set_return(res);
 }
 
-static uint16_t
-lv_btn_get_ink_out_time_wrapper(wasm_module_inst_t module_inst,
-                                lv_obj_t * btn)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_get_ink_out_time_wrapper)
 {
-    (void)module_inst;
-    return lv_btn_get_ink_out_time(btn);
+    uint16_t res;
+    wgl_native_return_type(uint16_t);
+    wgl_native_get_arg(lv_obj_t *, btn);
+
+    (void)exec_env;
+    res = lv_btn_get_ink_out_time(btn);
+    wgl_native_set_return(res);
 }
 
-static uint16_t
-lv_btn_get_ink_wait_time_wrapper(wasm_module_inst_t module_inst,
-                                 lv_obj_t * btn)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_get_ink_wait_time_wrapper)
 {
-    (void)module_inst;
-    return lv_btn_get_ink_wait_time(btn);
+    uint16_t res;
+    wgl_native_return_type(uint16_t);
+    wgl_native_get_arg(lv_obj_t *, btn);
+
+    (void)exec_env;
+    res = lv_btn_get_ink_wait_time(btn);
+    wgl_native_set_return(res);
 }
 
-static lv_btn_state_t
-lv_btn_get_state_wrapper(wasm_module_inst_t module_inst,
-                         lv_obj_t * btn)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_get_state_wrapper)
 {
-    (void)module_inst;
-    return lv_btn_get_state(btn);
+    lv_btn_state_t res;
+    wgl_native_return_type(lv_btn_state_t);
+    wgl_native_get_arg(lv_obj_t *, btn);
+
+    (void)exec_env;
+    res = lv_btn_get_state(btn);
+    wgl_native_set_return(res);
 }
 
-static bool
-lv_btn_get_toggle_wrapper(wasm_module_inst_t module_inst,
-                          lv_obj_t * btn)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_get_toggle_wrapper)
 {
-    (void)module_inst;
-    return lv_btn_get_toggle(btn);
+    bool res;
+    wgl_native_return_type(bool);
+    wgl_native_get_arg(lv_obj_t *, btn);
+
+    (void)exec_env;
+    res = lv_btn_get_toggle(btn);
+    wgl_native_set_return(res);
 }
 
-static void
-lv_btn_toggle_wrapper(wasm_module_inst_t module_inst,
-                      lv_obj_t * btn)
+DEFINE_WGL_NATIVE_WRAPPER(lv_btn_toggle_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, btn);
+
+    (void)exec_env;
     lv_btn_toggle(btn);
 }
 
 static WGLNativeFuncDef btn_native_func_defs[] = {
-    { BTN_FUNC_ID_CREATE, lv_btn_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1},  {-1} },
-    { BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle_wrapper, NO_RET, 3, {1, -1}, {-1} },
-    { BTN_FUNC_ID_SET_STATE, lv_btn_set_state_wrapper, NO_RET, 3, {1, -1}, {-1} },
-//    { BTN_FUNC_ID_SET_STYLE, _btn_set_style, NO_RET, 2, {0, -1}, {-1} },
-    { BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
-    { BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
-    { BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
-    { BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-    { BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-    { BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-    { BTN_FUNC_ID_GET_STATE, lv_btn_get_state_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-    { BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-    { BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, NO_RET, 2, {1, -1}, {-1} },
+    { BTN_FUNC_ID_CREATE,              lv_btn_create_wrapper,             2,  false },
+    { BTN_FUNC_ID_SET_TOGGLE,          lv_btn_set_toggle_wrapper,         2,  true },
+    { BTN_FUNC_ID_SET_STATE,           lv_btn_set_state_wrapper,          2,  true },
+    { BTN_FUNC_ID_SET_INK_IN_TIME,     lv_btn_set_ink_in_time_wrapper,    2,  true },
+    { BTN_FUNC_ID_SET_INK_OUT_TIME,    lv_btn_set_ink_out_time_wrapper,   2,  true },
+    { BTN_FUNC_ID_SET_INK_WAIT_TIME,   lv_btn_set_ink_wait_time_wrapper,  2,  true },
+    { BTN_FUNC_ID_GET_INK_IN_TIME,     lv_btn_get_ink_in_time_wrapper,    1,  true },
+    { BTN_FUNC_ID_GET_INK_OUT_TIME,    lv_btn_get_ink_out_time_wrapper,   1,  true },
+    { BTN_FUNC_ID_GET_INK_WAIT_TIME,   lv_btn_get_ink_wait_time_wrapper,  1,  true },
+    { BTN_FUNC_ID_GET_STATE,           lv_btn_get_state_wrapper,          1,  true },
+    { BTN_FUNC_ID_GET_TOGGLE,          lv_btn_get_toggle_wrapper,         1,  true },
+    { BTN_FUNC_ID_TOGGLE,              lv_btn_toggle_wrapper,             1,  true },
 
 };
 
@@ -128,10 +152,9 @@ void
 wasm_btn_native_call(wasm_exec_env_t exec_env,
                      int32 func_id, uint32 *argv, uint32 argc)
 {
-    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef);
 
-    wgl_native_func_call(module_inst,
+    wgl_native_func_call(exec_env,
                          btn_native_func_defs,
                          size,
                          func_id,

+ 62 - 37
core/app-framework/wgl/native/wgl_cb_wrapper.c

@@ -12,62 +12,88 @@
 /* -------------------------------------------------------------------------
  * Label widget native function wrappers
  * -------------------------------------------------------------------------*/
-static int32
-lv_cb_create_wrapper(wasm_module_inst_t module_inst,
-                     lv_obj_t *par, lv_obj_t *copy)
+DEFINE_WGL_NATIVE_WRAPPER(lv_cb_create_wrapper)
 {
-    return wgl_native_wigdet_create(WIDGET_TYPE_CB, par, copy, module_inst);
+    int32 res;
+    wgl_native_return_type(int32);
+    wgl_native_get_arg(uint32, par_obj_id);
+    wgl_native_get_arg(uint32, copy_obj_id);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    res = wgl_native_wigdet_create(WIDGET_TYPE_CB, par_obj_id, copy_obj_id, module_inst);
+    wgl_native_set_return(res);
 }
 
-static void
-lv_cb_set_text_wrapper(wasm_module_inst_t module_inst,
-                       lv_obj_t * cb, const char * txt)
+DEFINE_WGL_NATIVE_WRAPPER(lv_cb_set_text_wrapper)
 {
-    (void)module_inst;
-    lv_cb_set_text(cb, txt);
+    char *text;
+    wgl_native_get_arg(lv_obj_t *, cb);
+    wgl_native_get_arg(uint32, text_offset);
+    wgl_native_get_arg(uint32, text_len);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    if (!validate_app_addr(text_offset, text_len)
+        || !(text = addr_app_to_native(text_offset)))
+        return;
+
+    lv_cb_set_text(cb, text);
 }
 
-static void
-lv_cb_set_static_text_wrapper(wasm_module_inst_t module_inst,
-                              lv_obj_t * cb, const char * txt)
+DEFINE_WGL_NATIVE_WRAPPER(lv_cb_set_static_text_wrapper)
 {
-    (void)module_inst;
-    lv_cb_set_static_text(cb, txt);
+    char *text;
+    wgl_native_get_arg(lv_obj_t *, cb);
+    wgl_native_get_arg(uint32, text_offset);
+    wgl_native_get_arg(uint32, text_len);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    if (!validate_app_addr(text_offset, text_len)
+        || !(text = addr_app_to_native(text_offset)))
+        return;
+
+    lv_cb_set_static_text(cb, text);
 }
 
-static int32
-lv_cb_get_text_length_wrapper(wasm_module_inst_t module_inst,
-                              lv_obj_t *cb)
+DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_length_wrapper)
 {
-    const char *text = lv_cb_get_text(cb);
+    const char *text;
+    wgl_native_return_type(int32);
+    wgl_native_get_arg(lv_obj_t *, cb);
 
-    if (text == NULL)
-        return 0;
+    (void)exec_env;
 
-    return strlen(text);
+    text = lv_cb_get_text(cb);
+    wgl_native_set_return(text ? strlen(text): 0);
 }
 
-static char *
-lv_cb_get_text_wrapper(wasm_module_inst_t module_inst,
-                       lv_obj_t *cb, char *buffer, int buffer_len)
+DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_wrapper)
 {
-    const char *text = lv_cb_get_text(cb);
+    const char *text;
+    char *buffer;
+    wgl_native_return_type(uint32);
+    wgl_native_get_arg(lv_obj_t *, cb);
+    wgl_native_get_arg(uint32, buffer_offset);
+    wgl_native_get_arg(int, buffer_len);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
 
-    if (text == NULL)
-        return 0;
+    if (!validate_app_addr(buffer_offset, buffer_len)
+        || !(buffer = addr_app_to_native(buffer_offset)))
+        return;
 
-    strncpy(buffer, text, buffer_len - 1);
-    buffer[buffer_len - 1] = '\0';
+    if ((text = lv_cb_get_text(cb))) {
+        strncpy(buffer, text, buffer_len - 1);
+        buffer[buffer_len - 1] = '\0';
+    }
 
-    return buffer;
+    wgl_native_set_return(buffer_offset);
 }
 
 static WGLNativeFuncDef cb_native_func_defs[] = {
-        { CB_FUNC_ID_CREATE, lv_cb_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
-        { CB_FUNC_ID_SET_TEXT, lv_cb_set_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
-        { CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
-        { CB_FUNC_ID_GET_TEXT_LENGTH, lv_cb_get_text_length_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-        { CB_FUNC_ID_GET_TEXT, lv_cb_get_text_wrapper, RET_PTR, 4, {1, -1}, {2, -1} },
+    { CB_FUNC_ID_CREATE,            lv_cb_create_wrapper,            2,  false },
+    { CB_FUNC_ID_SET_TEXT,          lv_cb_set_text_wrapper,          3,  true },
+    { CB_FUNC_ID_SET_STATIC_TEXT,   lv_cb_set_static_text_wrapper,   3,  true },
+    { CB_FUNC_ID_GET_TEXT_LENGTH,   lv_cb_get_text_length_wrapper,   1,  true },
+    { CB_FUNC_ID_GET_TEXT,          lv_cb_get_text_wrapper,          3,  true },
 };
 
 /*************** Native Interface to Wasm App ***********/
@@ -75,10 +101,9 @@ void
 wasm_cb_native_call(wasm_exec_env_t exec_env,
                     int32 func_id, uint32 *argv, uint32 argc)
 {
-    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef);
 
-    wgl_native_func_call(module_inst,
+    wgl_native_func_call(exec_env,
                          cb_native_func_defs,
                          size,
                          func_id,

+ 48 - 30
core/app-framework/wgl/native/wgl_label_wrapper.c

@@ -12,53 +12,72 @@
 /* -------------------------------------------------------------------------
  * Label widget native function wrappers
  * -------------------------------------------------------------------------*/
-static int32
-lv_label_create_wrapper(wasm_module_inst_t module_inst,
-                        lv_obj_t *par, lv_obj_t *copy)
+DEFINE_WGL_NATIVE_WRAPPER(lv_label_create_wrapper)
 {
-    return wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par, copy, module_inst);
+    int32 res;
+    wgl_native_return_type(int32);
+    wgl_native_get_arg(uint32, par_obj_id);
+    wgl_native_get_arg(uint32, copy_obj_id);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    res = wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par_obj_id, copy_obj_id, module_inst);
+    wgl_native_set_return(res);
 }
 
-static void
-lv_label_set_text_wrapper(wasm_module_inst_t module_inst,
-                          lv_obj_t * label, const char * text)
+DEFINE_WGL_NATIVE_WRAPPER(lv_label_set_text_wrapper)
 {
-    (void)module_inst;
+    char *text;
+    wgl_native_get_arg(lv_obj_t *, label);
+    wgl_native_get_arg(uint32, text_offset);
+    wgl_native_get_arg(uint32, text_len);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    if (!validate_app_addr(text_offset, text_len)
+        || !(text = addr_app_to_native(text_offset)))
+        return;
+
     lv_label_set_text(label, text);
 }
 
-static int32
-lv_label_get_text_length_wrapper(wasm_module_inst_t module_inst,
-                                 lv_obj_t *label)
+DEFINE_WGL_NATIVE_WRAPPER(lv_label_get_text_length_wrapper)
 {
-    char *text = lv_label_get_text(label);
+    wgl_native_return_type(int32);
+    wgl_native_get_arg(lv_obj_t *, label);
+    const char *text;
 
-    if (text == NULL)
-        return 0;
+    (void)exec_env;
 
-    return strlen(text);
+    text = lv_label_get_text(label);
+    wgl_native_set_return(text ? strlen(text) : 0);
 }
 
-static char *
-lv_label_get_text_wrapper(wasm_module_inst_t module_inst,
-                          lv_obj_t *label, char *buffer, int buffer_len)
+DEFINE_WGL_NATIVE_WRAPPER(lv_label_get_text_wrapper)
 {
-    char *text = lv_label_get_text(label);
+    const char *text;
+    char *buffer;
+    wgl_native_return_type(uint32);
+    wgl_native_get_arg(lv_obj_t *, label);
+    wgl_native_get_arg(uint32, buffer_offset);
+    wgl_native_get_arg(int, buffer_len);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
 
-    if (text == NULL)
-        return 0;
+    if (!validate_app_addr(buffer_offset, buffer_len)
+        || !(buffer = addr_app_to_native(buffer_offset)))
+        return;
 
-    strncpy(buffer, text, buffer_len - 1);
-    buffer[buffer_len - 1] = '\0';
+    if ((text = lv_label_get_text(label))) {
+        strncpy(buffer, text, buffer_len - 1);
+        buffer[buffer_len - 1] = '\0';
+    }
 
-    return buffer;
+    wgl_native_set_return(buffer_offset);
 }
 
 static WGLNativeFuncDef label_native_func_defs[] = {
-        { LABEL_FUNC_ID_CREATE, lv_label_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
-        { LABEL_FUNC_ID_SET_TEXT, lv_label_set_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
-        { LABEL_FUNC_ID_GET_TEXT_LENGTH, lv_label_get_text_length_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-        { LABEL_FUNC_ID_GET_TEXT, lv_label_get_text_wrapper, RET_PTR, 4, {1, -1}, {2, -1} },
+    { LABEL_FUNC_ID_CREATE,           lv_label_create_wrapper,          2,  false },
+    { LABEL_FUNC_ID_SET_TEXT,         lv_label_set_text_wrapper,        3,  true },
+    { LABEL_FUNC_ID_GET_TEXT_LENGTH,  lv_label_get_text_length_wrapper, 1,  true },
+    { LABEL_FUNC_ID_GET_TEXT,         lv_label_get_text_wrapper,        3,  true },
 };
 
 /*************** Native Interface to Wasm App ***********/
@@ -66,10 +85,9 @@ void
 wasm_label_native_call(wasm_exec_env_t exec_env,
                        int32 func_id, uint32 *argv, uint32 argc)
 {
-    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef);
 
-    wgl_native_func_call(module_inst,
+    wgl_native_func_call(exec_env,
                          label_native_func_defs,
                          size,
                          func_id,

+ 31 - 17
core/app-framework/wgl/native/wgl_list_wrapper.c

@@ -12,38 +12,53 @@
 /* -------------------------------------------------------------------------
  * List widget native function wrappers
  * -------------------------------------------------------------------------*/
-static int32
-lv_list_create_wrapper(wasm_module_inst_t module_inst,
-                       lv_obj_t *par, lv_obj_t *copy)
+DEFINE_WGL_NATIVE_WRAPPER(lv_list_create_wrapper)
 {
-    return wgl_native_wigdet_create(WIDGET_TYPE_LIST, par, copy, module_inst);
+    int32 res;
+    wgl_native_return_type(int32);
+    wgl_native_get_arg(uint32, par_obj_id);
+    wgl_native_get_arg(uint32, copy_obj_id);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    res = wgl_native_wigdet_create(WIDGET_TYPE_LIST, par_obj_id, copy_obj_id, module_inst);
+    wgl_native_set_return(res);
 }
 
-static int32
-lv_list_add_btn_wrapper(wasm_module_inst_t module_inst,
-                        lv_obj_t *list, const char *text)
+DEFINE_WGL_NATIVE_WRAPPER(lv_list_add_btn_wrapper)
 {
+    wgl_native_return_type(int32);
+    wgl_native_get_arg(lv_obj_t *, list);
+    wgl_native_get_arg(uint32, text_offset);
+    wgl_native_get_arg(uint32, text_len);
     uint32 btn_obj_id;
     lv_obj_t *btn;
     uint32 mod_id;
+    char *text;
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
 
-    btn = lv_list_add_btn(list, NULL, text);
+    if (!validate_app_addr(text_offset, text_len)
+        || !(text = addr_app_to_native(text_offset)))
+        return;
 
-    if (btn == NULL)
-        return 0;
+    if (!(btn = lv_list_add_btn(list, NULL, text))) {
+        wasm_runtime_set_exception(module_inst, "add button to list fail.");
+        return;
+    }
 
     mod_id = app_manager_get_module_id(Module_WASM_App, module_inst);
     bh_assert(mod_id != ID_NONE);
 
-    if (wgl_native_add_object(btn, mod_id, &btn_obj_id))
-        return btn_obj_id; /* success return */
+    if (!wgl_native_add_object(btn, mod_id, &btn_obj_id)) {
+        wasm_runtime_set_exception(module_inst, "add button to object list fail.");
+        return;
+    }
 
-    return 0;
+    wgl_native_set_return(btn_obj_id);
 }
 
 static WGLNativeFuncDef list_native_func_defs[] = {
-    { LIST_FUNC_ID_CREATE, lv_list_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1},  {-1} },
-    { LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, HAS_RET, 3, {1, -1}, {2, -1} },
+    { LIST_FUNC_ID_CREATE,       lv_list_create_wrapper,       2,  false },
+    { LIST_FUNC_ID_ADD_BTN,      lv_list_add_btn_wrapper,      3,  true },
 };
 
 /*************** Native Interface to Wasm App ***********/
@@ -51,10 +66,9 @@ void
 wasm_list_native_call(wasm_exec_env_t exec_env,
                       int32 func_id, uint32 *argv, uint32 argc)
 {
-    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef);
 
-    wgl_native_func_call(module_inst,
+    wgl_native_func_call(exec_env,
                          list_native_func_defs,
                          size,
                          func_id,

+ 46 - 124
core/app-framework/wgl/native/wgl_native_utils.c

@@ -10,18 +10,28 @@
 
 #define THROW_EXC(msg) wasm_runtime_set_exception(module_inst, msg);
 
-void
-wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
-
-uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy,
+uint32 wgl_native_wigdet_create(int8 widget_type,
+                                uint32 par_obj_id,
+                                uint32 copy_obj_id,
                                 wasm_module_inst_t module_inst)
 {
     uint32 obj_id;
-    lv_obj_t *wigdet = NULL;
+    lv_obj_t *wigdet = NULL, *par = NULL, *copy = NULL;
     uint32 mod_id;
 
     //TODO: limit total widget number
 
+    /* validate the parent object id if not equal to 0 */
+    if (par_obj_id != 0 && !wgl_native_validate_object(par_obj_id, &par)) {
+        THROW_EXC("create widget with invalid parent object.");
+        return 0;
+    }
+    /* validate the copy object id if not equal to 0 */
+    if (copy_obj_id != 0 && !wgl_native_validate_object(copy_obj_id, &copy)) {
+        THROW_EXC("create widget with invalid copy object.");
+        return 0;
+    }
+
     if (par == NULL)
         par = lv_disp_get_scr_act(NULL);
 
@@ -48,146 +58,58 @@ uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy,
     return 0;
 }
 
-static void invokeNative(intptr_t argv[], uint32 argc, void (*native_code)())
-{
-    bh_assert(argc >= 1);
-
-    switch(argc) {
-        case 1:
-            native_code(argv[0]);
-            break;
-        case 2:
-            native_code(argv[0], argv[1]);
-            break;
-        case 3:
-            native_code(argv[0], argv[1], argv[2]);
-            break;
-        case 4:
-            native_code(argv[0], argv[1], argv[2], argv[3]);
-            break;
-        case 5:
-            native_code(argv[0], argv[1], argv[2], argv[3], argv[4]);
-            break;
-        case 6:
-            native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
-            break;
-        case 7:
-            native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
-                        argv[6]);
-            break;
-        case 8:
-            native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
-                        argv[6], argv[7]);
-            break;
-        case 9:
-            native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
-                        argv[6], argv[7], argv[8]);
-            break;
-        case 10:
-            native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
-                        argv[6], argv[7], argv[8], argv[9]);
-            break;
-
-        default:
-        {
-            /* FIXME: If this happen, add more cases. */
-            wasm_module_inst_t module_inst = (wasm_module_inst_t)argv[0];
-            THROW_EXC("the argument number of native function exceeds maximum");
-            return;
-        }
-    }
-}
-
-typedef void (*GenericFunctionPointer)();
-typedef int32 (*Int32FuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
-typedef void (*VoidFuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
-
-static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
-static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
-
-void wgl_native_func_call(wasm_module_inst_t module_inst,
+void wgl_native_func_call(wasm_exec_env_t exec_env,
                           WGLNativeFuncDef *funcs,
                           uint32 size,
                           int32 func_id,
                           uint32 *argv,
                           uint32 argc)
 {
+    typedef void (*WGLNativeFuncPtr)(wasm_exec_env_t, uint64*, uint32*);
+    WGLNativeFuncPtr wglNativeFuncPtr;
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     WGLNativeFuncDef *func_def = funcs;
     WGLNativeFuncDef *func_def_end = func_def + size;
 
+    /* Note: argv is validated in wasm_runtime_invoke_native()
+     * with pointer length equals to 1. Here validate the argv
+     * buffer again but with its total length in bytes */
+    if (!wasm_runtime_validate_native_addr(module_inst, argv, argc * sizeof(uint32)))
+        return;
+
     while (func_def < func_def_end) {
-        if (func_def->func_id == func_id) {
-            int i, obj_arg_num = 0, ptr_arg_num = 0, argc1 = 0;
-            intptr_t argv_copy_buf[16];
-            intptr_t *argv_copy = argv_copy_buf;
-
-            argc1++; /* module_inst */
-            argc1 += func_def->arg_num;
-            if (argc1 > 16) {
-                argv_copy = (intptr_t *)wasm_runtime_malloc(func_def->arg_num *
-                                                  sizeof(intptr_t));
-                if (argv_copy == NULL)
+        if (func_def->func_id == func_id
+            && (uint32)func_def->arg_num == argc) {
+            uint64 argv_copy_buf[16], size;
+            uint64 *argv_copy = argv_copy_buf;
+            int i;
+
+            if (argc > sizeof(argv_copy_buf) / sizeof(uint64)) {
+                size = sizeof(uint64) * (uint64)argc;
+                if (size >= UINT32_MAX
+                    || !(argv_copy = wasm_runtime_malloc((uint32)size))) {
+                    THROW_EXC("allocate memory failed.");
                     return;
+                }
+                memset(argv_copy, 0, (uint32)size);
             }
 
             /* Init argv_copy */
-            argv_copy[0] = (intptr_t)module_inst;
             for (i = 0; i < func_def->arg_num; i++)
-                argv_copy[i + 1] = (intptr_t)argv[i];
-
-            /* Validate object arguments */
-            i = 0;
-            for (; i < OBJ_ARG_NUM_MAX && func_def->obj_arg_indexes[i] != 0xff;
-                   i++, obj_arg_num++) {
-                uint8 index = func_def->obj_arg_indexes[i];
-                bool null_ok = index & NULL_OK;
-
-                index = index & (~NULL_OK);
-
-                /* Some API's allow to pass NULL obj, such as xxx_create() */
-                if (argv_copy[index] == 0) {
-                    if (!null_ok) {
-                        THROW_EXC("the object id is 0 and invalid");
-                        goto fail;
-                    }
-                    /* Continue so that to pass null object validation */
-                    continue;
-                }
+                *(uint32*)&argv_copy[i] = argv[i];
 
-                if (!wgl_native_validate_object(argv_copy[index],
-                                         (lv_obj_t **)&argv_copy[index])) {
+            /* Validate the first argument which is a lvgl object if needed */
+            if (func_def->check_obj) {
+                lv_obj_t *obj = NULL;
+                if (!wgl_native_validate_object(argv[0], &obj)) {
                     THROW_EXC("the object is invalid");
                     goto fail;
                 }
+                *(lv_obj_t **)&argv_copy[0] = obj;
             }
 
-            /* Validate address arguments */
-            i = 0;
-            for (; i < PTR_ARG_NUM_MAX && func_def->ptr_arg_indexes[i] != 0xff;
-                   i++, ptr_arg_num++) {
-                uint8 index = func_def->ptr_arg_indexes[i];
-
-                /* The index+1 arg is the data size to be validated */
-                if (!validate_app_addr(argv_copy[index], argv_copy[index + 1]))
-                    goto fail;
-
-                /* Convert to native address before call lvgl function */
-                argv_copy[index] = (intptr_t)addr_app_to_native(argv_copy[index]);
-            }
-
-            if (func_def->has_ret == NO_RET)
-                invokeNative_Void(argv_copy,
-                                  argc1,
-                                  func_def->func_ptr);
-            else {
-                argv[0] = invokeNative_Int32(argv_copy,
-                                             argc1,
-                                             func_def->func_ptr);
-                /* Convert to app memory offset if return value is a
-                 * native address pointer */
-                if (func_def->has_ret == RET_PTR)
-                    argv[0] = addr_native_to_app((char *)(intptr_t)argv[0]);
-            }
+            wglNativeFuncPtr = (WGLNativeFuncPtr)func_def->func_ptr;
+            wglNativeFuncPtr(exec_env, argv_copy, argv);
 
             if (argv_copy != argv_copy_buf)
                 wasm_runtime_free(argv_copy);

+ 10 - 25
core/app-framework/wgl/native/wgl_native_utils.h

@@ -15,19 +15,12 @@ extern "C" {
 #include "wasm_export.h"
 #include "bi-inc/wgl_shared_utils.h"
 
-#define OBJ_ARG_NUM_MAX 4
-#define PTR_ARG_NUM_MAX 4
+#define wgl_native_return_type(type) type *wgl_ret = (type*)(args_ret)
+#define wgl_native_get_arg(type, name) type name = *((type*)(args++))
+#define wgl_native_set_return(val) *wgl_ret = (val)
 
-#define NULL_OK  0x80
-
-enum {
-    /* The function has a normal return value (not a pointer) */
-    HAS_RET,
-    /* The function doesn't have return value */
-    NO_RET,
-    /* The function's return value is a native address pointer */
-    RET_PTR
-};
+#define DEFINE_WGL_NATIVE_WRAPPER(func_name) \
+static void func_name(wasm_exec_env_t exec_env, uint64 *args, uint32 *args_ret)
 
 enum {
   WIDGET_TYPE_BTN,
@@ -46,19 +39,11 @@ typedef struct WGLNativeFuncDef {
     /* Native function pointer */
     void *func_ptr;
 
-    /* whether has return value */
-    uint8 has_ret;
-
     /* argument number */
     uint8 arg_num;
 
-    /* low 7 bit: obj argument index
-     * highest 1 bit: allow obj be null or not
-     * -1 means the end of this array */
-    uint8 obj_arg_indexes[OBJ_ARG_NUM_MAX];
-
-    /* pointer argument indexes, -1 means the end of this array */
-    uint8 ptr_arg_indexes[PTR_ARG_NUM_MAX];
+    /* whether the first argument is lvgl object and needs validate */
+    bool check_obj;
 } WGLNativeFuncDef;
 
 bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj);
@@ -66,11 +51,11 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj);
 bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id);
 
 uint32 wgl_native_wigdet_create(int8 widget_type,
-                                lv_obj_t *par,
-                                lv_obj_t *copy,
+                                uint32 par_obj_id,
+                                uint32 copy_obj_id,
                                 wasm_module_inst_t module_inst);
 
-void wgl_native_func_call(wasm_module_inst_t module_inst,
+void wgl_native_func_call(wasm_exec_env_t exec_env,
                           WGLNativeFuncDef *funcs,
                           uint32 size,
                           int32 func_id,

+ 41 - 30
core/app-framework/wgl/native/wgl_obj_wrapper.c

@@ -322,29 +322,34 @@ void wgl_exit(void)
 /* -------------------------------------------------------------------------
  * Obj native function wrappers
  * -------------------------------------------------------------------------*/
-static lv_res_t
-lv_obj_del_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
+DEFINE_WGL_NATIVE_WRAPPER(lv_obj_del_wrapper)
 {
-    (void)module_inst;
+    lv_res_t res;
+    wgl_native_return_type(lv_res_t);
+    wgl_native_get_arg(lv_obj_t *, obj);
+
+    (void)exec_env;
 
     /* Recursively delete object node in the list belong to this
      * parent object including itself */
     _obj_del_recursive(obj);
-
-    return lv_obj_del(obj);
+    res = lv_obj_del(obj);
+    wgl_native_set_return(res);
 }
 
-static void
-lv_obj_del_async_wrapper(wasm_module_inst_t module_inst, lv_obj_t * obj)
+DEFINE_WGL_NATIVE_WRAPPER(lv_obj_del_async_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, obj);
+
+    (void)exec_env;
     lv_obj_del_async(obj);
 }
 
-static void
-lv_obj_clean_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
+DEFINE_WGL_NATIVE_WRAPPER(lv_obj_clean_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, obj);
+
+    (void)exec_env;
 
     /* Recursively delete child object node in the list belong to this
      * parent object */
@@ -354,33 +359,40 @@ lv_obj_clean_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
     lv_obj_clean(obj);
 }
 
-static void
-lv_obj_align_wrapper(wasm_module_inst_t module_inst,
-                     lv_obj_t * obj,
-                     const lv_obj_t * base,
-                     lv_align_t align,
-                     lv_coord_t x_mod,
-                     lv_coord_t y_mod)
+DEFINE_WGL_NATIVE_WRAPPER(lv_obj_align_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, obj);
+    wgl_native_get_arg(uint32, base_obj_id);
+    wgl_native_get_arg(lv_align_t, align);
+    wgl_native_get_arg(lv_coord_t, x_mod);
+    wgl_native_get_arg(lv_coord_t, y_mod);
+    lv_obj_t *base = NULL;
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+
+    /* validate the base object id if not equal to 0 */
+    if (base_obj_id != 0 && !wgl_native_validate_object(base_obj_id, &base)) {
+        wasm_runtime_set_exception(module_inst, "align with invalid base object.");
+        return;
+    }
+
     lv_obj_align(obj, base, align, x_mod, y_mod);
 }
 
-static void
-lv_obj_set_event_cb_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
+DEFINE_WGL_NATIVE_WRAPPER(lv_obj_set_event_cb_wrapper)
 {
-    (void)module_inst;
+    wgl_native_get_arg(lv_obj_t *, obj);
+    (void)exec_env;
     lv_obj_set_event_cb(obj, internal_lv_obj_event_cb);
 }
-/* ------------------------------------------------------------------------- */
 
+/* ------------------------------------------------------------------------- */
 
 static WGLNativeFuncDef obj_native_func_defs[] = {
-    { OBJ_FUNC_ID_DEL, lv_obj_del_wrapper, HAS_RET, 2, {1, -1}, {-1} },
-    { OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async_wrapper, NO_RET, 2, {1, -1}, {-1} },
-    { OBJ_FUNC_ID_CLEAN, lv_obj_clean_wrapper, NO_RET, 2, {1, -1}, {-1} },
-    { OBJ_FUNC_ID_ALIGN, lv_obj_align_wrapper, NO_RET, 6, {1, 2 | NULL_OK, -1}, {-1} },
-    { OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, NO_RET, 2, {1, -1}, {-1} },
+    { OBJ_FUNC_ID_DEL,         lv_obj_del_wrapper,            1,  true },
+    { OBJ_FUNC_ID_DEL_ASYNC,   lv_obj_del_async_wrapper,      1,  true },
+    { OBJ_FUNC_ID_CLEAN,       lv_obj_clean_wrapper,          1,  true },
+    { OBJ_FUNC_ID_ALIGN,       lv_obj_align_wrapper,          5,  true },
+    { OBJ_FUNC_ID_SET_EVT_CB,  lv_obj_set_event_cb_wrapper,   1,  true },
 };
 
 /*************** Native Interface to Wasm App ***********/
@@ -388,10 +400,9 @@ void
 wasm_obj_native_call(wasm_exec_env_t exec_env,
                      int32 func_id, uint32 *argv, uint32 argc)
 {
-    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef);
 
-    wgl_native_func_call(module_inst,
+    wgl_native_func_call(exec_env,
                          obj_native_func_defs,
                          size,
                          func_id,

+ 11 - 0
core/iwasm/include/wasm_export.h

@@ -362,6 +362,17 @@ wasm_application_execute_func(wasm_module_inst_t module_inst,
 const char *
 wasm_runtime_get_exception(wasm_module_inst_t module_inst);
 
+/**
+ * Set exception info of the WASM module instance.
+ *
+ * @param module_inst the WASM module instance
+ *
+ * @param exception the exception string
+ */
+void
+wasm_runtime_set_exception(wasm_module_inst_t module_inst,
+                           const char *exception);
+
 /**
  * Clear exception info of the WASM module instance.
  *