Explorar el Código

Add parameter module inst for native wrapper functions (#117)

And add asm code of em64/arm/mips version to call native wrapper functions;
Fix some issues of calling wrapper functions;
wenyongh hace 6 años
padre
commit
26149021ff
Se han modificado 55 ficheros con 1283 adiciones y 490 borrados
  1. 2 1
      core/app-mgr/app-manager/event.c
  2. 4 3
      core/app-mgr/app-manager/module_wasm_app.c
  3. 3 3
      core/app-mgr/app-manager/resource_reg.c
  4. 8 6
      core/iwasm/lib/app-libs/base/request.c
  5. 0 1
      core/iwasm/lib/app-libs/base/request.h
  6. 3 3
      core/iwasm/lib/app-libs/base/timer.c
  7. 0 1
      core/iwasm/lib/app-libs/base/wasm_app.h
  8. 4 4
      core/iwasm/lib/app-libs/extension/connection/connection.c
  9. 3 3
      core/iwasm/lib/app-libs/extension/gui/src/wgl_btn.c
  10. 2 2
      core/iwasm/lib/app-libs/extension/gui/src/wgl_cb.c
  11. 2 2
      core/iwasm/lib/app-libs/extension/gui/src/wgl_label.c
  12. 2 2
      core/iwasm/lib/app-libs/extension/gui/src/wgl_list.c
  13. 2 2
      core/iwasm/lib/app-libs/extension/gui/src/wgl_obj.c
  14. 3 3
      core/iwasm/lib/app-libs/extension/sensor/sensor.c
  15. 10 5
      core/iwasm/lib/native-interface/connection_api.h
  16. 18 8
      core/iwasm/lib/native-interface/gui_api.h
  17. 84 33
      core/iwasm/lib/native-interface/native_interface.h
  18. 1 1
      core/iwasm/lib/native-interface/readme.txt
  19. 43 0
      core/iwasm/lib/native-interface/req_resp_api.h
  20. 0 1
      core/iwasm/lib/native-interface/restful_utils.c
  21. 6 4
      core/iwasm/lib/native-interface/sensor_api.h
  22. 45 23
      core/iwasm/lib/native-interface/shared_utils.h
  23. 48 0
      core/iwasm/lib/native-interface/timer_api.h
  24. 102 0
      core/iwasm/lib/native-interface/wasm_export_api.h
  25. 10 15
      core/iwasm/lib/native/base/base_lib_export.c
  26. 10 9
      core/iwasm/lib/native/base/request_response.c
  27. 0 1
      core/iwasm/lib/native/base/runtime_lib.h
  28. 13 6
      core/iwasm/lib/native/base/timer_wrapper.c
  29. 11 8
      core/iwasm/lib/native/extension/connection/connection_wrapper.c
  30. 3 1
      core/iwasm/lib/native/extension/gui/wgl_btn_wrapper.c
  31. 3 1
      core/iwasm/lib/native/extension/gui/wgl_cb_wrapper.c
  32. 3 1
      core/iwasm/lib/native/extension/gui/wgl_label_wrapper.c
  33. 3 1
      core/iwasm/lib/native/extension/gui/wgl_list_wrapper.c
  34. 3 1
      core/iwasm/lib/native/extension/gui/wgl_obj_wrapper.c
  35. 24 14
      core/iwasm/lib/native/extension/sensor/runtime_sensor.c
  36. 10 5
      core/iwasm/lib/native/extension/sensor/runtime_sensor.h
  37. 61 65
      core/iwasm/lib/native/libc/libc_wrapper.c
  38. 49 45
      core/iwasm/runtime/platform/linux-sgx/wasm_native.c
  39. 54 51
      core/iwasm/runtime/platform/linux/wasm_native.c
  40. 75 0
      core/iwasm/runtime/vmcore-wasm/invokeNative_arm.s
  41. 72 0
      core/iwasm/runtime/vmcore-wasm/invokeNative_em64.s
  42. 85 0
      core/iwasm/runtime/vmcore-wasm/invokeNative_mips.s
  43. 5 3
      core/iwasm/runtime/vmcore-wasm/vmcore.cmake
  44. 12 64
      core/iwasm/runtime/vmcore-wasm/wasm_interp.c
  45. 1 1
      core/iwasm/runtime/vmcore-wasm/wasm_loader.c
  46. 278 25
      core/iwasm/runtime/vmcore-wasm/wasm_runtime.c
  47. 5 0
      core/iwasm/runtime/vmcore-wasm/wasm_runtime.h
  48. 2 2
      core/shared-lib/mem-alloc/ems/ems_alloc.c
  49. 1 1
      core/shared-lib/platform/darwin/bh_platform.c
  50. 31 14
      samples/littlevgl/vgl-wasm-runtime/src/display_indev.h
  51. 3 2
      samples/littlevgl/vgl-wasm-runtime/src/ext_lib_export.c
  52. 29 18
      samples/littlevgl/vgl-wasm-runtime/src/platform/linux/display_indev.c
  53. 29 18
      samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/display_indev.c
  54. 1 0
      test-tools/host-tool/CMakeLists.txt
  55. 2 7
      test-tools/host-tool/src/main.c

+ 2 - 1
core/app-mgr/app-manager/event.c

@@ -176,7 +176,8 @@ void am_publish_event(request_t * event)
                 if (c->subscriber_id == ID_HOST) {
                     send_request_to_host(event);
                 } else {
-                    module_request_handler(event, (void *)c->subscriber_id);
+                    module_request_handler
+                        (event, (void *)(uintptr_t)c->subscriber_id);
                 }
                 c = c->next;
             }

+ 4 - 3
core/app-mgr/app-manager/module_wasm_app.c

@@ -224,8 +224,8 @@ static void app_instance_queue_callback(void *queue_msg)
                 app_manager_printf("Cannot find function _on_timer_callback\n");
                 break;
             }
-            unsigned int timer_id = (unsigned int) bh_message_payload(
-                    queue_msg);
+            unsigned int timer_id = (unsigned int)(uintptr_t)
+                                    bh_message_payload(queue_msg);
             argv[0] = timer_id;
             if (!wasm_runtime_call_wasm(inst, NULL, func_onTimer, 1, argv)) {
                 app_manager_printf("Got exception running wasm code: %s\n",
@@ -642,7 +642,8 @@ static bool wasm_app_module_uninstall(request_t *msg)
 static bool wasm_app_module_handle_host_url(void *queue_msg)
 {
     //todo: implement in future
-    app_manager_printf("App handles host url address %d\n", (int) queue_msg);
+    app_manager_printf("App handles host url address %d\n",
+                       (int)(uintptr_t)queue_msg);
     return false;
 }
 

+ 3 - 3
core/app-mgr/app-manager/resource_reg.c

@@ -33,7 +33,7 @@ static app_res_register_t * g_resources = NULL;
 
 void module_request_handler(request_t *request, void *user_data)
 {
-    unsigned int mod_id = (unsigned int) user_data;
+    unsigned int mod_id = (unsigned int)(uintptr_t)user_data;
     bh_message_t msg;
     module_data *m_data;
     request_t *req;
@@ -99,7 +99,7 @@ void targeted_app_request_handler(request_t *request, void *unused)
         goto end;
     }
 
-    module_request_handler(request, (void *)m_data->id);
+    module_request_handler(request, (void *)(uintptr_t)m_data->id);
     end: request->url = url;
 
 }
@@ -138,7 +138,7 @@ void * am_dispatch_request(request_t *request)
 
     while (r) {
         if (check_url_start(request->url, strlen(request->url), r->url) > 0) {
-            r->request_handler(request, (void *)r->register_id);
+            r->request_handler(request, (void *)(uintptr_t)r->register_id);
             return r;
         }
         r = r->next;

+ 8 - 6
core/iwasm/lib/app-libs/base/request.c

@@ -18,6 +18,8 @@
 #include "request.h"
 #include "shared_utils.h"
 #include "wasm_app.h"
+#include "req_resp_api.h"
+#include "timer_api.h"
 
 #define TRANSACTION_TIMEOUT_MS 5000
 
@@ -138,15 +140,15 @@ static bool register_url_handler(const char *url,
 
     // tell app mgr to route this url to me
     if (reg_type == Reg_Request)
-        wasm_register_resource((int32)url);
+        wasm_register_resource(url);
     else
-        wasm_sub_event((int32)url);
+        wasm_sub_event(url);
 
     return true;
 }
 
 bool api_register_resource_handler(const char *url,
-        request_handler_f request_handler)
+                                   request_handler_f request_handler)
 {
     return register_url_handler(url, request_handler, Reg_Request);
 }
@@ -242,7 +244,7 @@ void api_send_request(request_t * request, response_handler_f response_handler,
         }
     }
 
-    wasm_post_request((int32)buffer, size);
+    wasm_post_request(buffer, size);
 
     free_req_resp_packet(buffer);
 }
@@ -329,7 +331,7 @@ void api_response_send(response_t *response)
     if (buffer == NULL)
         return;
 
-    wasm_response_send((int32)buffer, size);
+    wasm_response_send(buffer, size);
     free_req_resp_packet(buffer);
 }
 
@@ -343,7 +345,7 @@ bool api_publish_event(const char *url, int fmt, void *payload, int payload_len)
     char * buffer = pack_request(request, &size);
     if (buffer == NULL)
         return false;
-    wasm_post_request((int32)buffer, size);
+    wasm_post_request(buffer, size);
 
     free_req_resp_packet(buffer);
 

+ 0 - 1
core/iwasm/lib/app-libs/base/request.h

@@ -17,7 +17,6 @@
 #ifndef _AEE_REQUEST_H_
 #define _AEE_REQUEST_H_
 
-#include "native_interface.h"
 #include "shared_utils.h"
 
 #ifdef __cplusplus

+ 3 - 3
core/iwasm/lib/app-libs/base/timer.c

@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "timer_wasm_app.h"
-#include "native_interface.h"
-
 #include <stdlib.h>
 #include <string.h>
 
+#include "timer_wasm_app.h"
+#include "timer_api.h"
+
 #if 1
 #include <stdio.h>
 #else

+ 0 - 1
core/iwasm/lib/app-libs/base/wasm_app.h

@@ -32,7 +32,6 @@
 #ifndef _LIB_AEE_H_
 #define _LIB_AEE_H_
 
-#include "native_interface.h"
 #include "shared_utils.h"
 #include "attr_container.h"
 #include "request.h"

+ 4 - 4
core/iwasm/lib/app-libs/extension/connection/connection.c

@@ -15,7 +15,7 @@
  */
 
 #include "connection.h"
