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

@ -41,7 +41,7 @@ static volatile bool sdl_refr_qry = false;
static volatile bool sdl_quit_qry = false;
void monitor_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
const lv_color_t * color_p)
const lv_color_t * color)
{
/*Return if the area is out the screen*/
if (x2 < 0 || y2 < 0 || x1 > MONITOR_HOR_RES - 1
@ -53,10 +53,10 @@ void monitor_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
uint32_t w = x2 - x1 + 1;
for (y = y1; y <= y2; y++) {
memcpy(&tft_fb[y * MONITOR_HOR_RES + x1], color_p,
memcpy(&tft_fb[y * MONITOR_HOR_RES + x1], color,
w * sizeof(lv_color_t));
color_p += w;
color += w;
}
sdl_refr_qry = true;
@ -72,7 +72,7 @@ void monitor_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
* @param color fill color
*/
void monitor_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
lv_color_t color)
lv_color_t *color)
{
/*Return if the area is out the screen*/
if (x2 < 0)
@ -92,7 +92,7 @@ void monitor_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
int32_t x;
int32_t y;
uint32_t color32 = color.full; //lv_color_to32(color);
uint32_t color32 = color->full; //lv_color_to32(color);
for (x = act_x1; x <= act_x2; x++) {
for (y = act_y1; y <= act_y2; y++) {
@ -109,10 +109,10 @@ void monitor_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
* @param y1 top coordinate
* @param x2 right coordinate
* @param y2 bottom coordinate
* @param color_p an array of colors
* @param color an array of colors
*/
void monitor_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
const lv_color_t * color_p)
const lv_color_t *color)
{
/*Return if the area is out the screen*/
if (x2 < 0)
@ -135,11 +135,11 @@ void monitor_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
for (y = act_y1; y <= act_y2; y++) {
for (x = act_x1; x <= act_x2; x++) {
tft_fb[y * MONITOR_HOR_RES + x] = color_p->full; //lv_color_to32(*color_p);
color_p++;
tft_fb[y * MONITOR_HOR_RES + x] = color->full; //lv_color_to32(*color);
color++;
}
color_p += x2 - act_x2;
color += x2 - act_x2;
}
sdl_refr_qry = true;
@ -147,62 +147,64 @@ void monitor_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
void
display_init(wasm_exec_env_t exec_env)
display_init(void)
{
}
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))
return;
lv_color_t * color_p = wasm_runtime_addr_app_to_native(module_inst,
color_p_offset);
monitor_flush(x1, y1, x2, y2, color_p);
if (!wasm_runtime_validate_native_addr(module_inst,
color, sizeof(lv_color_t)))
return;
monitor_flush(x1, y1, x2, y2, color);
}
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)
{
monitor_fill(x1, y1, x2, y2, color_p);
monitor_fill(x1, y1, x2, y2, 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)
{
monitor_map(x1, y1, x2, y2, color_p);
monitor_map(x1, y1, x2, y2, color);
}
typedef struct display_input_data {
lv_point_t point;
int32 user_data_offset;
uint8 state;
} display_input_data;
bool
display_input_read(wasm_exec_env_t exec_env,
int32 data_p_offset)
void *input_data_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
display_input_data *data_app = (display_input_data*)input_data_app;
bool ret;
if (!wasm_runtime_validate_app_addr(module_inst, data_p_offset, 1))
if (!wasm_runtime_validate_native_addr(module_inst,
data_app,
sizeof(display_input_data)))
return false;
struct {
lv_point_t point;
int32 user_data_offset;
uint8 state;
} *data_app;
lv_indev_data_t data = {0};
ret = mouse_read(&data);
data_app = wasm_runtime_addr_app_to_native(module_inst,
data_p_offset);
data_app->point = data.point;
data_app->user_data_offset =
wasm_runtime_addr_native_to_app(module_inst, data.user_data);
@ -218,35 +220,17 @@ 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))
unsigned char *buf_xy = (unsigned char*)buf + 4 * x + 4 * 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);
unsigned char *buf_xy = buf + 4 * x + 4 * y * buf_w;
lv_color_t * temp = (lv_color_t *) buf_xy;
*temp = *color;
/*
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;
*/
*(lv_color_t *)buf_xy = *color;
}
int monitor_sdl_refr_thread(void * param)

View File

@ -32,6 +32,10 @@
#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"
#define MAX 2048
#ifndef CONNECTION_UART
@ -389,7 +393,9 @@ static bool parse_args(int argc, char *argv[])
{ "uart", required_argument, NULL, 'u' },
{ "baudrate", required_argument, NULL, 'b' },
#endif
#if WASM_ENABLE_LIBC_WASI != 0
{ "wasi_root", required_argument, NULL, 'w' },
#endif
{ "help", required_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
@ -421,12 +427,14 @@ static bool parse_args(int argc, char *argv[])
printf("uart baudrate: %s\n", optarg);
break;
#endif
#if WASM_ENABLE_LIBC_WASI != 0
case 'w':
if (!wasm_set_wasi_root_dir(optarg)) {
printf("Fail to set wasi root dir: %s\n", optarg);
return false;
}
break;
#endif
case 'h':
showUsage();
return false;
@ -439,10 +447,22 @@ static bool parse_args(int argc, char *argv[])
return true;
}
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")
};
// Driver function
int iwasm_main(int argc, char *argv[])
{
korp_thread tid;
uint32 n_native_symbols;
if (!parse_args(argc, argv))
return -1;
@ -457,6 +477,13 @@ int iwasm_main(int argc, char *argv[])
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;
}
if (!init_connection_framework()) {
vm_thread_sys_destroy();
goto fail1;