Implement native function pointer check, addr conversion and register, update documents (#185)

Modified WASM runtime API:
- wasm_runtime_module_malloc()
- wasm_runtime_lookup_function()
Introduced runtime API
- wasm_runtime_register_natives()
This commit is contained in:
wenyongh
2020-03-04 20:12:38 +08:00
committed by GitHub
parent 2e36149e32
commit 0d3f304191
96 changed files with 2293 additions and 2317 deletions

View File

@ -15,10 +15,12 @@
#define MONITOR_ZOOM 1
#endif
extern int ili9340_init();
static int lcd_initialized = 0;
void
display_init(wasm_exec_env_t exec_env)
display_init(void)
{
if (lcd_initialized != 0) {
return;
@ -32,23 +34,23 @@ display_init(wasm_exec_env_t exec_env)
void
display_flush(wasm_exec_env_t exec_env,
int32_t x1, int32_t y1, int32_t x2, int32_t y2,
int32 color_p_offset)
lv_color_t *color)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
struct display_buffer_descriptor desc;
if (!wasm_runtime_validate_native_addr(module_inst,
color, sizeof(lv_color_t)))
return;
lv_color_t * color_p = wasm_runtime_addr_app_to_native(module_inst,
color_p_offset);
u16_t w = x2 - x1 + 1;
u16_t h = y2 - y1 + 1;
struct display_buffer_descriptor desc;
desc.buf_size = 3 * w * h;
desc.width = w;
desc.pitch = w;
desc.height = h;
display_write(NULL, x1, y1, &desc, (void *) color_p);
display_write(NULL, x1, y1, &desc, (void *)color);
/*lv_flush_ready();*/
}
@ -56,27 +58,28 @@ display_flush(wasm_exec_env_t exec_env,
void
display_fill(wasm_exec_env_t exec_env,
int32_t x1, int32_t y1, int32_t x2, int32_t y2,
lv_color_t color_p)
lv_color_t *color)
{
}
void
display_map(wasm_exec_env_t exec_env,
int32_t x1, int32_t y1, int32_t x2, int32_t y2,
const lv_color_t * color_p)
const lv_color_t *color)
{
}
bool
display_input_read(wasm_exec_env_t exec_env, int32 data_p_offset)
display_input_read(wasm_exec_env_t exec_env, void *data)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
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,
data_p_offset);
lv_indev_data_t *lv_data = (lv_indev_data_t*)data;
return touchscreen_read(data);
if (!wasm_runtime_validate_native_addr(module_inst,
lv_data, sizeof(lv_indev_data_t)))
return false;
return touchscreen_read(lv_data);
}
void
@ -86,28 +89,16 @@ display_deinit(wasm_exec_env_t exec_env)
void
display_vdb_write(wasm_exec_env_t exec_env,
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 *buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
lv_color_t *color, lv_opa_t opa)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
u8_t *buf_xy = (u8_t*)buf + 3 * x + 3 * y * buf_w;
if (!wasm_runtime_validate_native_addr(module_inst,
color, sizeof(lv_color_t)))
return;
lv_color_t *color = wasm_runtime_addr_app_to_native(module_inst,
color_p_offset);
void *buf = wasm_runtime_addr_app_to_native(module_inst, buf_offset);
u8_t *buf_xy = buf + 3 * x + 3 * y * buf_w;
/*
if (opa != LV_OPA_COVER) {
lv_color_t mix_color;
mix_color.red = *buf_xy;
mix_color.green = *(buf_xy+1);
mix_color.blue = *(buf_xy+2);
color = lv_color_mix(color, mix_color, opa);
}
*/
*buf_xy = color->red;
*(buf_xy + 1) = color->green;
*(buf_xy + 2) = color->blue;

View File

@ -15,6 +15,9 @@
#include "bi-inc/attr_container.h"
#include "module_wasm_app.h"
#include "wasm_export.h"
#include "sensor_native_api.h"
#include "connection_native_api.h"
#include "display_indev.h"
#include <zephyr.h>
#include <drivers/uart.h>
@ -30,7 +33,6 @@ int uart_char_cnt = 0;
static void uart_irq_callback(struct device *dev)
{
unsigned char ch;
int size = 0;
while (uart_poll_in(dev, &ch) == 0) {
uart_char_cnt++;
@ -77,11 +79,21 @@ timer_ctx_t timer_ctx;
static char global_heap_buf[370 * 1024] = { 0 };
extern void display_init(void);
static NativeSymbol native_symbols[] = {
#include "runtime_sensor.inl"
#include "connection.inl"
EXPORT_WASM_API_WITH_SIG(display_input_read, "(*)i"),
EXPORT_WASM_API_WITH_SIG(display_flush, "(iiii*)"),
EXPORT_WASM_API_WITH_SIG(display_fill, "(iiii*)"),
EXPORT_WASM_API_WITH_SIG(display_vdb_write, "(*iii*i)"),
EXPORT_WASM_API_WITH_SIG(display_map, "(iiii*)"),
EXPORT_WASM_API_WITH_SIG(time_get_ms, "()i")
};
int iwasm_main()
{
korp_thread tid, tm_tid;
uint32 n_native_symbols;
host_init();
@ -95,6 +107,13 @@ int iwasm_main()
goto fail1;
}
/* Register native functions */
n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
if (!wasm_runtime_register_natives("env",
native_symbols, n_native_symbols)) {
goto fail1;
}
display_init();
// timer manager