-#include "native_interface.h"
+#include "connection_api.h"
 
 /* Raw connection structure */
 typedef struct _connection {
@@ -44,7 +44,7 @@ connection_t *api_open_connection(const char *name,
     char *args_buffer = (char *)args;
     uint32 handle, args_len = attr_container_get_serialize_length(args);
 
-    handle = wasm_open_connection((int32)name, (int32)args_buffer, args_len);
+    handle = wasm_open_connection(name, args_buffer, args_len);
     if (handle == -1)
         return NULL;
 
@@ -91,7 +91,7 @@ void api_close_connection(connection_t *c)
 
 int api_send_on_connection(connection_t *conn, const char *data, uint32 len)
 {
-    return wasm_send_on_connection(conn->handle, (int32)data, len);
+    return wasm_send_on_connection(conn->handle, data, len);
 }
 
 bool api_config_connection(connection_t *conn, attr_container_t *cfg)
@@ -99,7 +99,7 @@ bool api_config_connection(connection_t *conn, attr_container_t *cfg)
     char *cfg_buffer = (char *)cfg;
     uint32 cfg_len = attr_container_get_serialize_length(cfg);
 
-    return wasm_config_connection(conn->handle, (int32)cfg_buffer, cfg_len);
+    return wasm_config_connection(conn->handle, cfg_buffer, cfg_len);
 }
 
 void on_connection_data(uint32 handle, char *buffer, uint32 len)

+ 3 - 3
core/iwasm/lib/app-libs/extension/gui/src/wgl_btn.c

@@ -15,11 +15,11 @@
  */
 
 #include "wgl.h"
-#include "native_interface.h"
-
+#include "bh_platform.h"
+#include "gui_api.h"
 
 #define ARGC sizeof(argv)/sizeof(uint32)
-#define CALL_BTN_NATIVE_FUNC(id) wasm_btn_native_call(id, (int32)argv, ARGC)
+#define CALL_BTN_NATIVE_FUNC(id) wasm_btn_native_call(id, argv, ARGC)
 
 wgl_obj_t wgl_btn_create(wgl_obj_t par, wgl_obj_t copy)
 {

+ 2 - 2
core/iwasm/lib/app-libs/extension/gui/src/wgl_cb.c

@@ -15,12 +15,12 @@
  */
 
 #include "wgl.h"
-#include "native_interface.h"
+#include "gui_api.h"
 
 #include <string.h>
 
 #define ARGC sizeof(argv)/sizeof(uint32)
-#define CALL_CB_NATIVE_FUNC(id) wasm_cb_native_call(id, (uint32)argv, ARGC)
+#define CALL_CB_NATIVE_FUNC(id) wasm_cb_native_call(id, argv, ARGC)
 
 wgl_obj_t wgl_cb_create(wgl_obj_t par, const wgl_obj_t copy)
 {

+ 2 - 2
core/iwasm/lib/app-libs/extension/gui/src/wgl_label.c

@@ -16,12 +16,12 @@
 
 
 #include "wgl.h"
-#include "native_interface.h"
+#include "gui_api.h"
 #include <string.h>
 
 
 #define ARGC sizeof(argv)/sizeof(uint32)
-#define CALL_LABEL_NATIVE_FUNC(id) wasm_label_native_call(id, (uint32)argv, ARGC)
+#define CALL_LABEL_NATIVE_FUNC(id) wasm_label_native_call(id, argv, ARGC)
 
 wgl_obj_t wgl_label_create(wgl_obj_t par, wgl_obj_t copy)
 {

+ 2 - 2
core/iwasm/lib/app-libs/extension/gui/src/wgl_list.c

@@ -15,12 +15,12 @@
  */
 
 #include "wgl.h"
-#include "native_interface.h"
+#include "gui_api.h"
 
 #include <string.h>
 
 #define ARGC sizeof(argv)/sizeof(uint32)
-#define CALL_LIST_NATIVE_FUNC(id) wasm_list_native_call(id, (int32)argv, ARGC)
+#define CALL_LIST_NATIVE_FUNC(id) wasm_list_native_call(id, argv, ARGC)
 
 
 wgl_obj_t wgl_list_create(wgl_obj_t par, const wgl_obj_t copy)

+ 2 - 2
core/iwasm/lib/app-libs/extension/gui/src/wgl_obj.c

@@ -15,12 +15,12 @@
  */
 
 #include "wgl.h"
-#include "native_interface.h"
+#include "gui_api.h"
 #include <stdlib.h>
 #include <string.h>
 
 #define ARGC sizeof(argv)/sizeof(uint32)
-#define CALL_OBJ_NATIVE_FUNC(id) wasm_obj_native_call(id, (int32)argv, ARGC)
+#define CALL_OBJ_NATIVE_FUNC(id) wasm_obj_native_call(id, argv, ARGC)
 
 typedef struct _obj_evt_cb {
     struct _obj_evt_cb *next;

+ 3 - 3
core/iwasm/lib/app-libs/extension/sensor/sensor.c

@@ -15,7 +15,7 @@
  */
 
 #include "sensor.h"
-#include "native_interface.h"
+#include "sensor_api.h"
 
 typedef struct _sensor {
     struct _sensor * next;
@@ -31,7 +31,7 @@ sensor_t sensor_open(const char* name, int index,
         sensor_event_handler_f sensor_event_handler,
         void *user_data)
 {
-    uint32 id = wasm_sensor_open((int32)name, index);
+    uint32 id = wasm_sensor_open(name, index);
     if (id == -1)
         return NULL;
 
@@ -66,7 +66,7 @@ bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg)
     char *buffer = (char *)cfg;
     int len = attr_container_get_serialize_length(cfg);
 
-    return wasm_sensor_config_with_attr_container(sensor->handle, (int32)buffer, len);
+    return wasm_sensor_config_with_attr_container(sensor->handle, buffer, len);
 }
 
 bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay)

+ 10 - 5
core/iwasm/lib/native-interface/connection_api.h

@@ -16,23 +16,28 @@
 
 #ifndef CONNECTION_API_H_
 #define CONNECTION_API_H_
+
 #include "bh_platform.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len);
+uint32
+wasm_open_connection(const char *name, char *args_buf, uint32 args_buf_len);
 
-void wasm_close_connection(uint32 handle);
+void
+wasm_close_connection(uint32 handle);
 
-int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len);
+int
+wasm_send_on_connection(uint32 handle, const char *data, uint32 data_len);
 
-bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len);
+bool
+wasm_config_connection(uint32 handle, const char *cfg_buf, uint32 cfg_buf_len);
 
 #ifdef __cplusplus
 }
 #endif
 
 
-#endif /* CONNECTION_API_H_ */
+#endif /* end of CONNECTION_API_H_ */

+ 18 - 8
core/iwasm/lib/native-interface/gui_api.h

@@ -14,19 +14,29 @@
  * limitations under the License.
  */
 
-#ifndef GUI_API_H_
-#define GUI_API_H_
+#ifndef _GUI_API_H_
+#define _GUI_API_H_
+
 #include "bh_platform.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-void wasm_obj_native_call(int32 func_id, uint32 argv_offset, uint32 argc);
-void wasm_btn_native_call(int32 func_id, uint32 argv_offset, uint32 argc);
-void wasm_label_native_call(int32 func_id, uint32 argv_offset, uint32 argc);
-void wasm_cb_native_call(int32 func_id, uint32 argv_offset, uint32 argc);
-void wasm_list_native_call(int32 func_id, uint32 argv_offset, uint32 argc);
+void
+wasm_obj_native_call(int32 func_id, uint32 *argv, uint32 argc);
+
+void
+wasm_btn_native_call(int32 func_id, uint32 *argv, uint32 argc);
+
+void
+wasm_label_native_call(int32 func_id, uint32 *argv, uint32 argc);
+
+void
+wasm_cb_native_call(int32 func_id, uint32 *argv, uint32 argc);
+
+void
+wasm_list_native_call(int32 func_id, uint32 *argv, uint32 argc);
 
 
 #ifdef __cplusplus
@@ -34,4 +44,4 @@ void wasm_list_native_call(int32 func_id, uint32 argv_offset, uint32 argc);
 #endif
 
 
-#endif /* GUI_API_H_ */
+#endif /* end of _GUI_API_H_ */

+ 84 - 33
core/iwasm/lib/native-interface/native_interface.h

@@ -14,12 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_
-#define DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_
+#ifndef _NATIVE_INTERFACE_H_
+#define _NATIVE_INTERFACE_H_
 
-// note: the bh_plaform.h is the only head file separately
-//       implemented by both [app] and [native] worlds
+/* Note: the bh_plaform.h is the only head file separately
+         implemented by both [app] and [native] worlds */
 #include "bh_platform.h"
+#include "wasm_export.h"
 
 #define get_module_inst() \
     wasm_runtime_get_current_module_inst()
@@ -39,52 +40,102 @@
 #define module_free(offset) \
     wasm_runtime_module_free(module_inst, offset)
 
-char *wa_strdup(const char *);
+/*char *wa_strdup(const char *);*/
 
-bool
-wasm_response_send(int32 buffer_offset, int size);
-
-void wasm_register_resource(int32 url_offset);
-
-void wasm_post_request(int32 buffer_offset, int size);
+/*
+ * request/response interfaces
+ */
 
-void wasm_sub_event(int32 url_offset);
+bool
+wasm_response_send(wasm_module_inst_t module_inst,
+                   int32 buffer_offset, int size);
+void
+wasm_register_resource(wasm_module_inst_t module_inst,
+                       int32 url_offset);
+void
+wasm_post_request(wasm_module_inst_t module_inst,
+                  int32 buffer_offset, int size);
+void
+wasm_sub_event(wasm_module_inst_t module_inst,
+               int32 url_offset);
 
 /*
- *   *************  sensor interfaces  *************
+ * sensor interfaces
  */
 
 bool
-wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
+wasm_sensor_config(wasm_module_inst_t module_inst,
+                   uint32 sensor, int interval, int bit_cfg, int delay);
 uint32
-wasm_sensor_open(int32 name_offset, int instance);
+wasm_sensor_open(wasm_module_inst_t module_inst,
+                 int32 name_offset, int instance);
 bool
-wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
-        int len);
-
+wasm_sensor_config_with_attr_container(wasm_module_inst_t module_inst,
+                                       uint32 sensor,
+                                       int32 buffer_offset, int len);
 bool
-wasm_sensor_close(uint32 sensor);
+wasm_sensor_close(wasm_module_inst_t module_inst,
+                  uint32 sensor);
 
 /*
- *   *** timer interface ***
+ * timer interfaces
  */
 
 typedef unsigned int timer_id_t;
-timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start);
-void wasm_timer_destory(timer_id_t timer_id);
-void wasm_timer_cancel(timer_id_t timer_id);
-void wasm_timer_restart(timer_id_t timer_id, int interval);
-uint32 wasm_get_sys_tick_ms(void);
+
+timer_id_t
+wasm_create_timer(wasm_module_inst_t module_inst,
+                  int interval, bool is_period, bool auto_start);
+void
+wasm_timer_destory(wasm_module_inst_t module_inst, timer_id_t timer_id);
+void
+wasm_timer_cancel(wasm_module_inst_t module_inst, timer_id_t timer_id);
+void
+wasm_timer_restart(wasm_module_inst_t module_inst,
+                   timer_id_t timer_id, int interval);
+uint32
+wasm_get_sys_tick_ms(wasm_module_inst_t module_inst);
 
 /*
- *   *** connection interface ***
+ * connection interfaces
  */
-uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len);
-void wasm_close_connection(uint32 handle);
-int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len);
-bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len);
 
-#include "gui_api.h"
+uint32
+wasm_open_connection(wasm_module_inst_t module_inst,
+                     int32 name_offset, int32 args_offset, uint32 len);
+void
+wasm_close_connection(wasm_module_inst_t module_inst,
+                      uint32 handle);
+int
+wasm_send_on_connection(wasm_module_inst_t module_inst,
+                        uint32 handle, int32 data_offset, uint32 len);
+bool
+wasm_config_connection(wasm_module_inst_t module_inst,
+                       uint32 handle, int32 cfg_offset, uint32 len);
+
+/**
+ * gui interfaces
+ */
+
+void
+wasm_obj_native_call(wasm_module_inst_t module_inst,
+                     int32 func_id, uint32 argv_offset, uint32 argc);
+
+void
+wasm_btn_native_call(wasm_module_inst_t module_inst,
+                     int32 func_id, uint32 argv_offset, uint32 argc);
+
+void
+wasm_label_native_call(wasm_module_inst_t module_inst,
+                       int32 func_id, uint32 argv_offset, uint32 argc);
+
+void
+wasm_cb_native_call(wasm_module_inst_t module_inst,
+                    int32 func_id, uint32 argv_offset, uint32 argc);
+
+void
+wasm_list_native_call(wasm_module_inst_t module_inst,
+                      int32 func_id, uint32 argv_offset, uint32 argc);
+
+#endif /* end of _NATIVE_INTERFACE_H */
 
-#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_PO
-C_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_ */

+ 1 - 1
core/iwasm/lib/native-interface/readme.txt

@@ -1,6 +1,6 @@
 Attention: 
 =======
-Only add files are shared by both wasm application and native runtime into this directory!
+Only add files which are shared by both wasm application and native runtime into this directory!
 
 The c files are both compiled into the the WASM APP and native runtime.
 

+ 43 - 0
core/iwasm/lib/native-interface/req_resp_api.h

@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _REQ_RESP_API_H_
+#define _REQ_RESP_API_H_
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+bool
+wasm_response_send(const char *buf, int size);
+
+void
+wasm_register_resource(const char *url);
+
+void
+wasm_post_request(const char *buf, int size);
+
+void
+wasm_sub_event(const char *url);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _REQ_RESP_API_H_ */
+

+ 0 - 1
core/iwasm/lib/native-interface/restful_utils.c

@@ -19,7 +19,6 @@
 #include <stdbool.h>
 #include <stdio.h>
 
-#include "native_interface.h"
 #include "shared_utils.h"
 
 /* Serialization of request and response message

+ 6 - 4
core/iwasm/lib/native-interface/sensor_api.h

@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
-#ifndef DEPS_IWASM_APP_LIBS_NATIVE_INTERFACE_SENSOR_API_H_
-#define DEPS_IWASM_APP_LIBS_NATIVE_INTERFACE_SENSOR_API_H_
+#ifndef _SENSOR_API_H_
+#define _SENSOR_API_H_
+
 #include "bh_platform.h"
 
 #ifdef __cplusplus
@@ -29,7 +30,7 @@ bool
 wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
 
 bool
-wasm_sensor_config_with_attr_container(uint32 sensor, char * buffer, int len);
+wasm_sensor_config_with_attr_container(uint32 sensor, char *buffer, int len);
 
 bool
 wasm_sensor_close(uint32 sensor);
@@ -38,4 +39,5 @@ wasm_sensor_close(uint32 sensor);
 }
 #endif
 
-#endif /* DEPS_IWASM_APP_LIBS_NATIVE_INTERFACE_SENSOR_API_H_ */
+#endif /* end of _SENSOR_API_H_ */
+

+ 45 - 23
core/iwasm/lib/native-interface/shared_utils.h

@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_
-#define DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_
+#ifndef _SHARED_UTILS_H_
+#define _SHARED_UTILS_H_
 
-#include "native_interface.h"
+#include "bh_platform.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -71,16 +71,27 @@ typedef struct response {
     unsigned long reciever;
 } response_t;
 
-int check_url_start(const char* url, int url_len, const char * leading_str);
-bool match_url(char * pattern, char * matched);
-char * find_key_value(char * buffer, int buffer_len, char * key, char * value,
-        int value_len, char delimiter);
+int
+check_url_start(const char* url, int url_len, const char * leading_str);
 
-request_t *clone_request(request_t *request);
-void request_cleaner(request_t *request);
+bool
+match_url(char * pattern, char * matched);
 
-response_t * clone_response(response_t * response);
-void response_cleaner(response_t * response);
+char *
+find_key_value(char * buffer, int buffer_len, char * key, char * value,
+               int value_len, char delimiter);
+
+request_t *
+clone_request(request_t *request);
+
+void
+request_cleaner(request_t *request);
+
+response_t *
+clone_response(response_t * response);
+
+void
+response_cleaner(response_t * response);
 
 /**
  * @brief Set fields of response.
@@ -95,8 +106,9 @@ void response_cleaner(response_t * response);
  *
  * @warning the response pointer MUST NOT be NULL
  */
-response_t * set_response(response_t * response, int status, int fmt,
-        const char *payload, int payload_len);
+response_t *
+set_response(response_t * response, int status, int fmt,
+             const char *payload, int payload_len);
 
 /**
  * @brief Make a response for a request.
@@ -108,8 +120,8 @@ response_t * set_response(response_t * response, int status, int fmt,
  *
  * @warning the request and response pointers MUST NOT be NULL
  */
-response_t * make_response_for_request(request_t * request,
-        response_t * response);
+response_t *
+make_response_for_request(request_t * request, response_t * response);
 
 /**
  * @brief Initialize a request.
@@ -125,14 +137,24 @@ response_t * make_response_for_request(request_t * request,
  *
  * @warning the request pointer MUST NOT be NULL
  */
-request_t * init_request(request_t * request, char *url, int action, int fmt,
-        void *payload, int payload_len);
+request_t *
+init_request(request_t * request, char *url, int action, int fmt,
+             void *payload, int payload_len);
+
+char *
+pack_request(request_t *request, int * size);
+
+request_t *
+unpack_request(char * packet, int size, request_t * request);
+
+char *
+pack_response(response_t *response, int * size);
+
+response_t *
+unpack_response(char * packet, int size, response_t * response);
 
-char * pack_request(request_t *request, int * size);
-request_t * unpack_request(char * packet, int size, request_t * request);
-char * pack_response(response_t *response, int * size);
-response_t * unpack_response(char * packet, int size, response_t * response);
-void free_req_resp_packet(char * packet);
+void
+free_req_resp_packet(char * packet);
 
 #include "wgl_shared_utils.h"
 
@@ -140,4 +162,4 @@ void free_req_resp_packet(char * packet);
 }
 #endif
 
-#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_ */
+#endif /* end of _SHARED_UTILS_H_ */

+ 48 - 0
core/iwasm/lib/native-interface/timer_api.h

@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _TIMER_API_H_
+#define _TIMER_API_H_
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef unsigned int timer_id_t;
+
+timer_id_t
+wasm_create_timer(int interval, bool is_period, bool auto_start);
+
+void
+wasm_timer_destory(timer_id_t timer_id);
+
+void
+wasm_timer_cancel(timer_id_t timer_id);
+
+void
+wasm_timer_restart(timer_id_t timer_id, int interval);
+
+uint32
+wasm_get_sys_tick_ms(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _TIMER_API_H_ */
+

+ 102 - 0
core/iwasm/lib/native-interface/wasm_export_api.h

@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _WASM_EXPORT_H
+#define _WASM_EXPORT_H
+
+#include <inttypes.h>
+#include <stdbool.h>
+
+/**
+ * API exported to WASM application
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Get current WASM module instance of the current native thread
+ *
+ * @return current WASM module instance of the current native thread, 0
+ *         if not found
+ * Note: the return type is uint64_t but not pointer type, because that
+ *       the we only supports WASM-32, in which the pointer type is
+ *       compiled to WASM i32 type, but the pointer type in native can be
+ *       32-bit and 64-bit. And if the native pointer is 64-bit, data loss
+ *       occurs after converting it to WASM i32 type.
+ */
+uint64_t
+wasm_runtime_get_current_module_inst();
+
+/**
+ * Validate the app address, check whether it belongs to WASM module
+ * instance's address space, or in its heap space or memory space.
+ *
+ * @param module_inst the WASM module instance
+ * @param app_offset the app address to validate, which is a relative address
+ * @param size the size bytes of the app address
+ *
+ * @return true if success, false otherwise.
+ */
+bool
+wasm_runtime_validate_app_addr(uint64_t module_inst,
+                               int32_t app_offset, uint32_t size);
+
+/**
+ * Validate the native address, check whether it belongs to WASM module
+ * instance's address space, or in its heap space or memory space.
+ *
+ * @param module_inst the WASM module instance
+ * @param native_ptr the native address to validate, which is an absolute
+ *        address
+ * @param size the size bytes of the app address
+ *
+ * @return true if success, false otherwise.
+ */
+bool
+wasm_runtime_validate_native_addr(uint64_t module_inst,
+                                  uint64_t native_ptr, uint32_t size);
+
+/**
+ * Convert app address(relative address) to native address(absolute address)
+ *
+ * @param module_inst the WASM module instance
+ * @param app_offset the app adress
+ *
+ * @return the native address converted
+ */
+uint64_t
+wasm_runtime_addr_app_to_native(uint64_t module_inst,
+                                int32_t app_offset);
+
+/**
+ * Convert native address(absolute address) to app address(relative address)
+ *
+ * @param module_inst the WASM module instance
+ * @param native_ptr the native address
+ *
+ * @return the app address converted
+ */
+int32_t
+wasm_runtime_addr_native_to_app(uint64_t module_inst,
+                                uint64_t native_ptr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _WASM_EXPORT_H */

+ 10 - 15
core/iwasm/lib/native/base/base_lib_export.c

@@ -26,19 +26,17 @@
 #endif
 
 static uint64
-wasm_runtime_get_current_module_inst_wrapper()
+wasm_runtime_get_current_module_inst_wrapper(wasm_module_inst_t module_inst)
 {
-    return (uint64)(uintptr_t)
-        wasm_runtime_get_current_module_inst();
+    return (uint64)(uintptr_t)module_inst;
 }
 
 static bool
-wasm_runtime_validate_app_addr_wrapper(uint32 inst_part0, uint32 inst_part1,
+wasm_runtime_validate_app_addr_wrapper(wasm_module_inst_t module_inst,
+                                       uint32 inst_part0, uint32 inst_part1,
                                        int32 app_offset, uint32 size)
 {
     bool ret;
-    wasm_module_inst_t module_inst =
-        wasm_runtime_get_current_module_inst();
     union { uint64 u64; uint32 parts[2]; } inst;
 
     inst.parts[0] = inst_part0;
@@ -56,14 +54,13 @@ wasm_runtime_validate_app_addr_wrapper(uint32 inst_part0, uint32 inst_part1,
 }
 
 static bool
-wasm_runtime_validate_native_addr_wrapper(uint32 inst_part0, uint32 inst_part1,
+wasm_runtime_validate_native_addr_wrapper(wasm_module_inst_t module_inst,
+                                          uint32 inst_part0, uint32 inst_part1,
                                           uint32 native_ptr_part0,
                                           uint32 native_ptr_part1,
                                           uint32 size)
 {
     bool ret;
-    wasm_module_inst_t module_inst =
-        wasm_runtime_get_current_module_inst();
     union { uint64 u64; uint32 parts[2]; } inst;
     union { uint64 u64; uint32 parts[2]; } native_ptr;
 
@@ -86,11 +83,10 @@ wasm_runtime_validate_native_addr_wrapper(uint32 inst_part0, uint32 inst_part1,
 }
 
 static uint64
-wasm_runtime_addr_app_to_native_wrapper(uint32 inst_part0, uint32 inst_part1,
+wasm_runtime_addr_app_to_native_wrapper(wasm_module_inst_t module_inst,
+                                        uint32 inst_part0, uint32 inst_part1,
                                         int32 app_offset)
 {
-    wasm_module_inst_t module_inst =
-        wasm_runtime_get_current_module_inst();
     union { uint64 u64; uint32 parts[2]; } inst;
 
     inst.parts[0] = inst_part0;
@@ -105,12 +101,11 @@ wasm_runtime_addr_app_to_native_wrapper(uint32 inst_part0, uint32 inst_part1,
 }
 
 static int32
-wasm_runtime_addr_native_to_app_wrapper(uint32 inst_part0, uint32 inst_part1,
+wasm_runtime_addr_native_to_app_wrapper(wasm_module_inst_t module_inst,
+                                        uint32 inst_part0, uint32 inst_part1,
                                         uint32 native_ptr_part0,
                                         uint32 native_ptr_part1)
 {
-    wasm_module_inst_t module_inst =
-        wasm_runtime_get_current_module_inst();
     union { uint64 u64; uint32 parts[2]; } inst;
     union { uint64 u64; uint32 parts[2]; } native_ptr;
 

+ 10 - 9
core/iwasm/lib/native/base/request_response.c

@@ -14,16 +14,16 @@
  * limitations under the License.
  */
 
-#include "native_interface.h"
 #include "app_manager_export.h"
 #include "coap_ext.h"
 #include "wasm_export.h"
 
 extern void module_request_handler(request_t *request, void *user_data);
 
-bool wasm_response_send(int32 buffer_offset, int size)
+bool
+wasm_response_send(wasm_module_inst_t module_inst,
+                   int32 buffer_offset, int size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *buffer = NULL;
 
     if (!validate_app_addr(buffer_offset, size))
@@ -45,9 +45,9 @@ bool wasm_response_send(int32 buffer_offset, int size)
     return false;
 }
 
-void wasm_register_resource(int32 url_offset)
+void
+wasm_register_resource(wasm_module_inst_t module_inst, int32 url_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *url = NULL;
 
     if (!validate_app_addr(url_offset, 1))
@@ -61,9 +61,10 @@ void wasm_register_resource(int32 url_offset)
     }
 }
 
-void wasm_post_request(int32 buffer_offset, int size)
+void
+wasm_post_request(wasm_module_inst_t module_inst,
+                  int32 buffer_offset, int size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *buffer = NULL;
 
     if (!validate_app_addr(buffer_offset, size))
@@ -92,9 +93,9 @@ void wasm_post_request(int32 buffer_offset, int size)
     }
 }
 
-void wasm_sub_event(int32 url_offset)
+void
+wasm_sub_event(wasm_module_inst_t module_inst, int32 url_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *url = NULL;
 
     if (!validate_app_addr(url_offset, 1))

+ 0 - 1
core/iwasm/lib/native/base/runtime_lib.h

@@ -17,7 +17,6 @@
 #ifndef LIB_BASE_RUNTIME_LIB_H_
 #define LIB_BASE_RUNTIME_LIB_H_
 
-#include "native_interface.h"
 
 #include "runtime_timer.h"
 

+ 13 - 6
core/iwasm/lib/native/base/timer_wrapper.c

@@ -37,7 +37,7 @@ void wasm_timer_callback(timer_id_t id, unsigned int mod_id)
 
     // !!! the length parameter must be 0, so the receiver will
     //     not free the payload pointer.
-    bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *) id, 0);
+    bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *)(uintptr_t)id, 0);
 }
 
 ///
@@ -149,30 +149,37 @@ timer_ctx_t get_wasm_timer_ctx()
     return m->timer_ctx;
 }
 
-timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start)
+timer_id_t
+wasm_create_timer(wasm_module_inst_t module_inst,
+                  int interval, bool is_period, bool auto_start)
 {
     return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
                             auto_start);
 }
 
-void wasm_timer_destory(timer_id_t timer_id)
+void
+wasm_timer_destory(wasm_module_inst_t module_inst, timer_id_t timer_id)
 {
     sys_timer_destory(get_wasm_timer_ctx(), timer_id);
 }
 
-void wasm_timer_cancel(timer_id_t timer_id)
+void
+wasm_timer_cancel(wasm_module_inst_t module_inst, timer_id_t timer_id)
 {
     sys_timer_cancel(get_wasm_timer_ctx(), timer_id);
 }
 
-void wasm_timer_restart(timer_id_t timer_id, int interval)
+void
+wasm_timer_restart(wasm_module_inst_t module_inst,
+                   timer_id_t timer_id, int interval)
 {
     sys_timer_restart(get_wasm_timer_ctx(), timer_id, interval);
 }
 
 extern uint32 get_sys_tick_ms();
 
-uint32 wasm_get_sys_tick_ms(void)
+uint32
+wasm_get_sys_tick_ms(wasm_module_inst_t module_inst)
 {
     return (uint32) bh_get_tick_ms();
 }

+ 11 - 8
core/iwasm/lib/native/extension/connection/connection_wrapper.c

@@ -23,10 +23,10 @@
  * This file is the consumer of connection lib which is implemented by different platforms
  */
 
-
-uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len)
+uint32
+wasm_open_connection(wasm_module_inst_t module_inst,
+                     int32 name_offset, int32 args_offset, uint32 len)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     attr_container_t *args;
     char *name, *args_buf;
 
@@ -44,15 +44,17 @@ uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len)
     return -1;
 }
 
-void wasm_close_connection(uint32 handle)
+void
+wasm_close_connection(wasm_module_inst_t module_inst, uint32 handle)
 {
     if (connection_impl._close != NULL)
         connection_impl._close(handle);
 }
 
-int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len)
+int
+wasm_send_on_connection(wasm_module_inst_t module_inst,
+                        uint32 handle, int32 data_offset, uint32 len)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *data;
 
     if (!validate_app_addr(data_offset, len) ||
@@ -65,9 +67,10 @@ int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len)
     return -1;
 }
 
-bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len)
+bool
+wasm_config_connection(wasm_module_inst_t module_inst,
+                       uint32 handle, int32 cfg_offset, uint32 len)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *cfg_buf;
     attr_container_t *cfg;
 

+ 3 - 1
core/iwasm/lib/native/extension/gui/wgl_btn_wrapper.c

@@ -45,7 +45,9 @@ static WGLNativeFuncDef btn_native_func_defs[] = {
 };
 
 /*************** Native Interface to Wasm App ***********/
-void wasm_btn_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
+void
+wasm_btn_native_call(wasm_module_inst_t module_inst,
+                     int32 func_id, uint32 argv_offset, uint32 argc)
 {
     uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef);
 

+ 3 - 1
core/iwasm/lib/native/extension/gui/wgl_cb_wrapper.c

@@ -61,7 +61,9 @@ static WGLNativeFuncDef cb_native_func_defs[] = {
 };
 
 /*************** Native Interface to Wasm App ***********/
-void wasm_cb_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
+void
+wasm_cb_native_call(wasm_module_inst_t module_inst,
+                    int32 func_id, uint32 argv_offset, uint32 argc)
 {
     uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef);
 

+ 3 - 1
core/iwasm/lib/native/extension/gui/wgl_label_wrapper.c

@@ -60,7 +60,9 @@ static WGLNativeFuncDef label_native_func_defs[] = {
 };
 
 /*************** Native Interface to Wasm App ***********/
-void wasm_label_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
+void
+wasm_label_native_call(wasm_module_inst_t module_inst,
+                       int32 func_id, uint32 argv_offset, uint32 argc)
 {
     uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef);
 

+ 3 - 1
core/iwasm/lib/native/extension/gui/wgl_list_wrapper.c

@@ -51,7 +51,9 @@ static WGLNativeFuncDef list_native_func_defs[] = {
 };
 
 /*************** Native Interface to Wasm App ***********/
-void wasm_list_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
+void
+wasm_list_native_call(wasm_module_inst_t module_inst,
+                      int32 func_id, uint32 argv_offset, uint32 argc)
 {
     uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef);
 

+ 3 - 1
core/iwasm/lib/native/extension/gui/wgl_obj_wrapper.c

@@ -341,7 +341,9 @@ static WGLNativeFuncDef obj_native_func_defs[] = {
 };
 
 /*************** Native Interface to Wasm App ***********/
-void wasm_obj_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
+void
+wasm_obj_native_call(wasm_module_inst_t module_inst,
+                     int32 func_id, uint32 argv_offset, uint32 argc)
 {
     uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef);
 

+ 24 - 14
core/iwasm/lib/native/extension/sensor/runtime_sensor.c

@@ -22,8 +22,10 @@
 
 static sys_sensor_t * g_sys_sensors = NULL;
 static int g_sensor_id_max = 0;
-static sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
-        unsigned int client_id, bool remove_if_found);
+
+static sensor_client_t *
+find_sensor_client(sys_sensor_t * sensor,
+                   unsigned int client_id, bool remove_if_found);
 
 void (*rechedule_sensor_callback)() = NULL;
 
@@ -32,7 +34,8 @@ void (*rechedule_sensor_callback)() = NULL;
  *
  */
 
-static void sensor_event_cleaner(sensor_event_data_t *sensor_event)
+static void
+sensor_event_cleaner(sensor_event_data_t *sensor_event)
 {
     if (sensor_event->data != NULL) {
         if (sensor_event->data_fmt == FMT_ATTR_CONTAINER)
@@ -44,8 +47,8 @@ static void sensor_event_cleaner(sensor_event_data_t *sensor_event)
     bh_free(sensor_event);
 }
 
-static void wasm_sensor_callback(void *client, uint32 sensor_id,
-                                 void *user_data)
+static void
+wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
 {
     attr_container_t *sensor_data = (attr_container_t *) user_data;
     attr_container_t *sensor_data_clone;
@@ -92,7 +95,10 @@ static void wasm_sensor_callback(void *client, uint32 sensor_id,
     bh_post_msg2(module->queue, msg);
 }
 
-bool wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay)
+bool
+wasm_sensor_config(wasm_module_inst_t module_inst,
+                   uint32 sensor, int interval,
+                   int bit_cfg, int delay)
 {
     attr_container_t * attr_cont;
     sensor_client_t * c;
@@ -132,9 +138,10 @@ bool wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay)
     return true;
 }
 
-uint32 wasm_sensor_open(int32 name_offset, int instance)
+uint32
+wasm_sensor_open(wasm_module_inst_t module_inst,
+                 int32 name_offset, int instance)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *name = NULL;
 
     if (!validate_app_addr(name_offset, 1))
@@ -185,10 +192,11 @@ uint32 wasm_sensor_open(int32 name_offset, int instance)
     return -1;
 }
 
-bool wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
-        int len)
+bool
+wasm_sensor_config_with_attr_container(wasm_module_inst_t module_inst,
+                                       uint32 sensor, int32 buffer_offset,
+                                       int len)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *buffer = NULL;
 
     if (!validate_app_addr(buffer_offset, len))
@@ -211,7 +219,8 @@ bool wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
     return false;
 }
 
-bool wasm_sensor_close(uint32 sensor)
+bool
+wasm_sensor_close(wasm_module_inst_t module_inst, uint32 sensor)
 {
     unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
     unsigned int client_id = mod_id;
@@ -271,8 +280,9 @@ void refresh_read_interval(sensor_obj_t sensor)
     sensor->read_interval = interval;
 }
 
-sensor_obj_t add_sys_sensor(char * name, char * description, int instance,
-        uint32 default_interval, void * read_func, void * config_func)
+sensor_obj_t
+add_sys_sensor(char * name, char * description, int instance,
+               uint32 default_interval, void * read_func, void * config_func)
 {
     sys_sensor_t * s = (sys_sensor_t *) bh_malloc(sizeof(sys_sensor_t));
     if (s == NULL)

+ 10 - 5
core/iwasm/lib/native/extension/sensor/runtime_sensor.h

@@ -19,6 +19,8 @@
 
 #include "bh_platform.h"
 #include "attr_container.h"
+#include "wasm_export.h"
+
 struct _sys_sensor;
 typedef struct _sys_sensor* sensor_obj_t;
 
@@ -60,16 +62,19 @@ int check_sensor_timers();
 void reschedule_sensor_read();
 
 uint32
-wasm_sensor_open(int32 name_offset, int instance);
+wasm_sensor_open(wasm_module_inst_t module_inst,
+                 int32 name_offset, int instance);
 
 bool
-wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
+wasm_sensor_config(wasm_module_inst_t module_inst,
+                   uint32 sensor, int interval, int bit_cfg, int delay);
 
 bool
-wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
-        int len);
+wasm_sensor_config_with_attr_container(wasm_module_inst_t module_inst,
+                                       uint32 sensor, int32 buffer_offset,
+                                       int len);
 
 bool
-wasm_sensor_close(uint32 sensor);
+wasm_sensor_close(wasm_module_inst_t module_inst, uint32 sensor);
 
 #endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */

+ 61 - 65
core/iwasm/lib/native/libc/libc_wrapper.c

@@ -34,9 +34,6 @@ wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
 void
 wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
 
-#define get_module_inst() \
-    wasm_runtime_get_current_module_inst()
-
 #define validate_app_addr(offset, size) \
     wasm_runtime_validate_app_addr(module_inst, offset, size)
 
@@ -454,9 +451,9 @@ parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
 }
 
 static int
-_printf_wrapper(int32 fmt_offset, int32 va_list_offset)
+_printf_wrapper(wasm_module_inst_t module_inst,
+                int32 fmt_offset, int32 va_list_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     struct str_context ctx = { NULL, 0, 0 };
     const char *fmt;
     _va_list va_args;
@@ -470,9 +467,9 @@ _printf_wrapper(int32 fmt_offset, int32 va_list_offset)
 }
 
 static int
-_sprintf_wrapper(int32 str_offset, int32 fmt_offset, int32 va_list_offset)
+_sprintf_wrapper(wasm_module_inst_t module_inst,
+                 int32 str_offset, int32 fmt_offset, int32 va_list_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     int32 app_end_offset;
     struct str_context ctx;
     char *str;
@@ -505,10 +502,10 @@ _sprintf_wrapper(int32 str_offset, int32 fmt_offset, int32 va_list_offset)
 }
 
 static int
-_snprintf_wrapper(int32 str_offset, int32 size, int32 fmt_offset,
+_snprintf_wrapper(wasm_module_inst_t module_inst,
+                  int32 str_offset, int32 size, int32 fmt_offset,
                   int32 va_list_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     struct str_context ctx;
     char *str;
     const char *fmt;
@@ -537,9 +534,9 @@ _snprintf_wrapper(int32 str_offset, int32 size, int32 fmt_offset,
 }
 
 static int
-_puts_wrapper(int32 str_offset)
+_puts_wrapper(wasm_module_inst_t module_inst,
+              int32 str_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     const char *str;
 
     if (!validate_str_addr(module_inst, str_offset))
@@ -550,16 +547,16 @@ _puts_wrapper(int32 str_offset)
 }
 
 static int
-_putchar_wrapper(int c)
+_putchar_wrapper(wasm_module_inst_t module_inst, int c)
 {
     bh_printf("%c", c);
     return 1;
 }
 
 static int32
-_strdup_wrapper(int32 str_offset)
+_strdup_wrapper(wasm_module_inst_t module_inst,
+                int32 str_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *str, *str_ret;
     uint32 len;
     int32 str_ret_offset = 0;
@@ -583,9 +580,9 @@ _strdup_wrapper(int32 str_offset)
 }
 
 static int32
-_memcmp_wrapper(int32 s1_offset, int32 s2_offset, int32 size)
+_memcmp_wrapper(wasm_module_inst_t module_inst,
+                int32 s1_offset, int32 s2_offset, int32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     void *s1, *s2;
 
     if (!validate_app_addr(s1_offset, size)
@@ -598,9 +595,9 @@ _memcmp_wrapper(int32 s1_offset, int32 s2_offset, int32 size)
 }
 
 static int32
-_memcpy_wrapper(int32 dst_offset, int32 src_offset, int32 size)
+_memcpy_wrapper(wasm_module_inst_t module_inst,
+                int32 dst_offset, int32 src_offset, int32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     void *dst, *src;
 
     if (size == 0)
@@ -617,9 +614,9 @@ _memcpy_wrapper(int32 dst_offset, int32 src_offset, int32 size)
 }
 
 static int32
-_memmove_wrapper(int32 dst_offset, int32 src_offset, int32 size)
+_memmove_wrapper(wasm_module_inst_t module_inst,
+                 int32 dst_offset, int32 src_offset, int32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     void *dst, *src;
 
     if (!validate_app_addr(dst_offset, size)
@@ -633,9 +630,9 @@ _memmove_wrapper(int32 dst_offset, int32 src_offset, int32 size)
 }
 
 static int32
-_memset_wrapper(int32 s_offset, int32 c, int32 size)
+_memset_wrapper(wasm_module_inst_t module_inst,
+                int32 s_offset, int32 c, int32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     void *s;
 
     if (!validate_app_addr(s_offset, size))
@@ -647,9 +644,9 @@ _memset_wrapper(int32 s_offset, int32 c, int32 size)
 }
 
 static int32
-_strchr_wrapper(int32 s_offset, int32 c)
+_strchr_wrapper(wasm_module_inst_t module_inst,
+                int32 s_offset, int32 c)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     const char *s;
     char *ret;
 
@@ -662,9 +659,9 @@ _strchr_wrapper(int32 s_offset, int32 c)
 }
 
 static int32
-_strcmp_wrapper(int32 s1_offset, int32 s2_offset)
+_strcmp_wrapper(wasm_module_inst_t module_inst,
+                int32 s1_offset, int32 s2_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     void *s1, *s2;
 
     if (!validate_str_addr(module_inst, s1_offset)
@@ -677,9 +674,9 @@ _strcmp_wrapper(int32 s1_offset, int32 s2_offset)
 }
 
 static int32
-_strncmp_wrapper(int32 s1_offset, int32 s2_offset, uint32 size)
+_strncmp_wrapper(wasm_module_inst_t module_inst,
+                 int32 s1_offset, int32 s2_offset, uint32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     void *s1, *s2;
 
     if (!validate_app_addr(s1_offset, size)
@@ -692,9 +689,9 @@ _strncmp_wrapper(int32 s1_offset, int32 s2_offset, uint32 size)
 }
 
 static int32
-_strcpy_wrapper(int32 dst_offset, int32 src_offset)
+_strcpy_wrapper(wasm_module_inst_t module_inst,
+                int32 dst_offset, int32 src_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *dst, *src;
     uint32 len;
 
@@ -713,9 +710,9 @@ _strcpy_wrapper(int32 dst_offset, int32 src_offset)
 }
 
 static int32
-_strncpy_wrapper(int32 dst_offset, int32 src_offset, uint32 size)
+_strncpy_wrapper(wasm_module_inst_t module_inst,
+                 int32 dst_offset, int32 src_offset, uint32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *dst, *src;
 
     if (!validate_app_addr(dst_offset, size)
@@ -729,9 +726,9 @@ _strncpy_wrapper(int32 dst_offset, int32 src_offset, uint32 size)
 }
 
 static uint32
-_strlen_wrapper(int32 s_offset)
+_strlen_wrapper(wasm_module_inst_t module_inst,
+                int32 s_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char *s;
 
     if (!validate_str_addr(module_inst, s_offset))
@@ -742,17 +739,17 @@ _strlen_wrapper(int32 s_offset)
 }
 
 static int32
-_malloc_wrapper(uint32 size)
+_malloc_wrapper(wasm_module_inst_t module_inst,
+                uint32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     return module_malloc(size);
 }
 
 static int32
-_calloc_wrapper(uint32 nmemb, uint32 size)
+_calloc_wrapper(wasm_module_inst_t module_inst,
+                uint32 nmemb, uint32 size)
 {
     uint64 total_size = (uint64) nmemb * (uint64) size;
-    wasm_module_inst_t module_inst = get_module_inst();
     uint32 ret_offset = 0;
     uint8 *ret_ptr;
 
@@ -769,31 +766,30 @@ _calloc_wrapper(uint32 nmemb, uint32 size)
 }
 
 static void
-_free_wrapper(int32 ptr_offset)
+_free_wrapper(wasm_module_inst_t module_inst,
+              int32 ptr_offset)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
-
     if (!validate_app_addr(ptr_offset, 4))
         return;
     return module_free(ptr_offset);
 }
 
 static void
-setTempRet0_wrapper(uint32 temp_ret)
+setTempRet0_wrapper(wasm_module_inst_t module_inst,
+                    uint32 temp_ret)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     wasm_runtime_set_temp_ret(module_inst, temp_ret);
 }
 
 static uint32
-getTempRet0_wrapper()
+getTempRet0_wrapper(wasm_module_inst_t module_inst)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     return wasm_runtime_get_temp_ret(module_inst);
 }
 
 static uint32
-_llvm_bswap_i16_wrapper(uint32 data)
+_llvm_bswap_i16_wrapper(wasm_module_inst_t module_inst,
+                        uint32 data)
 {
     return (data & 0xFFFF0000)
            | ((data & 0xFF) << 8)
@@ -801,7 +797,8 @@ _llvm_bswap_i16_wrapper(uint32 data)
 }
 
 static uint32
-_llvm_bswap_i32_wrapper(uint32 data)
+_llvm_bswap_i32_wrapper(wasm_module_inst_t module_inst,
+                        uint32 data)
 {
     return ((data & 0xFF) << 24)
            | ((data & 0xFF00) << 8)
@@ -810,10 +807,10 @@ _llvm_bswap_i32_wrapper(uint32 data)
 }
 
 static uint32
-_bitshift64Lshr_wrapper(uint32 uint64_part0, uint32 uint64_part1,
+_bitshift64Lshr_wrapper(wasm_module_inst_t module_inst,
+                        uint32 uint64_part0, uint32 uint64_part1,
                         uint32 bits)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     union {
         uint64 value;
         uint32 parts[2];
@@ -829,10 +826,10 @@ _bitshift64Lshr_wrapper(uint32 uint64_part0, uint32 uint64_part1,
 }
 
 static uint32
-_bitshift64Shl_wrapper(uint32 int64_part0, uint32 int64_part1,
+_bitshift64Shl_wrapper(wasm_module_inst_t module_inst,
+                       uint32 int64_part0, uint32 int64_part1,
                        uint32 bits)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     union {
         int64 value;
         uint32 parts[2];
@@ -848,26 +845,25 @@ _bitshift64Shl_wrapper(uint32 int64_part0, uint32 int64_part1,
 }
 
 static void
-_llvm_stackrestore_wrapper(uint32 llvm_stack)
+_llvm_stackrestore_wrapper(wasm_module_inst_t module_inst,
+                           uint32 llvm_stack)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     bh_printf("_llvm_stackrestore called!\n");
     wasm_runtime_set_llvm_stack(module_inst, llvm_stack);
 }
 
 static uint32
-_llvm_stacksave_wrapper()
+_llvm_stacksave_wrapper(wasm_module_inst_t module_inst)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     bh_printf("_llvm_stacksave called!\n");
     return wasm_runtime_get_llvm_stack(module_inst);
 }
 
 static int32
-_emscripten_memcpy_big_wrapper(int32 dst_offset, int32 src_offset,
+_emscripten_memcpy_big_wrapper(wasm_module_inst_t module_inst,
+                               int32 dst_offset, int32 src_offset,
                                uint32 size)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     void *dst, *src;
 
     if (!validate_app_addr(dst_offset, size)
@@ -882,27 +878,27 @@ _emscripten_memcpy_big_wrapper(int32 dst_offset, int32 src_offset,
 }
 
 static void
-abort_wrapper(int32 code)
+abort_wrapper(wasm_module_inst_t module_inst,
+              int32 code)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char buf[32];
     snprintf(buf, sizeof(buf), "env.abort(%i)", code);
     wasm_runtime_set_exception(module_inst, buf);
 }
 
 static void
-abortStackOverflow_wrapper(int32 code)
+abortStackOverflow_wrapper(wasm_module_inst_t module_inst,
+                           int32 code)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char buf[32];
     snprintf(buf, sizeof(buf), "env.abortStackOverflow(%i)", code);
     wasm_runtime_set_exception(module_inst, buf);
 }
 
 static void
-nullFunc_X_wrapper(int32 code)
+nullFunc_X_wrapper(wasm_module_inst_t module_inst,
+                   int32 code)
 {
-    wasm_module_inst_t module_inst = get_module_inst();
     char buf[32];
     snprintf(buf, sizeof(buf), "env.nullFunc_X(%i)", code);
     wasm_runtime_set_exception(module_inst, buf);
@@ -912,13 +908,13 @@ nullFunc_X_wrapper(int32 code)
 
 #ifdef ENABLE_SPEC_TEST
 static void
-print_i32_wrapper(int i32)
+print_i32_wrapper(wasm_module_inst_t module_inst, int i32)
 {
     bh_printf("%d\n", i32);
 }
 
 static void
-print_wrapper(int i32)
+print_wrapper(wasm_module_inst_t module_inst, int i32)
 {
     bh_printf("%d\n", i32);
 }

+ 49 - 45
core/iwasm/runtime/platform/linux-sgx/wasm_native.c

@@ -35,9 +35,6 @@
 #include <errno.h>
 
 
-#define get_module_inst() \
-    wasm_runtime_get_current_module_inst()
-
 #define validate_app_addr(offset, size) \
     wasm_runtime_validate_app_addr(module_inst, offset, size)
 
@@ -55,7 +52,7 @@
 
 
 static int32
-__syscall0_wrapper(int32 arg0)
+__syscall0_wrapper(WASMModuleInstance *module_inst, int32 arg0)
 {
     switch (arg0) {
         case 199: /* getuid */
@@ -67,7 +64,7 @@ __syscall0_wrapper(int32 arg0)
 }
 
 static int32
-__syscall1_wrapper(int32 arg0, int32 arg1)
+__syscall1_wrapper(WASMModuleInstance *module_inst, int32 arg0, int32 arg1)
 {
     switch (arg0) {
         case 6: /* close */
@@ -79,7 +76,8 @@ __syscall1_wrapper(int32 arg0, int32 arg1)
 }
 
 static int32
-__syscall2_wrapper(int32 arg0, int32 arg1, int32 arg2)
+__syscall2_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2)
 {
     switch (arg0) {
         case 183: /* getcwd */
@@ -91,10 +89,9 @@ __syscall2_wrapper(int32 arg0, int32 arg1, int32 arg2)
 }
 
 static int32
-__syscall3_wrapper(int32 arg0, int32 arg1, int32 arg2, int32 arg3)
+__syscall3_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2, int32 arg3)
 {
-    WASMModuleInstance *module_inst = get_module_inst();
-
     switch (arg0) {
         case 146: /* writev */
         {
@@ -145,7 +142,8 @@ __syscall3_wrapper(int32 arg0, int32 arg1, int32 arg2, int32 arg3)
 }
 
 static int32
-__syscall4_wrapper(int32 arg0, int32 arg1, int32 arg2,
+__syscall4_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2,
                    int32 arg3, int32 arg4)
 {
     bh_printf("##_syscall4 called, syscall id: %d\n", arg0);
@@ -153,7 +151,8 @@ __syscall4_wrapper(int32 arg0, int32 arg1, int32 arg2,
 }
 
 static int32
-__syscall5_wrapper(int32 arg0, int32 arg1, int32 arg2,
+__syscall5_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2,
                    int32 arg3, int32 arg4, int32 arg5)
 {
     switch (arg0) {
@@ -166,46 +165,54 @@ __syscall5_wrapper(int32 arg0, int32 arg1, int32 arg2,
 }
 
 #define GET_EMCC_SYSCALL_ARGS()                                     \
-  WASMModuleInstance *module_inst = get_module_inst();              \
   int32 *args;                                                      \
   if (!validate_app_addr(args_off, 1))                              \
     return 0;                                                       \
   args = addr_app_to_native(args_off)                               \
 
-#define EMCC_SYSCALL_WRAPPER0(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id) {                \
-    return __syscall0_wrapper(id);                                  \
+#define EMCC_SYSCALL_WRAPPER0(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id) {                    \
+    return __syscall0_wrapper(module_inst, id);                         \
   }
 
-#define EMCC_SYSCALL_WRAPPER1(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall1_wrapper(id, args[0]);                         \
+#define EMCC_SYSCALL_WRAPPER1(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall1_wrapper(module_inst, id, args[0]);                \
   }
 
-#define EMCC_SYSCALL_WRAPPER2(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall2_wrapper(id, args[0], args[1]);                \
+#define EMCC_SYSCALL_WRAPPER2(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off){     \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall2_wrapper(module_inst, id, args[0], args[1]);       \
   }
 
-#define EMCC_SYSCALL_WRAPPER3(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall3_wrapper(id, args[0], args[1], args[2]);       \
+#define EMCC_SYSCALL_WRAPPER3(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall3_wrapper(module_inst, id,                          \
+                              args[0], args[1], args[2]);               \
   }
 
-#define EMCC_SYSCALL_WRAPPER4(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall4_wrapper(id, args[0], args[1], args[2], args[3]);\
+#define EMCC_SYSCALL_WRAPPER4(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall4_wrapper(module_inst, id,                          \
+                              args[0], args[1], args[2], args[3]);      \
   }
 
-#define EMCC_SYSCALL_WRAPPER5(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall5_wrapper(id, args[0], args[1], args[2],        \
-                              args[3], args[4]);                    \
+#define EMCC_SYSCALL_WRAPPER5(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall5_wrapper(module_inst, id,                          \
+                              args[0], args[1], args[2],                \
+                              args[3], args[4]);                        \
   }
 
 EMCC_SYSCALL_WRAPPER0(199)
@@ -224,18 +231,16 @@ EMCC_SYSCALL_WRAPPER3(221)
 EMCC_SYSCALL_WRAPPER5(140)
 
 static int32
-getTotalMemory_wrapper()
+getTotalMemory_wrapper(WASMModuleInstance *module_inst)
 {
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     WASMMemoryInstance *memory = module_inst->default_memory;
     return NumBytesPerPage * memory->cur_page_count;
 }
 
 static int32
-enlargeMemory_wrapper()
+enlargeMemory_wrapper(WASMModuleInstance *module_inst)
 {
     bool ret;
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     WASMMemoryInstance *memory = module_inst->default_memory;
     uint32 DYNAMICTOP_PTR_offset = module_inst->DYNAMICTOP_PTR_offset;
     uint32 addr_data_offset = *(uint32*)(memory->global_data + DYNAMICTOP_PTR_offset);
@@ -254,9 +259,8 @@ enlargeMemory_wrapper()
 }
 
 static void
-_abort_wrapper(int32 code)
+_abort_wrapper(WASMModuleInstance *module_inst, int32 code)
 {
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     char buf[32];
 
     snprintf(buf, sizeof(buf), "env.abort(%i)", code);
@@ -264,14 +268,13 @@ _abort_wrapper(int32 code)
 }
 
 static void
-abortOnCannotGrowMemory_wrapper()
+abortOnCannotGrowMemory_wrapper(WASMModuleInstance *module_inst)
 {
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     wasm_runtime_set_exception(module_inst, "abort on cannot grow memory");
 }
 
 static void
-___setErrNo_wrapper(int32 error_no)
+___setErrNo_wrapper(WASMModuleInstance *module_inst, int32 error_no)
 {
     errno = error_no;
 }
@@ -330,3 +333,4 @@ wasm_platform_native_func_lookup(const char *module_name,
 
     return NULL;
 }
+

+ 54 - 51
core/iwasm/runtime/platform/linux/wasm_native.c

@@ -35,9 +35,6 @@
 #include <errno.h>
 
 
-#define get_module_inst() \
-    wasm_runtime_get_current_module_inst()
-
 #define validate_app_addr(offset, size) \
     wasm_runtime_validate_app_addr(module_inst, offset, size)
 
@@ -55,46 +52,46 @@
 
 
 static int32
-__syscall0_wrapper(int32 arg0)
+__syscall0_wrapper(WASMModuleInstance *module_inst, int32 arg0)
 {
     switch (arg0) {
         case 199: /* getuid */
             /* TODO */
         default:
-            printf("##_syscall0 called, syscall id: %d\n", arg0);
+            bh_printf("##_syscall0 called, syscall id: %d\n", arg0);
     }
     return 0;
 }
 
 static int32
-__syscall1_wrapper(int32 arg0, int32 arg1)
+__syscall1_wrapper(WASMModuleInstance *module_inst, int32 arg0, int32 arg1)
 {
     switch (arg0) {
         case 6: /* close */
             /* TODO */
         default:
-            printf("##_syscall1 called, syscall id: %d\n", arg0);
+            bh_printf("##_syscall1 called, syscall id: %d\n", arg0);
     }
     return 0;
 }
 
 static int32
-__syscall2_wrapper(int32 arg0, int32 arg1, int32 arg2)
+__syscall2_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2)
 {
     switch (arg0) {
         case 183: /* getcwd */
             /* TODO */
         default:
-            printf("##_syscall2 called, syscall id: %d\n", arg0);
+            bh_printf("##_syscall2 called, syscall id: %d\n", arg0);
     }
     return 0;
 }
 
 static int32
-__syscall3_wrapper(int32 arg0, int32 arg1, int32 arg2, int32 arg3)
+__syscall3_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2, int32 arg3)
 {
-    WASMModuleInstance *module_inst = get_module_inst();
-
     switch (arg0) {
         case 54: /* ioctl */
         {
@@ -152,73 +149,83 @@ __syscall3_wrapper(int32 arg0, int32 arg1, int32 arg2, int32 arg3)
         case 221: /* fcntl */
         /* TODO */
         default:
-        printf("##_syscall3 called, syscall id: %d\n", arg0);
+            bh_printf("##_syscall3 called, syscall id: %d\n", arg0);
     }
     return 0;
 }
 
 static int32
-__syscall4_wrapper(int32 arg0, int32 arg1, int32 arg2,
+__syscall4_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2,
                    int32 arg3, int32 arg4)
 {
-    printf("##_syscall4 called, syscall id: %d\n", arg0);
+    bh_printf("##_syscall4 called, syscall id: %d\n", arg0);
     return 0;
 }
 
 static int32
-__syscall5_wrapper(int32 arg0, int32 arg1, int32 arg2,
+__syscall5_wrapper(WASMModuleInstance *module_inst,
+                   int32 arg0, int32 arg1, int32 arg2,
                    int32 arg3, int32 arg4, int32 arg5)
 {
     switch (arg0) {
         case 140: /* llseek */
             /* TODO */
         default:
-            printf("##_syscall5 called, args[0]: %d\n", arg0);
+            bh_printf("##_syscall5 called, args[0]: %d\n", arg0);
     }
     return 0;
 }
 
 #define GET_EMCC_SYSCALL_ARGS()                                     \
-  WASMModuleInstance *module_inst = get_module_inst();              \
   int32 *args;                                                      \
   if (!validate_app_addr(args_off, 1))                              \
     return 0;                                                       \
   args = addr_app_to_native(args_off)                               \
 
-#define EMCC_SYSCALL_WRAPPER0(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id) {                \
-    return __syscall0_wrapper(id);                                  \
+#define EMCC_SYSCALL_WRAPPER0(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id) {                    \
+    return __syscall0_wrapper(module_inst, id);                         \
   }
 
-#define EMCC_SYSCALL_WRAPPER1(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall1_wrapper(id, args[0]);                         \
+#define EMCC_SYSCALL_WRAPPER1(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall1_wrapper(module_inst, id, args[0]);                \
   }
 
-#define EMCC_SYSCALL_WRAPPER2(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall2_wrapper(id, args[0], args[1]);                \
+#define EMCC_SYSCALL_WRAPPER2(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off){     \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall2_wrapper(module_inst, id, args[0], args[1]);       \
   }
 
-#define EMCC_SYSCALL_WRAPPER3(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall3_wrapper(id, args[0], args[1], args[2]);       \
+#define EMCC_SYSCALL_WRAPPER3(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall3_wrapper(module_inst, id,                          \
+                              args[0], args[1], args[2]);               \
   }
 
-#define EMCC_SYSCALL_WRAPPER4(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall4_wrapper(id, args[0], args[1], args[2], args[3]);\
+#define EMCC_SYSCALL_WRAPPER4(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall4_wrapper(module_inst, id,                          \
+                              args[0], args[1], args[2], args[3]);      \
   }
 
-#define EMCC_SYSCALL_WRAPPER5(id)                                   \
-  static int32 ___syscall##id##_wrapper(int32 _id, int32 args_off) {\
-    GET_EMCC_SYSCALL_ARGS();                                        \
-    return __syscall5_wrapper(id, args[0], args[1], args[2],        \
-                              args[3], args[4]);                    \
+#define EMCC_SYSCALL_WRAPPER5(id)                                       \
+  static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\
+                                        int32 _id, int32 args_off) {    \
+    GET_EMCC_SYSCALL_ARGS();                                            \
+    return __syscall5_wrapper(module_inst, id,                          \
+                              args[0], args[1], args[2],                \
+                              args[3], args[4]);                        \
   }
 
 EMCC_SYSCALL_WRAPPER0(199)
@@ -237,18 +244,16 @@ EMCC_SYSCALL_WRAPPER3(221)
 EMCC_SYSCALL_WRAPPER5(140)
 
 static int32
-getTotalMemory_wrapper()
+getTotalMemory_wrapper(WASMModuleInstance *module_inst)
 {
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     WASMMemoryInstance *memory = module_inst->default_memory;
     return NumBytesPerPage * memory->cur_page_count;
 }
 
 static int32
-enlargeMemory_wrapper()
+enlargeMemory_wrapper(WASMModuleInstance *module_inst)
 {
     bool ret;
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     WASMMemoryInstance *memory = module_inst->default_memory;
     uint32 DYNAMICTOP_PTR_offset = module_inst->DYNAMICTOP_PTR_offset;
     uint32 addr_data_offset = *(uint32*)(memory->global_data + DYNAMICTOP_PTR_offset);
@@ -267,9 +272,8 @@ enlargeMemory_wrapper()
 }
 
 static void
-_abort_wrapper(int32 code)
+_abort_wrapper(WASMModuleInstance *module_inst, int32 code)
 {
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     char buf[32];
 
     snprintf(buf, sizeof(buf), "env.abort(%i)", code);
@@ -277,14 +281,13 @@ _abort_wrapper(int32 code)
 }
 
 static void
-abortOnCannotGrowMemory_wrapper()
+abortOnCannotGrowMemory_wrapper(WASMModuleInstance *module_inst)
 {
-    WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
     wasm_runtime_set_exception(module_inst, "abort on cannot grow memory");
 }
 
 static void
-___setErrNo_wrapper(int32 error_no)
+___setErrNo_wrapper(WASMModuleInstance *module_inst, int32 error_no)
 {
     errno = error_no;
 }

+ 75 - 0
core/iwasm/runtime/vmcore-wasm/invokeNative_arm.s

@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+        .text
+        .align  2
+        .global invokeNative
+        .type   invokeNative,function
+
+/*
+ * Arguments passed in:
+ *
+ * r0 argv
+ * r1 argc
+ * r2 function pntr
+ */
+
+invokeNative:
+        stmfd   sp!, {r4, r5, r6, r7, lr}
+        mov     r4, r0           /* get argv */
+        mov     r5, r1           /* get argc */
+        mov     ip, r2           /* get function ptr */
+
+        cmp     r5, #2           /* is argc < 2 ? */
+        blt     return
+
+        ldr     r0, [r4], #4     /* argv[0] */
+        ldr     r1, [r4], #4     /* argv[1] */
+
+        mov     r6, #0
+
+        cmp     r5, #2
+        beq     call_func
+        ldr     r2, [r4], #4
+        cmp     r5, #3
+        beq     call_func
+        ldr     r3, [r4], #4
+
+        subs    r5, r5, #4       /* now we have r0 ~ r3 */
+
+        /* Ensure address is 8 byte aligned */
+        mov     r6, r5, lsl#2
+        add     r6, r6, #7
+        bic     r6, r6, #7
+        add     r6, r6, #4       /* +4 because only odd(5) registers are in stack */
+        subs    sp, sp, r6       /* for stacked args */
+        mov     r7, sp
+
+loop_args:
+        cmp     r5, #0
+        beq     call_func
+        ldr     lr, [r4], #4
+        str     lr, [r7], #4
+        subs    r5, r5, #1
+        b       loop_args
+
+call_func:
+        blx     ip
+
+        add     sp, sp, r6       /* recover sp */
+
+return:
+        ldmfd   sp!, {r4, r5, r6, r7, lr}
+        bx      lr

+ 72 - 0
core/iwasm/runtime/vmcore-wasm/invokeNative_em64.s

@@ -0,0 +1,72 @@
+//    Licensed to the Apache Software Foundation (ASF) under one or more
+//    contributor license agreements.  See the NOTICE file distributed with
+//    this work for additional information regarding copyright ownership.
+//    The ASF licenses this file to You under the Apache License, Version 2.0
+//    (the "License"); you may not use this file except in compliance with
+//    the License.  You may obtain a copy of the License at
+// 
+//      http://www.apache.org/licenses/LICENSE-2.0
+// 
+//   Unless required by applicable law or agreed to in writing, software
+//   distributed under the License is distributed on an "AS IS" BASIS,
+//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//   See the License for the specific language governing permissions and
+//   limitations under the License.
+//
+//   Author: Ivan Volosyuk
+//
+    .text
+    .align 2
+.globl invokeNative
+    .type    invokeNative, @function
+invokeNative:
+    /*  rdi - memory */
+    /*  rsi - n fp args */
+    /*  rdx - n mem args */
+    /*  rcx - function ptr */
+
+    push %rbp
+    mov %rsp, %rbp
+
+    /* cycle to fill all fp args */
+    movq 8(%rdi), %xmm0
+    movq 16(%rdi), %xmm1
+    movq 24(%rdi), %xmm2
+    movq 32(%rdi), %xmm3
+    movq 40(%rdi), %xmm4
+    movq 48(%rdi), %xmm5
+    movq 56(%rdi), %xmm6
+    movq 64(%rdi), %xmm7
+
+    mov %rsp, %r10 /* Check that stack is aligned on */
+    and $8, %r10   /* 16 bytes. This code may be removed */
+    jz no_abort    /* when we are sure that compiler always */
+    int3           /* calls us with aligned stack */
+no_abort:
+    mov %rdx, %r10 /* Align stack on 16 bytes before pushing */
+    and $1, %r10   /* stack arguments in case we have an odd */
+    shl $3, %r10   /* number of stack arguments */
+    sub %r10, %rsp
+    /* store memory args */
+    movq %rcx, %r10 /* func ptr */
+    movq %rdx, %rcx /* counter */
+    lea 8+64+48-8(%rdi,%rcx,8), %rdx
+    sub %rsp, %rdx
+    cmpq $0, %rcx
+    jz cycle_end
+cycle:
+    push 0(%rsp,%rdx)
+    loop cycle
+cycle_end:
+    movq 80(%rdi), %rsi
+    movq 88(%rdi), %rdx
+    movq 96(%rdi), %rcx
+    movq 104(%rdi), %r8
+    movq 112(%rdi), %r9
+
+    movq 72(%rdi), %rdi
+
+    call *%r10
+    leave
+    ret
+

+ 85 - 0
core/iwasm/runtime/vmcore-wasm/invokeNative_mips.s

@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+    .text
+    .align 2
+    .globl invokeNative
+    .ent invokeNative
+    .type invokeNative, @function
+
+/**
+ * On function entry parameters:
+ * $4 = args
+ * $5 = arg_num
+ * $6 = func_ptr
+ */
+
+invokeNative:
+    .frame $fp, 8, $0
+    .mask 0x00000000, 0
+    .fmask 0x00000000, 0
+
+    /* Fixed part of frame */
+    subu $sp, 8
+
+    /* save registers */
+    sw $31, 4($sp)
+    sw $fp, 0($sp)
+
+    /* set frame pointer to bottom of fixed frame */
+    move $fp, $sp
+
+    /* allocate enough stack space */
+    sll $11, $5, 2
+    subu $sp, $11
+
+    /* make 8-byte aligned */
+    and $sp, ~7 
+
+    move $9, $sp
+    move $25, $6    /* $25 = func_ptr */
+
+push_args:
+    beq $5, 0, done /* arg_num == 0 ? */
+    lw $8, 0($4)
+    sw $8, 0($9)
+    addu $4, 4
+    addu $9, 4
+    subu $5, 1      /* arg_index-- */
+    j push_args
+
+done:
+    lw $4, 0($sp)   /* Load $4..$7 from stack */
+    lw $5, 4($sp)
+    lw $6, 8($sp)
+    lw $7, 12($sp)
+    ldc1 $f12, 0($sp) /* Load $f12, $f13, $f14, $f15 */
+    ldc1 $f14, 8($sp)
+
+    jalr $25       /* call function */
+
+    nop
+
+    /* restore saved registers */
+    move $sp, $fp
+    lw $31, 4($sp)
+    lw $fp, 0($sp)
+
+    /* pop frame */
+    addu $sp, $sp, 8
+
+    j $31
+    .end invokeNative

+ 5 - 3
core/iwasm/runtime/vmcore-wasm/vmcore.cmake

@@ -17,11 +17,13 @@ set (VMCORE_LIB_DIR ${CMAKE_CURRENT_LIST_DIR})
 include_directories(${VMCORE_LIB_DIR})
 include_directories(${VMCORE_LIB_DIR}/../include)
 
+file (GLOB_RECURSE c_source_all ${VMCORE_LIB_DIR}/*.c)
+list (REMOVE_ITEM c_source_all ${VMCORE_LIB_DIR}/invokeNative_general.c)
+
 if (${BUILD_AS_64BIT_SUPPORT} STREQUAL "YES")
-file (GLOB_RECURSE source_all ${VMCORE_LIB_DIR}/*.c)
+set (source_all ${c_source_all} ${VMCORE_LIB_DIR}/invokeNative_em64.s)
 else ()
-file (GLOB_RECURSE source_all ${VMCORE_LIB_DIR}/*.c ${VMCORE_LIB_DIR}/*.s)
-list (REMOVE_ITEM source_all ${VMCORE_LIB_DIR}/invokeNative_general.c)
+set (source_all ${c_source_all} ${VMCORE_LIB_DIR}/invokeNative_ia32.s)
 endif ()
 
 set (VMCORE_LIB_SOURCE ${source_all})

+ 12 - 64
core/iwasm/runtime/vmcore-wasm/wasm_interp.c

@@ -284,7 +284,7 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
     }
     if (sign && (shift < maxbits) && (byte & 0x40)) {
         /* Sign extend */
-        result |= - (1 << shift);
+        result |= - ((uint64)1 << shift);
     }
     return result;
 }
@@ -622,21 +622,6 @@ FREE_FRAME(WASMThread *self, WASMInterpFrame *frame)
     wasm_thread_free_wasm_frame(self, frame);
 }
 
-typedef void (*GenericFunctionPointer)();
-int64 invokeNative(uint32 *args, uint32 sz, GenericFunctionPointer f);
-
-typedef float64 (*Float64FuncPtr)(uint32*, uint32, GenericFunctionPointer);
-typedef float32 (*Float32FuncPtr)(uint32*, uint32, GenericFunctionPointer);
-typedef int64 (*Int64FuncPtr)(uint32*, uint32, GenericFunctionPointer);
-typedef int32 (*Int32FuncPtr)(uint32*, uint32, GenericFunctionPointer);
-typedef void (*VoidFuncPtr)(uint32*, uint32, GenericFunctionPointer);
-
-static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
-static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
-static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
-static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
-static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
-
 static void
 wasm_interp_call_func_native(WASMThread *self,
                              WASMFunctionInstance *cur_func,
@@ -644,9 +629,8 @@ wasm_interp_call_func_native(WASMThread *self,
 {
     unsigned local_cell_num = 2;
     WASMInterpFrame *frame;
-    typedef void (*F)(WASMThread*, uint32 *argv);
-    union { F f; void *v; } u;
-    uint32 argv_buf[32], *argv, argc = cur_func->param_cell_num;
+    uint32 argv_ret[2];
+    bool ret;
 
     if (!(frame = ALLOC_FRAME
                 (self, wasm_interp_interp_frame_size(local_cell_num), prev_frame)))
@@ -658,60 +642,24 @@ wasm_interp_call_func_native(WASMThread *self,
 
     wasm_thread_set_cur_frame (self, frame);
 
-    if (argc <= 32)
-        argv = argv_buf;
-    else {
-        if (!(argv = wasm_malloc(sizeof(uint32) * argc))) {
-            wasm_runtime_set_exception(self->module_inst,
-                    "WASM call native failed: allocate memory failed.");
-            return;
-        }
-    }
-
-    word_copy(argv, frame->lp, argc);
+    ret = wasm_runtime_invoke_native(cur_func->u.func_import->func_ptr_linked,
+                                     cur_func->u.func_import->func_type,
+                                     self->module_inst,
+                                     frame->lp, cur_func->param_cell_num, argv_ret);
 
-    u.v = cur_func->u.func_import->func_ptr_linked;
-    {
-        WASMType *func_type = cur_func->u.func_import->func_type;
-        uint8 ret_type = func_type->result_count
-                         ? func_type->types[func_type->param_count]
-                         : VALUE_TYPE_VOID;
-        GenericFunctionPointer f = (GenericFunctionPointer)(uintptr_t)u.v;
-
-        if (func_type->result_count == 0) {
-            invokeNative_Void(argv, argc, f);
-        }
-        else {
-            switch (ret_type) {
-                case VALUE_TYPE_I32:
-                    argv[0] = invokeNative_Int32(argv, argc, f);
-                    break;
-                case VALUE_TYPE_I64:
-                    PUT_I64_TO_ADDR(argv, invokeNative_Int64(argv, argc, f));
-                    break;
-                case VALUE_TYPE_F32:
-                    *(float32*)argv = invokeNative_Float32(argv, argc, f);
-                    break;
-                case VALUE_TYPE_F64:
-                    PUT_F64_TO_ADDR(argv, invokeNative_Float64(argv, argc, f));
-                    break;
-            }
-        }
-    }
+    if (!ret)
+        return;
 
     if (cur_func->ret_cell_num == 1) {
-        prev_frame->sp[0] = argv[0];
+        prev_frame->sp[0] = argv_ret[0];
         prev_frame->sp++;
     }
     else if (cur_func->ret_cell_num == 2) {
-        prev_frame->sp[0] = argv[0];
-        prev_frame->sp[1] = argv[1];
+        prev_frame->sp[0] = argv_ret[0];
+        prev_frame->sp[1] = argv_ret[1];
         prev_frame->sp += 2;
     }
 
-    if (argc > 32)
-        wasm_free(argv);
-
     FREE_FRAME(self, frame);
     wasm_thread_set_cur_frame(self, prev_frame);
 }

+ 1 - 1
core/iwasm/runtime/vmcore-wasm/wasm_loader.c

@@ -72,7 +72,7 @@ read_leb(const uint8 *buf, const uint8 *buf_end,
     }
     if (sign && (shift < maxbits) && (byte & 0x40)) {
         /* Sign extend */
-        result |= - (1 << shift);
+        result |= - ((uint64)1 << shift);
     }
     *p_result = result;
     return true;

+ 278 - 25
core/iwasm/runtime/vmcore-wasm/wasm_runtime.c

@@ -70,33 +70,41 @@ wasm_runtime_call_wasm(WASMModuleInstance *module_inst,
                        WASMFunctionInstance *function,
                        unsigned argc, uint32 argv[])
 {
-    if (!exec_env) {
-        if (!module_inst->wasm_stack) {
-            if (!(module_inst->wasm_stack =
-                        wasm_malloc(module_inst->wasm_stack_size))) {
-                wasm_runtime_set_exception(module_inst, "allocate memory failed.");
-                return false;
-            }
-        }
-
-        init_wasm_stack(&module_inst->main_tlr.wasm_stack,
-                        module_inst->wasm_stack, module_inst->wasm_stack_size);
-    }
-    else {
-        uintptr_t stack = (uintptr_t)exec_env->stack;
-        uint32 stack_size;
-
-        /* Set to 8 bytes align */
-        stack = (stack + 7) & ~7;
-        stack_size = exec_env->stack_size - (stack - (uintptr_t)exec_env->stack);
+    /* Only init stack when no application is running. */
+    if (!wasm_runtime_get_self()->cur_frame) {
+       if (!exec_env) {
+            if (!module_inst->wasm_stack) {
+                if (!(module_inst->wasm_stack =
+                            wasm_malloc(module_inst->wasm_stack_size))) {
+                    wasm_runtime_set_exception(module_inst,
+                                               "allocate memory failed.");
+                    return false;
+                }
 
-        if (!exec_env->stack || exec_env->stack_size <= 0
-            || exec_env->stack_size < stack - (uintptr_t)exec_env->stack) {
-            wasm_runtime_set_exception(module_inst, "Invalid execution stack info.");
-            return false;
-        }
+                init_wasm_stack(&module_inst->main_tlr.wasm_stack,
+                                module_inst->wasm_stack,
+                                module_inst->wasm_stack_size);
+            }
+       }
+       else {
+           uintptr_t stack = (uintptr_t)exec_env->stack;
+           uint32 stack_size;
+
+           /* Set to 8 bytes align */
+           stack = (stack + 7) & ~7;
+           stack_size = exec_env->stack_size
+                        - (stack - (uintptr_t)exec_env->stack);
+
+           if (!exec_env->stack || exec_env->stack_size <= 0
+               || exec_env->stack_size < stack - (uintptr_t)exec_env->stack) {
+               wasm_runtime_set_exception(module_inst,
+                                          "Invalid execution stack info.");
+               return false;
+            }
 
-        init_wasm_stack(&module_inst->main_tlr.wasm_stack, (uint8*)stack, stack_size);
+            init_wasm_stack(&module_inst->main_tlr.wasm_stack,
+                            (uint8*)stack, stack_size);
+       }
     }
 
     wasm_interp_call_wasm(function, argc, argv);
@@ -1419,3 +1427,248 @@ wasm_runtime_load_aot(uint8 *aot_file, uint32 aot_file_size,
     return NULL;
 }
 
+static inline void
+word_copy(uint32 *dest, uint32 *src, unsigned num)
+{
+    for (; num > 0; num--)
+        *dest++ = *src++;
+}
+
+#define PUT_I64_TO_ADDR(addr, value) do {       \
+    union { int64 val; uint32 parts[2]; } u;    \
+    u.val = (value);                            \
+    (addr)[0] = u.parts[0];                     \
+    (addr)[1] = u.parts[1];                     \
+  } while (0)
+
+#define PUT_F64_TO_ADDR(addr, value) do {       \
+    union { float64 val; uint32 parts[2]; } u;  \
+    u.val = (value);                            \
+    (addr)[0] = u.parts[0];                     \
+    (addr)[1] = u.parts[1];                     \
+  } while (0)
+
+#if !defined(__x86_64__) && !defined(__amd_64__)
+
+typedef void (*GenericFunctionPointer)();
+int64 invokeNative(uint32 *args, uint32 sz, GenericFunctionPointer f);
+
+typedef float64 (*Float64FuncPtr)(uint32*, uint32, GenericFunctionPointer);
+typedef float32 (*Float32FuncPtr)(uint32*, uint32, GenericFunctionPointer);
+typedef int64 (*Int64FuncPtr)(uint32*, uint32, GenericFunctionPointer);
+typedef int32 (*Int32FuncPtr)(uint32*, uint32, GenericFunctionPointer);
+typedef void (*VoidFuncPtr)(uint32*, uint32, GenericFunctionPointer);
+
+static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
+static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
+static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
+static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
+static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
+
+/* As JavaScript can't represent int64s, emcc compiles C int64 argument
+   into two WASM i32 arguments, see:
+        https://github.com/emscripten-core/emscripten/issues/7199
+   And also JavaScript float point is always 64-bit, emcc compiles
+   float32 argument into WASM f64 argument.
+   But clang compiles C int64 argument into WASM i64 argument, and
+   compiles C float32 argument into WASM f32 argument.
+   So for the compatability of emcc and clang, we treat i64 as two i32s,
+   treat f32 as f64 while passing arguments to the native function, and
+   require the native function uses two i32 arguments instead one i64
+   argument, and uses double argument instead of float argment. */
+
+bool
+wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
+                           WASMModuleInstance *module_inst,
+                           uint32 *argv, uint32 argc, uint32 *ret)
+{
+    union { float64 val; int32 parts[2]; } u;
+    uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
+    uint64 size;
+
+    argc1 = func_type->param_count * 2 + 2;
+
+    if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
+        size = ((uint64)sizeof(uint32)) * argc1;
+        if (size >= UINT_MAX
+            || !(argv1 = wasm_malloc((uint32)size))) {
+            wasm_runtime_set_exception(module_inst, "allocate memory failed.");
+            return false;
+        }
+    }
+
+    for (i = 0; i < sizeof(WASMModuleInstance*) / sizeof(uint32); i++)
+        argv1[j++] = ((uint32*)&module_inst)[i];
+
+    for (i = 0; i < func_type->param_count; i++) {
+        switch (func_type->types[i]) {
+            case VALUE_TYPE_I32:
+                argv1[j++] = *argv++;
+                break;
+            case VALUE_TYPE_I64:
+            case VALUE_TYPE_F64:
+                argv1[j++] = *argv++;
+                argv1[j++] = *argv++;
+                break;
+            case VALUE_TYPE_F32:
+                u.val = *(float32*)argv++;
+#if defined(__arm__) || defined(__mips__)
+                /* 64-bit data must be 8 bytes alined in arm and mips */
+                if (j & 1)
+                    j++;
+#endif
+                argv1[j++] = u.parts[0];
+                argv1[j++] = u.parts[1];
+                break;
+            default:
+                wasm_assert(0);
+                break;
+        }
+    }
+
+    if (func_type->result_count == 0) {
+        invokeNative_Void(argv1, argc1, func_ptr);
+    }
+    else {
+        switch (func_type->types[func_type->param_count]) {
+            case VALUE_TYPE_I32:
+                ret[0] = invokeNative_Int32(argv1, argc1, func_ptr);
+                break;
+            case VALUE_TYPE_I64:
+                PUT_I64_TO_ADDR(ret, invokeNative_Int64(argv1, argc1, func_ptr));
+                break;
+            case VALUE_TYPE_F32:
+                *(float32*)ret = invokeNative_Float32(argv1, argc1, func_ptr);
+                break;
+            case VALUE_TYPE_F64:
+                PUT_F64_TO_ADDR(ret, invokeNative_Float64(argv1, argc1, func_ptr));
+                break;
+        }
+    }
+
+    if (argv1 != argv_buf)
+        wasm_free(argv1);
+    return true;
+}
+
+#else /* else of !defined(__x86_64__) && !defined(__amd_64__) */
+
+typedef void (*GenericFunctionPointer)();
+int64 invokeNative(uint64 *args, uint64 n_fps, uint64 n_stacks, GenericFunctionPointer f);
+
+typedef float64 (*Float64FuncPtr)(uint64*, uint64, uint64, GenericFunctionPointer);
+typedef float32 (*Float32FuncPtr)(uint64*, uint64, uint64, GenericFunctionPointer);
+typedef int64 (*Int64FuncPtr)(uint64*,uint64, uint64, GenericFunctionPointer);
+typedef int32 (*Int32FuncPtr)(uint64*, uint64, uint64, GenericFunctionPointer);
+typedef void (*VoidFuncPtr)(uint64*, uint64, uint64, GenericFunctionPointer);
+
+static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
+static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
+static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
+static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
+static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
+
+#if defined(_WIN32) || defined(_WIN32_)
+#define MAX_REG_FLOATS  4
+#define MAX_REG_INTS  4
+#else
+#define MAX_REG_FLOATS  8
+#define MAX_REG_INTS  6
+#endif
+
+bool
+wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
+                           WASMModuleInstance *module_inst,
+                           uint32 *argv, uint32 argc, uint32 *ret)
+{
+    uint64 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size;
+    uint32 *argv_src = argv, i, j, argc1, n_ints = 0, n_stacks = 0;
+#if defined(_WIN32) || defined(_WIN32_)
+    /* important difference in calling conventions */
+#define n_fps n_ints
+#else
+    int n_fps = 0;
+#endif
+
+    argc1 = 1 + MAX_REG_FLOATS + func_type->param_count + 2;
+    if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
+        size = sizeof(uint64) * argc1;
+        if (size >= UINT32_MAX
+            || !(argv1 = wasm_malloc(size))) {
+            wasm_runtime_set_exception(module_inst, "allocate memory failed.");
+            return false;
+        }
+    }
+
+    fps = argv1 + 1;
+    ints = fps + MAX_REG_FLOATS;
+    stacks = ints + MAX_REG_INTS;
+
+    ints[n_ints++] = (uint64)(uintptr_t)module_inst;
+
+    for (i = 0; i < func_type->param_count; i++) {
+        switch (func_type->types[i]) {
+            case VALUE_TYPE_I32:
+                if (n_ints < MAX_REG_INTS)
+                    ints[n_ints++] = *argv_src++;
+                else
+                    stacks[n_stacks++] = *argv_src++;
+                break;
+            case VALUE_TYPE_I64:
+                for (j = 0; j < 2; j++) {
+                    if (n_ints < MAX_REG_INTS)
+                        ints[n_ints++] = *argv_src++;
+                    else
+                        stacks[n_stacks++] = *argv_src++;
+                }
+                break;
+            case VALUE_TYPE_F32:
+                if (n_fps < MAX_REG_FLOATS)
+                    *(float64*)&fps[n_fps++] = *(float32*)argv_src++;
+                else
+                    *(float64*)&stacks[n_stacks++] = *(float32*)argv_src++;
+                break;
+            case VALUE_TYPE_F64:
+                if (n_fps < MAX_REG_FLOATS)
+                    *(float64*)&fps[n_fps++] = *(float64*)argv_src;
+                else
+                    *(float64*)&stacks[n_stacks++] = *(float64*)argv_src;
+                argv_src += 2;
+                break;
+            default:
+                wasm_assert(0);
+                break;
+        }
+    }
+
+    if (func_type->result_count == 0) {
+        invokeNative_Void(argv1, n_fps, n_stacks, func_ptr);
+    }
+    else {
+        switch (func_type->types[func_type->param_count]) {
+            case VALUE_TYPE_I32:
+                ret[0] = invokeNative_Int32(argv1, n_fps, n_stacks, func_ptr);
+                break;
+            case VALUE_TYPE_I64:
+                PUT_I64_TO_ADDR(ret, invokeNative_Int64(argv1, n_fps, n_stacks, func_ptr));
+                break;
+            case VALUE_TYPE_F32:
+                *(float32*)ret = invokeNative_Float32(argv1, n_fps, n_stacks, func_ptr);
+                break;
+            case VALUE_TYPE_F64:
+                PUT_F64_TO_ADDR(ret, invokeNative_Float64(argv1, n_fps, n_stacks, func_ptr));
+                break;
+            default:
+                wasm_assert(0);
+                break;
+        }
+    }
+
+    if (argv1 != argv_buf)
+        wasm_free(argv1);
+
+    return true;
+}
+
+#endif /* end of !defined(__x86_64__) && !defined(__amd_64__) */
+

+ 5 - 0
core/iwasm/runtime/vmcore-wasm/wasm_runtime.h

@@ -315,6 +315,11 @@ int32
 wasm_runtime_addr_native_to_app(WASMModuleInstance *module_inst,
                                 void *native_ptr);
 
+bool
+wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
+                           WASMModuleInstance *module_inst,
+                           uint32 *argv, uint32 argc, uint32 *ret);
+
 #ifdef __cplusplus
 }
 #endif

+ 2 - 2
core/shared-lib/mem-alloc/ems/ems_alloc.c

@@ -152,7 +152,7 @@ void gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
     bh_assert(
             hmu && (gc_uint8*) hmu >= heap->base_addr
                     && (gc_uint8*) hmu < heap->base_addr + heap->current_size);
-    bh_assert(((gc_uint32) hmu_to_obj(hmu) & 7) == 0);
+    bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0);
     bh_assert(
             size > 0
                     && ((gc_uint8*) hmu) + size
@@ -242,7 +242,7 @@ BH_STATIC hmu_t *alloc_hmu(gc_heap_t *heap, gc_size_t size)
 
             p = node->next;
             node->next = p->next;
-            bh_assert(((gc_int32) hmu_to_obj(p) & 7) == 0);
+            bh_assert(((gc_int32)(uintptr_t)hmu_to_obj(p) & 7) == 0);
 
             if ((gc_size_t) node_idx
                     != init_node_idx&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { /* with bigger size*/

+ 1 - 1
core/shared-lib/platform/darwin/bh_platform.c

@@ -1,5 +1,5 @@
 #include "lib_export.h"
-#include "native_interface.h"
+#include "sensor_api.h"
 #include "connection_api.h"
 #include "gui_api.h"
 

+ 31 - 14
samples/littlevgl/vgl-wasm-runtime/src/display_indev.h

@@ -1,9 +1,10 @@
 #ifndef DISPLAY_INDEV_H_
 #define DISPLAY_INDEV_H_
 #include <stdio.h>
-#include "bh_platform.h"
 #include <stdbool.h>
 #include <inttypes.h>
+#include "bh_platform.h"
+#include "wasm_export.h"
 
 #define USE_MOUSE 1
 typedef union {
@@ -54,21 +55,37 @@ enum {
     LV_OPA_100 = 255,
     LV_OPA_COVER = 255,
 };
-extern void display_init(void);
-extern void display_deinit(void);
-extern int time_get_ms();
-extern bool touchscreen_read(lv_indev_data_t * data);
 
 extern void xpt2046_init(void);
-extern void display_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        int32 color_p_offset);
-extern void display_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        lv_color_t color_p);
-extern void display_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        const lv_color_t * color_p);
-extern bool display_input_read(int32 data_offset);
-void display_vdb_write(int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
-        lv_coord_t y, int32 color_p_offset, lv_opa_t opa);
+
+extern bool touchscreen_read(lv_indev_data_t * data);
+
 extern bool mouse_read(lv_indev_data_t * data);
+
+extern void display_init(wasm_module_inst_t module_inst);
+
+extern void display_deinit(wasm_module_inst_t module_inst);
+
+extern int time_get_ms(wasm_module_inst_t module_inst);
+
+extern void display_flush(wasm_module_inst_t module_inst,
+                          int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+                          int32 color_p_offset);
+
+extern void display_fill(wasm_module_inst_t module_inst,
+                         int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+                         lv_color_t color_p);
+
+extern void display_map(wasm_module_inst_t module_inst,
+                        int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+                        const lv_color_t * color_p);
+
+extern bool display_input_read(wasm_module_inst_t module_inst,
+                               int32 data_offset);
+
+void display_vdb_write(wasm_module_inst_t module_inst,
+                       int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
+                       lv_coord_t y, int32 color_p_offset, lv_opa_t opa);
+
 #endif
 

+ 3 - 2
samples/littlevgl/vgl-wasm-runtime/src/ext_lib_export.c

@@ -1,5 +1,5 @@
 #include "lib_export.h"
-#include "native_interface.h"
+#include "sensor_api.h"
 #include "connection_api.h"
 #include "display_indev.h"
 
@@ -12,6 +12,7 @@ static NativeSymbol extended_native_symbol_defs[] = {
         EXPORT_WASM_API(display_fill),
         EXPORT_WASM_API(display_vdb_write),
         EXPORT_WASM_API(display_map),
-        EXPORT_WASM_API(time_get_ms), };
+        EXPORT_WASM_API(time_get_ms)
+};
 
 #include "ext_lib_export.h"

+ 29 - 18
samples/littlevgl/vgl-wasm-runtime/src/platform/linux/display_indev.c

@@ -35,7 +35,8 @@ static uint32_t tft_fb[MONITOR_HOR_RES * MONITOR_VER_RES];
 
 
 
-int time_get_ms()
+int
+time_get_ms(wasm_module_inst_t module_inst)
 {
     static struct timeval tv;
     gettimeofday(&tv, NULL);
@@ -157,15 +158,16 @@ void monitor_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
 }
 
 
-void display_init(void)
+void
+display_init(wasm_module_inst_t module_inst)
 {
 }
 
-void display_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        int32 color_p_offset)
+void
+display_flush(wasm_module_inst_t module_inst,
+              int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+              int32 color_p_offset)
 {
-
-    wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
     if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
         return;
     lv_color_t * color_p = wasm_runtime_addr_app_to_native(module_inst,
@@ -173,21 +175,28 @@ void display_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
 
     monitor_flush(x1, y1, x2, y2, color_p);
 }
-void display_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        lv_color_t color_p)
+
+void
+display_fill(wasm_module_inst_t module_inst,
+             int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+             lv_color_t color_p)
 {
     monitor_fill(x1, y1, x2, y2, color_p);
 }
-void display_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        const lv_color_t * color_p)
+
+void
+display_map(wasm_module_inst_t module_inst,
+            int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+            const lv_color_t * color_p)
 {
     monitor_map(x1, y1, x2, y2, color_p);
 }
 
-bool display_input_read(int32 data_p_offset)
+bool
+display_input_read(wasm_module_inst_t module_inst,
+                   int32 data_p_offset)
 {
     bool ret;
-    wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
     if (!wasm_runtime_validate_app_addr(module_inst, data_p_offset, 1))
         return false;
 
@@ -205,21 +214,23 @@ bool display_input_read(int32 data_p_offset)
                                                data_p_offset);
 
     data_app->point = data.point;
-    data_app->user_data_offset = (int32_t)data.user_data;
+    data_app->user_data_offset =
+        wasm_runtime_addr_native_to_app(module_inst, data.user_data);
     data_app->state = data.state;
 
     return ret;
 }
 
-void display_deinit(void)
+void
+display_deinit(wasm_module_inst_t module_inst)
 {
-
 }
 
-void display_vdb_write(int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
-        lv_coord_t y, int32 color_p_offset, lv_opa_t opa)
+void
+display_vdb_write(wasm_module_inst_t module_inst,
+                  int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
+                  lv_coord_t y, int32 color_p_offset, lv_opa_t opa)
 {
-    wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
     if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
         return;
     lv_color_t *color = wasm_runtime_addr_app_to_native(module_inst,

+ 29 - 18
samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/display_indev.c

@@ -24,8 +24,11 @@
 #ifndef MONITOR_ZOOM
 #define MONITOR_ZOOM        1
 #endif
-int lcd_initialized = 0;
-void display_init(void)
+
+static int lcd_initialized = 0;
+
+void
+display_init(wasm_module_inst_t module_inst)
 {
     if (lcd_initialized != 0) {
         return;
@@ -36,10 +39,11 @@ void display_init(void)
     display_blanking_off(NULL);
 }
 
-void display_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        int32 color_p_offset)
+void
+display_flush(wasm_module_inst_t module_inst,
+              int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+              int32 color_p_offset)
 {
-    wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
     if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
         return;
     lv_color_t * color_p = wasm_runtime_addr_app_to_native(module_inst,
@@ -57,20 +61,24 @@ void display_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
 
     /*lv_flush_ready();*/
 }
-void display_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        lv_color_t color_p)
-{
 
-}
-void display_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
-        const lv_color_t * color_p)
+void
+display_fill(wasm_module_inst_t module_inst,
+             int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+             lv_color_t color_p)
 {
+}
 
+void
+display_map(wasm_module_inst_t module_inst,
+            int32_t x1, int32_t y1, int32_t x2, int32_t y2,
+            const lv_color_t * color_p)
+{
 }
 
-bool display_input_read(int32 data_p_offset)
+bool
+display_input_read(wasm_module_inst_t module_inst, int32 data_p_offset)
 {
-    wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
     if (!wasm_runtime_validate_app_addr(module_inst, data_p_offset, 1))
         return false;
     lv_indev_data_t * data = wasm_runtime_addr_app_to_native(module_inst,
@@ -80,15 +88,17 @@ bool display_input_read(int32 data_p_offset)
 
 }
 
-void display_deinit(void)
+void
+display_deinit(wasm_module_inst_t module_inst)
 {
 
 }
 
-void display_vdb_write(int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
-        lv_coord_t y, int32 color_p_offset, lv_opa_t opa)
+void
+display_vdb_write(wasm_module_inst_t module_inst,
+                  int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
+                  lv_coord_t y, int32 color_p_offset, lv_opa_t opa)
 {
-    wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
     if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
         return;
     lv_color_t *color = wasm_runtime_addr_app_to_native(module_inst,
@@ -112,7 +122,8 @@ void display_vdb_write(int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
     *(buf_xy + 2) = color->blue;
 }
 
-int time_get_ms()
+int
+time_get_ms(wasm_module_inst_t module_inst)
 {
     return k_uptime_get_32();
 }

+ 1 - 0
test-tools/host-tool/CMakeLists.txt

@@ -51,6 +51,7 @@ add_definitions(-Wall -Wno-pointer-sign -DMALLOC_MEMORY_FROM_SYSTEM)
 
 include_directories(
     ${CMAKE_CURRENT_LIST_DIR}/src
+    ${WASM_DIR}/runtime/include
 )
 
 file (GLOB_RECURSE HOST_TOOL_SRC src/*.c)

+ 2 - 7
test-tools/host-tool/src/main.c

@@ -95,11 +95,6 @@ typedef enum REPLY_PACKET_TYPE {
     REPLY_TYPE_EVENT = 0, REPLY_TYPE_RESPONSE = 1
 } REPLY_PACKET_TYPE;
 
-/* Package Type */
-typedef enum {
-    Wasm_Module_Bytecode = 0, Wasm_Module_AoT, Package_Type_Unknown = 0xFFFF
-} PackageType;
-
 static uint32_t g_timeout_ms = DEFAULT_TIMEOUT_MS;
 static uint32_t g_alive_time_ms = DEFAULT_ALIVE_TIME_MS;
 static char *g_redirect_file_name = NULL;
@@ -153,7 +148,7 @@ static int send_request(request_t *request, bool is_install_wasm_bytecode_app)
     return ret;
 }
 
-static PackageType get_package_type(const char *buf, int size)
+static package_type_t get_app_package_type(const char *buf, int size)
 {
     if (buf && size > 4) {
         if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
@@ -205,7 +200,7 @@ static int install(inst_info *info)
     request->mid = gen_random_id();
 
     if ((info->module_type == NULL || strcmp(info->module_type, "wasm") == 0)
-            && get_package_type(app_file_buf, app_size) == Wasm_Module_Bytecode)
+            && get_app_package_type(app_file_buf, app_size) == Wasm_Module_Bytecode)
         is_wasm_bytecode_app = true;
     else
         is_wasm_bytecode_app = false;