Merge pull request #1 from intel/master

sync with master
This commit is contained in:
Wang Xin 2019-09-07 08:20:41 +08:00 committed by GitHub
commit 9b07113d77
251 changed files with 18569 additions and 856 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode
**/*build/

21
Dockerfile Normal file
View File

@ -0,0 +1,21 @@
# Currently supports clang-8 compiler
# Using the "test.c" app from the README.md:
# clang-8 --target=wasm32 -O3 -Wl,--initial-memory=131072,--allow-undefined,--export=main,--no-threads,--strip-all,--no-entry -nostdlib -o test.wasm test.c
# Pay attention to spacing above! ^
# iwasm test.wasm
FROM ubuntu:latest
RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y build-essential clang-8 cmake g++-multilib git lib32gcc-5-dev llvm-8 lld-8 nano
WORKDIR /root
RUN git clone https://github.com/intel/wasm-micro-runtime
RUN cd wasm-micro-runtime/core/iwasm/products/linux/ && mkdir build && \
cd build && cmake .. && make
RUN cd /usr/bin && ln -s wasm-ld-8 wasm-ld
RUN cd /usr/bin && ln -s ~/wasm-micro-runtime/core/iwasm/products/linux/build/iwasm iwasm

117
README.md
View File

@ -67,6 +67,52 @@ cd build
cmake ..
make
```
Mac
-------------------------
Make sure to install Xcode from App Store firstly, and install cmake.
If you use Homebrew, install cmake from the command line:
``` Bash
brew install cmake
```
Then build the source codes:
```
cd core/iwasm/products/darwin/
mkdir build
cd build
cmake ..
make
```
VxWorks
-------------------------
VxWorks 7 SR0620 release is validated.
First you need to build a VSB. Make sure *UTILS_UNIX* layer is added in the VSB.
After the VSB is built, export the VxWorks toolchain path by:
```
export <vsb_dir_path>/host/vx-compiler/bin:$PATH
```
Now switch to iwasm source tree to build the source code:
```
cd core/iwasm/products/vxworks/
mkdir build
cd build
cmake ..
make
```
Create a VIP based on the VSB. Make sure the following components are added:
* INCLUDE_POSIX_PTHREADS
* INCLUDE_POSIX_PTHREAD_SCHEDULER
* INCLUDE_SHARED_DATA
* INCLUDE_SHL
Copy the generated iwasm executable, the test WASM binary as well as the needed
shared libraries (libc.so.1, libllvm.so.1 or libgnu.so.1 depending on the VSB,
libunix.so.1) to a supported file system (eg: romfs).
Zephyr
-------------------------
You need to download the Zephyr source code first and embed WAMR into it.
@ -124,6 +170,23 @@ AliOS-Things
```
9. download the binary to developerkit board, check the output from serial port
Docker
-------------------------
[Docker](https://www.docker.com/) will download all the dependencies and build WAMR Core on your behalf.
Make sure you have Docker installed on your machine: [macOS](https://docs.docker.com/docker-for-mac/install/), [Windows](https://docs.docker.com/docker-for-windows/install/) or [Linux](https://docs.docker.com/install/linux/docker-ce/ubuntu/).
Build the Docker image:
``` Bash
docker build --rm -f "Dockerfile" -t wamr:latest .
```
Run the image in interactive mode:
``` Bash
docker run --rm -it wamr:latest
```
You'll now enter the container at `/root`.
Build WASM app
=========================
You can write a simple ```test.c``` as the first sample.
@ -154,7 +217,7 @@ int main(int argc, char **argv)
}
```
There are two methods to build a WASM binary. One is using Emscripten tool, another is using clang compiler.
There are three methods to build a WASM binary. They are Emscripten, the clang compiler and Docker.
## Use Emscripten tool
@ -215,12 +278,25 @@ clang-8 --target=wasm32 -O3 -Wl,--initial-memory=131072,--allow-undefined,--expo
You will get ```test.wasm``` which is the WASM app binary.
## Using Docker
The last method availble is using [Docker](https://www.docker.com/). We assume you've already configured Docker (see Platform section above) and have a running interactive shell. Currently the Dockerfile only supports compiling apps with clang, with Emscripten planned for the future.
Use the clang-8 command below to build the WASM C source code into the WASM binary.
```Bash
clang-8 --target=wasm32 -O3 -Wl,--initial-memory=131072,--allow-undefined,--export=main,
--no-threads,--strip-all,--no-entry -nostdlib -o test.wasm test.c
```
You will get ```test.wasm``` which is the WASM app binary.
Run WASM app
========================
Assume you are using Linux, the command to run the test.wasm is:
``` Bash
cd iwasm/products/linux/bin
cd iwasm/products/linux/build
./iwasm test.wasm
```
You will get the following output:
@ -336,7 +412,9 @@ void api_timer_restart(user_timer_t timer, int interval);
```
**Library extension reference**<br/>
Currently we provide the sensor API's as one library extension sample. In the header file ```lib/app-libs/extension/sensor/sensor.h```, the API set is defined as below:
Currently we provide several kinds of extension library for reference including sensor, connection and GUI.
Sensor API: In the header file ```lib/app-libs/extension/sensor/sensor.h```, the API set is defined as below:
``` C
sensor_t sensor_open(const char* name, int index,
void(*on_sensor_event)(sensor_t, attr_container_t *, void *),
@ -345,6 +423,31 @@ bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay);
bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg);
bool sensor_close(sensor_t sensor);
```
Connection API: In the header file `lib/app-libs/extension/connection/connection.h.`, the API set is defined as below:
``` C
/* Connection event type */
typedef enum {
/* Data is received */
CONN_EVENT_TYPE_DATA = 1,
/* Connection is disconnected */
CONN_EVENT_TYPE_DISCONNECT
} conn_event_type_t;
typedef void (*on_connection_event_f)(connection_t *conn,
conn_event_type_t type,
const char *data,
uint32 len,
void *user_data);
connection_t *api_open_connection(const char *name,
attr_container_t *args,
on_connection_event_f on_event,
void *user_data);
void api_close_connection(connection_t *conn);
int api_send_on_connection(connection_t *conn, const char *data, uint32 len);
bool api_config_connection(connection_t *conn, attr_container_t *cfg);
```
GUI API: The API's is list in header file ```lib/app-libs/extension/gui/wgl.h``` which is implemented based open soure 2D graphic library [LittlevGL](https://docs.littlevgl.com/en/html/index.html). Currently supported widgets include button, label, list and check box and more wigdet would be provided in future.
The mechanism of exporting native API to WASM application
=======================================================
@ -572,7 +675,9 @@ Please refer to the ```samples/simple``` folder for samples of WASM application
2D graphic user interface with LittlevGL
------------------------------------------------
This sample demonstrates that a graphic user interface application in WebAssembly integrates the LittlevGL, an open-source embedded 2d graphic library. The sample source code is under ```samples/littlevgl```
We have 2 samples for 2D graphic user interface.
One of them demonstrates that a graphic user interface application in WebAssembly integrates the LittlevGL, an open-source embedded 2d graphic library. The sample source code is under ```samples/littlevgl```
In this sample, the LittlevGL source code is built into the WebAssembly code with the user application source files. The platform interfaces defined by LittlevGL is implemented in the runtime and exported to the application through the declarations from source "ext_lib_export.c" as below:
@ -595,6 +700,10 @@ Below pictures show the WASM application is running on an STM board with an LCD
The sample also provides the native Linux version of application without the runtime under folder "vgl-native-ui-app". It can help to check differences between the implementations in native and WebAssembly.
<img src="./doc/pics/vgl_linux.PNG">
The other sample demonstrates that a graphic user interface application in WebAssembly programming with WAMR graphic library(WGL), which is implemented based on LittlevGL, an open-source embedded 2d graphic library. The sample source code is under ```samples/gui```
Unlike `sample/littlevgl/val-wasm-runtime`, in this sample, the LittlevGL source code is built into the WAMR runtime and exported to Webassembly applicaton but not directly built into Webassembly application. And WGL provides a group of WebAssembly wrapper API's for user to write graphic application. These API's are listed in: `<wamr_root>/core/iwasm/lib/app-libs/extension/gui/wgl.h`. Currently only a few API's are provided and there will be more.
Submit issues and contact the maintainers
=========================================

View File

@ -39,8 +39,11 @@ extern "C" {
#define SECTION_TYPE_DATA 11
enum {
WASM_Msg_Start = BASE_EVENT_MAX, TIMER_EVENT_WASM, SENSOR_EVENT_WASM,
WASM_Msg_Start = BASE_EVENT_MAX,
TIMER_EVENT_WASM,
SENSOR_EVENT_WASM,
CONNECTION_EVENT_WASM,
WIDGET_EVENT_WASM,
WASM_Msg_End = WASM_Msg_Start + 100
};

8
core/iwasm/lib/3rdparty/LICENCE.txt vendored Normal file
View File

@ -0,0 +1,8 @@
MIT licence
Copyright (c) 2016 Gábor Kiss-Vámosi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

495
core/iwasm/lib/3rdparty/lv_conf.h vendored Executable file
View File

@ -0,0 +1,495 @@
/**
* @file lv_conf.h
*
*/
/*
* COPY THIS FILE AS `lv_conf.h` NEXT TO the `lvgl` FOLDER
*/
#if 1 /*Set it to "1" to enable content*/
#ifndef LV_CONF_H
#define LV_CONF_H
/* clang-format off */
#include <stdint.h>
/*====================
Graphical settings
*====================*/
/* Maximal horizontal and vertical resolution to support by the library.*/
#define LV_HOR_RES_MAX (320)
#define LV_VER_RES_MAX (240)
/* Color depth:
* - 1: 1 byte per pixel
* - 8: RGB233
* - 16: RGB565
* - 32: ARGB8888
*/
#define LV_COLOR_DEPTH 32
/* Swap the 2 bytes of RGB565 color.
* Useful if the display has a 8 bit interface (e.g. SPI)*/
#define LV_COLOR_16_SWAP 0
/* 1: Enable screen transparency.
* Useful for OSD or other overlapping GUIs.
* Requires `LV_COLOR_DEPTH = 32` colors and the screen's style should be modified: `style.body.opa = ...`*/
#define LV_COLOR_SCREEN_TRANSP 0
/*Images pixels with this color will not be drawn (with chroma keying)*/
#define LV_COLOR_TRANSP LV_COLOR_LIME /*LV_COLOR_LIME: pure green*/
/* Enable anti-aliasing (lines, and radiuses will be smoothed) */
#define LV_ANTIALIAS 1
/* Default display refresh period.
* Can be changed in the display driver (`lv_disp_drv_t`).*/
#define LV_DISP_DEF_REFR_PERIOD 30 /*[ms]*/
/* Dot Per Inch: used to initialize default sizes.
* E.g. a button with width = LV_DPI / 2 -> half inch wide
* (Not so important, you can adjust it to modify default sizes and spaces)*/
#define LV_DPI 100 /*[px]*/
/* Type of coordinates. Should be `int16_t` (or `int32_t` for extreme cases) */
typedef int16_t lv_coord_t;
/*=========================
Memory manager settings
*=========================*/
/* LittelvGL's internal memory manager's settings.
* The graphical objects and other related data are stored here. */
/* 1: use custom malloc/free, 0: use the built-in `lv_mem_alloc` and `lv_mem_free` */
#define LV_MEM_CUSTOM 1
#if LV_MEM_CUSTOM == 0
/* Size of the memory used by `lv_mem_alloc` in bytes (>= 2kB)*/
# define LV_MEM_SIZE (128U * 1024U)
/* Complier prefix for a big array declaration */
# define LV_MEM_ATTR
/* Set an address for the memory pool instead of allocating it as an array.
* Can be in external SRAM too. */
# define LV_MEM_ADR 0
/* Automatically defrag. on free. Defrag. means joining the adjacent free cells. */
# define LV_MEM_AUTO_DEFRAG 1
#else /*LV_MEM_CUSTOM*/
# define LV_MEM_CUSTOM_INCLUDE "bh_memory.h" /*Header for the dynamic memory function*/
# define LV_MEM_CUSTOM_ALLOC bh_malloc /*Wrapper to malloc*/
# define LV_MEM_CUSTOM_FREE bh_free /*Wrapper to free*/
#endif /*LV_MEM_CUSTOM*/
/* Garbage Collector settings
* Used if lvgl is binded to higher level language and the memory is managed by that language */
#define LV_ENABLE_GC 0
#if LV_ENABLE_GC != 0
# define LV_GC_INCLUDE "gc.h" /*Include Garbage Collector related things*/
# define LV_MEM_CUSTOM_REALLOC your_realloc /*Wrapper to realloc*/
# define LV_MEM_CUSTOM_GET_SIZE your_mem_get_size /*Wrapper to lv_mem_get_size*/
#endif /* LV_ENABLE_GC */
/*=======================
Input device settings
*=======================*/
/* Input device default settings.
* Can be changed in the Input device driver (`lv_indev_drv_t`)*/
/* Input device read period in milliseconds */
#define LV_INDEV_DEF_READ_PERIOD 30
/* Drag threshold in pixels */
#define LV_INDEV_DEF_DRAG_LIMIT 10
/* Drag throw slow-down in [%]. Greater value -> faster slow-down */
#define LV_INDEV_DEF_DRAG_THROW 20
/* Long press time in milliseconds.
* Time to send `LV_EVENT_LONG_PRESSSED`) */
#define LV_INDEV_DEF_LONG_PRESS_TIME 400
/* Repeated trigger period in long press [ms]
* Time between `LV_EVENT_LONG_PRESSED_REPEAT */
#define LV_INDEV_DEF_LONG_PRESS_REP_TIME 100
/*==================
* Feature usage
*==================*/
/*1: Enable the Animations */
#define LV_USE_ANIMATION 1
#if LV_USE_ANIMATION
/*Declare the type of the user data of animations (can be e.g. `void *`, `int`, `struct`)*/
typedef void * lv_anim_user_data_t;
#endif
/* 1: Enable shadow drawing*/
#define LV_USE_SHADOW 1
/* 1: Enable object groups (for keyboard/encoder navigation) */
#define LV_USE_GROUP 0
#if LV_USE_GROUP
typedef void * lv_group_user_data_t;
#endif /*LV_USE_GROUP*/
/* 1: Enable GPU interface*/
#define LV_USE_GPU 1
/* 1: Enable file system (might be required for images */
#define LV_USE_FILESYSTEM 1
#if LV_USE_FILESYSTEM
/*Declare the type of the user data of file system drivers (can be e.g. `void *`, `int`, `struct`)*/
typedef void * lv_fs_drv_user_data_t;
#endif
/*1: Add a `user_data` to drivers and objects*/
#define LV_USE_USER_DATA 0
/*========================
* Image decoder and cache
*========================*/
/* 1: Enable indexed (palette) images */
#define LV_IMG_CF_INDEXED 1
/* 1: Enable alpha indexed images */
#define LV_IMG_CF_ALPHA 1
/* Default image cache size. Image caching keeps the images opened.
* If only the built-in image formats are used there is no real advantage of caching.
* (I.e. no new image decoder is added)
* With complex image decoders (e.g. PNG or JPG) caching can save the continuous open/decode of images.
* However the opened images might consume additional RAM.
* LV_IMG_CACHE_DEF_SIZE must be >= 1 */
#define LV_IMG_CACHE_DEF_SIZE 1
/*Declare the type of the user data of image decoder (can be e.g. `void *`, `int`, `struct`)*/
typedef void * lv_img_decoder_user_data_t;
/*=====================
* Compiler settings
*====================*/
/* Define a custom attribute to `lv_tick_inc` function */
#define LV_ATTRIBUTE_TICK_INC
/* Define a custom attribute to `lv_task_handler` function */
#define LV_ATTRIBUTE_TASK_HANDLER
/* With size optimization (-Os) the compiler might not align data to
* 4 or 8 byte boundary. This alignment will be explicitly applied where needed.
* E.g. __attribute__((aligned(4))) */
#define LV_ATTRIBUTE_MEM_ALIGN
/* Attribute to mark large constant arrays for example
* font's bitmaps */
#define LV_ATTRIBUTE_LARGE_CONST
/*===================
* HAL settings
*==================*/
/* 1: use a custom tick source.
* It removes the need to manually update the tick with `lv_tick_inc`) */
#define LV_TICK_CUSTOM 1
#if LV_TICK_CUSTOM == 1
#define LV_TICK_CUSTOM_INCLUDE "bh_time.h" /*Header for the sys time function*/
#define LV_TICK_CUSTOM_SYS_TIME_EXPR (_bh_time_get_boot_millisecond()) /*Expression evaluating to current systime in ms*/
#endif /*LV_TICK_CUSTOM*/
typedef void * lv_disp_drv_user_data_t; /*Type of user data in the display driver*/
typedef void * lv_indev_drv_user_data_t; /*Type of user data in the input device driver*/
/*================
* Log settings
*===============*/
/*1: Enable the log module*/
#define LV_USE_LOG 1
#if LV_USE_LOG
/* How important log should be added:
* LV_LOG_LEVEL_TRACE A lot of logs to give detailed information
* LV_LOG_LEVEL_INFO Log important events
* LV_LOG_LEVEL_WARN Log if something unwanted happened but didn't cause a problem
* LV_LOG_LEVEL_ERROR Only critical issue, when the system may fail
* LV_LOG_LEVEL_NONE Do not log anything
*/
# define LV_LOG_LEVEL LV_LOG_LEVEL_WARN
/* 1: Print the log with 'printf';
* 0: user need to register a callback with `lv_log_register_print`*/
# define LV_LOG_PRINTF 1
#endif /*LV_USE_LOG*/
/*================
* THEME USAGE
*================*/
#define LV_THEME_LIVE_UPDATE 1 /*1: Allow theme switching at run time. Uses 8..10 kB of RAM*/
#define LV_USE_THEME_TEMPL 1 /*Just for test*/
#define LV_USE_THEME_DEFAULT 1 /*Built mainly from the built-in styles. Consumes very few RAM*/
#define LV_USE_THEME_ALIEN 1 /*Dark futuristic theme*/
#define LV_USE_THEME_NIGHT 1 /*Dark elegant theme*/
#define LV_USE_THEME_MONO 1 /*Mono color theme for monochrome displays*/
#define LV_USE_THEME_MATERIAL 1 /*Flat theme with bold colors and light shadows*/
#define LV_USE_THEME_ZEN 1 /*Peaceful, mainly light theme */
#define LV_USE_THEME_NEMO 1 /*Water-like theme based on the movie "Finding Nemo"*/
/*==================
* FONT USAGE
*===================*/
/* The built-in fonts contains the ASCII range and some Symbols with 4 bit-per-pixel.
* The symbols are available via `LV_SYMBOL_...` defines
* More info about fonts: https://docs.littlevgl.com/#Fonts
* To create a new font go to: https://littlevgl.com/ttf-font-to-c-array
*/
/* Robot fonts with bpp = 4
* https://fonts.google.com/specimen/Roboto */
#define LV_FONT_ROBOTO_12 1
#define LV_FONT_ROBOTO_16 1
#define LV_FONT_ROBOTO_22 1
#define LV_FONT_ROBOTO_28 1
/*Pixel perfect monospace font
* http://pelulamu.net/unscii/ */
#define LV_FONT_UNSCII_8 1
/* Optionally declare your custom fonts here.
* You can use these fonts as default font too
* and they will be available globally. E.g.
* #define LV_FONT_CUSTOM_DECLARE LV_FONT_DECLARE(my_font_1) \
* LV_FONT_DECLARE(my_font_2)
*/
#define LV_FONT_CUSTOM_DECLARE
/*Always set a default font from the built-in fonts*/
#define LV_FONT_DEFAULT &lv_font_roboto_16
/* Enable it if you have fonts with a lot of characters.
* The limit depends on the font size, font face and bpp
* but with > 10,000 characters if you see issues probably you need to enable it.*/
#define LV_FONT_FMT_TXT_LARGE 1
/*Declare the type of the user data of fonts (can be e.g. `void *`, `int`, `struct`)*/
typedef void * lv_font_user_data_t;
/*=================
* Text settings
*=================*/
/* Select a character encoding for strings.
* Your IDE or editor should have the same character encoding
* - LV_TXT_ENC_UTF8
* - LV_TXT_ENC_ASCII
* */
#define LV_TXT_ENC LV_TXT_ENC_UTF8
/*Can break (wrap) texts on these chars*/
#define LV_TXT_BREAK_CHARS " ,.;:-_"
/*===================
* LV_OBJ SETTINGS
*==================*/
/*Declare the type of the user data of object (can be e.g. `void *`, `int`, `struct`)*/
typedef void * lv_obj_user_data_t;
/*1: enable `lv_obj_realaign()` based on `lv_obj_align()` parameters*/
#define LV_USE_OBJ_REALIGN 1
/* Enable to make the object clickable on a larger area.
* LV_EXT_CLICK_AREA_OFF or 0: Disable this feature
* LV_EXT_CLICK_AREA_TINY: The extra area can be adjusted horizontally and vertically (0..255 px)
* LV_EXT_CLICK_AREA_FULL: The extra area can be adjusted in all 4 directions (-32k..+32k px)
*/
#define LV_USE_EXT_CLICK_AREA LV_EXT_CLICK_AREA_FULL
/*==================
* LV OBJ X USAGE
*================*/
/*
* Documentation of the object types: https://docs.littlevgl.com/#Object-types
*/
/*Arc (dependencies: -)*/
#define LV_USE_ARC 1
/*Bar (dependencies: -)*/
#define LV_USE_BAR 1
/*Button (dependencies: lv_cont*/
#define LV_USE_BTN 1
#if LV_USE_BTN != 0
/*Enable button-state animations - draw a circle on click (dependencies: LV_USE_ANIMATION)*/
# define LV_BTN_INK_EFFECT 1
#endif
/*Button matrix (dependencies: -)*/
#define LV_USE_BTNM 1
/*Calendar (dependencies: -)*/
#define LV_USE_CALENDAR 1
/*Canvas (dependencies: lv_img)*/
#define LV_USE_CANVAS 1
/*Check box (dependencies: lv_btn, lv_label)*/
#define LV_USE_CB 1
/*Chart (dependencies: -)*/
#define LV_USE_CHART 1
#if LV_USE_CHART
# define LV_CHART_AXIS_TICK_LABEL_MAX_LEN 20
#endif
/*Container (dependencies: -*/
#define LV_USE_CONT 1
/*Drop down list (dependencies: lv_page, lv_label, lv_symbol_def.h)*/
#define LV_USE_DDLIST 1
#if LV_USE_DDLIST != 0
/*Open and close default animation time [ms] (0: no animation)*/
# define LV_DDLIST_DEF_ANIM_TIME 200
#endif
/*Gauge (dependencies:lv_bar, lv_lmeter)*/
#define LV_USE_GAUGE 1
/*Image (dependencies: lv_label*/
#define LV_USE_IMG 1
/*Image Button (dependencies: lv_btn*/
#define LV_USE_IMGBTN 1
#if LV_USE_IMGBTN
/*1: The imgbtn requires left, mid and right parts and the width can be set freely*/
# define LV_IMGBTN_TILED 0
#endif
/*Keyboard (dependencies: lv_btnm)*/
#define LV_USE_KB 1
/*Label (dependencies: -*/
#define LV_USE_LABEL 1
#if LV_USE_LABEL != 0
/*Hor, or ver. scroll speed [px/sec] in 'LV_LABEL_LONG_ROLL/ROLL_CIRC' mode*/
# define LV_LABEL_DEF_SCROLL_SPEED 25
/* Waiting period at beginning/end of animation cycle */
# define LV_LABEL_WAIT_CHAR_COUNT 3
/*Enable selecting text of the label */
# define LV_LABEL_TEXT_SEL 1
/*Store extra some info in labels (12 bytes) to speed up drawing of very long texts*/
# define LV_LABEL_LONG_TXT_HINT 0
#endif
/*LED (dependencies: -)*/
#define LV_USE_LED 1
/*Line (dependencies: -*/
#define LV_USE_LINE 1
/*List (dependencies: lv_page, lv_btn, lv_label, (lv_img optionally for icons ))*/
#define LV_USE_LIST 1
#if LV_USE_LIST != 0
/*Default animation time of focusing to a list element [ms] (0: no animation) */
# define LV_LIST_DEF_ANIM_TIME 100
#endif
/*Line meter (dependencies: *;)*/
#define LV_USE_LMETER 1
/*Message box (dependencies: lv_rect, lv_btnm, lv_label)*/
#define LV_USE_MBOX 1
/*Page (dependencies: lv_cont)*/
#define LV_USE_PAGE 1
#if LV_USE_PAGE != 0
/*Focus default animation time [ms] (0: no animation)*/
# define LV_PAGE_DEF_ANIM_TIME 400
#endif
/*Preload (dependencies: lv_arc, lv_anim)*/
#define LV_USE_PRELOAD 1
#if LV_USE_PRELOAD != 0
# define LV_PRELOAD_DEF_ARC_LENGTH 60 /*[deg]*/
# define LV_PRELOAD_DEF_SPIN_TIME 1000 /*[ms]*/
# define LV_PRELOAD_DEF_ANIM LV_PRELOAD_TYPE_SPINNING_ARC
#endif
/*Roller (dependencies: lv_ddlist)*/
#define LV_USE_ROLLER 1
#if LV_USE_ROLLER != 0
/*Focus animation time [ms] (0: no animation)*/
# define LV_ROLLER_DEF_ANIM_TIME 200
/*Number of extra "pages" when the roller is infinite*/
# define LV_ROLLER_INF_PAGES 7
#endif
/*Slider (dependencies: lv_bar)*/
#define LV_USE_SLIDER 1
/*Spinbox (dependencies: lv_ta)*/
#define LV_USE_SPINBOX 1
/*Switch (dependencies: lv_slider)*/
#define LV_USE_SW 1
/*Text area (dependencies: lv_label, lv_page)*/
#define LV_USE_TA 1
#if LV_USE_TA != 0
# define LV_TA_DEF_CURSOR_BLINK_TIME 400 /*ms*/
# define LV_TA_DEF_PWD_SHOW_TIME 1500 /*ms*/
#endif
/*Table (dependencies: lv_label)*/
#define LV_USE_TABLE 1
#if LV_USE_TABLE
# define LV_TABLE_COL_MAX 12
#endif
/*Tab (dependencies: lv_page, lv_btnm)*/
#define LV_USE_TABVIEW 1
# if LV_USE_TABVIEW != 0
/*Time of slide animation [ms] (0: no animation)*/
# define LV_TABVIEW_DEF_ANIM_TIME 300
#endif
/*Tileview (dependencies: lv_page) */
#define LV_USE_TILEVIEW 1
#if LV_USE_TILEVIEW
/*Time of slide animation [ms] (0: no animation)*/
# define LV_TILEVIEW_DEF_ANIM_TIME 300
#endif
/*Window (dependencies: lv_cont, lv_btn, lv_label, lv_img, lv_page)*/
#define LV_USE_WIN 1
/*==================
* Non-user section
*==================*/
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) /* Disable warnings for Visual Studio*/
# define _CRT_SECURE_NO_WARNINGS
#endif
/*--END OF LV_CONF_H--*/
/*Be sure every define has a default value*/
#include "lvgl/src/lv_conf_checker.h"
#endif /*LV_CONF_H*/
#endif /*End of "Content enable"*/

View File

@ -37,7 +37,9 @@
#include "attr_container.h"
#include "request.h"
#include "sensor.h"
#include "connection.h"
#include "timer_wasm_app.h"
#include "wgl.h"
#ifdef __cplusplus
extern "C" {

View File

@ -0,0 +1,130 @@
/*
* 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.
*/
#include "connection.h"
#include "native_interface.h"
/* Raw connection structure */
typedef struct _connection {
/* Next connection */
struct _connection *next;
/* Handle of the connection */
uint32 handle;
/* Callback function called when event on this connection occurs */
on_connection_event_f on_event;
/* User data */
void *user_data;
} connection_t;
/* Raw connections list */
static connection_t *g_conns = NULL;
connection_t *api_open_connection(const char *name,
attr_container_t *args,
on_connection_event_f on_event,
void *user_data)
{
connection_t *conn;
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);
if (handle == -1)
return NULL;
conn = (connection_t *)malloc(sizeof(*conn));
if (conn == NULL) {
wasm_close_connection(handle);
return NULL;
}
memset(conn, 0, sizeof(*conn));
conn->handle = handle;
conn->on_event = on_event;
conn->user_data = user_data;
if (g_conns != NULL) {
conn->next = g_conns;
g_conns = conn;
} else {
g_conns = conn;
}
return conn;
}
void api_close_connection(connection_t *c)
{
connection_t *conn = g_conns, *prev = NULL;
while (conn) {
if (conn == c) {
wasm_close_connection(c->handle);
if (prev != NULL)
prev->next = conn->next;
else
g_conns = conn->next;
free(conn);
return;
} else {
prev = conn;
conn = conn->next;
}
}
}
int api_send_on_connection(connection_t *conn, const char *data, uint32 len)
{
return wasm_send_on_connection(conn->handle, (int32)data, len);
}
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);
}
void on_connection_data(uint32 handle, char *buffer, uint32 len)
{
connection_t *conn = g_conns;
while (conn != NULL) {
if (conn->handle == handle) {
if (len == 0) {
conn->on_event(conn,
CONN_EVENT_TYPE_DISCONNECT,
NULL,
0,
conn->user_data);
} else {
conn->on_event(conn,
CONN_EVENT_TYPE_DATA,
buffer,
len,
conn->user_data);
}
return;
}
conn = conn->next;
}
}

View File

@ -0,0 +1,106 @@
/*
* 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 _CONNECTION_H_
#define _CONNECTION_H_
#include "attr_container.h"
#ifdef __cplusplus
extern "C" {
#endif
struct _connection;
typedef struct _connection connection_t;
/* Connection event type */
typedef enum {
/* Data is received */
CONN_EVENT_TYPE_DATA = 1,
/* Connection is disconnected */
CONN_EVENT_TYPE_DISCONNECT
} conn_event_type_t;
/*
* @typedef on_connection_event_f
*
* @param conn the connection that the event belongs to
* @param type event type
* @param data the data received for CONN_EVENT_TYPE_DATA event
* @param len length of the data in byte
* @param user_data user data
*/
typedef void (*on_connection_event_f)(connection_t *conn,
conn_event_type_t type,
const char *data,
uint32 len,
void *user_data);
/*
*****************
* Connection API's
*****************
*/
/*
* @brief Open a connection.
*
* @param name name of the connection, "TCP", "UDP" or "UART"
* @param args connection arguments, such as: ip:127.0.0.1, port:8888
* @param on_event callback function called when event occurs
* @param user_data user data
*
* @return the connection or NULL means fail
*/
connection_t *api_open_connection(const char *name,
attr_container_t *args,
on_connection_event_f on_event,
void *user_data);
/*
* @brief Close a connection.
*
* @param conn connection
*/
void api_close_connection(connection_t *conn);
/*
* Send data to the connection in non-blocking manner which returns immediately
*
* @param conn the connection
* @param data data buffer to be sent
* @param len length of the data in byte
*
* @return actual length sent, or -1 if fail(maybe underlying buffer is full)
*/
int api_send_on_connection(connection_t *conn, const char *data, uint32 len);
/*
* @brief Configure connection.
*
* @param conn the connection
* @param cfg configurations
*
* @return true if success, false otherwise
*/
bool api_config_connection(connection_t *conn, attr_container_t *cfg);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,8 @@
MIT licence
Copyright (c) 2016 Gábor Kiss-Vámosi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,173 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_BTN_H
#define WAMR_GRAPHIC_LIBRARY_BTN_H
#ifdef __cplusplus
extern "C" {
#endif
/** Possible states of a button.
* It can be used not only by buttons but other button-like objects too*/
enum {
/**Released*/
WGL_BTN_STATE_REL,
/**Pressed*/
WGL_BTN_STATE_PR,
/**Toggled released*/
WGL_BTN_STATE_TGL_REL,
/**Toggled pressed*/
WGL_BTN_STATE_TGL_PR,
/**Inactive*/
WGL_BTN_STATE_INA,
/**Number of states*/
_WGL_BTN_STATE_NUM,
};
typedef uint8_t wgl_btn_state_t;
/**Styles*/
enum {
/** Release style */
WGL_BTN_STYLE_REL,
/**Pressed style*/
WGL_BTN_STYLE_PR,
/** Toggle released style*/
WGL_BTN_STYLE_TGL_REL,
/** Toggle pressed style */
WGL_BTN_STYLE_TGL_PR,
/** Inactive style*/
WGL_BTN_STYLE_INA,
};
typedef uint8_t wgl_btn_style_t;
/* Create a button */
wgl_obj_t wgl_btn_create(wgl_obj_t par, wgl_obj_t copy);
/*=====================
* Setter functions
*====================*/
/**
* Enable the toggled states. On release the button will change from/to toggled state.
* @param btn pointer to a button object
* @param tgl true: enable toggled states, false: disable
*/
void wgl_btn_set_toggle(wgl_obj_t btn, bool tgl);
/**
* Set the state of the button
* @param btn pointer to a button object
* @param state the new state of the button (from wgl_btn_state_t enum)
*/
void wgl_btn_set_state(wgl_obj_t btn, wgl_btn_state_t state);
/**
* Toggle the state of the button (ON->OFF, OFF->ON)
* @param btn pointer to a button object
*/
void wgl_btn_toggle(wgl_obj_t btn);
/**
* Set time of the ink effect (draw a circle on click to animate in the new state)
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void wgl_btn_set_ink_in_time(wgl_obj_t btn, uint16_t time);
/**
* Set the wait time before the ink disappears
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void wgl_btn_set_ink_wait_time(wgl_obj_t btn, uint16_t time);
/**
* Set time of the ink out effect (animate to the released state)
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void wgl_btn_set_ink_out_time(wgl_obj_t btn, uint16_t time);
/**
* Set a style of a button.
* @param btn pointer to button object
* @param type which style should be set
* @param style pointer to a style
* */
//void wgl_btn_set_style(wgl_obj_t btn, wgl_btn_style_t type, const wgl_style_t * style);
/*=====================
* Getter functions
*====================*/
/**
* Get the current state of the button
* @param btn pointer to a button object
* @return the state of the button (from wgl_btn_state_t enum)
*/
wgl_btn_state_t wgl_btn_get_state(wgl_obj_t btn);
/**
* Get the toggle enable attribute of the button
* @param btn pointer to a button object
* @return true: toggle enabled, false: disabled
*/
bool wgl_btn_get_toggle(wgl_obj_t btn);
/**
* Get time of the ink in effect (draw a circle on click to animate in the new state)
* @param btn pointer to a button object
* @return the time of the ink animation
*/
uint16_t wgl_btn_get_ink_in_time(wgl_obj_t btn);
/**
* Get the wait time before the ink disappears
* @param btn pointer to a button object
* @return the time of the ink animation
*/
uint16_t wgl_btn_get_ink_wait_time(wgl_obj_t btn);
/**
* Get time of the ink out effect (animate to the releases state)
* @param btn pointer to a button object
* @return the time of the ink animation
*/
uint16_t wgl_btn_get_ink_out_time(wgl_obj_t btn);
/**
* Get style of a button.
* @param btn pointer to button object
* @param type which style should be get
* @return style pointer to the style
* */
//const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn, wgl_btn_style_t type);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_BTN_H */

View File

@ -0,0 +1,90 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_CB_H
#define WAMR_GRAPHIC_LIBRARY_CB_H
#ifdef __cplusplus
extern "C" {
#endif
/** Checkbox styles. */
enum {
WGL_CB_STYLE_BG, /**< Style of object background. */
WGL_CB_STYLE_BOX_REL, /**< Style of box (released). */
WGL_CB_STYLE_BOX_PR, /**< Style of box (pressed). */
WGL_CB_STYLE_BOX_TGL_REL, /**< Style of box (released but checked). */
WGL_CB_STYLE_BOX_TGL_PR, /**< Style of box (pressed and checked). */
WGL_CB_STYLE_BOX_INA, /**< Style of disabled box */
};
typedef uint8_t wgl_cb_style_t;
/**
* Create a check box objects
* @param par pointer to an object, it will be the parent of the new check box
* @param copy pointer to a check box object, if not NULL then the new object will be copied from it
* @return pointer to the created check box
*/
wgl_obj_t wgl_cb_create(wgl_obj_t par, const wgl_obj_t copy);
/*=====================
* Setter functions
*====================*/
/**
* Set the text of a check box. `txt` will be copied and may be deallocated
* after this function returns.
* @param cb pointer to a check box
* @param txt the text of the check box. NULL to refresh with the current text.
*/
void wgl_cb_set_text(wgl_obj_t cb, const char * txt);
/**
* Set the text of a check box. `txt` must not be deallocated during the life
* of this checkbox.
* @param cb pointer to a check box
* @param txt the text of the check box. NULL to refresh with the current text.
*/
void wgl_cb_set_static_text(wgl_obj_t cb, const char * txt);
/*=====================
* Getter functions
*====================*/
/**
* Get the length of the text of a check box
* @param label the check box object
* @return the length of the text of the check box
*/
unsigned int wgl_cb_get_text_length(wgl_obj_t cb);
/**
* Get the text of a check box
* @param label the check box object
* @param buffer buffer to save the text
* @param buffer_len length of the buffer
* @return the text of the check box, note that the text will be truncated if buffer is not long enough
*/
char *wgl_cb_get_text(wgl_obj_t cb, char *buffer, int buffer_len);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_CB_H */

View File

@ -0,0 +1,77 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_LABEL_H
#define WAMR_GRAPHIC_LIBRARY_LABEL_H
#ifdef __cplusplus
extern "C" {
#endif
/** Long mode behaviors. Used in 'wgl_label_ext_t' */
enum {
WGL_LABEL_LONG_EXPAND, /**< Expand the object size to the text size*/
WGL_LABEL_LONG_BREAK, /**< Keep the object width, break the too long lines and expand the object
height*/
WGL_LABEL_LONG_DOT, /**< Keep the size and write dots at the end if the text is too long*/
WGL_LABEL_LONG_SROLL, /**< Keep the size and roll the text back and forth*/
WGL_LABEL_LONG_SROLL_CIRC, /**< Keep the size and roll the text circularly*/
WGL_LABEL_LONG_CROP, /**< Keep the size and crop the text out of it*/
};
typedef uint8_t wgl_label_long_mode_t;
/** Label align policy*/
enum {
WGL_LABEL_ALIGN_LEFT, /**< Align text to left */
WGL_LABEL_ALIGN_CENTER, /**< Align text to center */
WGL_LABEL_ALIGN_RIGHT, /**< Align text to right */
};
typedef uint8_t wgl_label_align_t;
/** Label styles*/
enum {
WGL_LABEL_STYLE_MAIN,
};
typedef uint8_t wgl_label_style_t;
/* Create a label */
wgl_obj_t wgl_label_create(wgl_obj_t par, wgl_obj_t copy);
/* Set text for the label */
void wgl_label_set_text(wgl_obj_t label, const char * text);
/**
* Get the length of the text of a label
* @param label the label object
* @return the length of the text of the label
*/
unsigned int wgl_label_get_text_length(wgl_obj_t label);
/**
* Get the text of a label
* @param label the label object
* @param buffer buffer to save the text
* @param buffer_len length of the buffer
* @return the text of the label, note that the text will be truncated if buffer is not long enough
*/
char *wgl_label_get_text(wgl_obj_t label, char *buffer, int buffer_len);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LABEL_H */

View File

@ -0,0 +1,65 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_LIST_H
#define WAMR_GRAPHIC_LIBRARY_LIST_H
#ifdef __cplusplus
extern "C" {
#endif
/** List styles. */
enum {
WGL_LIST_STYLE_BG, /**< List background style */
WGL_LIST_STYLE_SCRL, /**< List scrollable area style. */
WGL_LIST_STYLE_SB, /**< List scrollbar style. */
WGL_LIST_STYLE_EDGE_FLASH, /**< List edge flash style. */
WGL_LIST_STYLE_BTN_REL, /**< Same meaning as the ordinary button styles. */
WGL_LIST_STYLE_BTN_PR,
WGL_LIST_STYLE_BTN_TGL_REL,
WGL_LIST_STYLE_BTN_TGL_PR,
WGL_LIST_STYLE_BTN_INA,
};
typedef uint8_t wgl_list_style_t;
/**
* Create a list objects
* @param par pointer to an object, it will be the parent of the new list
* @param copy pointer to a list object, if not NULL then the new object will be copied from it
* @return pointer to the created list
*/
wgl_obj_t wgl_list_create(wgl_obj_t par, wgl_obj_t copy);
/*======================
* Add/remove functions
*=====================*/
/**
* Add a list element to the list
* @param list pointer to list object
* @param img_fn file name of an image before the text (NULL if unused)
* @param txt text of the list element (NULL if unused)
* @return pointer to the new list element which can be customized (a button)
*/
wgl_obj_t wgl_list_add_btn(wgl_obj_t list, const void * img_src, const char * txt);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LIST_H */

View File

@ -0,0 +1,101 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_OBJ_H
#define WAMR_GRAPHIC_LIBRARY_OBJ_H
#ifdef __cplusplus
extern "C" {
#endif
typedef void * wgl_obj_t;
enum {
WGL_EVENT_PRESSED, /**< The object has been pressed*/
WGL_EVENT_PRESSING, /**< The object is being pressed (called continuously while pressing)*/
WGL_EVENT_PRESS_LOST, /**< User is still pressing but slid cursor/finger off of the object */
WGL_EVENT_SHORT_CLICKED, /**< User pressed object for a short period of time, then released it. Not called if dragged. */
WGL_EVENT_LONG_PRESSED, /**< Object has been pressed for at least `WGL_INDEV_LONG_PRESS_TIME`. Not called if dragged.*/
WGL_EVENT_LONG_PRESSED_REPEAT, /**< Called after `WGL_INDEV_LONG_PRESS_TIME` in every
`WGL_INDEV_LONG_PRESS_REP_TIME` ms. Not called if dragged.*/
WGL_EVENT_CLICKED, /**< Called on release if not dragged (regardless to long press)*/
WGL_EVENT_RELEASED, /**< Called in every cases when the object has been released*/
WGL_EVENT_DRAG_BEGIN,
WGL_EVENT_DRAG_END,
WGL_EVENT_DRAG_THROW_BEGIN,
WGL_EVENT_KEY,
WGL_EVENT_FOCUSED,
WGL_EVENT_DEFOCUSED,
WGL_EVENT_VALUE_CHANGED, /**< The object's value has changed (i.e. slider moved) */
WGL_EVENT_INSERT,
WGL_EVENT_REFRESH,
WGL_EVENT_APPLY, /**< "Ok", "Apply" or similar specific button has clicked*/
WGL_EVENT_CANCEL, /**< "Close", "Cancel" or similar specific button has clicked*/
WGL_EVENT_DELETE, /**< Object is being deleted */
};
typedef uint8_t wgl_event_t; /**< Type of event being sent to the object. */
/** Object alignment. */
enum {
WGL_ALIGN_CENTER = 0,
WGL_ALIGN_IN_TOP_LEFT,
WGL_ALIGN_IN_TOP_MID,
WGL_ALIGN_IN_TOP_RIGHT,
WGL_ALIGN_IN_BOTTOM_LEFT,
WGL_ALIGN_IN_BOTTOM_MID,
WGL_ALIGN_IN_BOTTOM_RIGHT,
WGL_ALIGN_IN_LEFT_MID,
WGL_ALIGN_IN_RIGHT_MID,
WGL_ALIGN_OUT_TOP_LEFT,
WGL_ALIGN_OUT_TOP_MID,
WGL_ALIGN_OUT_TOP_RIGHT,
WGL_ALIGN_OUT_BOTTOM_LEFT,
WGL_ALIGN_OUT_BOTTOM_MID,
WGL_ALIGN_OUT_BOTTOM_RIGHT,
WGL_ALIGN_OUT_LEFT_TOP,
WGL_ALIGN_OUT_LEFT_MID,
WGL_ALIGN_OUT_LEFT_BOTTOM,
WGL_ALIGN_OUT_RIGHT_TOP,
WGL_ALIGN_OUT_RIGHT_MID,
WGL_ALIGN_OUT_RIGHT_BOTTOM,
};
typedef uint8_t wgl_align_t;
enum {
WGL_DRAG_DIR_HOR = 0x1, /**< Object can be dragged horizontally. */
WGL_DRAG_DIR_VER = 0x2, /**< Object can be dragged vertically. */
WGL_DRAG_DIR_ALL = 0x3, /**< Object can be dragged in all directions. */
};
typedef uint8_t wgl_drag_dir_t;
typedef void (*wgl_event_cb_t)(wgl_obj_t obj, wgl_event_t event);
void wgl_obj_align(wgl_obj_t obj, wgl_obj_t base, wgl_align_t align, wgl_coord_t x_mod, wgl_coord_t y_mod);
void wgl_obj_set_event_cb(wgl_obj_t obj, wgl_event_cb_t event_cb);
wgl_res_t wgl_obj_del(wgl_obj_t obj);
void wgl_obj_del_async(wgl_obj_t obj);
void wgl_obj_clean(wgl_obj_t obj);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_OBJ_H */

View File

@ -0,0 +1,40 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_TYPES_H
#define WAMR_GRAPHIC_LIBRARY_TYPES_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/**
* WGL error codes.
*/
enum {
WGL_RES_INV = 0, /*Typically indicates that the object is deleted (become invalid) in the action
function or an operation was failed*/
WGL_RES_OK, /*The object is valid (no deleted) after the action*/
};
typedef uint8_t wgl_res_t;
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_TYPES_H */

View File

@ -0,0 +1,8 @@
MIT licence
Copyright (c) 2016 Gábor Kiss-Vámosi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,86 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_BTN_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_BTN_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_btn.h"
/** Possible states of a button.
* It can be used not only by buttons but other button-like objects too*/
enum {
/**Released*/
LV_BTN_STATE_REL,
/**Pressed*/
LV_BTN_STATE_PR,
/**Toggled released*/
LV_BTN_STATE_TGL_REL,
/**Toggled pressed*/
LV_BTN_STATE_TGL_PR,
/**Inactive*/
LV_BTN_STATE_INA,
/**Number of states*/
_LV_BTN_STATE_NUM,
};
typedef wgl_btn_state_t lv_btn_state_t;
/**Styles*/
enum {
/** Release style */
LV_BTN_STYLE_REL,
/**Pressed style*/
LV_BTN_STYLE_PR,
/** Toggle released style*/
LV_BTN_STYLE_TGL_REL,
/** Toggle pressed style */
LV_BTN_STYLE_TGL_PR,
/** Inactive style*/
LV_BTN_STYLE_INA,
};
typedef wgl_btn_style_t lv_btn_style_t;
#define lv_btn_create wgl_btn_create
#define lv_btn_set_toggle wgl_btn_set_toggle
#define lv_btn_set_state wgl_btn_set_state
#define lv_btn_toggle wgl_btn_toggle
#define lv_btn_set_ink_in_time wgl_btn_set_ink_in_time
#define lv_btn_set_ink_wait_time wgl_btn_set_ink_wait_time
#define lv_btn_set_ink_out_time wgl_btn_set_ink_out_time
#define lv_btn_get_state wgl_btn_get_state
#define lv_btn_get_toggle wgl_btn_get_toggle
#define lv_btn_get_ink_in_time wgl_btn_get_ink_in_time
#define lv_btn_get_ink_wait_time wgl_btn_get_ink_wait_time
#define lv_btn_get_ink_out_time wgl_btn_get_ink_out_time
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_BTN_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,47 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_CB_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_CB_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_cb.h"
/** Checkbox styles. */
enum {
LV_CB_STYLE_BG, /**< Style of object background. */
LV_CB_STYLE_BOX_REL, /**< Style of box (released). */
LV_CB_STYLE_BOX_PR, /**< Style of box (pressed). */
LV_CB_STYLE_BOX_TGL_REL, /**< Style of box (released but checked). */
LV_CB_STYLE_BOX_TGL_PR, /**< Style of box (pressed and checked). */
LV_CB_STYLE_BOX_INA, /**< Style of disabled box */
};
typedef wgl_cb_style_t lv_cb_style_t;
#define lv_cb_create wgl_cb_create
#define lv_cb_set_text wgl_cb_set_text
#define lv_cb_set_static_text wgl_cb_set_static_text
#define lv_cb_get_text(cb) wgl_cb_get_text(cb, g_widget_text, 100)
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_CB_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,61 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_LABEL_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_LABEL_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_label.h"
/** Long mode behaviors. Used in 'lv_label_ext_t' */
enum {
LV_LABEL_LONG_EXPAND, /**< Expand the object size to the text size*/
LV_LABEL_LONG_BREAK, /**< Keep the object width, break the too long lines and expand the object
height*/
LV_LABEL_LONG_DOT, /**< Keep the size and write dots at the end if the text is too long*/
LV_LABEL_LONG_SROLL, /**< Keep the size and roll the text back and forth*/
LV_LABEL_LONG_SROLL_CIRC, /**< Keep the size and roll the text circularly*/
LV_LABEL_LONG_CROP, /**< Keep the size and crop the text out of it*/
};
typedef wgl_label_long_mode_t lv_label_long_mode_t;
/** Label align policy*/
enum {
LV_LABEL_ALIGN_LEFT, /**< Align text to left */
LV_LABEL_ALIGN_CENTER, /**< Align text to center */
LV_LABEL_ALIGN_RIGHT, /**< Align text to right */
};
typedef wgl_label_align_t lv_label_align_t;
/** Label styles*/
enum {
LV_LABEL_STYLE_MAIN,
};
typedef wgl_label_style_t lv_label_style_t;
#define lv_label_create wgl_label_create
#define lv_label_set_text wgl_label_set_text
#define lv_label_get_text(label) wgl_label_get_text(label, g_widget_text, 100)
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LABEL_LVGL_COMPATIBLE_H */

View File

@ -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 WAMR_GRAPHIC_LIBRARY_LIST_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_LIST_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_list.h"
/** List styles. */
enum {
LV_LIST_STYLE_BG, /**< List background style */
LV_LIST_STYLE_SCRL, /**< List scrollable area style. */
LV_LIST_STYLE_SB, /**< List scrollbar style. */
LV_LIST_STYLE_EDGE_FLASH, /**< List edge flash style. */
LV_LIST_STYLE_BTN_REL, /**< Same meaning as the ordinary button styles. */
LV_LIST_STYLE_BTN_PR,
LV_LIST_STYLE_BTN_TGL_REL,
LV_LIST_STYLE_BTN_TGL_PR,
LV_LIST_STYLE_BTN_INA,
};
typedef wgl_list_style_t lv_list_style_t;
#define lv_list_create wgl_list_create
#define lv_list_add_btn wgl_list_add_btn
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LIST_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,100 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_OBJ_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_OBJ_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_obj.h"
typedef void lv_obj_t;
enum {
LV_EVENT_PRESSED,
LV_EVENT_PRESSING,
LV_EVENT_PRESS_LOST,
LV_EVENT_SHORT_CLICKED,
LV_EVENT_LONG_PRESSED,
LV_EVENT_LONG_PRESSED_REPEAT,
LV_EVENT_CLICKED,
LV_EVENT_RELEASED,
LV_EVENT_DRAG_BEGIN,
LV_EVENT_DRAG_END,
LV_EVENT_DRAG_THROW_BEGIN,
LV_EVENT_KEY,
LV_EVENT_FOCUSED,
LV_EVENT_DEFOCUSED,
LV_EVENT_VALUE_CHANGED,
LV_EVENT_INSERT,
LV_EVENT_REFRESH,
LV_EVENT_APPLY,
LV_EVENT_CANCEL,
LV_EVENT_DELETE,
};
typedef wgl_event_t lv_event_t;
/** Object alignment. */
enum {
LV_ALIGN_CENTER,
LV_ALIGN_IN_TOP_LEFT,
LV_ALIGN_IN_TOP_MID,
LV_ALIGN_IN_TOP_RIGHT,
LV_ALIGN_IN_BOTTOM_LEFT,
LV_ALIGN_IN_BOTTOM_MID,
LV_ALIGN_IN_BOTTOM_RIGHT,
LV_ALIGN_IN_LEFT_MID,
LV_ALIGN_IN_RIGHT_MID,
LV_ALIGN_OUT_TOP_LEFT,
LV_ALIGN_OUT_TOP_MID,
LV_ALIGN_OUT_TOP_RIGHT,
LV_ALIGN_OUT_BOTTOM_LEFT,
LV_ALIGN_OUT_BOTTOM_MID,
LV_ALIGN_OUT_BOTTOM_RIGHT,
LV_ALIGN_OUT_LEFT_TOP,
LV_ALIGN_OUT_LEFT_MID,
LV_ALIGN_OUT_LEFT_BOTTOM,
LV_ALIGN_OUT_RIGHT_TOP,
LV_ALIGN_OUT_RIGHT_MID,
LV_ALIGN_OUT_RIGHT_BOTTOM,
};
typedef wgl_align_t lv_align_t;
enum {
LV_DRAG_DIR_HOR,
LV_DRAG_DIR_VER,
LV_DRAG_DIR_ALL,
};
typedef wgl_drag_dir_t lv_drag_dir_t;
#define lv_obj_align wgl_obj_align
#define lv_obj_set_event_cb wgl_obj_set_event_cb
#define lv_obj_del wgl_obj_del
#define lv_obj_del_async wgl_obj_del_async
#define lv_obj_clean wgl_obj_clean
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_OBJ_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,39 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_TYPES_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_TYPES_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_types.h"
/**
* error codes.
*/
enum {
LV_RES_INV,
LV_RES_OK,
};
typedef wgl_res_t lv_res_t;
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_TYPES_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,37 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "wgl_shared_utils.h" /* shared types between app and native */
#include "lvgl-compatible/lv_types.h"
#include "lvgl-compatible/lv_obj.h"
#include "lvgl-compatible/lv_btn.h"
#include "lvgl-compatible/lv_cb.h"
#include "lvgl-compatible/lv_label.h"
#include "lvgl-compatible/lv_list.h"
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,131 @@
/*
* 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.
*/
#include "wgl.h"
#include "native_interface.h"
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_BTN_NATIVE_FUNC(id) wasm_btn_native_call(id, (int32)argv, ARGC)
wgl_obj_t wgl_btn_create(wgl_obj_t par, wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
void wgl_btn_set_toggle(wgl_obj_t btn, bool tgl)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = tgl;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_TOGGLE);
}
void wgl_btn_set_state(wgl_obj_t btn, wgl_btn_state_t state)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = state;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_STATE);
}
void wgl_btn_toggle(wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_TOGGLE);
}
void wgl_btn_set_ink_in_time(wgl_obj_t btn, uint16_t time)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_IN_TIME);
}
void wgl_btn_set_ink_wait_time(wgl_obj_t btn, uint16_t time)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_WAIT_TIME);
}
void wgl_btn_set_ink_out_time(wgl_obj_t btn, uint16_t time)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_OUT_TIME);
}
//void wgl_btn_set_style(wgl_obj_t btn, wgl_btn_style_t type, const wgl_style_t * style)
//{
// //TODO: pack style
// //wasm_btn_set_style(btn, type, style);
//}
//
wgl_btn_state_t wgl_btn_get_state(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_STATE);
return (wgl_btn_state_t)argv[0];
}
bool wgl_btn_get_toggle(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_TOGGLE);
return (bool)argv[0];
}
uint16_t wgl_btn_get_ink_in_time(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_IN_TIME);
return (uint16_t)argv[0];
}
uint16_t wgl_btn_get_ink_wait_time(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_WAIT_TIME);
return (uint16_t)argv[0];
}
uint16_t wgl_btn_get_ink_out_time(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_OUT_TIME);
return (uint16_t)argv[0];
}
//
//const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn, wgl_btn_style_t type)
//{
// //TODO: pack style
// //wasm_btn_get_style(btn, type);
// return NULL;
//}

View File

@ -0,0 +1,83 @@
/*
* 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.
*/
#include "wgl.h"
#include "native_interface.h"
#include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_CB_NATIVE_FUNC(id) wasm_cb_native_call(id, (uint32)argv, ARGC)
wgl_obj_t wgl_cb_create(wgl_obj_t par, const wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
void wgl_cb_set_text(wgl_obj_t cb, const char * txt)
{
uint32 argv[3] = {0};
argv[0] = (uint32)cb;
argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_TEXT);
}
void wgl_cb_set_static_text(wgl_obj_t cb, const char * txt)
{
uint32 argv[3] = {0};
argv[0] = (uint32)cb;
argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_STATIC_TEXT);
}
//void wgl_cb_set_style(wgl_obj_t cb, wgl_cb_style_t type, const wgl_style_t * style)
//{
// //TODO:
//}
//
unsigned int wgl_cb_get_text_length(wgl_obj_t cb)
{
uint32 argv[1] = {0};
argv[0] = (uint32)cb;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_GET_TEXT_LENGTH);
return argv[0];
}
char *wgl_cb_get_text(wgl_obj_t cb, char *buffer, int buffer_len)
{
uint32 argv[3] = {0};
argv[0] = (uint32)cb;
argv[1] = (uint32)buffer;
argv[2] = buffer_len;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_GET_TEXT);
return (char *)argv[0];
}
//const wgl_style_t * wgl_cb_get_style(const wgl_obj_t cb, wgl_cb_style_t type)
//{
// //TODO
// return NULL;
//}
//

View File

@ -0,0 +1,262 @@
/*
* 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.
*/
#include "wgl.h"
#include "native_interface.h"
#include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_LABEL_NATIVE_FUNC(id) wasm_label_native_call(id, (uint32)argv, ARGC)
wgl_obj_t wgl_label_create(wgl_obj_t par, wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
void wgl_label_set_text(wgl_obj_t label, const char * text)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)text;
argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT);
}
void wgl_label_set_array_text(wgl_obj_t label, const char * array, uint16_t size)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)array;
argv[2] = size;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ARRAY_TEXT);
}
void wgl_label_set_static_text(wgl_obj_t label, const char * text)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)text;
argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_STATIC_TEXT);
}
void wgl_label_set_long_mode(wgl_obj_t label, wgl_label_long_mode_t long_mode)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = long_mode;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_LONG_MODE);
}
void wgl_label_set_align(wgl_obj_t label, wgl_label_align_t align)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = align;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ALIGN);
}
void wgl_label_set_recolor(wgl_obj_t label, bool en)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_RECOLOR);
}
void wgl_label_set_body_draw(wgl_obj_t label, bool en)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_BODY_DRAW);
}
void wgl_label_set_anim_speed(wgl_obj_t label, uint16_t anim_speed)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = anim_speed;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ANIM_SPEED);
}
void wgl_label_set_text_sel_start(wgl_obj_t label, uint16_t index)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_START);
}
void wgl_label_set_text_sel_end(wgl_obj_t label, uint16_t index)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_END);
}
unsigned int wgl_label_get_text_length(wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_LENGTH);
return argv[0];
}
char * wgl_label_get_text(wgl_obj_t label, char *buffer, int buffer_len)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)buffer;
argv[2] = buffer_len;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT);
return (char *)argv[0];
}
wgl_label_long_mode_t wgl_label_get_long_mode(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LONG_MODE);
return (wgl_label_long_mode_t)argv[0];
}
wgl_label_align_t wgl_label_get_align(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ALIGN);
return (wgl_label_align_t)argv[0];
}
bool wgl_label_get_recolor(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_RECOLOR);
return (bool)argv[0];
}
bool wgl_label_get_body_draw(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_BODY_DRAW);
return (bool)argv[0];
}
uint16_t wgl_label_get_anim_speed(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ANIM_SPEED);
return (uint16_t)argv[0];
}
void wgl_label_get_letter_pos(const wgl_obj_t label, uint16_t index, wgl_point_t * pos)
{
uint32 argv[4] = {0};
argv[0] = (uint32)label;
argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS);
pos->x = argv[2];
pos->y = argv[3];
}
uint16_t wgl_label_get_letter_on(const wgl_obj_t label, wgl_point_t * pos)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = pos->x;
argv[2] = pos->y;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS);
return (uint16_t)argv[0];
}
bool wgl_label_is_char_under_pos(const wgl_obj_t label, wgl_point_t * pos)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = pos->x;
argv[2] = pos->y;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS);
return (bool)argv[0];
}
uint16_t wgl_label_get_text_sel_start(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_START);
return (uint16_t)argv[0];
}
uint16_t wgl_label_get_text_sel_end(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_END);
return (uint16_t)argv[0];
}
void wgl_label_ins_text(wgl_obj_t label, uint32_t pos, const char * txt)
{
uint32 argv[4] = {0};
argv[0] = (uint32)label;
argv[1] = pos;
argv[2] = (uint32)txt;
argv[3] = strlen(txt) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_INS_TEXT);
}
void wgl_label_cut_text(wgl_obj_t label, uint32_t pos, uint32_t cnt)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = pos;
argv[2] = cnt;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CUT_TEXT);
}

View File

@ -0,0 +1,162 @@
/*
* 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.
*/
#include "wgl.h"
#include "native_interface.h"
#include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_LIST_NATIVE_FUNC(id) wasm_list_native_call(id, (int32)argv, ARGC)
wgl_obj_t wgl_list_create(wgl_obj_t par, const wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_LIST_NATIVE_FUNC(LIST_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
//
//
//void wgl_list_clean(wgl_obj_t obj)
//{
// wasm_list_clean(obj);
//}
//
wgl_obj_t wgl_list_add_btn(wgl_obj_t list, const void * img_src, const char * txt)
{
(void)img_src; /* doesn't support img src currently */
uint32 argv[3] = {0};
argv[0] = (uint32)list;
argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
CALL_LIST_NATIVE_FUNC(LIST_FUNC_ID_ADD_BTN);
return (wgl_obj_t)argv[0];
}
//
//
//bool wgl_list_remove(const wgl_obj_t list, uint16_t index)
//{
// return wasm_list_remove(list, index);
//}
//
//
//void wgl_list_set_single_mode(wgl_obj_t list, bool mode)
//{
// wasm_list_set_single_mode(list, mode);
//}
//
//#if LV_USE_GROUP
//
//
//void wgl_list_set_btn_selected(wgl_obj_t list, wgl_obj_t btn)
//{
// wasm_list_set_btn_selected(list, btn);
//}
//#endif
//
//
//void wgl_list_set_style(wgl_obj_t list, wgl_list_style_t type, const wgl_style_t * style)
//{
// //TODO
//}
//
//
//bool wgl_list_get_single_mode(wgl_obj_t list)
//{
// return wasm_list_get_single_mode(list);
//}
//
//
//const char * wgl_list_get_btn_text(const wgl_obj_t btn)
//{
// return wasm_list_get_btn_text(btn);
//}
//
//wgl_obj_t wgl_list_get_btn_label(const wgl_obj_t btn)
//{
// return wasm_list_get_btn_label(btn);
//}
//
//
//wgl_obj_t wgl_list_get_btn_img(const wgl_obj_t btn)
//{
// return wasm_list_get_btn_img(btn);
//}
//
//
//wgl_obj_t wgl_list_get_prev_btn(const wgl_obj_t list, wgl_obj_t prev_btn)
//{
// return wasm_list_get_prev_btn(list, prev_btn);
//}
//
//
//wgl_obj_t wgl_list_get_next_btn(const wgl_obj_t list, wgl_obj_t prev_btn)
//{
// return wasm_list_get_next_btn(list, prev_btn);
//}
//
//
//int32_t wgl_list_get_btn_index(const wgl_obj_t list, const wgl_obj_t btn)
//{
// return wasm_list_get_btn_index(list, btn);
//}
//
//
//uint16_t wgl_list_get_size(const wgl_obj_t list)
//{
// return wasm_list_get_size(list);
//}
//
//#if LV_USE_GROUP
//
//wgl_obj_t wgl_list_get_btn_selected(const wgl_obj_t list)
//{
// return wasm_list_get_btn_selected(list);
//}
//#endif
//
//
//
//const wgl_style_t * wgl_list_get_style(const wgl_obj_t list, wgl_list_style_t type)
//{
// //TODO
// return NULL;
//}
//
//
//void wgl_list_up(const wgl_obj_t list)
//{
// wasm_list_up(list);
//}
//
//void wgl_list_down(const wgl_obj_t list)
//{
// wasm_list_down(list);
//}
//
//
//void wgl_list_focus(const wgl_obj_t btn, wgl_anim_enable_t anim)
//{
// wasm_list_focus(btn, anim);
//}
//

View File

@ -0,0 +1,127 @@
/*
* 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.
*/
#include "wgl.h"
#include "native_interface.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)
typedef struct _obj_evt_cb {
struct _obj_evt_cb *next;
wgl_obj_t obj;
wgl_event_cb_t event_cb;
} obj_evt_cb_t;
static obj_evt_cb_t *g_obj_evt_cb_list = NULL;
/* For lvgl compatible */
char g_widget_text[100];
wgl_res_t wgl_obj_del(wgl_obj_t obj)
{
uint32 argv[1] = {0};
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL);
return (wgl_res_t)argv[0];
}
void wgl_obj_del_async(wgl_obj_t obj)
{
uint32 argv[1] = {0};
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL_ASYNC);
}
void wgl_obj_clean(wgl_obj_t obj)
{
uint32 argv[1] = {0};
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_CLEAN);
}
void wgl_obj_align(wgl_obj_t obj, const wgl_obj_t base, wgl_align_t align, wgl_coord_t x_mod, wgl_coord_t y_mod)
{
uint32 argv[5] = {0};
argv[0] = (uint32)obj;
argv[1] = (uint32)base;
argv[2] = align;
argv[3] = x_mod;
argv[4] = y_mod;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_ALIGN);
}
wgl_event_cb_t wgl_obj_get_event_cb(const wgl_obj_t obj)
{
obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb != NULL) {
if (obj_evt_cb->obj == obj) {
return obj_evt_cb->event_cb;
}
obj_evt_cb = obj_evt_cb->next;
}
return NULL;
}
void wgl_obj_set_event_cb(wgl_obj_t obj, wgl_event_cb_t event_cb)
{
obj_evt_cb_t *obj_evt_cb;
uint32 argv[1] = {0};
obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb) {
if (obj_evt_cb->obj == obj) {
obj_evt_cb->event_cb = event_cb;
return;
}
}
obj_evt_cb = (obj_evt_cb_t *)malloc(sizeof(*obj_evt_cb));
if (obj_evt_cb == NULL)
return;
memset(obj_evt_cb, 0, sizeof(*obj_evt_cb));
obj_evt_cb->obj = obj;
obj_evt_cb->event_cb = event_cb;
if (g_obj_evt_cb_list != NULL) {
obj_evt_cb->next = g_obj_evt_cb_list;
g_obj_evt_cb_list = obj_evt_cb;
} else {
g_obj_evt_cb_list = obj_evt_cb;
}
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_SET_EVT_CB);
}
void on_widget_event(wgl_obj_t obj, wgl_event_t event)
{
obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb != NULL) {
if (obj_evt_cb->obj == obj) {
obj_evt_cb->event_cb(obj, event);
return;
}
obj_evt_cb = obj_evt_cb->next;
}
}

View File

@ -0,0 +1,37 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_H
#define WAMR_GRAPHIC_LIBRARY_H
#ifdef __cplusplus
extern "C" {
#endif
#include "wgl_shared_utils.h" /* shared types between app and native */
#include "inc/wgl_types.h"
#include "inc/wgl_obj.h"
#include "inc/wgl_btn.h"
#include "inc/wgl_cb.h"
#include "inc/wgl_label.h"
#include "inc/wgl_list.h"
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_H */

View File

@ -14,43 +14,25 @@
* limitations under the License.
*/
#include "wasm_platform.h"
#ifndef CONNECTION_API_H_
#define CONNECTION_API_H_
#include "bh_platform.h"
#ifndef CONFIG_AEE_ENABLE
static int
_stdout_hook_iwasm(int c)
{
printk("%c", (char)c);
return 1;
}
extern void __stdout_hook_install(int (*hook)(int));
#ifdef __cplusplus
extern "C" {
#endif
bool is_little_endian = false;
uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len);
bool __is_little_endian()
{
union w
{
int a;
char b;
}c;
void wasm_close_connection(uint32 handle);
c.a = 1;
return (c.b == 1);
int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len);
bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len);
#ifdef __cplusplus
}
int wasm_platform_init()
{
if (__is_little_endian())
is_little_endian = true;
#ifndef CONFIG_AEE_ENABLE
/* Enable printf() in Zephyr */
__stdout_hook_install(_stdout_hook_iwasm);
#endif
return 0;
}
#endif /* CONNECTION_API_H_ */

View File

@ -0,0 +1,37 @@
/*
* 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 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);
#ifdef __cplusplus
}
#endif
#endif /* GUI_API_H_ */

View File

@ -75,4 +75,16 @@ 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);
#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_ */
/*
* *** connection interface ***
*/
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"
#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_PO
C_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_ */

View File

@ -134,6 +134,8 @@ 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);
#include "wgl_shared_utils.h"
#ifdef __cplusplus
}
#endif

View File

@ -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 */

View File

@ -0,0 +1,342 @@
/*
* 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 WAMR_GRAPHIC_LIBRARY_SHARED_UTILS_H
#define WAMR_GRAPHIC_LIBRARY_SHARED_UTILS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include "../3rdparty/lv_conf.h"
typedef lv_coord_t wgl_coord_t; /* lv_coord_t is defined in lv_conf.h */
/**
* Represents a point on the screen.
*/
typedef struct
{
lv_coord_t x;
lv_coord_t y;
} wgl_point_t;
/** Represents an area of the screen. */
typedef struct
{
lv_coord_t x1;
lv_coord_t y1;
lv_coord_t x2;
lv_coord_t y2;
} wgl_area_t;
/** Describes the properties of a glyph. */
typedef struct
{
uint16_t adv_w; /**< The glyph needs this space. Draw the next glyph after this width. 8 bit integer, 4 bit fractional */
uint8_t box_w; /**< Width of the glyph's bounding box*/
uint8_t box_h; /**< Height of the glyph's bounding box*/
int8_t ofs_x; /**< x offset of the bounding box*/
int8_t ofs_y; /**< y offset of the bounding box*/
uint8_t bpp; /**< Bit-per-pixel: 1, 2, 4, 8*/
}wgl_font_glyph_dsc_t;
/*Describe the properties of a font*/
typedef struct _wgl_font_struct
{
/** Get a glyph's descriptor from a font*/
bool (*get_glyph_dsc)(const struct _wgl_font_struct *, wgl_font_glyph_dsc_t *, uint32_t letter, uint32_t letter_next);
/** Get a glyph's bitmap from a font*/
const uint8_t * (*get_glyph_bitmap)(const struct _wgl_font_struct *, uint32_t);
/*Pointer to the font in a font pack (must have the same line height)*/
uint8_t line_height; /**< The real line height where any text fits*/
uint8_t base_line; /**< Base line measured from the top of the line_height*/
void * dsc; /**< Store implementation specific data here*/
#if LV_USE_USER_DATA
wgl_font_user_data_t user_data; /**< Custom user data for font. */
#endif
} wgl_font_t;
#if LV_COLOR_DEPTH == 1
#define LV_COLOR_SIZE 8
#elif LV_COLOR_DEPTH == 8
#define LV_COLOR_SIZE 8
#elif LV_COLOR_DEPTH == 16
#define LV_COLOR_SIZE 16
#elif LV_COLOR_DEPTH == 32
#define LV_COLOR_SIZE 32
#else
#error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!"
#endif
/**********************
* TYPEDEFS
**********************/
typedef union
{
uint8_t blue : 1;
uint8_t green : 1;
uint8_t red : 1;
uint8_t full : 1;
} wgl_color1_t;
typedef union
{
struct
{
uint8_t blue : 2;
uint8_t green : 3;
uint8_t red : 3;
} ch;
uint8_t full;
} wgl_color8_t;
typedef union
{
struct
{
#if LV_COLOR_16_SWAP == 0
uint16_t blue : 5;
uint16_t green : 6;
uint16_t red : 5;
#else
uint16_t green_h : 3;
uint16_t red : 5;
uint16_t blue : 5;
uint16_t green_l : 3;
#endif
} ch;
uint16_t full;
} wgl_color16_t;
typedef union
{
struct
{
uint8_t blue;
uint8_t green;
uint8_t red;
uint8_t alpha;
} ch;
uint32_t full;
} wgl_color32_t;
#if LV_COLOR_DEPTH == 1
typedef uint8_t wgl_color_int_t;
typedef wgl_color1_t wgl_color_t;
#elif LV_COLOR_DEPTH == 8
typedef uint8_t wgl_color_int_t;
typedef wgl_color8_t wgl_color_t;
#elif LV_COLOR_DEPTH == 16
typedef uint16_t wgl_color_int_t;
typedef wgl_color16_t wgl_color_t;
#elif LV_COLOR_DEPTH == 32
typedef uint32_t wgl_color_int_t;
typedef wgl_color32_t wgl_color_t;
#else
#error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!"
#endif
typedef uint8_t wgl_opa_t;
/*Border types (Use 'OR'ed values)*/
enum {
WGL_BORDER_NONE = 0x00,
WGL_BORDER_BOTTOM = 0x01,
WGL_BORDER_TOP = 0x02,
WGL_BORDER_LEFT = 0x04,
WGL_BORDER_RIGHT = 0x08,
WGL_BORDER_FULL = 0x0F,
WGL_BORDER_INTERNAL = 0x10, /**< FOR matrix-like objects (e.g. Button matrix)*/
};
typedef uint8_t wgl_border_part_t;
/*Shadow types*/
enum {
WGL_SHADOW_BOTTOM = 0, /**< Only draw bottom shadow */
WGL_SHADOW_FULL, /**< Draw shadow on all sides */
};
typedef uint8_t wgl_shadow_type_t;
/**
* Objects in LittlevGL can be assigned a style - which holds information about
* how the object should be drawn.
*
* This allows for easy customization without having to modify the object's design
* function.
*/
typedef struct
{
uint8_t glass : 1; /**< 1: Do not inherit this style*/
/** Object background. */
struct
{
wgl_color_t main_color; /**< Object's main background color. */
wgl_color_t grad_color; /**< Second color. If not equal to `main_color` a gradient will be drawn for the background. */
wgl_coord_t radius; /**< Object's corner radius. You can use #WGL_RADIUS_CIRCLE if you want to draw a circle. */
wgl_opa_t opa; /**< Object's opacity (0-255). */
struct
{
wgl_color_t color; /**< Border color */
wgl_coord_t width; /**< Border width */
wgl_border_part_t part; /**< Which borders to draw */
wgl_opa_t opa; /**< Border opacity. */
} border;
struct
{
wgl_color_t color;
wgl_coord_t width;
wgl_shadow_type_t type; /**< Which parts of the shadow to draw */
} shadow;
struct
{
wgl_coord_t top;
wgl_coord_t bottom;
wgl_coord_t left;
wgl_coord_t right;
wgl_coord_t inner;
} padding;
} body;
/** Style for text drawn by this object. */
struct
{
wgl_color_t color; /**< Text color */
wgl_color_t sel_color; /**< Text selection background color. */
const wgl_font_t * font;
wgl_coord_t letter_space; /**< Space between letters */
wgl_coord_t line_space; /**< Space between lines (vertical) */
wgl_opa_t opa; /**< Text opacity */
} text;
/**< Style of images. */
struct
{
wgl_color_t color; /**< Color to recolor the image with */
wgl_opa_t intense; /**< Opacity of recoloring (0 means no recoloring) */
wgl_opa_t opa; /**< Opacity of whole image */
} image;
/**< Style of lines (not borders). */
struct
{
wgl_color_t color;
wgl_coord_t width;
wgl_opa_t opa;
uint8_t rounded : 1; /**< 1: rounded line endings*/
} line;
} wgl_style_t;
/* Object native function IDs */
enum {
OBJ_FUNC_ID_DEL,
OBJ_FUNC_ID_DEL_ASYNC,
OBJ_FUNC_ID_CLEAN,
OBJ_FUNC_ID_SET_EVT_CB,
OBJ_FUNC_ID_ALIGN,
/* Number of functions */
_OBJ_FUNC_ID_NUM,
};
/* Button native function IDs */
enum {
BTN_FUNC_ID_CREATE,
BTN_FUNC_ID_SET_TOGGLE,
BTN_FUNC_ID_SET_STATE,
BTN_FUNC_ID_TOGGLE,
BTN_FUNC_ID_SET_INK_IN_TIME,
BTN_FUNC_ID_SET_INK_WAIT_TIME,
BTN_FUNC_ID_SET_INK_OUT_TIME,
BTN_FUNC_ID_GET_STATE,
BTN_FUNC_ID_GET_TOGGLE,
BTN_FUNC_ID_GET_INK_IN_TIME,
BTN_FUNC_ID_GET_INK_WAIT_TIME,
BTN_FUNC_ID_GET_INK_OUT_TIME,
/* Number of functions */
_BTN_FUNC_ID_NUM,
};
/* Check box native function IDs */
enum {
CB_FUNC_ID_CREATE,
CB_FUNC_ID_SET_TEXT,
CB_FUNC_ID_SET_STATIC_TEXT,
CB_FUNC_ID_GET_TEXT,
CB_FUNC_ID_GET_TEXT_LENGTH,
/* Number of functions */
_CB_FUNC_ID_NUM,
};
/* List native function IDs */
enum {
LIST_FUNC_ID_CREATE,
LIST_FUNC_ID_ADD_BTN,
/* Number of functions */
_LIST_FUNC_ID_NUM,
};
/* Label native function IDs */
enum {
LABEL_FUNC_ID_CREATE,
LABEL_FUNC_ID_SET_TEXT,
LABEL_FUNC_ID_SET_ARRAY_TEXT,
LABEL_FUNC_ID_SET_STATIC_TEXT,
LABEL_FUNC_ID_SET_LONG_MODE,
LABEL_FUNC_ID_SET_ALIGN,
LABEL_FUNC_ID_SET_RECOLOR,
LABEL_FUNC_ID_SET_BODY_DRAW,
LABEL_FUNC_ID_SET_ANIM_SPEED,
LABEL_FUNC_ID_SET_TEXT_SEL_START,
LABEL_FUNC_ID_SET_TEXT_SEL_END,
LABEL_FUNC_ID_GET_TEXT,
LABEL_FUNC_ID_GET_TEXT_LENGTH,
LABEL_FUNC_ID_GET_LONG_MODE,
LABEL_FUNC_ID_GET_ALIGN,
LABEL_FUNC_ID_GET_RECOLOR,
LABEL_FUNC_ID_GET_BODY_DRAW,
LABEL_FUNC_ID_GET_ANIM_SPEED,
LABEL_FUNC_ID_GET_LETTER_POS,
LABEL_FUNC_ID_GET_TEXT_SEL_START,
LABEL_FUNC_ID_GET_TEXT_SEL_END,
LABEL_FUNC_ID_INS_TEXT,
LABEL_FUNC_ID_CUT_TEXT,
/* Number of functions */
_LABEL_FUNC_ID_NUM,
};
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_SHARED_UTILS_H */

View File

@ -18,11 +18,116 @@
#include <stdlib.h>
#include <string.h>
#include "lib_export.h"
#include "bh_platform.h"
#include "wasm_export.h"
#ifdef WASM_ENABLE_BASE_LIB
#include "base_lib_export.h"
#endif
static uint64
wasm_runtime_get_current_module_inst_wrapper()
{
return (uint64)(uintptr_t)
wasm_runtime_get_current_module_inst();
}
static bool
wasm_runtime_validate_app_addr_wrapper(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;
inst.parts[1] = inst_part1;
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
printf("Invalid module instance\n");
return false;
}
ret = wasm_runtime_validate_app_addr(module_inst, app_offset, size);
if (!ret)
wasm_runtime_clear_exception(module_inst);
return ret;
}
static bool
wasm_runtime_validate_native_addr_wrapper(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;
inst.parts[0] = inst_part0;
inst.parts[1] = inst_part1;
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
printf("Invalid module instance\n");
return false;
}
native_ptr.parts[0] = native_ptr_part0;
native_ptr.parts[1] = native_ptr_part1;
ret = wasm_runtime_validate_native_addr(module_inst,
(void*)(uintptr_t)native_ptr.u64,
size);
if (!ret)
wasm_runtime_clear_exception(module_inst);
return ret;
}
static uint64
wasm_runtime_addr_app_to_native_wrapper(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;
inst.parts[1] = inst_part1;
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
printf("Invalid module instance\n");
return 0;
}
return (uint64)(uintptr_t)
wasm_runtime_addr_app_to_native(module_inst, app_offset);
}
static int32
wasm_runtime_addr_native_to_app_wrapper(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;
inst.parts[0] = inst_part0;
inst.parts[1] = inst_part1;
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
printf("Invalid module instance\n");
return 0;
}
native_ptr.parts[0] = native_ptr_part0;
native_ptr.parts[1] = native_ptr_part1;
return wasm_runtime_addr_native_to_app(module_inst,
(void*)(uintptr_t)native_ptr.u64);
}
static NativeSymbol extended_native_symbol_defs[] = {
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
add functions to register. */
@ -38,6 +143,11 @@ static NativeSymbol extended_native_symbol_defs[] = {
EXPORT_WASM_API(wasm_timer_restart),
EXPORT_WASM_API(wasm_get_sys_tick_ms),
#endif
EXPORT_WASM_API2(wasm_runtime_get_current_module_inst),
EXPORT_WASM_API2(wasm_runtime_validate_app_addr),
EXPORT_WASM_API2(wasm_runtime_validate_native_addr),
EXPORT_WASM_API2(wasm_runtime_addr_app_to_native),
EXPORT_WASM_API2(wasm_runtime_addr_native_to_app),
};
int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)

View File

@ -0,0 +1,20 @@
/*
* 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.
*/
EXPORT_WASM_API(wasm_open_connection),
EXPORT_WASM_API(wasm_close_connection),
EXPORT_WASM_API(wasm_send_on_connection),
EXPORT_WASM_API(wasm_config_connection),

View File

@ -0,0 +1,86 @@
/*
* 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 CONNECTION_LIB_H_
#define CONNECTION_LIB_H_
#include "attr_container.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
*****************
* This file defines connection library which should be implemented by different platforms
*****************
*/
/*
* @brief Open a connection.
*
* @param name name of the connection, "TCP", "UDP" or "UART"
* @param args connection arguments, such as: ip:127.0.0.1, port:8888
*
* @return 0~0xFFFFFFFE means id of the connection, otherwise(-1) means fail
*/
typedef uint32 (*connection_open_f)(const char *name, attr_container_t *args);
/*
* @brief Close a connection.
*
* @param handle of the connection
*/
typedef void (*connection_close_f)(uint32 handle);
/*
* @brief Send data to the connection in non-blocking manner.
*
* @param handle of the connection
* @param data data buffer to be sent
* @param len length of the data in byte
*
* @return actual length sent, -1 if fail
*/
typedef int (*connection_send_f)(uint32 handle, const char *data, int len);
/*
* @brief Configure connection.
*
* @param handle of the connection
* @param cfg configurations
*
* @return true if success, false otherwise
*/
typedef bool (*connection_config_f)(uint32 handle, attr_container_t *cfg);
/* Raw connection interface for platform to implement */
typedef struct _connection_interface {
connection_open_f _open;
connection_close_f _close;
connection_send_f _send;
connection_config_f _config;
} connection_interface_t;
/* Platform must define this interface */
extern connection_interface_t connection_impl;
#ifdef __cplusplus
}
#endif
#endif /* CONNECTION_LIB_H_ */

View File

@ -0,0 +1,84 @@
/*
* 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.
*/
#include "connection_lib.h"
#include "wasm_export.h"
#include "native_interface.h"
/* Note:
*
* 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)
{
wasm_module_inst_t module_inst = get_module_inst();
attr_container_t *args;
char *name, *args_buf;
if (!validate_app_addr(name_offset, 1) ||
!validate_app_addr(args_offset, len) ||
!(name = addr_app_to_native(name_offset)) ||
!(args_buf = addr_app_to_native(args_offset)))
return -1;
args = (attr_container_t *)args_buf;
if (connection_impl._open != NULL)
return connection_impl._open(name, args);
return -1;
}
void wasm_close_connection(uint32 handle)
{
if (connection_impl._close != NULL)
connection_impl._close(handle);
}
int wasm_send_on_connection(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) ||
!(data = addr_app_to_native(data_offset)))
return -1;
if (connection_impl._send != NULL)
return connection_impl._send(handle, data, len);
return -1;
}
bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len)
{
wasm_module_inst_t module_inst = get_module_inst();
char *cfg_buf;
attr_container_t *cfg;
if (!validate_app_addr(cfg_offset, len) ||
!(cfg_buf = addr_app_to_native(cfg_offset)))
return false;
cfg = (attr_container_t *)cfg_buf;
if (connection_impl._config != NULL)
return connection_impl._config(handle, cfg);
return false;
}

View File

@ -0,0 +1,62 @@
/*
* 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.
*/
#include "conn_tcp.h"
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
int tcp_open(char *address, uint16 port)
{
int sock, ret;
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(address);
servaddr.sin_port = htons(port);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1)
return -1;
ret = connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr));
if (ret == -1) {
close(sock);
return -1;
}
/* Put the socket in non-blocking mode */
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
close(sock);
return -1;
}
return sock;
}
int tcp_send(int sock, const char *data, int size)
{
return send(sock, data, size, 0);
}
int tcp_recv(int sock, char *buffer, int buf_size)
{
return recv(sock, buffer, buf_size, 0);
}

View File

@ -14,25 +14,24 @@
* limitations under the License.
*/
#ifndef _WASM_TYPES_H
#define _WASM_TYPES_H
#ifndef CONN_LINUX_TCP_H_
#define CONN_LINUX_TCP_H_
#include "wasm_config.h"
#include "bh_platform.h"
typedef unsigned char uint8;
typedef char int8;
typedef unsigned short uint16;
typedef short int16;
typedef unsigned int uint32;
typedef int int32;
#include "wasm_platform.h"
#ifndef __cplusplus
#define true 1
#define false 0
#define inline __inline
#ifdef __cplusplus
extern "C" {
#endif
#endif /* end of _WASM_TYPES_H */
int tcp_open(char *address, uint16 port);
int tcp_send(int sock, const char *data, int size);
int tcp_recv(int sock, char *buffer, int buf_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,110 @@
/*
* 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.
*/
#include "conn_uart.h"
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
static int parse_baudrate(int baud)
{
switch (baud) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 2000000:
return B2000000;
case 2500000:
return B2500000;
case 3000000:
return B3000000;
case 3500000:
return B3500000;
case 4000000:
return B4000000;
default:
return -1;
}
}
int uart_open(char* device, int baudrate)
{
int uart_fd;
struct termios uart_term;
uart_fd = open(device, O_RDWR | O_NOCTTY);
if (uart_fd <= 0)
return -1;
memset(&uart_term, 0, sizeof(uart_term));
uart_term.c_cflag = parse_baudrate(baudrate) | CS8 | CLOCAL | CREAD;
uart_term.c_iflag = IGNPAR;
uart_term.c_oflag = 0;
/* set noncanonical mode */
uart_term.c_lflag = 0;
uart_term.c_cc[VTIME] = 30;
uart_term.c_cc[VMIN] = 1;
tcflush(uart_fd, TCIFLUSH);
if (tcsetattr(uart_fd, TCSANOW, &uart_term) != 0) {
close(uart_fd);
return -1;
}
/* Put the fd in non-blocking mode */
if (fcntl(uart_fd, F_SETFL, fcntl(uart_fd, F_GETFL) | O_NONBLOCK) < 0) {
close(uart_fd);
return -1;
}
return uart_fd;
}
int uart_send(int fd, const char *data, int size)
{
return write(fd, data, size);
}
int uart_recv(int fd, char *buffer, int buf_size)
{
return read(fd, buffer, buf_size);
}

View File

@ -0,0 +1,37 @@
/*
* 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 CONN_LINUX_UART_H_
#define CONN_LINUX_UART_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
int uart_open(char* device, int baudrate);
int uart_send(int fd, const char *data, int size);
int uart_recv(int fd, char *buffer, int buf_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,68 @@
/*
* 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.
*/
#include "conn_udp.h"
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
int udp_open(uint16 port)
{
int sock, ret;
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
return -1;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
ret = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
if (ret == -1)
return -1;
/* Put the socket in non-blocking mode */
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
close(sock);
return -1;
}
return sock;
}
int udp_send(int sock, struct sockaddr *dest, const char *data, int size)
{
return sendto(sock, data, size, MSG_CONFIRM, dest, sizeof(*dest));
}
int udp_recv(int sock, char *buffer, int buf_size)
{
struct sockaddr_in remaddr;
socklen_t addrlen = sizeof(remaddr);
return recvfrom(sock,
buffer,
buf_size,
0,
(struct sockaddr *)&remaddr,
&addrlen);
}

View File

@ -0,0 +1,37 @@
/*
* 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 CONN_LINUX_UDP_H_
#define CONN_LINUX_UDP_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
int udp_open(uint16 port);
int udp_send(int sock, struct sockaddr *dest, const char *data, int size);
int udp_recv(int sock, char *buffer, int buf_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,572 @@
/*
* 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.
*/
/*
* Note:
* This file implements the linux version connection library which is
* defined in connection_lib.h.
* It also provides a reference implementation of connections manager.
*/
#include "connection_lib.h"
#include "bh_thread.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "conn_tcp.h"
#include "conn_udp.h"
#include "conn_uart.h"
#include "bh_definition.h"
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAX_EVENTS 10
#define IO_BUF_SIZE 256
/* Connection type */
typedef enum conn_type {
CONN_TYPE_TCP,
CONN_TYPE_UDP,
CONN_TYPE_UART,
CONN_TYPE_UNKNOWN
} conn_type_t;
/* Sys connection */
typedef struct sys_connection {
/* Next connection */
struct sys_connection *next;
/* Type */
conn_type_t type;
/* Handle to interact with wasm app */
uint32 handle;
/* Underlying connection ID, may be socket fd */
int fd;
/* Module id that the connection belongs to */
uint32 module_id;
/* Argument, such as dest addr for udp */
void *arg;
} sys_connection_t;
/* Epoll instance */
static int epollfd;
/* Connections list */
static sys_connection_t *g_connections = NULL;
/* Max handle */
static uint32 g_handle_max = 0;
/* Lock to protect g_connections and g_handle_max */
static korp_mutex g_lock;
/* Epoll events */
static struct epoll_event epoll_events[MAX_EVENTS];
/* Buffer to receive data */
static char io_buf[IO_BUF_SIZE];
static uint32 _conn_open(const char *name, attr_container_t *args);
static void _conn_close(uint32 handle);
static int _conn_send(uint32 handle, const char *data, int len);
static bool _conn_config(uint32 handle, attr_container_t *cfg);
/*
* Platform implementation of connection library
*/
connection_interface_t connection_impl = {
._open = _conn_open,
._close = _conn_close,
._send = _conn_send,
._config = _conn_config
};
static void add_connection(sys_connection_t *conn)
{
vm_mutex_lock(&g_lock);
g_handle_max++;
if (g_handle_max == -1)
g_handle_max++;
conn->handle = g_handle_max;
if (g_connections) {
conn->next = g_connections;
g_connections = conn;
} else {
g_connections = conn;
}
vm_mutex_unlock(&g_lock);
}
#define FREE_CONNECTION(conn) do { \
if (conn->arg) \
bh_free(conn->arg); \
bh_free(conn); \
} while (0)
static int get_app_conns_num(uint32 module_id)
{
sys_connection_t *conn;
int num = 0;
vm_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
if (conn->module_id == module_id)
num++;
conn = conn->next;
}
vm_mutex_unlock(&g_lock);
return num;
}
static sys_connection_t *find_connection(uint32 handle, bool remove_found)
{
sys_connection_t *conn, *prev = NULL;
vm_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
if (conn->handle == handle) {
if (remove_found) {
if (prev != NULL) {
prev->next = conn->next;
} else {
g_connections = conn->next;
}
}
vm_mutex_unlock(&g_lock);
return conn;
} else {
prev = conn;
conn = conn->next;
}
}
vm_mutex_unlock(&g_lock);
return NULL;
}
static void cleanup_connections(uint32 module_id)
{
sys_connection_t *conn, *prev = NULL;
vm_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
if (conn->module_id == module_id) {
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
close(conn->fd);
if (prev != NULL) {
prev->next = conn->next;
FREE_CONNECTION(conn);
conn = prev->next;
} else {
g_connections = conn->next;
FREE_CONNECTION(conn);
conn = g_connections;
}
} else {
prev = conn;
conn = conn->next;
}
}
vm_mutex_unlock(&g_lock);
}
static conn_type_t get_conn_type(const char *name)
{
if (strcmp(name, "TCP") == 0)
return CONN_TYPE_TCP;
if (strcmp(name, "UDP") == 0)
return CONN_TYPE_UDP;
if (strcmp(name, "UART") == 0)
return CONN_TYPE_UART;
return CONN_TYPE_UNKNOWN;
}
/* --- connection lib function --- */
static uint32 _conn_open(const char *name, attr_container_t *args)
{
int fd;
sys_connection_t *conn;
struct epoll_event ev;
uint32 module_id = app_manager_get_module_id(Module_WASM_App);
if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP)
return -1;
conn = (sys_connection_t *)bh_malloc(sizeof(*conn));
if (conn == NULL)
return -1;
memset(conn, 0, sizeof(*conn));
conn->module_id = module_id;
conn->type = get_conn_type(name);
/* Generate a handle and add to list */
add_connection(conn);
if (conn->type == CONN_TYPE_TCP) {
char *address;
uint16 port;
/* Check and parse connection parameters */
if (!attr_container_contain_key(args, "address") ||
!attr_container_contain_key(args, "port"))
goto fail;
address = attr_container_get_as_string(args, "address");
port = attr_container_get_as_uint16(args, "port");
/* Connect to TCP server */
if ((fd = tcp_open(address, port)) == -1)
goto fail;
} else if (conn->type == CONN_TYPE_UDP) {
uint16 port;
/* Check and parse connection parameters */
if (!attr_container_contain_key(args, "bind port"))
goto fail;
port = attr_container_get_as_uint16(args, "bind port");
/* Bind port */
if ((fd = udp_open(port)) == -1)
goto fail;
} else if (conn->type == CONN_TYPE_UART) {
char *device;
int baud;
/* Check and parse connection parameters */
if (!attr_container_contain_key(args, "device") ||
!attr_container_contain_key(args, "baudrate"))
goto fail;
device = attr_container_get_as_string(args, "device");
baud = attr_container_get_as_int(args, "baudrate");
/* Open device */
if ((fd = uart_open(device, baud)) == -1)
goto fail;
}
conn->fd = fd;
/* Set current connection as event data */
ev.events = EPOLLIN;
ev.data.ptr = conn;
/* Monitor incoming data */
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
close(fd);
goto fail;
}
return conn->handle;
fail:
find_connection(conn->handle, true);
bh_free(conn);
return -1;
}
/* --- connection lib function --- */
static void _conn_close(uint32 handle)
{
sys_connection_t *conn = find_connection(handle, true);
if (conn != NULL) {
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
close(conn->fd);
FREE_CONNECTION(conn);
}
}
/* --- connection lib function --- */
static int _conn_send(uint32 handle, const char *data, int len)
{
sys_connection_t *conn = find_connection(handle, false);
if (conn == NULL)
return -1;
if (conn->type == CONN_TYPE_TCP)
return tcp_send(conn->fd, data, len);
if (conn->type == CONN_TYPE_UDP) {
struct sockaddr *addr = (struct sockaddr *)conn->arg;
return udp_send(conn->fd, addr, data, len);
}
if (conn->type == CONN_TYPE_UART)
return uart_send(conn->fd, data, len);
return -1;
}
/* --- connection lib function --- */
static bool _conn_config(uint32 handle, attr_container_t *cfg)
{
sys_connection_t *conn = find_connection(handle, false);
if (conn == NULL)
return false;
if (conn->type == CONN_TYPE_UDP) {
char *address;
uint16_t port;
struct sockaddr_in *addr;
/* Parse remote address/port */
if (!attr_container_contain_key(cfg, "address") ||
!attr_container_contain_key(cfg, "port"))
return false;
address = attr_container_get_as_string(cfg, "address");
port = attr_container_get_as_uint16(cfg, "port");
if (conn->arg == NULL) {
addr = (struct sockaddr_in *)bh_malloc(sizeof(*addr));
if (addr == NULL)
return false;
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(address);
addr->sin_port = htons(port);
/* Set remote address as connection arg */
conn->arg = addr;
} else {
addr = (struct sockaddr_in *)conn->arg;
addr->sin_addr.s_addr = inet_addr(address);
addr->sin_port = htons(port);
}
return true;
}
return false;
}
/* --- connection manager reference implementation ---*/
typedef struct connection_event {
uint32 handle;
char *data;
uint32 len;
} connection_event_t;
static void connection_event_cleaner(connection_event_t *conn_event)
{
if (conn_event->data != NULL)
bh_free(conn_event->data);
bh_free(conn_event);
}
static void post_msg_to_module(sys_connection_t *conn,
char *data,
uint32 len)
{
module_data *module = module_data_list_lookup_id(conn->module_id);
char *data_copy = NULL;
connection_event_t *conn_data_event;
bh_message_t msg;
if (module == NULL)
return;
conn_data_event = (connection_event_t *)bh_malloc(sizeof(*conn_data_event));
if (conn_data_event == NULL)
return;
if (len > 0) {
data_copy = (char *)bh_malloc(len);
if (data_copy == NULL) {
bh_free(conn_data_event);
return;
}
memcpy(data_copy, data, len);
}
memset(conn_data_event, 0, sizeof(*conn_data_event));
conn_data_event->handle = conn->handle;
conn_data_event->data = data_copy;
conn_data_event->len = len;
msg = bh_new_msg(CONNECTION_EVENT_WASM,
conn_data_event,
sizeof(*conn_data_event),
connection_event_cleaner);
if (!msg) {
connection_event_cleaner(conn_data_event);
return;
}
bh_post_msg2(module->queue, msg);
}
static void* polling_thread_routine (void *arg)
{
while (true) {
int i, n;
n = epoll_wait(epollfd, epoll_events, MAX_EVENTS, -1);
if (n == -1 && errno != EINTR)
continue;
for (i = 0; i < n; i++) {
sys_connection_t *conn
= (sys_connection_t *)epoll_events[i].data.ptr;
if (conn->type == CONN_TYPE_TCP) {
int count = tcp_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count <= 0) {
/* Connection is closed by peer */
post_msg_to_module(conn, NULL, 0);
_conn_close(conn->handle);
} else {
/* Data is received */
post_msg_to_module(conn, io_buf, count);
}
} else if (conn->type == CONN_TYPE_UDP) {
int count = udp_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count > 0)
post_msg_to_module(conn, io_buf, count);
} else if (conn->type == CONN_TYPE_UART) {
int count = uart_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count > 0)
post_msg_to_module(conn, io_buf, count);
}
}
}
return NULL;
}
void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
{
uint32 argv[3];
wasm_function_inst_t func_on_conn_data;
bh_assert(CONNECTION_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
connection_event_t *conn_event
= (connection_event_t *)bh_message_payload(msg);
int32 data_offset;
if (conn_event == NULL)
return;
func_on_conn_data = wasm_runtime_lookup_function(inst, "_on_connection_data",
"(i32i32i32)");
if (!func_on_conn_data) {
printf("Cannot find function _on_connection_data\n");
return;
}
/* 0 len means connection closed */
if (conn_event->len == 0) {
argv[0] = conn_event->handle;
argv[1] = 0;
argv[2] = 0;
if (!wasm_runtime_call_wasm(inst, NULL, func_on_conn_data, 3, argv)) {
printf(":Got exception running wasm code: %s\n",
wasm_runtime_get_exception(inst));
wasm_runtime_clear_exception(inst);
return;
}
} else {
data_offset = wasm_runtime_module_dup_data(inst,
conn_event->data,
conn_event->len);
if (data_offset == 0) {
printf("Got exception running wasm code: %s\n",
wasm_runtime_get_exception(inst));
wasm_runtime_clear_exception(inst);
return;
}
argv[0] = conn_event->handle;
argv[1] = (uint32) data_offset;
argv[2] = conn_event->len;
if (!wasm_runtime_call_wasm(inst, NULL, func_on_conn_data, 3, argv)) {
printf(":Got exception running wasm code: %s\n",
wasm_runtime_get_exception(inst));
wasm_runtime_clear_exception(inst);
wasm_runtime_module_free(inst, data_offset);
return;
}
wasm_runtime_module_free(inst, data_offset);
}
}
bool init_connection_framework()
{
korp_thread tid;
epollfd = epoll_create(MAX_EVENTS);
if (epollfd == -1)
return false;
if (vm_mutex_init(&g_lock) != BH_SUCCESS) {
close(epollfd);
return false;
}
if (!wasm_register_cleanup_callback(cleanup_connections)) {
goto fail;
}
if (!wasm_register_msg_callback(CONNECTION_EVENT_WASM,
app_mgr_connection_event_callback)) {
goto fail;
}
if (vm_thread_create(&tid,
polling_thread_routine,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE) != BH_SUCCESS) {
goto fail;
}
return true;
fail:
vm_mutex_destroy(&g_lock);
close(epollfd);
return false;
}

View File

@ -0,0 +1,24 @@
# 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.
set (WASM_LIB_CONN_MGR_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_LIB_CONN_MGR_DIR})
file (GLOB_RECURSE source_all ${WASM_LIB_CONN_MGR_DIR}/*.c)
set (WASM_LIB_CONN_MGR_SOURCE ${source_all})

View File

@ -0,0 +1,23 @@
# 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.
set (WASM_LIB_CONN_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_LIB_CONN_DIR})
file (GLOB source_all ${WASM_LIB_CONN_DIR}/*.c)
set (WASM_LIB_CONN_SOURCE ${source_all})

View File

@ -0,0 +1,34 @@
/*
* 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.
*/
/*
* Note:
* This file implements the linux version connection library which is
* defined in connection_lib.h.
* It also provides a reference impl of connections manager.
*/
#include "connection_lib.h"
/*
* Platform implementation of connection library
*/
connection_interface_t connection_impl = {
._open = NULL,
._close = NULL,
._send = NULL,
._config = NULL
};

View File

@ -0,0 +1,39 @@
/*
* 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.
*/
/* button */
EXPORT_WASM_API(wasm_btn_native_call),
/* obj */
EXPORT_WASM_API(wasm_obj_native_call),
/* label */
EXPORT_WASM_API(wasm_label_native_call),
/* cont */
//EXPORT_WASM_API(wasm_cont_native_call),
/* page */
//EXPORT_WASM_API(wasm_page_native_call),
/* list */
EXPORT_WASM_API(wasm_list_native_call),
/* drop down list */
//EXPORT_WASM_API(wasm_ddlist_native_call),
/* check box */
EXPORT_WASM_API(wasm_cb_native_call),

View File

@ -0,0 +1,25 @@
# 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.
set (WASM_LIB_GUI_DIR ${CMAKE_CURRENT_LIST_DIR})
set (THIRD_PARTY_DIR ${WASM_LIB_GUI_DIR}/../../../3rdparty)
include_directories(${WASM_LIB_GUI_DIR} ${THIRD_PARTY_DIR} ${THIRD_PARTY_DIR}/lvgl)
file (GLOB_RECURSE lvgl_source ${THIRD_PARTY_DIR}/lvgl/*.c)
file (GLOB_RECURSE wrapper_source ${WASM_LIB_GUI_DIR}/*.c)
set (WASM_LIB_GUI_SOURCE ${wrapper_source} ${lvgl_source})

View File

@ -0,0 +1,57 @@
/*
* 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.
*/
#include "native_interface.h"
#include "lvgl.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
/* -------------------------------------------------------------------------
* Button widget native function wrappers
* -------------------------------------------------------------------------*/
static int32 _btn_create(lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_BTN, par, copy);
}
static WGLNativeFuncDef btn_native_func_defs[] = {
{ BTN_FUNC_ID_CREATE, _btn_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_STATE, lv_btn_set_state, NO_RET, 2, {0, -1}, {-1} },
// { BTN_FUNC_ID_SET_STYLE, _btn_set_style, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_STATE, lv_btn_get_state, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_TOGGLE, lv_btn_toggle, NO_RET, 1, {0, -1}, {-1} },
};
/*************** Native Interface to Wasm App ***********/
void wasm_btn_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
{
uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(btn_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,73 @@
/*
* 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.
*/
#include "lvgl.h"
#include "wasm_export.h"
#include "native_interface.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
/* -------------------------------------------------------------------------
* Label widget native function wrappers
* -------------------------------------------------------------------------*/
static int32 _cb_create(lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_CB, par, copy);
}
static int32 _cb_get_text_length(lv_obj_t *cb)
{
const char *text = lv_cb_get_text(cb);
if (text == NULL)
return 0;
return strlen(text);
}
static int32 _cb_get_text(lv_obj_t *cb, char *buffer, int buffer_len)
{
wasm_module_inst_t module_inst = get_module_inst();
const char *text = lv_cb_get_text(cb);
if (text == NULL)
return 0;
strncpy(buffer, text, buffer_len - 1);
buffer[buffer_len - 1] = '\0';
return addr_native_to_app(buffer);
}
static WGLNativeFuncDef cb_native_func_defs[] = {
{ CB_FUNC_ID_CREATE, _cb_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ CB_FUNC_ID_SET_TEXT, lv_cb_set_text, NO_RET, 2, {0, -1}, {1, -1} },
{ CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text, NO_RET, 2, {0, -1}, {1, -1} },
{ CB_FUNC_ID_GET_TEXT_LENGTH, _cb_get_text_length, HAS_RET, 1, {0, -1}, {-1} },
{ CB_FUNC_ID_GET_TEXT, _cb_get_text, HAS_RET, 3, {0, -1}, {1, -1} },
};
/*************** Native Interface to Wasm App ***********/
void wasm_cb_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
{
uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(cb_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,18 @@
/*
* 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.
*/
#include "lvgl.h"
#include "module_wasm_app.h"

View File

@ -0,0 +1,72 @@
/*
* 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.
*/
#include "lvgl.h"
#include "wasm_export.h"
#include "native_interface.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
/* -------------------------------------------------------------------------
* Label widget native function wrappers
* -------------------------------------------------------------------------*/
static int32 _label_create(lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par, copy);
}
static int32 _label_get_text_length(lv_obj_t *label)
{
char *text = lv_label_get_text(label);
if (text == NULL)
return 0;
return strlen(text);
}
static int32 _label_get_text(lv_obj_t *label, char *buffer, int buffer_len)
{
wasm_module_inst_t module_inst = get_module_inst();
char *text = lv_label_get_text(label);
if (text == NULL)
return 0;
strncpy(buffer, text, buffer_len - 1);
buffer[buffer_len - 1] = '\0';
return addr_native_to_app(buffer);
}
static WGLNativeFuncDef label_native_func_defs[] = {
{ LABEL_FUNC_ID_CREATE, _label_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ LABEL_FUNC_ID_SET_TEXT, lv_label_set_text, NO_RET, 2, {0, -1}, {1, -1} },
{ LABEL_FUNC_ID_GET_TEXT_LENGTH, _label_get_text_length, HAS_RET, 1, {0, -1}, {-1} },
{ LABEL_FUNC_ID_GET_TEXT, _label_get_text, HAS_RET, 3, {0, -1}, {1, -1} },
};
/*************** Native Interface to Wasm App ***********/
void wasm_label_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
{
uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(label_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,63 @@
/*
* 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.
*/
#include "native_interface.h"
#include "lvgl.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
/* -------------------------------------------------------------------------
* List widget native function wrappers
* -------------------------------------------------------------------------*/
static int32 _list_create(lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_LIST, par, copy);
}
static int32 _list_add_btn(lv_obj_t *list, const char *text)
{
uint32 btn_obj_id;
lv_obj_t *btn;
btn = lv_list_add_btn(list, NULL, text);
if (btn == NULL)
return 0;
if (wgl_native_add_object(btn,
app_manager_get_module_id(Module_WASM_App),
&btn_obj_id))
return btn_obj_id; /* success return */
return 0;
}
static WGLNativeFuncDef list_native_func_defs[] = {
{ LIST_FUNC_ID_CREATE, _list_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ LIST_FUNC_ID_ADD_BTN, _list_add_btn, HAS_RET, 2, {0, -1}, {1, -1} },
};
/*************** Native Interface to Wasm App ***********/
void wasm_list_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
{
uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(list_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,201 @@
#include "wgl_native_utils.h"
#include "lvgl.h"
#include "module_wasm_app.h"
#include "wasm_export.h"
#include <stdint.h>
#define THROW_EXC(msg) wasm_runtime_set_exception(get_module_inst(), msg);
void
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy)
{
uint32 obj_id;
lv_obj_t *wigdet;
//TODO: limit total widget number
if (par == NULL)
par = lv_disp_get_scr_act(NULL);
if (widget_type == WIDGET_TYPE_BTN)
wigdet = lv_btn_create(par, copy);
else if (widget_type == WIDGET_TYPE_LABEL)
wigdet = lv_label_create(par, copy);
else if (widget_type == WIDGET_TYPE_CB)
wigdet = lv_cb_create(par, copy);
else if (widget_type == WIDGET_TYPE_LIST)
wigdet = lv_list_create(par, copy);
else if (widget_type == WIDGET_TYPE_DDLIST)
wigdet = lv_ddlist_create(par, copy);
if (wigdet == NULL)
return 0;
if (wgl_native_add_object(wigdet,
app_manager_get_module_id(Module_WASM_App),
&obj_id))
return obj_id; /* success return */
return 0;
}
static void invokeNative(intptr_t argv[], uint32 argc, void (*native_code)())
{
switch(argc) {
case 0:
native_code();
break;
case 1:
native_code(argv[0]);
break;
case 2:
native_code(argv[0], argv[1]);
break;
case 3:
native_code(argv[0], argv[1], argv[2]);
break;
case 4:
native_code(argv[0], argv[1], argv[2], argv[3]);
break;
case 5:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4]);
break;
case 6:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
break;
case 7:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6]);
break;
case 8:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7]);
break;
case 9:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8]);
break;
case 10:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9]);
break;
default:
/* FIXME: If this happen, add more cases. */
wasm_runtime_set_exception(get_module_inst(),
"the argument number of native function exceeds maximum");
return;
}
}
typedef void (*GenericFunctionPointer)();
typedef int32 (*Int32FuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
typedef void (*VoidFuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
void wgl_native_func_call(WGLNativeFuncDef *funcs,
uint32 size,
int32 func_id,
uint32 argv_offset,
uint32 argc)
{
WGLNativeFuncDef *func_def = funcs;
WGLNativeFuncDef *func_def_end = func_def + size;
uint32 *argv;
wasm_module_inst_t module_inst = get_module_inst();
if (!validate_app_addr(argv_offset, argc * sizeof(uint32)))
return;
argv = addr_app_to_native(argv_offset);
while (func_def < func_def_end) {
if (func_def->func_id == func_id) {
int i, obj_arg_num = 0, ptr_arg_num = 0;
intptr_t argv_copy_buf[16];
intptr_t *argv_copy = argv_copy_buf;
if (func_def->arg_num > 16) {
argv_copy = (intptr_t *)bh_malloc(func_def->arg_num *
sizeof(intptr_t));
if (argv_copy == NULL)
return;
}
/* Init argv_copy */
for (i = 0; i < func_def->arg_num; i++)
argv_copy[i] = (intptr_t)argv[i];
/* Validate object arguments */
i = 0;
for (; i < OBJ_ARG_NUM_MAX && func_def->obj_arg_indexes[i] != 0xff;
i++, obj_arg_num++) {
uint8 index = func_def->obj_arg_indexes[i];
bool null_ok = index & NULL_OK;
index = index & (~NULL_OK);
/* Some API's allow to pass NULL obj, such as xxx_create() */
if (argv[index] == 0) {
if (!null_ok) {
THROW_EXC("the object id is 0 and invalid");
goto fail;
}
/* Continue so that to pass null object validation */
continue;
}
if (!wgl_native_validate_object(argv[index], (lv_obj_t **)&argv_copy[index])) {
THROW_EXC("the object is invalid");
goto fail;
}
}
/* Validate address arguments */
i = 0;
for (; i < PTR_ARG_NUM_MAX && func_def->ptr_arg_indexes[i] != 0xff;
i++, ptr_arg_num++) {
uint8 index = func_def->ptr_arg_indexes[i];
/* The index+1 arg is the data size to be validated */
if (!validate_app_addr(argv[index], argv[index + 1]))
goto fail;
/* Convert to native address before call lvgl function */
argv_copy[index] = (intptr_t)addr_app_to_native(argv[index]);
}
if (func_def->has_ret == NO_RET)
invokeNative_Void(argv_copy,
func_def->arg_num,
func_def->func_ptr);
else
argv[0] = invokeNative_Int32(argv_copy,
func_def->arg_num,
func_def->func_ptr);
if (argv_copy != argv_copy_buf)
bh_free(argv_copy);
/* success return */
return;
fail:
if (argv_copy != argv_copy_buf)
bh_free(argv_copy);
return;
}
func_def++;
}
THROW_EXC("the native widget function is not found!");
}

View File

@ -0,0 +1,73 @@
#ifndef WAMR_GRAPHIC_LIBRARY_NATIVE_UTILS_H
#define WAMR_GRAPHIC_LIBRARY_NATIVE_UTILS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "bh_platform.h"
#include "lvgl.h"
#define OBJ_ARG_NUM_MAX 4
#define PTR_ARG_NUM_MAX 4
#define NULL_OK 0x80
enum {
HAS_RET,
NO_RET
};
enum {
WIDGET_TYPE_BTN,
WIDGET_TYPE_LABEL,
WIDGET_TYPE_CB,
WIDGET_TYPE_LIST,
WIDGET_TYPE_DDLIST,
_WIDGET_TYPE_NUM,
};
typedef struct WGLNativeFuncDef {
/* Function id */
int32 func_id;
/* Native function pointer */
void *func_ptr;
/* whether has return value */
uint8 has_ret;
/* argument number */
uint8 arg_num;
/* low 7 bit: obj argument index
* highest 1 bit: allow obj be null or not
* -1 means the end of this array */
uint8 obj_arg_indexes[OBJ_ARG_NUM_MAX];
/* pointer argument indexes, -1 means the end of this array */
uint8 ptr_arg_indexes[PTR_ARG_NUM_MAX];
} WGLNativeFuncDef;
bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj);
bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id);
uint32 wgl_native_wigdet_create(int8 widget_type,
lv_obj_t *par,
lv_obj_t *copy);
void wgl_native_func_call(WGLNativeFuncDef *funcs,
uint32 size,
int32 func_id,
uint32 argv_offset,
uint32 argc);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_NATIVE_UTILS_H */

View File

@ -0,0 +1,353 @@
/*
* 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.
*/
#include "lvgl.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "bh_list.h"
#include "bh_thread.h"
#include "wgl_native_utils.h"
typedef struct {
bh_list_link l;
/* The object id. */
uint32 obj_id;
/* The lv object */
lv_obj_t *obj;
/* Module id that the obj belongs to */
uint32 module_id;
} object_node_t;
typedef struct {
int32 obj_id;
lv_event_t event;
} object_event_t;
/* Max obj id */
static uint32 g_obj_id_max = 0;
static bh_list g_object_list;
static korp_mutex g_object_list_mutex;
static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg)
{
uint32 argv[2];
wasm_function_inst_t func_on_object_event;
bh_assert(WIDGET_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
object_event_t *object_event
= (object_event_t *)bh_message_payload(msg);
if (object_event == NULL)
return;
func_on_object_event = wasm_runtime_lookup_function(inst, "_on_widget_event",
"(i32i32)");
if (!func_on_object_event) {
printf("Cannot find function _on_object_event\n");
return;
}
argv[0] = object_event->obj_id;
argv[1] = object_event->event;
if (!wasm_runtime_call_wasm(inst, NULL, func_on_object_event, 2, argv)) {
printf(":Got exception running wasm code: %s\n",
wasm_runtime_get_exception(inst));
wasm_runtime_clear_exception(inst);
return;
}
}
static void cleanup_object_list(uint32 module_id)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
while (true) {
bool found = false;
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
/* delete the leaf node belongs to the module firstly */
if (module_id == elem->module_id &&
lv_obj_count_children(elem->obj) == 0) {
object_node_t *next = (object_node_t *)bh_list_elem_next(elem);
found = true;
lv_obj_del(elem->obj);
bh_list_remove(&g_object_list, elem);
bh_free(elem);
elem = next;
} else {
elem = (object_node_t *)bh_list_elem_next(elem);
}
}
if (!found)
break;
}
vm_mutex_unlock(&g_object_list_mutex);
}
static bool init_object_event_callback_framework()
{
if (!wasm_register_cleanup_callback(cleanup_object_list)) {
goto fail;
}
if (!wasm_register_msg_callback(WIDGET_EVENT_WASM,
app_mgr_object_event_callback)) {
goto fail;
}
return true;
fail:
return false;
}
bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj_id == elem->obj_id) {
if (obj != NULL)
*obj = elem->obj;
vm_mutex_unlock(&g_object_list_mutex);
return true;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
return false;
}
bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id)
{
object_node_t *node;
node = (object_node_t *) bh_malloc(sizeof(object_node_t));
if (node == NULL)
return false;
/* Generate an obj id */
g_obj_id_max++;
if (g_obj_id_max == -1)
g_obj_id_max = 1;
memset(node, 0, sizeof(*node));
node->obj = obj;
node->obj_id = g_obj_id_max;
node->module_id = module_id;
vm_mutex_lock(&g_object_list_mutex);
bh_list_insert(&g_object_list, node);
vm_mutex_unlock(&g_object_list_mutex);
if (obj_id != NULL)
*obj_id = node->obj_id;
return true;
}
static void _obj_del_recursive(lv_obj_t *obj)
{
object_node_t *elem;
lv_obj_t * i;
lv_obj_t * i_next;
i = lv_ll_get_head(&(obj->child_ll));
while (i != NULL) {
/*Get the next object before delete this*/
i_next = lv_ll_get_next(&(obj->child_ll), i);
/*Call the recursive del to the child too*/
_obj_del_recursive(i);
/*Set i to the next node*/
i = i_next;
}
vm_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj == elem->obj) {
bh_list_remove(&g_object_list, elem);
bh_free(elem);
vm_mutex_unlock(&g_object_list_mutex);
return;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
}
static void _obj_clean_recursive(lv_obj_t *obj)
{
lv_obj_t * i;
lv_obj_t * i_next;
i = lv_ll_get_head(&(obj->child_ll));
while (i != NULL) {
/*Get the next object before delete this*/
i_next = lv_ll_get_next(&(obj->child_ll), i);
/*Call the recursive del to the child too*/
_obj_del_recursive(i);
/*Set i to the next node*/
i = i_next;
}
}
static void post_widget_msg_to_module(object_node_t *object_node, lv_event_t event)
{
module_data *module = module_data_list_lookup_id(object_node->module_id);
object_event_t *object_event;
if (module == NULL)
return;
object_event = (object_event_t *)bh_malloc(sizeof(*object_event));
if (object_event == NULL)
return;
memset(object_event, 0, sizeof(*object_event));
object_event->obj_id = object_node->obj_id;
object_event->event = event;
bh_post_msg(module->queue,
WIDGET_EVENT_WASM,
object_event,
sizeof(*object_event));
}
static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj == elem->obj) {
post_widget_msg_to_module(elem, event);
vm_mutex_unlock(&g_object_list_mutex);
return;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
}
static void* lv_task_handler_thread_routine (void *arg)
{
korp_sem sem;
vm_sem_init(&sem, 0);
while (true) {
vm_sem_reltimedwait(&sem, 100);
lv_task_handler();
}
return NULL;
}
void wgl_init(void)
{
korp_thread tid;
lv_init();
bh_list_init(&g_object_list);
vm_recursive_mutex_init(&g_object_list_mutex);
init_object_event_callback_framework();
/* new a thread, call lv_task_handler periodically */
vm_thread_create(&tid,
lv_task_handler_thread_routine,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
}
/* -------------------------------------------------------------------------
* Obj native function wrappers
* -------------------------------------------------------------------------*/
static lv_res_t _obj_del(lv_obj_t *obj)
{
/* Recursively delete object node in the list belong to this
* parent object including itself */
_obj_del_recursive(obj);
return lv_obj_del(obj);
}
static void _obj_clean(lv_obj_t *obj)
{
/* Recursively delete child object node in the list belong to this
* parent object */
_obj_clean_recursive(obj);
/* Delete all of its children */
lv_obj_clean(obj);
}
static void _obj_set_event_cb(lv_obj_t *obj)
{
lv_obj_set_event_cb(obj, internal_lv_obj_event_cb);
}
/* ------------------------------------------------------------------------- */
static WGLNativeFuncDef obj_native_func_defs[] = {
{ OBJ_FUNC_ID_DEL, _obj_del, HAS_RET, 1, {0, -1}, {-1} },
{ OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async, NO_RET, 1, {0, -1}, {-1} },
{ OBJ_FUNC_ID_CLEAN, _obj_clean, NO_RET, 1, {0, -1}, {-1} },
{ OBJ_FUNC_ID_ALIGN, lv_obj_align, NO_RET, 5, {0, 1 | NULL_OK, -1}, {-1} },
{ OBJ_FUNC_ID_SET_EVT_CB, _obj_set_event_cb, NO_RET, 1, {0, -1}, {-1} },
};
/*************** Native Interface to Wasm App ***********/
void wasm_obj_native_call(int32 func_id, uint32 argv_offset, uint32 argc)
{
uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(obj_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -52,6 +52,29 @@ wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
#define module_free(offset) \
wasm_runtime_module_free(module_inst, offset)
static bool
validate_str_addr(wasm_module_inst_t module_inst, int32 str_offset)
{
int32 app_end_offset;
char *str, *str_end;
if (!wasm_runtime_get_app_addr_range(module_inst, str_offset,
NULL, &app_end_offset))
goto fail;
str = addr_app_to_native(str_offset);
str_end = str + (app_end_offset - str_offset);
while (str < str_end && *str != '\0')
str++;
if (str == str_end)
goto fail;
return true;
fail:
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
return false;
}
typedef int (*out_func_t)(int c, void *ctx);
enum pad_type {
@ -64,9 +87,14 @@ enum pad_type {
typedef char *_va_list;
#define _INTSIZEOF(n) \
((sizeof(n) + 3) & ~3)
#define _va_arg(ap,t) \
#define _va_arg(ap, t) \
(*(t*)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))
#define CHECK_VA_ARG(ap, t) do { \
if ((uint8*)ap + _INTSIZEOF(t) > native_end_addr) \
goto fail; \
} while (0)
/**
* @brief Output an unsigned int in hex format
*
@ -172,14 +200,19 @@ print_err(out_func_t out, void *ctx)
out('R', ctx);
}
static void
_vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
static bool
_vprintf_wa(out_func_t out, void *ctx, const char *fmt, _va_list ap,
wasm_module_inst_t module_inst)
{
int might_format = 0; /* 1 if encountered a '%' */
enum pad_type padding = PAD_NONE;
int min_width = -1;
int long_ctr = 0;
uint8 *native_end_addr;
if (!wasm_runtime_get_native_addr_range(module_inst, (uint8*)ap,
NULL, &native_end_addr))
goto fail;
/* fmt has already been adjusted if needed */
@ -232,10 +265,13 @@ _vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
int32 d;
if (long_ctr < 2) {
CHECK_VA_ARG(ap, int32);
d = _va_arg(ap, int32);
}
else {
int64 lld = _va_arg(ap, int64);
int64 lld;
CHECK_VA_ARG(ap, int64);
lld = _va_arg(ap, int64);
if (lld > INT32_MAX || lld < INT32_MIN) {
print_err(out, ctx);
break;
@ -255,10 +291,13 @@ _vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
uint32 u;
if (long_ctr < 2) {
CHECK_VA_ARG(ap, uint32);
u = _va_arg(ap, uint32);
}
else {
uint64 llu = _va_arg(ap, uint64);
uint64 llu;
CHECK_VA_ARG(ap, uint64);
llu = _va_arg(ap, uint64);
if (llu > INT32_MAX) {
print_err(out, ctx);
break;
@ -281,8 +320,10 @@ _vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
bool is_ptr = (*fmt == 'p') ? true : false;
if (long_ctr < 2) {
CHECK_VA_ARG(ap, uint32);
x = _va_arg(ap, uint32);
} else {
CHECK_VA_ARG(ap, uint64);
x = _va_arg(ap, uint64);
}
_printf_hex_uint(out, ctx, x, !is_ptr, padding, min_width);
@ -292,11 +333,13 @@ _vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
case 's': {
char *s;
char *start;
int32 s_offset = _va_arg(ap, uint32);
int32 s_offset;
if (!validate_app_addr(s_offset, 1)) {
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
return;
CHECK_VA_ARG(ap, uint32);
s_offset = _va_arg(ap, uint32);
if (!validate_str_addr(module_inst, s_offset)) {
return false;
}
s = start = addr_app_to_native(s_offset);
@ -314,7 +357,9 @@ _vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
}
case 'c': {
int c = _va_arg(ap, int);
int c;
CHECK_VA_ARG(ap, int);
c = _va_arg(ap, int);
out(c, ctx);
break;
}
@ -336,6 +381,11 @@ _vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
still_might_format:
++fmt;
}
return true;
fail:
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
return false;
}
struct str_context {
@ -364,7 +414,7 @@ sprintf_out(int c, struct str_context *ctx)
static int
printf_out(int c, struct str_context *ctx)
{
printf("%c", c);
bh_printf("%c", c);
ctx->count++;
return c;
}
@ -391,7 +441,7 @@ parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
_va_list v;
} u;
if (!validate_app_addr(fmt_offset, 1)
if (!validate_str_addr(module_inst, fmt_offset)
|| !validate_app_addr(va_list_offset, sizeof(int32)))
return false;
@ -414,7 +464,8 @@ _printf_wrapper(int32 fmt_offset, int32 va_list_offset)
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
return 0;
_vprintf((out_func_t) printf_out, &ctx, fmt, va_args, module_inst);
if (!_vprintf_wa((out_func_t)printf_out, &ctx, fmt, va_args, module_inst))
return 0;
return ctx.count;
}
@ -422,13 +473,17 @@ static int
_sprintf_wrapper(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;
const char *fmt;
_va_list va_args;
if (!validate_app_addr(str_offset, 1))
return 0;
if (!wasm_runtime_get_app_addr_range(module_inst, str_offset,
NULL, &app_end_offset)) {
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
return false;
}
str = addr_app_to_native(str_offset);
@ -436,10 +491,11 @@ _sprintf_wrapper(int32 str_offset, int32 fmt_offset, int32 va_list_offset)
return 0;
ctx.str = str;
ctx.max = INT_MAX;
ctx.max = app_end_offset - str_offset;
ctx.count = 0;
_vprintf((out_func_t) sprintf_out, &ctx, fmt, va_args, module_inst);
if (!_vprintf_wa((out_func_t)sprintf_out, &ctx, fmt, va_args, module_inst))
return 0;
if (ctx.count < ctx.max) {
str[ctx.count] = '\0';
@ -470,7 +526,8 @@ _snprintf_wrapper(int32 str_offset, int32 size, int32 fmt_offset,
ctx.max = size;
ctx.count = 0;
_vprintf((out_func_t) sprintf_out, &ctx, fmt, va_args, module_inst);
if (!_vprintf_wa((out_func_t)sprintf_out, &ctx, fmt, va_args, module_inst))
return 0;
if (ctx.count < ctx.max) {
str[ctx.count] = '\0';
@ -485,17 +542,17 @@ _puts_wrapper(int32 str_offset)
wasm_module_inst_t module_inst = get_module_inst();
const char *str;
if (!validate_app_addr(str_offset, 1))
if (!validate_str_addr(module_inst, str_offset))
return 0;
str = addr_app_to_native(str_offset);
return printf("%s\n", str);
return bh_printf("%s\n", str);
}
static int
_putchar_wrapper(int c)
{
printf("%c", c);
bh_printf("%c", c);
return 1;
}
@ -507,7 +564,7 @@ _strdup_wrapper(int32 str_offset)
uint32 len;
int32 str_ret_offset = 0;
if (!validate_app_addr(str_offset, 1))
if (!validate_str_addr(module_inst, str_offset))
return 0;
str = addr_app_to_native(str_offset);
@ -596,7 +653,7 @@ _strchr_wrapper(int32 s_offset, int32 c)
const char *s;
char *ret;
if (!validate_app_addr(s_offset, 1))
if (!validate_str_addr(module_inst, s_offset))
return s_offset;
s = addr_app_to_native(s_offset);
@ -610,8 +667,8 @@ _strcmp_wrapper(int32 s1_offset, int32 s2_offset)
wasm_module_inst_t module_inst = get_module_inst();
void *s1, *s2;
if (!validate_app_addr(s1_offset, 1)
|| !validate_app_addr(s2_offset, 1))
if (!validate_str_addr(module_inst, s1_offset)
|| !validate_str_addr(module_inst, s2_offset))
return 0;
s1 = addr_app_to_native(s1_offset);
@ -639,14 +696,19 @@ _strcpy_wrapper(int32 dst_offset, int32 src_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
char *dst, *src;
uint32 len;
if (!validate_app_addr(dst_offset, 1)
|| !validate_app_addr(src_offset, 1))
if (!validate_str_addr(module_inst, src_offset))
return 0;
src = addr_app_to_native(src_offset);
len = strlen(src);
if (!validate_app_addr(dst_offset, len + 1))
return 0;
dst = addr_app_to_native(dst_offset);
src = addr_app_to_native(src_offset);
strcpy(dst, src);
strncpy(dst, src, len + 1);
return dst_offset;
}
@ -672,7 +734,7 @@ _strlen_wrapper(int32 s_offset)
wasm_module_inst_t module_inst = get_module_inst();
char *s;
if (!validate_app_addr(s_offset, 1))
if (!validate_str_addr(module_inst, s_offset))
return 0;
s = addr_app_to_native(s_offset);
@ -789,7 +851,7 @@ static void
_llvm_stackrestore_wrapper(uint32 llvm_stack)
{
wasm_module_inst_t module_inst = get_module_inst();
printf("_llvm_stackrestore called!\n");
bh_printf("_llvm_stackrestore called!\n");
wasm_runtime_set_llvm_stack(module_inst, llvm_stack);
}
@ -797,7 +859,7 @@ static uint32
_llvm_stacksave_wrapper()
{
wasm_module_inst_t module_inst = get_module_inst();
printf("_llvm_stacksave called!\n");
bh_printf("_llvm_stacksave called!\n");
return wasm_runtime_get_llvm_stack(module_inst);
}
@ -846,6 +908,22 @@ nullFunc_X_wrapper(int32 code)
wasm_runtime_set_exception(module_inst, buf);
}
/*#define ENABLE_SPEC_TEST 1*/
#ifdef ENABLE_SPEC_TEST
static void
print_i32_wrapper(int i32)
{
bh_printf("%d\n", i32);
}
static void
print_wrapper(int i32)
{
bh_printf("%d\n", i32);
}
#endif
/* TODO: add function parameter/result types check */
#define REG_NATIVE_FUNC(module_name, func_name) \
{ #module_name, #func_name, func_name##_wrapper }
@ -857,6 +935,10 @@ typedef struct WASMNativeFuncDef {
} WASMNativeFuncDef;
static WASMNativeFuncDef native_func_defs[] = {
#ifdef ENABLE_SPEC_TEST
REG_NATIVE_FUNC(spectest, print_i32),
REG_NATIVE_FUNC(spectest, print),
#endif
REG_NATIVE_FUNC(env, _printf),
REG_NATIVE_FUNC(env, _sprintf),
REG_NATIVE_FUNC(env, _snprintf),
@ -927,6 +1009,9 @@ typedef struct WASMNativeGlobalDef {
} WASMNativeGlobalDef;
static WASMNativeGlobalDef native_global_defs[] = {
#ifdef ENABLE_SPEC_TEST
{ "spectest", "global_i32", .global_data.u32 = 0 },
#endif
{ "env", "STACKTOP", .global_data.u32 = 0 },
{ "env", "STACK_MAX", .global_data.u32 = 0 },
{ "env", "ABORT", .global_data.u32 = 0 },
@ -961,23 +1046,6 @@ wasm_native_global_lookup(const char *module_name, const char *global_name,
global_def++;
}
/* Lookup non-constant globals which cannot be defined by table */
if (!strcmp(module_name, "env")) {
if (!strcmp(global_name, "_stdin")) {
global->global_data_linked.addr = (uintptr_t)stdin;
global->is_addr = true;
return true;
} else if (!strcmp(global_name, "_stdout")) {
global->global_data_linked.addr = (uintptr_t)stdout;
global->is_addr = true;
return true;
} else if (!strcmp(global_name, "_stderr")) {
global->global_data_linked.addr = (uintptr_t)stderr;
global->is_addr = true;
return true;
}
}
return false;
}

View File

@ -28,9 +28,7 @@ GLOBAL_INCLUDES += ${IWASM_ROOT}/runtime/include \
$(NAME)_SOURCES := ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c \
${IWASM_ROOT}/runtime/utils/wasm_log.c \
${IWASM_ROOT}/runtime/utils/wasm_dlfcn.c \
${IWASM_ROOT}/runtime/platform/alios/wasm_math.c \
${IWASM_ROOT}/runtime/platform/alios/wasm_platform.c \
${IWASM_ROOT}/runtime/platform/alios/wasm-native.c \
${IWASM_ROOT}/runtime/platform/alios/wasm_native.c \
${IWASM_ROOT}/runtime/vmcore-wasm/wasm_application.c \
${IWASM_ROOT}/runtime/vmcore-wasm/wasm_interp.c \
${IWASM_ROOT}/runtime/vmcore-wasm/wasm_loader.c \
@ -41,6 +39,7 @@ $(NAME)_SOURCES := ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c \
${SHARED_LIB_ROOT}/platform/alios/bh_platform.c \
${SHARED_LIB_ROOT}/platform/alios/bh_assert.c \
${SHARED_LIB_ROOT}/platform/alios/bh_thread.c \
${SHARED_LIB_ROOT}/platform/alios/bh_math.c \
${SHARED_LIB_ROOT}/mem-alloc/bh_memory.c \
${SHARED_LIB_ROOT}/mem-alloc/mem_alloc.c \
${SHARED_LIB_ROOT}/mem-alloc/ems/ems_kfc.c \

View File

@ -16,9 +16,9 @@
#include <stdlib.h>
#include <string.h>
#include "bh_platform.h"
#include "wasm_assert.h"
#include "wasm_log.h"
#include "wasm_platform.h"
#include "wasm_platform_log.h"
#include "wasm_thread.h"
#include "wasm_export.h"
@ -29,6 +29,20 @@
static int app_argc;
static char **app_argv;
/**
* Find the unique main function from a WASM module instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the main function is called, false otherwise.
*/
bool
wasm_application_execute_main(wasm_module_inst_t module_inst,
int argc, char *argv[]);
static void*
app_instance_main(wasm_module_inst_t module_inst)
{

View File

@ -0,0 +1,107 @@
# 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.
cmake_minimum_required (VERSION 2.8)
project (iwasm)
set (PLATFORM "darwin")
# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# Enable repl mode if want to test spec cases
# add_definitions(-DWASM_ENABLE_REPL)
if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
add_definitions(-DNVALGRIND)
endif ()
# Currently build as 64-bit by default.
set (BUILD_AS_64BIT_SUPPORT "YES")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (${BUILD_AS_64BIT_SUPPORT} STREQUAL "YES")
# Add -fPIC flag if build as 64-bit
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -fPIC")
else ()
add_definitions (-m32)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
endif ()
endif ()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif (NOT CMAKE_BUILD_TYPE)
message ("CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")
set (CMAKE_MACOSX_RPATH True)
set (SHARED_LIB_DIR ../../../shared-lib)
include_directories (.
../../runtime/include
../../runtime/platform/include
${SHARED_LIB_DIR}/include)
enable_language (ASM)
include (../../runtime/platform/${PLATFORM}/platform.cmake)
include (../../runtime/utils/utils.cmake)
include (../../runtime/vmcore-wasm/vmcore.cmake)
include (../../lib/native/base/wasm_lib_base.cmake)
include (../../lib/native/libc/wasm_libc.cmake)
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
include (${SHARED_LIB_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_LIB_DIR}/utils/shared_utils.cmake)
add_library (vmlib
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
add_executable (iwasm main.c ext_lib_export.c)
install (TARGETS iwasm DESTINATION bin)
target_link_libraries (iwasm vmlib -lm -ldl -lpthread)
add_library (libiwasm SHARED
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE}
ext_lib_export.c)
install (TARGETS libiwasm DESTINATION lib)
set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
target_link_libraries (libiwasm -lm -ldl -lpthread)

View File

@ -0,0 +1,21 @@
/*
* 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.
*/
#include "lib_export.h"
static NativeSymbol extended_native_symbol_defs[] = { };
#include "ext_lib_export.h"

View File

@ -0,0 +1,251 @@
/*
* 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 _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bh_platform.h"
#include "wasm_application.h"
#include "wasm_assert.h"
#include "wasm_log.h"
#include "wasm_platform_log.h"
#include "wasm_thread.h"
#include "wasm_export.h"
#include "wasm_memory.h"
#include "bh_memory.h"
static int app_argc;
static char **app_argv;
static int print_help()
{
wasm_printf("Usage: iwasm [-options] wasm_file [args...]\n");
wasm_printf("options:\n");
wasm_printf(" -f|--function name Specify function name to run in module\n"
" rather than main\n");
#if WASM_ENABLE_LOG != 0
wasm_printf(" -v=X Set log verbose level (0 to 2, default is 1),\n"
" larger level with more log\n");
#endif
wasm_printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
" that runs commands in the form of `FUNC ARG...`\n");
return 1;
}
static void*
app_instance_main(wasm_module_inst_t module_inst)
{
const char *exception;
wasm_application_execute_main(module_inst, app_argc, app_argv);
if ((exception = wasm_runtime_get_exception(module_inst)))
wasm_printf("%s\n", exception);
return NULL;
}
static void*
app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
{
const char *exception;
wasm_application_execute_func(module_inst, func_name, app_argc - 1,
app_argv + 1);
if ((exception = wasm_runtime_get_exception(module_inst)))
wasm_printf("%s\n", exception);
return NULL;
}
/**
* Split a space separated strings into an array of strings
* Returns NULL on failure
* Memory must be freed by caller
* Based on: http://stackoverflow.com/a/11198630/471795
*/
static char **
split_string(char *str, int *count)
{
char **res = NULL;
char *p;
int idx = 0;
/* split string and append tokens to 'res' */
do {
p = strtok(str, " ");
str = NULL;
res = (char**) realloc(res, sizeof(char*) * (idx + 1));
if (res == NULL) {
return NULL;
}
res[idx++] = p;
} while (p);
if (count) {
*count = idx - 1;
}
return res;
}
static void*
app_instance_repl(wasm_module_inst_t module_inst)
{
char *cmd = NULL;
size_t len = 0;
ssize_t n;
while ((wasm_printf("webassembly> "), n = getline(&cmd, &len, stdin)) != -1) {
wasm_assert(n > 0);
if (cmd[n - 1] == '\n') {
if (n == 1)
continue;
else
cmd[n - 1] = '\0';
}
app_argv = split_string(cmd, &app_argc);
if (app_argv == NULL) {
LOG_ERROR("Wasm prepare param failed: split string failed.\n");
break;
}
if (app_argc != 0) {
wasm_application_execute_func(module_inst, app_argv[0],
app_argc - 1, app_argv + 1);
}
free(app_argv);
}
free(cmd);
return NULL;
}
#define USE_GLOBAL_HEAP_BUF 0
#if USE_GLOBAL_HEAP_BUF != 0
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
#endif
int main(int argc, char *argv[])
{
char *wasm_file = NULL;
const char *func_name = NULL;
uint8 *wasm_file_buf = NULL;
int wasm_file_size;
wasm_module_t wasm_module = NULL;
wasm_module_inst_t wasm_module_inst = NULL;
char error_buf[128];
#if WASM_ENABLE_LOG != 0
int log_verbose_level = 1;
#endif
bool is_repl_mode = false;
/* Process options. */
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
argc--, argv++;
if (argc < 2) {
print_help();
return 0;
}
func_name = argv[0];
}
#if WASM_ENABLE_LOG != 0
else if (!strncmp(argv[0], "-v=", 3)) {
log_verbose_level = atoi(argv[0] + 3);
if (log_verbose_level < 0 || log_verbose_level > 2)
return print_help();
}
#endif
else if (!strcmp(argv[0], "--repl"))
is_repl_mode = true;
else
return print_help();
}
if (argc == 0)
return print_help();
wasm_file = argv[0];
app_argc = argc;
app_argv = argv;
#if USE_GLOBAL_HEAP_BUF != 0
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
!= 0) {
wasm_printf("Init memory with global heap buffer failed.\n");
return -1;
}
#else
if (bh_memory_init_with_allocator(malloc, free)) {
wasm_printf("Init memory with memory allocator failed.\n");
return -1;
}
#endif
/* initialize runtime environment */
if (!wasm_runtime_init())
goto fail1;
wasm_log_set_verbose_level(log_verbose_level);
/* load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf = (uint8*) bh_read_file_to_buffer(wasm_file,
&wasm_file_size)))
goto fail2;
/* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail3;
}
/* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
64 * 1024, /* stack size */
64 * 1024, /* heap size */
error_buf,
sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail4;
}
if (is_repl_mode)
app_instance_repl(wasm_module_inst);
else if (func_name)
app_instance_func(wasm_module_inst, func_name);
else
app_instance_main(wasm_module_inst);
/* destroy the module instance */
wasm_runtime_deinstantiate(wasm_module_inst);
fail4:
/* unload the module */
wasm_runtime_unload(wasm_module);
fail3:
/* free the file buffer */
wasm_free(wasm_file_buf);
fail2:
/* destroy runtime environment */
wasm_runtime_destroy();
fail1:
bh_memory_destroy();
return 0;
}

View File

@ -0,0 +1,90 @@
# 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.
cmake_minimum_required (VERSION 2.8)
project (iwasm)
set (PLATFORM "linux-sgx")
# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
add_definitions(-DUSE_SGX=1)
add_definitions(-DOPS_INPUT_OUTPUT=1)
add_definitions(-DOPS_UNSAFE_BUFFERS=0)
add_definitions(-DWASM_ENABLE_LOG=0)
add_definitions(-Dbh_printf=bh_printf_sgx)
# Enable repl mode if want to test spec cases
# add_definitions(-DWASM_ENABLE_REPL)
if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
add_definitions(-DNVALGRIND)
endif ()
# Currently build as 64-bit by default.
set (BUILD_AS_64BIT_SUPPORT "YES")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (${BUILD_AS_64BIT_SUPPORT} STREQUAL "YES")
# Add -fPIC flag if build as 64-bit
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -fPIC")
else ()
add_definitions (-m32)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
endif ()
endif ()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif (NOT CMAKE_BUILD_TYPE)
message ("CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")
set (SHARED_LIB_DIR ../../../shared-lib)
include_directories (.
../../runtime/include
../../runtime/platform/include
${SHARED_LIB_DIR}/include
$ENV{SGX_SDK}/include)
enable_language (ASM)
include (../../runtime/platform/${PLATFORM}/platform.cmake)
include (../../runtime/utils/utils.cmake)
include (../../runtime/vmcore-wasm/vmcore.cmake)
include (../../lib/native/base/wasm_lib_base.cmake)
include (../../lib/native/libc/wasm_libc.cmake)
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
include (${SHARED_LIB_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_LIB_DIR}/utils/shared_utils.cmake)
add_library (vmlib
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
add_library (extlib ext_lib_export.c)

View File

@ -0,0 +1,21 @@
/*
* 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.
*/
#include "lib_export.h"
static NativeSymbol extended_native_symbol_defs[] = { };
#include "ext_lib_export.h"

View File

@ -68,6 +68,7 @@ include (../../lib/native/base/wasm_lib_base.cmake)
include (../../lib/native/libc/wasm_libc.cmake)
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
include (${SHARED_LIB_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_LIB_DIR}/utils/shared_utils.cmake)
add_library (vmlib
${WASM_PLATFORM_LIB_SOURCE}
@ -76,7 +77,8 @@ add_library (vmlib
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE})
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
add_executable (iwasm main.c ext_lib_export.c)
@ -91,7 +93,8 @@ add_library (libiwasm SHARED
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE})
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
install (TARGETS libiwasm DESTINATION lib)

View File

@ -19,9 +19,9 @@
#endif
#include <stdlib.h>
#include <string.h>
#include "bh_platform.h"
#include "wasm_assert.h"
#include "wasm_log.h"
#include "wasm_platform.h"
#include "wasm_platform_log.h"
#include "wasm_thread.h"
#include "wasm_export.h"
@ -46,6 +46,35 @@ static int print_help()
return 1;
}
/**
* Find the unique main function from a WASM module instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the main function is called, false otherwise.
*/
bool
wasm_application_execute_main(wasm_module_inst_t module_inst,
int argc, char *argv[]);
/**
* Find the specified function in argv[0] from WASM module of current instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param name the name of the function to execute
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the specified function is called, false otherwise.
*/
bool
wasm_application_execute_func(wasm_module_inst_t module_inst,
const char *name, int argc, char *argv[]);
static void*
app_instance_main(wasm_module_inst_t module_inst)
{
@ -129,7 +158,11 @@ app_instance_repl(wasm_module_inst_t module_inst)
return NULL;
}
static char global_heap_buf[512 * 1024] = { 0 };
#define USE_GLOBAL_HEAP_BUF 0
#if USE_GLOBAL_HEAP_BUF != 0
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
#endif
int main(int argc, char *argv[])
{
@ -175,11 +208,18 @@ int main(int argc, char *argv[])
app_argc = argc;
app_argv = argv;
#if USE_GLOBAL_HEAP_BUF != 0
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
!= 0) {
wasm_printf("Init global heap failed.\n");
wasm_printf("Init memory with global heap buffer failed.\n");
return -1;
}
#else
if (bh_memory_init_with_allocator(malloc, free)) {
wasm_printf("Init memory with memory allocator failed.\n");
return -1;
}
#endif
/* initialize runtime environment */
if (!wasm_runtime_init())
@ -188,7 +228,7 @@ int main(int argc, char *argv[])
wasm_log_set_verbose_level(log_verbose_level);
/* load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf = (uint8*) wasm_read_file_to_buffer(wasm_file,
if (!(wasm_file_buf = (uint8*) bh_read_file_to_buffer(wasm_file,
&wasm_file_size)))
goto fail2;
@ -201,8 +241,8 @@ int main(int argc, char *argv[])
/* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
16 * 1024, /* stack size */
8 * 1024, /* heap size */
64 * 1024, /* stack size */
64 * 1024, /* heap size */
error_buf,
sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);

View File

@ -0,0 +1,107 @@
# 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.
cmake_minimum_required (VERSION 2.8)
project (iwasm)
set (PLATFORM "vxworks")
# Specify the compiler driver provided in the VSB
SET(CMAKE_C_COMPILER vx-cc)
SET(CMAKE_AR vx-ar)
SET(CMAKE_RANLIB vx-ranlib)
# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# Enable repl mode if want to test spec cases
# add_definitions(-DWASM_ENABLE_REPL)
if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
add_definitions(-DNVALGRIND)
endif ()
# Currently build as 64-bit by default.
set (BUILD_AS_64BIT_SUPPORT "YES")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (${BUILD_AS_64BIT_SUPPORT} STREQUAL "YES")
# Add -fPIC flag if build as 64-bit
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -fPIC")
else ()
add_definitions (-m32)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
endif ()
endif ()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif (NOT CMAKE_BUILD_TYPE)
message ("CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")
set (SHARED_LIB_DIR ../../../shared-lib)
include_directories (.
../../runtime/include
../../runtime/platform/include
${SHARED_LIB_DIR}/include)
include (../../runtime/platform/${PLATFORM}/platform.cmake)
include (../../runtime/utils/utils.cmake)
include (../../runtime/vmcore-wasm/vmcore.cmake)
include (../../lib/native/base/wasm_lib_base.cmake)
include (../../lib/native/libc/wasm_libc.cmake)
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
include (${SHARED_LIB_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_LIB_DIR}/utils/shared_utils.cmake)
add_library (vmlib
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
add_executable (iwasm main.c ext_lib_export.c)
install (TARGETS iwasm DESTINATION bin)
target_link_libraries (iwasm vmlib -lm -ldl -lunix)
add_library (libiwasm SHARED
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
install (TARGETS libiwasm DESTINATION lib)
set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
target_link_libraries (libiwasm -lm -ldl -lunix)

View File

@ -0,0 +1,21 @@
/*
* 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.
*/
#include "lib_export.h"
static NativeSymbol extended_native_symbol_defs[] = { };
#include "ext_lib_export.h"

View File

@ -0,0 +1,267 @@
/*
* 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 _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <string.h>
#include "wasm_assert.h"
#include "wasm_log.h"
#include "wasm_platform.h"
#include "wasm_platform_log.h"
#include "wasm_thread.h"
#include "wasm_export.h"
#include "wasm_memory.h"
#include "bh_memory.h"
static int app_argc;
static char **app_argv;
static int print_help()
{
wasm_printf("Usage: iwasm [-options] wasm_file [args...]\n");
wasm_printf("options:\n");
wasm_printf(" -f|--function name Specify function name to run in module\n"
" rather than main\n");
#if WASM_ENABLE_LOG != 0
wasm_printf(" -v=X Set log verbose level (0 to 2, default is 1),\n"
" larger level with more log\n");
#endif
wasm_printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
" that runs commands in the form of `FUNC ARG...`\n");
return 1;
}
/**
* Find the unique main function from a WASM module instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the main function is called, false otherwise.
*/
bool
wasm_application_execute_main(wasm_module_inst_t module_inst,
int argc, char *argv[]);
/**
* Find the specified function in argv[0] from WASM module of current instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param name the name of the function to execute
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the specified function is called, false otherwise.
*/
bool
wasm_application_execute_func(wasm_module_inst_t module_inst,
const char *name, int argc, char *argv[]);
static void*
app_instance_main(wasm_module_inst_t module_inst)
{
const char *exception;
wasm_application_execute_main(module_inst, app_argc, app_argv);
if ((exception = wasm_runtime_get_exception(module_inst)))
wasm_printf("%s\n", exception);
return NULL;
}
static void*
app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
{
const char *exception;
wasm_application_execute_func(module_inst, func_name, app_argc - 1,
app_argv + 1);
if ((exception = wasm_runtime_get_exception(module_inst)))
wasm_printf("%s\n", exception);
return NULL;
}
/**
* Split a space separated strings into an array of strings
* Returns NULL on failure
* Memory must be freed by caller
* Based on: http://stackoverflow.com/a/11198630/471795
*/
static char **
split_string(char *str, int *count)
{
char **res = NULL;
char *p;
int idx = 0;
/* split string and append tokens to 'res' */
do {
p = strtok(str, " ");
str = NULL;
res = (char**) realloc(res, sizeof(char*) * (idx + 1));
if (res == NULL) {
return NULL;
}
res[idx++] = p;
} while (p);
if (count) {
*count = idx - 1;
}
return res;
}
static void*
app_instance_repl(wasm_module_inst_t module_inst)
{
char *cmd = NULL;
size_t len = 0;
ssize_t n;
while ((wasm_printf("webassembly> "), n = getline(&cmd, &len, stdin)) != -1) {
wasm_assert(n > 0);
if (cmd[n - 1] == '\n') {
if (n == 1)
continue;
else
cmd[n - 1] = '\0';
}
app_argv = split_string(cmd, &app_argc);
if (app_argv == NULL) {
LOG_ERROR("Wasm prepare param failed: split string failed.\n");
break;
}
if (app_argc != 0) {
wasm_application_execute_func(module_inst, app_argv[0],
app_argc - 1, app_argv + 1);
}
free(app_argv);
}
free(cmd);
return NULL;
}
static char global_heap_buf[512 * 1024] = { 0 };
int main(int argc, char *argv[])
{
char *wasm_file = NULL;
const char *func_name = NULL;
uint8 *wasm_file_buf = NULL;
int wasm_file_size;
wasm_module_t wasm_module = NULL;
wasm_module_inst_t wasm_module_inst = NULL;
char error_buf[128];
#if WASM_ENABLE_LOG != 0
int log_verbose_level = 1;
#endif
bool is_repl_mode = false;
/* Process options. */
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
argc--, argv++;
if (argc < 2) {
print_help();
return 0;
}
func_name = argv[0];
}
#if WASM_ENABLE_LOG != 0
else if (!strncmp(argv[0], "-v=", 3)) {
log_verbose_level = atoi(argv[0] + 3);
if (log_verbose_level < 0 || log_verbose_level > 2)
return print_help();
}
#endif
else if (!strcmp(argv[0], "--repl"))
is_repl_mode = true;
else
return print_help();
}
if (argc == 0)
return print_help();
wasm_file = argv[0];
app_argc = argc;
app_argv = argv;
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
!= 0) {
wasm_printf("Init global heap failed.\n");
return -1;
}
/* initialize runtime environment */
if (!wasm_runtime_init())
goto fail1;
wasm_log_set_verbose_level(log_verbose_level);
/* load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf = (uint8*) bh_read_file_to_buffer(wasm_file,
&wasm_file_size)))
goto fail2;
/* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail3;
}
/* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
16 * 1024, /* stack size */
8 * 1024, /* heap size */
error_buf,
sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail4;
}
if (is_repl_mode)
app_instance_repl(wasm_module_inst);
else if (func_name)
app_instance_func(wasm_module_inst, func_name);
else
app_instance_main(wasm_module_inst);
/* destroy the module instance */
wasm_runtime_deinstantiate(wasm_module_inst);
fail4:
/* unload the module */
wasm_runtime_unload(wasm_module);
fail3:
/* free the file buffer */
wasm_free(wasm_file_buf);
fail2:
/* destroy runtime environment */
wasm_runtime_destroy();
fail1:
bh_memory_destroy();
return 0;
}

View File

@ -35,8 +35,6 @@ include_directories (${IWASM_ROOT}/runtime/include
set (IWASM_SRCS ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c
${IWASM_ROOT}/runtime/utils/wasm_log.c
${IWASM_ROOT}/runtime/utils/wasm_dlfcn.c
${IWASM_ROOT}/runtime/platform/zephyr/wasm_math.c
${IWASM_ROOT}/runtime/platform/zephyr/wasm_platform.c
${IWASM_ROOT}/runtime/platform/zephyr/wasm_native.c
${IWASM_ROOT}/runtime/vmcore-wasm/wasm_application.c
${IWASM_ROOT}/runtime/vmcore-wasm/wasm_interp.c
@ -48,6 +46,7 @@ set (IWASM_SRCS ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c
${SHARED_LIB_ROOT}/platform/zephyr/bh_platform.c
${SHARED_LIB_ROOT}/platform/zephyr/bh_assert.c
${SHARED_LIB_ROOT}/platform/zephyr/bh_thread.c
${SHARED_LIB_ROOT}/platform/zephyr/bh_math.c
${SHARED_LIB_ROOT}/mem-alloc/bh_memory.c
${SHARED_LIB_ROOT}/mem-alloc/mem_alloc.c
${SHARED_LIB_ROOT}/mem-alloc/ems/ems_kfc.c

View File

@ -16,9 +16,9 @@
#include <stdlib.h>
#include <string.h>
#include "bh_platform.h"
#include "wasm_assert.h"
#include "wasm_log.h"
#include "wasm_platform.h"
#include "wasm_platform_log.h"
#include "wasm_thread.h"
#include "wasm_export.h"
@ -26,9 +26,28 @@
#include "bh_memory.h"
#include "test_wasm.h"
#define CONFIG_GLOBAL_HEAP_BUF_SIZE 131072
#define CONFIG_APP_STACK_SIZE 8192
#define CONFIG_APP_HEAP_SIZE 8192
#define CONFIG_MAIN_THREAD_STACK_SIZE 4096
static int app_argc;
static char **app_argv;
/**
* Find the unique main function from a WASM module instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the main function is called, false otherwise.
*/
bool
wasm_application_execute_main(wasm_module_inst_t module_inst,
int argc, char *argv[]);
static void*
app_instance_main(wasm_module_inst_t module_inst)
{
@ -40,7 +59,7 @@ app_instance_main(wasm_module_inst_t module_inst)
return NULL;
}
static char global_heap_buf[512 * 1024] = { 0 };
static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 };
void iwasm_main(void *arg1, void *arg2, void *arg3)
{
@ -83,8 +102,11 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
}
/* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024,
8 * 1024, error_buf, sizeof(error_buf)))) {
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
CONFIG_APP_STACK_SIZE,
CONFIG_APP_HEAP_SIZE,
error_buf,
sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail3;
}
@ -105,24 +127,23 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
fail1: bh_memory_destroy();
}
#define DEFAULT_THREAD_STACKSIZE (6 * 1024)
#define DEFAULT_THREAD_PRIORITY 5
#define MAIN_THREAD_STACK_SIZE (CONFIG_MAIN_THREAD_STACK_SIZE)
#define MAIN_THREAD_PRIORITY 5
K_THREAD_STACK_DEFINE(iwasm_main_thread_stack, DEFAULT_THREAD_STACKSIZE);
K_THREAD_STACK_DEFINE(iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE);
static struct k_thread iwasm_main_thread;
bool iwasm_init(void)
{
k_tid_t tid = k_thread_create(&iwasm_main_thread, iwasm_main_thread_stack,
DEFAULT_THREAD_STACKSIZE, iwasm_main, NULL, NULL, NULL,
DEFAULT_THREAD_PRIORITY, 0, K_NO_WAIT);
MAIN_THREAD_STACK_SIZE,
iwasm_main, NULL, NULL, NULL,
MAIN_THREAD_PRIORITY, 0, K_NO_WAIT);
return tid ? true : false;
}
#ifndef CONFIG_AEE_ENABLE
void main(void)
{
iwasm_init();
}
#endif

View File

@ -0,0 +1,110 @@
/*
* 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 _BH_MEMORY_H
#define _BH_MEMORY_H
#ifdef __cplusplus
extern "C" {
#endif
#define BH_KB (1024)
#define BH_MB ((BH_KB)*1024)
#define BH_GB ((BH_MB)*1024)
/**
* Initialize memory allocator with a pool, the bh_malloc/bh_free function
* will malloc/free memory from the pool
*
* @param mem the pool buffer
* @param bytes the size bytes of the buffer
*
* @return 0 if success, -1 otherwise
*/
int bh_memory_init_with_pool(void *mem, unsigned int bytes);
/**
* Initialize memory allocator with memory allocator, the bh_malloc/bh_free
* function will malloc/free memory with the allocator passed
*
* @param malloc_func the malloc function
* @param free_func the free function
*
* @return 0 if success, -1 otherwise
*/
int bh_memory_init_with_allocator(void *malloc_func, void *free_func);
/**
* Destroy memory
*/
void bh_memory_destroy();
/**
* Get the pool size of memory, if memory is initialized with allocator,
* return 1GB by default.
*/
int bh_memory_pool_size();
#if BEIHAI_ENABLE_MEMORY_PROFILING == 0
/**
* This function allocates a memory chunk from system
*
* @param size bytes need allocate
*
* @return the pointer to memory allocated
*/
void* bh_malloc(unsigned int size);
/**
* This function frees memory chunk
*
* @param ptr the pointer to memory need free
*/
void bh_free(void *ptr);
#else
void* bh_malloc_profile(const char *file, int line, const char *func, unsigned int size);
void bh_free_profile(const char *file, int line, const char *func, void *ptr);
#define bh_malloc(size) bh_malloc_profile(__FILE__, __LINE__, __func__, size)
#define bh_free(ptr) bh_free_profile(__FILE__, __LINE__, __func__, ptr)
/**
* Print current memory profiling data
*
* @param file file name of the caller
* @param line line of the file of the caller
* @param func function name of the caller
*/
void memory_profile_print(const char *file, int line, const char *func, int alloc);
/**
* Summarize memory usage and print it out
* Can use awk to analyze the output like below:
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
*/
void memory_usage_summarize();
#endif
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _BH_MEMORY_H */

View File

@ -40,7 +40,13 @@ int
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
/**
* Get the exported APIs of extend lib
* Get the exported APIs of extended lib, this API isn't provided by WASM VM,
* it must be provided by developer to register the extended native APIs,
* for example, developer can register his native APIs to extended_native_symbol_defs,
* array, and include file ext_lib_export.h which implements this API.
* And if developer hasn't any native API to register, he can define an empty
* extended_native_symbol_defs array, and then include file ext_lib_export.h to
* implements this API.
*
* @param p_base_lib_apis return the exported API array of extend lib
*

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2019 Taobao (China) Inc. 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_APPLICATION_H
#define _WASM_APPLICATION_H
//#include "wasm_runtime.h"
#ifdef __cplusplus
extern "C" {
#endif
struct WASMModuleInstance;
/**
* Find the unique main function from a WASM module instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the main function is called, false otherwise and exception will be thrown,
* the caller can call wasm_runtime_get_exception to get exception info.
*/
bool
wasm_application_execute_main(struct WASMModuleInstance *module_inst,
int argc, char *argv[]);
/**
* Find the specified function in argv[0] from a WASM module instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param name the name of the function to execute
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the specified function is called, false otherwise and exception will be thrown,
* the caller can call wasm_runtime_get_exception to get exception info.
*/
bool
wasm_application_execute_func(struct WASMModuleInstance *module_inst,
char *name, int argc, char *argv[]);
#ifdef __cplusplus
}
#endif
#endif /* end of _WASM_APPLICATION_H */

View File

@ -14,27 +14,19 @@
* limitations under the License.
*/
#include "wasm_platform.h"
#ifndef _WASM_DLFCN_H
#define _WASM_DLFCN_H
bool is_little_endian = false;
#ifdef __cplusplus
extern "C" {
#endif
bool __is_little_endian()
{
union w
{
int a;
char b;
}c;
void *
wasm_dlsym(void *handle, const char *symbol);
c.a = 1;
return (c.b == 1);
#ifdef __cplusplus
}
#endif
int wasm_platform_init()
{
if (__is_little_endian())
is_little_endian = true;
return 0;
}
#endif /* end of _WASM_DLFCN_H */

View File

@ -152,6 +152,13 @@ wasm_runtime_instantiate(const wasm_module_t module,
void
wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
bool
wasm_runtime_set_ext_memory(wasm_module_inst_t module_inst,
uint8_t *ext_mem_data, uint32_t ext_mem_size,
char *error_buf, uint32_t error_buf_size);
#endif
/**
* Load WASM module instance from AOT file.
*
@ -385,34 +392,36 @@ wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
void *native_ptr);
/**
* Find the unique main function from a WASM module instance
* and execute that function.
* Get the app address range (relative address) that a app address belongs to
*
* @param module_inst the WASM module instance
* @param argc the number of arguments
* @param argv the arguments array
* @param app_offset the app address to retrieve
* @param p_app_start_offset buffer to output the app start offset if not NULL
* @param p_app_end_offset buffer to output the app end offset if not NULL
*
* @return true if the main function is called, false otherwise.
* @return true if success, false otherwise.
*/
bool
wasm_application_execute_main(wasm_module_inst_t module_inst,
int argc, char *argv[]);
wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
int32_t app_offset,
int32_t *p_app_start_offset,
int32_t *p_app_end_offset);
/**
* Find the specified function in argv[0] from WASM module of current instance
* and execute that function.
* Get the native address range (absolute address) that a native address belongs to
*
* @param module_inst the WASM module instance
* @param name the name of the function to execute
* @param argc the number of arguments
* @param argv the arguments array
* @param native_ptr the native address to retrieve
* @param p_native_start_addr buffer to output the native start address if not NULL
* @param p_native_end_addr buffer to output the native end address if not NULL
*
* @return true if the specified function is called, false otherwise.
* @return true if success, false otherwise.
*/
bool
wasm_application_execute_func(wasm_module_inst_t module_inst,
const char *name, int argc, char *argv[]);
wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
uint8_t *native_ptr,
uint8_t **p_native_start_addr,
uint8_t **p_native_end_addr);
#ifdef __cplusplus
}

View File

@ -17,7 +17,7 @@
#ifndef WASM_HASHMAP_H
#define WASM_HASHMAP_H
#include "wasm_platform.h"
#include "bh_platform.h"
#ifdef __cplusplus

View File

@ -30,7 +30,7 @@
#ifndef _WASM_LOG_H
#define _WASM_LOG_H
#include "wasm_platform.h"
#include "bh_platform.h"
#ifdef __cplusplus

View File

@ -17,7 +17,7 @@
#ifndef _WASM_VECTOR_H
#define _WASM_VECTOR_H
#include "wasm_platform.h"
#include "bh_platform.h"
#ifdef __cplusplus

View File

@ -1,104 +0,0 @@
/*
* 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_PLATFORM_H
#define _WASM_PLATFORM_H
#include "wasm_config.h"
#include "wasm_types.h"
#include <aos/kernel.h>
#include <inttypes.h>
#include <stdbool.h>
typedef uint64_t uint64;
typedef int64_t int64;
typedef float float32;
typedef double float64;
#ifndef NULL
# define NULL ((void*) 0)
#endif
#define WASM_PLATFORM "AliOS"
#define __ALIOS__ 1
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
/**
* Return the offset of the given field in the given type.
*
* @param Type the type containing the filed
* @param field the field in the type
*
* @return the offset of field in Type
*/
#ifndef offsetof
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
#endif
typedef aos_task_t korp_thread;
typedef korp_thread *korp_tid;
typedef aos_mutex_t korp_mutex;
int wasm_platform_init();
extern bool is_little_endian;
#include <string.h>
/* The following operations declared in string.h may be defined as
macros on Linux, so don't declare them as functions here. */
/* memset */
/* memcpy */
/* memmove */
/* #include <stdio.h> */
/* Unit test framework is based on C++, where the declaration of
snprintf is different. */
#ifndef __cplusplus
int snprintf(char *buffer, size_t count, const char *format, ...);
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* math functions */
double sqrt(double x);
double floor(double x);
double ceil(double x);
double fmin(double x, double y);
double fmax(double x, double y);
double rint(double x);
double fabs(double x);
double trunc(double x);
int signbit(double x);
int isnan(double x);
void*
wasm_dlsym(void *handle, const char *symbol);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,25 @@
# 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.
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L)
set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${PLATFORM_LIB_DIR})
include_directories(${PLATFORM_LIB_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c)
set (WASM_PLATFORM_LIB_SOURCE ${source_all})

View File

@ -0,0 +1,25 @@
/*
* 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.
*/
#include "wasm_native.h"
void*
wasm_platform_native_func_lookup(const char *module_name,
const char *func_name)
{
return NULL;
}

View File

@ -17,7 +17,9 @@
#ifndef _WASM_PLATFORM_LOG
#define _WASM_PLATFORM_LOG
#define wasm_printf printf
#include "bh_platform.h"
#define wasm_printf bh_printf
#define wasm_vprintf vprintf

View File

@ -0,0 +1,25 @@
# 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.
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L)
set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${PLATFORM_LIB_DIR})
include_directories(${PLATFORM_LIB_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c)
set (WASM_PLATFORM_LIB_SOURCE ${source_all})

View File

@ -0,0 +1,332 @@
/*
* 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 _GNU_SOURCE
#define _GNU_SOURCE /* for O_DIRECT */
#endif
#include "wasm_native.h"
#include "wasm_runtime.h"
#include "wasm_log.h"
#include "wasm_memory.h"
#include "wasm_platform_log.h"
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <fcntl.h>
#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)
#define addr_app_to_native(offset) \
wasm_runtime_addr_app_to_native(module_inst, offset)
#define addr_native_to_app(ptr) \
wasm_runtime_addr_native_to_app(module_inst, ptr)
#define module_malloc(size) \
wasm_runtime_module_malloc(module_inst, size)
#define module_free(offset) \
wasm_runtime_module_free(module_inst, offset)
static int32
__syscall0_wrapper(int32 arg0)
{
switch (arg0) {
case 199: /* getuid */
/* TODO */
default:
bh_printf("##_syscall0 called, syscall id: %d\n", arg0);
}
return 0;
}
static int32
__syscall1_wrapper(int32 arg0, int32 arg1)
{
switch (arg0) {
case 6: /* close */
/* TODO */
default:
bh_printf("##_syscall1 called, syscall id: %d\n", arg0);
}
return 0;
}
static int32
__syscall2_wrapper(int32 arg0, int32 arg1, int32 arg2)
{
switch (arg0) {
case 183: /* getcwd */
/* TODO */
default:
bh_printf("##_syscall2 called, syscall id: %d\n", arg0);
}
return 0;
}
static int32
__syscall3_wrapper(int32 arg0, int32 arg1, int32 arg2, int32 arg3)
{
WASMModuleInstance *module_inst = get_module_inst();
switch (arg0) {
case 146: /* writev */
{
/* Implement syscall 54 and syscall 146 to support printf()
for non SIDE_MODULE=1 mode */
struct iovec_app {
int32 iov_base_offset;
uint32 iov_len;
} *vec;
int32 vec_offset = arg2, str_offset;
uint32 iov_count = arg3, i;
int32 count = 0;
char *iov_base, *str;
if (!validate_app_addr(vec_offset, sizeof(struct iovec_app)))
return 0;
vec = (struct iovec_app *)addr_app_to_native(vec_offset);
for (i = 0; i < iov_count; i++, vec++) {
if (vec->iov_len > 0) {
if (!validate_app_addr(vec->iov_base_offset, 1))
return 0;
iov_base = (char*)addr_app_to_native(vec->iov_base_offset);
if (!(str_offset = module_malloc(vec->iov_len + 1)))
return 0;
str = addr_app_to_native(str_offset);
memcpy(str, iov_base, vec->iov_len);
str[vec->iov_len] = '\0';
count += wasm_printf("%s", str);
module_free(str_offset);
}
}
return count;
}
case 145: /* readv */
case 3: /* read*/
case 5: /* open */
case 221: /* fcntl */
/* TODO */
default:
bh_printf("##_syscall3 called, syscall id: %d\n", arg0);
}
return 0;
}
static int32
__syscall4_wrapper(int32 arg0, int32 arg1, int32 arg2,
int32 arg3, int32 arg4)
{
bh_printf("##_syscall4 called, syscall id: %d\n", arg0);
return 0;
}
static int32
__syscall5_wrapper(int32 arg0, int32 arg1, int32 arg2,
int32 arg3, int32 arg4, int32 arg5)
{
switch (arg0) {
case 140: /* llseek */
/* TODO */
default:
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_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_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_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_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_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]); \
}
EMCC_SYSCALL_WRAPPER0(199)
EMCC_SYSCALL_WRAPPER1(6)
EMCC_SYSCALL_WRAPPER2(183)
EMCC_SYSCALL_WRAPPER3(3)
EMCC_SYSCALL_WRAPPER3(5)
EMCC_SYSCALL_WRAPPER3(54)
EMCC_SYSCALL_WRAPPER3(145)
EMCC_SYSCALL_WRAPPER3(146)
EMCC_SYSCALL_WRAPPER3(221)
EMCC_SYSCALL_WRAPPER5(140)
static int32
getTotalMemory_wrapper()
{
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()
{
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);
uint32 *DYNAMICTOP_PTR = (uint32*)(memory->memory_data + addr_data_offset);
uint32 memory_size_expected = *DYNAMICTOP_PTR;
uint32 total_page_count = (memory_size_expected + NumBytesPerPage - 1) / NumBytesPerPage;
if (total_page_count < memory->cur_page_count) {
return 1;
}
else {
ret = wasm_runtime_enlarge_memory(module_inst, total_page_count -
memory->cur_page_count);
return ret ? 1 : 0;
}
}
static void
_abort_wrapper(int32 code)
{
WASMModuleInstance *module_inst = wasm_runtime_get_current_module_inst();
char buf[32];
snprintf(buf, sizeof(buf), "env.abort(%i)", code);
wasm_runtime_set_exception(module_inst, buf);
}
static void
abortOnCannotGrowMemory_wrapper()
{
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)
{
errno = error_no;
}
/* TODO: add function parameter/result types check */
#define REG_NATIVE_FUNC(module_name, func_name) \
{#module_name, #func_name, func_name##_wrapper}
typedef struct WASMNativeFuncDef {
const char *module_name;
const char *func_name;
void *func_ptr;
} WASMNativeFuncDef;
static WASMNativeFuncDef native_func_defs[] = {
REG_NATIVE_FUNC(env, __syscall0),
REG_NATIVE_FUNC(env, __syscall1),
REG_NATIVE_FUNC(env, __syscall2),
REG_NATIVE_FUNC(env, __syscall3),
REG_NATIVE_FUNC(env, __syscall4),
REG_NATIVE_FUNC(env, __syscall5),
REG_NATIVE_FUNC(env, ___syscall3),
REG_NATIVE_FUNC(env, ___syscall5),
REG_NATIVE_FUNC(env, ___syscall6),
REG_NATIVE_FUNC(env, ___syscall54),
REG_NATIVE_FUNC(env, ___syscall140),
REG_NATIVE_FUNC(env, ___syscall145),
REG_NATIVE_FUNC(env, ___syscall146),
REG_NATIVE_FUNC(env, ___syscall183),
REG_NATIVE_FUNC(env, ___syscall199),
REG_NATIVE_FUNC(env, ___syscall221),
REG_NATIVE_FUNC(env, _abort),
REG_NATIVE_FUNC(env, abortOnCannotGrowMemory),
REG_NATIVE_FUNC(env, enlargeMemory),
REG_NATIVE_FUNC(env, getTotalMemory),
REG_NATIVE_FUNC(env, ___setErrNo),
};
void*
wasm_platform_native_func_lookup(const char *module_name,
const char *func_name)
{
uint32 size = sizeof(native_func_defs) / sizeof(WASMNativeFuncDef);
WASMNativeFuncDef *func_def = native_func_defs;
WASMNativeFuncDef *func_def_end = func_def + size;
if (!module_name || !func_name)
return NULL;
while (func_def < func_def_end) {
if (!strcmp(func_def->module_name, module_name)
&& !strcmp(func_def->func_name, func_name))
return (void*)(uintptr_t)func_def->func_ptr;
func_def++;
}
return NULL;
}

View File

@ -0,0 +1,25 @@
# 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.
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L)
set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${PLATFORM_LIB_DIR})
include_directories(${PLATFORM_LIB_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c)
set (WASM_PLATFORM_LIB_SOURCE ${source_all})

View File

@ -0,0 +1,26 @@
/*
* 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.
*/
#include "wasm_native.h"
void*
wasm_platform_native_func_lookup(const char *module_name,
const char *func_name)
{
return NULL;
}

View File

@ -1,105 +0,0 @@
/*
* 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_PLATFORM_H
#define _WASM_PLATFORM_H
#include "wasm_config.h"
#include "wasm_types.h"
#include <autoconf.h>
#include <zephyr.h>
#include <kernel.h>
#include <inttypes.h>
#include <stdbool.h>
typedef uint64_t uint64;
typedef int64_t int64;
typedef float float32;
typedef double float64;
#ifndef NULL
# define NULL ((void*) 0)
#endif
#define WASM_PLATFORM "Zephyr"
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
/**
* Return the offset of the given field in the given type.
*
* @param Type the type containing the filed
* @param field the field in the type
*
* @return the offset of field in Type
*/
#ifndef offsetof
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
#endif
typedef struct k_thread korp_thread;
typedef korp_thread *korp_tid;
typedef struct k_mutex korp_mutex;
int wasm_platform_init();
extern bool is_little_endian;
#include <string.h>
/* The following operations declared in string.h may be defined as
macros on Linux, so don't declare them as functions here. */
/* memset */
/* memcpy */
/* memmove */
/* #include <stdio.h> */
/* Unit test framework is based on C++, where the declaration of
snprintf is different. */
#ifndef __cplusplus
int snprintf(char *buffer, size_t count, const char *format, ...);
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* math functions */
double sqrt(double x);
double floor(double x);
double ceil(double x);
double fmin(double x, double y);
double fmax(double x, double y);
double rint(double x);
double fabs(double x);
double trunc(double x);
int signbit(double x);
int isnan(double x);
void*
wasm_dlsym(void *handle, const char *symbol);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "wasm_platform.h"
#include "bh_platform.h"
static bool sort_flag = false;

View File

@ -17,7 +17,7 @@
#ifndef _WASM_H_
#define _WASM_H_
#include "wasm_platform.h"
#include "bh_platform.h"
#include "wasm_hashmap.h"
#include "wasm_assert.h"

View File

@ -18,6 +18,7 @@
#include <stdlib.h>
#include <string.h>
#include "wasm.h"
#include "wasm_application.h"
#include "wasm_interp.h"
#include "wasm_runtime.h"
#include "wasm_thread.h"
@ -160,6 +161,13 @@ union ieee754_double {
} ieee;
};
static union {
int a;
char b;
} __ue = { .a = 1 };
#define is_little_endian() (__ue.b == 1)
bool
wasm_application_execute_func(WASMModuleInstance *module_inst,
char *name, int argc, char *argv[])
@ -222,7 +230,7 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
union ieee754_float u;
sig = strtoul(endptr + 1, &endptr, 0);
u.f = f32;
if (is_little_endian)
if (is_little_endian())
u.ieee.ieee_little_endian.mantissa = sig;
else
u.ieee.ieee_big_endian.mantissa = sig;
@ -245,7 +253,7 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
union ieee754_double ud;
sig = strtoull(endptr + 1, &endptr, 0);
ud.d = u.val;
if (is_little_endian) {
if (is_little_endian()) {
ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
ud.ieee.ieee_little_endian.mantissa1 = sig;
}
@ -286,10 +294,15 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
break;
case VALUE_TYPE_I64:
{
char buf[16];
union { uint64 val; uint32 parts[2]; } u;
u.parts[0] = argv1[0];
u.parts[1] = argv1[1];
wasm_printf("0x%llx:i64", u.val);
if (sizeof(long) == 4)
snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
else
snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
wasm_printf(buf, u.val);
break;
}
case VALUE_TYPE_F32:

View File

@ -74,6 +74,24 @@ GET_F64_FROM_ADDR (uint32 *addr)
}
#endif /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
#define CHECK_EXT_MEMORY_SPACE() \
else if (module->ext_mem_data \
&& module->ext_mem_base_offset <= offset1 \
&& offset1 < module->ext_mem_base_offset \
+ module->ext_mem_size) { \
maddr = module->ext_mem_data \
+ (offset1 - module->ext_mem_base_offset); \
if (maddr < module->ext_mem_data) \
goto out_of_bounds; \
maddr1 = maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD]; \
if (maddr1 > module->ext_mem_data_end) \
goto out_of_bounds; \
}
#else
#define CHECK_EXT_MEMORY_SPACE()
#endif
#define CHECK_MEMORY_OVERFLOW() do { \
uint32 offset1 = offset + addr; \
uint8 *maddr1; \
@ -89,7 +107,8 @@ GET_F64_FROM_ADDR (uint32 *addr)
if (maddr1 > memory->end_addr) \
goto out_of_bounds; \
} \
else { \
else if (offset1 < memory->heap_base_offset \
+ (memory->heap_data_end - memory->heap_data)) { \
maddr = memory->heap_data + offset1 - memory->heap_base_offset; \
if (maddr < memory->heap_data) \
goto out_of_bounds; \
@ -97,6 +116,9 @@ GET_F64_FROM_ADDR (uint32 *addr)
if (maddr1 > memory->heap_data_end) \
goto out_of_bounds; \
} \
CHECK_EXT_MEMORY_SPACE() \
else \
goto out_of_bounds; \
} while (0)
static inline uint32
@ -588,7 +610,7 @@ ALLOC_FRAME(WASMThread *self, uint32 size, WASMInterpFrame *prev_frame)
frame->prev_frame = prev_frame;
else {
wasm_runtime_set_exception(self->module_inst,
"WASM interp failed, alloc frame failed.");
"WASM interp failed: stack overflow.");
}
return frame;
@ -641,7 +663,7 @@ wasm_interp_call_func_native(WASMThread *self,
else {
if (!(argv = wasm_malloc(sizeof(uint32) * argc))) {
wasm_runtime_set_exception(self->module_inst,
"WASM call native failed: alloc memory for argv failed.");
"WASM call native failed: allocate memory failed.");
return;
}
}
@ -731,7 +753,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
uint32 i, depth, cond, count, fidx, tidx, frame_size = 0, all_cell_num = 0;
int32 didx, val;
uint8 *else_addr, *end_addr;
uint8 *maddr;
uint8 *maddr = NULL;
#if WASM_ENABLE_LABELS_AS_VALUES != 0
#define HANDLE_OPCODE(op) &&HANDLE_##op
@ -768,7 +790,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
BLOCK_TYPE_BLOCK,
&else_addr, &end_addr,
NULL, 0)) {
wasm_runtime_set_exception(module, "find block addr failed");
wasm_runtime_set_exception(module, "find block address failed");
goto got_exception;
}
@ -783,7 +805,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
BLOCK_TYPE_LOOP,
&else_addr, &end_addr,
NULL, 0)) {
wasm_runtime_set_exception(module, "find block addr failed");
wasm_runtime_set_exception(module, "find block address failed");
goto got_exception;
}
@ -798,7 +820,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
BLOCK_TYPE_IF,
&else_addr, &end_addr,
NULL, 0)) {
wasm_runtime_set_exception(module, "find block addr failed");
wasm_runtime_set_exception(module, "find block address failed");
goto got_exception;
}
@ -855,8 +877,9 @@ wasm_interp_call_func_bytecode(WASMThread *self,
depths = depth_buf;
else {
if (!(depths = wasm_malloc(sizeof(uint32) * count))) {
wasm_runtime_set_exception(module, "WASM interp failed, "
"alloc block memory for br_table failed.");
wasm_runtime_set_exception(module,
"WASM interp failed: "
"allocate memory failed.");
goto got_exception;
}
}
@ -931,7 +954,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP (WASM_OP_DROP):
{
wasm_runtime_set_exception(module,
"wasm interp failed: unsupported opcode");
"WASM interp failed: unsupported opcode.");
goto got_exception;
}
@ -950,7 +973,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP (WASM_OP_SELECT):
{
wasm_runtime_set_exception(module,
"wasm interp failed: unsupported opcode");
"WASM interp failed: unsupported opcode.");
goto got_exception;
}
@ -997,7 +1020,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
break;
default:
wasm_runtime_set_exception(module,
"get local type is invalid");
"invalid local type");
goto got_exception;
}
(void)local_count;
@ -1026,7 +1049,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
break;
default:
wasm_runtime_set_exception(module,
"set local type is invalid");
"invalid local type");
goto got_exception;
}
(void)local_count;
@ -1054,7 +1077,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
SET_LOCAL_F64(local_idx, GET_F64_FROM_ADDR(frame_sp - 2));
break;
default:
wasm_runtime_set_exception(module, "tee local type is invalid");
wasm_runtime_set_exception(module, "invalid local type");
goto got_exception;
}
(void)local_count;
@ -1085,7 +1108,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
PUSH_F64(*(float64*)get_global_addr(memory, global));
break;
default:
wasm_runtime_set_exception(module, "get global type is invalid");
wasm_runtime_set_exception(module, "invalid global type");
goto got_exception;
}
HANDLE_OP_END ();
@ -1117,7 +1140,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
PUT_F64_TO_ADDR((uint32*)global_addr, POP_F64());
break;
default:
wasm_runtime_set_exception(module, "set global index is overflow");
wasm_runtime_set_exception(module, "invalid global type");
goto got_exception;
}
HANDLE_OP_END ();
@ -1125,70 +1148,98 @@ wasm_interp_call_func_bytecode(WASMThread *self,
/* memory load instructions */
HANDLE_OP (WASM_OP_I32_LOAD):
DEF_OP_LOAD(PUSH_I32(*(int32*)maddr));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_LOAD):
DEF_OP_LOAD(PUSH_I64(GET_I64_FROM_ADDR((uint32*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_F32_LOAD):
DEF_OP_LOAD(PUSH_F32(*(float32*)maddr));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_F64_LOAD):
DEF_OP_LOAD(PUSH_F64(GET_F64_FROM_ADDR((uint32*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I32_LOAD8_S):
DEF_OP_LOAD(PUSH_I32(sign_ext_8_32(*(int8*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I32_LOAD8_U):
DEF_OP_LOAD(PUSH_I32((uint32)(*(uint8*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I32_LOAD16_S):
DEF_OP_LOAD(PUSH_I32(sign_ext_16_32(*(int16*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I32_LOAD16_U):
DEF_OP_LOAD(PUSH_I32((uint32)(*(uint16*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_LOAD8_S):
DEF_OP_LOAD(PUSH_I64(sign_ext_8_64(*(int8*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_LOAD8_U):
DEF_OP_LOAD(PUSH_I64((uint64)(*(uint8*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_LOAD16_S):
DEF_OP_LOAD(PUSH_I64(sign_ext_16_64(*(int16*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_LOAD16_U):
DEF_OP_LOAD(PUSH_I64((uint64)(*(uint16*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_LOAD32_S):
DEF_OP_LOAD(PUSH_I64(sign_ext_32_64(*(int32*)maddr)));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_LOAD32_U):
DEF_OP_LOAD(PUSH_I64((uint64)(*(uint32*)maddr)));
{
uint32 offset, flags, addr;
GET_OPCODE();
read_leb_uint32(frame_ip, frame_ip_end, flags);
read_leb_uint32(frame_ip, frame_ip_end, offset);
addr = POP_I32();
CHECK_MEMORY_OVERFLOW();
#if WASM_ENABLE_LABELS_AS_VALUES != 0
static const void *handle_load_table[] = {
&&HANDLE_LOAD_WASM_OP_I32_LOAD,
&&HANDLE_LOAD_WASM_OP_I64_LOAD,
&&HANDLE_LOAD_WASM_OP_F32_LOAD,
&&HANDLE_LOAD_WASM_OP_F64_LOAD,
&&HANDLE_LOAD_WASM_OP_I32_LOAD8_S,
&&HANDLE_LOAD_WASM_OP_I32_LOAD8_U,
&&HANDLE_LOAD_WASM_OP_I32_LOAD16_S,
&&HANDLE_LOAD_WASM_OP_I32_LOAD16_U,
&&HANDLE_LOAD_WASM_OP_I64_LOAD8_S,
&&HANDLE_LOAD_WASM_OP_I64_LOAD8_U,
&&HANDLE_LOAD_WASM_OP_I64_LOAD16_S,
&&HANDLE_LOAD_WASM_OP_I64_LOAD16_U,
&&HANDLE_LOAD_WASM_OP_I64_LOAD32_S,
&&HANDLE_LOAD_WASM_OP_I64_LOAD32_U
};
#define HANDLE_OP_LOAD(opcode) HANDLE_LOAD_##opcode
goto *handle_load_table[opcode - WASM_OP_I32_LOAD];
#else
#define HANDLE_OP_LOAD(opcode) case opcode
switch (opcode)
#endif
{
HANDLE_OP_LOAD(WASM_OP_I32_LOAD):
PUSH_I32(*(int32*)maddr);
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I64_LOAD):
PUSH_I64(GET_I64_FROM_ADDR((uint32*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_F32_LOAD):
PUSH_F32(*(float32*)maddr);
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_F64_LOAD):
PUSH_F64(GET_F64_FROM_ADDR((uint32*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_S):
PUSH_I32(sign_ext_8_32(*(int8*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_U):
PUSH_I32((uint32)(*(uint8*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_S):
PUSH_I32(sign_ext_16_32(*(int16*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_U):
PUSH_I32((uint32)(*(uint16*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_S):
PUSH_I64(sign_ext_8_64(*(int8*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_U):
PUSH_I64((uint64)(*(uint8*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_S):
PUSH_I64(sign_ext_16_64(*(int16*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_U):
PUSH_I64((uint64)(*(uint16*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_S):
PUSH_I64(sign_ext_32_64(*(int32*)maddr));
HANDLE_OP_END();
HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_U):
PUSH_I64((uint64)(*(uint32*)maddr));
HANDLE_OP_END();
}
(void)flags;
HANDLE_OP_END ();
}
/* memory store instructions */
HANDLE_OP (WASM_OP_I32_STORE):
DEF_OP_STORE(uint32, I32, *(int32*)maddr = sval);
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_STORE):
DEF_OP_STORE(uint64, I64, PUT_I64_TO_ADDR((uint32*)maddr, sval));
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_F32_STORE):
{
uint32 offset, flags, addr;
@ -1218,25 +1269,63 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_I32_STORE):
HANDLE_OP (WASM_OP_I32_STORE8):
DEF_OP_STORE(uint32, I32, *(uint8*)maddr = (uint8)sval);
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I32_STORE16):
DEF_OP_STORE(uint32, I32, *(uint16*)maddr = (uint16)sval);
{
uint32 offset, flags, addr;
uint32 sval;
GET_OPCODE();
read_leb_uint32(frame_ip, frame_ip_end, flags);
read_leb_uint32(frame_ip, frame_ip_end, offset);
sval = POP_I32();
addr = POP_I32();
CHECK_MEMORY_OVERFLOW();
switch (opcode) {
case WASM_OP_I32_STORE:
*(int32*)maddr = sval;
break;
case WASM_OP_I32_STORE8:
*(uint8*)maddr = (uint8)sval;
break;
case WASM_OP_I32_STORE16:
*(uint16*)maddr = (uint16)sval;
break;
}
(void)flags;
HANDLE_OP_END ();
}
HANDLE_OP (WASM_OP_I64_STORE):
HANDLE_OP (WASM_OP_I64_STORE8):
DEF_OP_STORE(uint64, I64, *(uint8*)maddr = (uint8)sval);
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_STORE16):
DEF_OP_STORE(uint64, I64, *(uint16*)maddr = (uint16)sval);
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_I64_STORE32):
DEF_OP_STORE(uint64, I64, *(uint32*)maddr = (uint32)sval);
{
uint32 offset, flags, addr;
uint64 sval;
GET_OPCODE();
read_leb_uint32(frame_ip, frame_ip_end, flags);
read_leb_uint32(frame_ip, frame_ip_end, offset);
sval = POP_I64();
addr = POP_I32();
CHECK_MEMORY_OVERFLOW();
switch (opcode) {
case WASM_OP_I64_STORE:
PUT_I64_TO_ADDR((uint32*)maddr, sval);
break;
case WASM_OP_I64_STORE8:
*(uint8*)maddr = (uint8)sval;
break;
case WASM_OP_I64_STORE16:
*(uint16*)maddr = (uint16)sval;
break;
case WASM_OP_I64_STORE32:
*(uint32*)maddr = (uint32)sval;
break;
}
(void)flags;
HANDLE_OP_END ();
}
/* memory size and memory grow instructions */
HANDLE_OP (WASM_OP_MEMORY_SIZE):
@ -1977,7 +2066,8 @@ wasm_interp_call_func_bytecode(WASMThread *self,
#if WASM_ENABLE_LABELS_AS_VALUES == 0
default:
wasm_runtime_set_exception(module, "wasm interp failed: unsupported opcode");
wasm_runtime_set_exception(module,
"WASM interp failed: unsupported opcode.");
goto got_exception;
}
#endif
@ -2005,7 +2095,8 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP (WASM_OP_UNUSED_0x26):
HANDLE_OP (WASM_OP_UNUSED_0x27):
{
wasm_runtime_set_exception(module, "wasm interp failed: unsupported opcode");
wasm_runtime_set_exception(module,
"WASM interp failed: unsupported opcode.");
goto got_exception;
}
#endif

View File

@ -21,6 +21,7 @@
#include "wasm_runtime.h"
#include "wasm_log.h"
#include "wasm_memory.h"
#include "wasm_dlfcn.h"
/* Read a value of given type from the address pointed to by the given
pointer and increase the pointer to the position just after the
@ -140,7 +141,8 @@ const_str_set_insert(const uint8 *str, int32 len, WASMModule *module,
if (!c_str) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: alloc memory failed.");
"WASM module load failed: "
"allocate memory failed.");
return NULL;
}
@ -233,7 +235,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->type_count = type_count;
if (!(module->types = wasm_malloc(sizeof(WASMType*) * type_count))) {
set_error_buf(error_buf, error_buf_size,
"Load type section failed: alloc memory failed.");
"Load type section failed: allocate memory failed.");
return false;
}
@ -255,13 +257,20 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
CHECK_BUF(p, p_end, param_count);
p += param_count;
read_leb_uint32(p, p_end, result_count);
wasm_assert(result_count <= 1);
if (result_count > 1) {
set_error_buf(error_buf, error_buf_size,
"Load type section failed: invalid result count.");
return false;
}
CHECK_BUF(p, p_end, result_count);
p = p_org;
if (!(type = module->types[i] = wasm_malloc(offsetof(WASMType, types) +
sizeof(uint8) * (param_count + result_count))))
sizeof(uint8) * (param_count + result_count)))) {
set_error_buf(error_buf, error_buf_size,
"Load type section failed: allocate memory failed.");
return false;
}
/* Resolve param types and result types */
type->param_count = param_count;
@ -412,7 +421,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->import_count = import_count;
if (!(module->imports = wasm_malloc(sizeof(WASMImport) * import_count))) {
set_error_buf(error_buf, error_buf_size,
"Load import section failed: alloc memory failed.");
"Load import section failed: allocate memory failed.");
return false;
}
@ -634,7 +643,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
module->function_count = func_count;
if (!(module->functions = wasm_malloc(sizeof(WASMFunction*) * func_count))) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: alloc memory failed.");
"Load function section failed: allocate memory failed.");
return false;
}
@ -651,7 +660,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
}
read_leb_uint32(p_code, buf_code_end, code_size);
if (code_size == 0) {
if (code_size == 0
|| p_code + code_size > buf_code_end) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: "
"invalid function code size.");
@ -677,7 +687,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
if (!(func = module->functions[i] = wasm_malloc(total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: alloc memory failed.");
"Load function section failed: "
"allocate memory failed.");
return false;
}
@ -695,7 +706,20 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
local_type_index = 0;
for (j = 0; j < local_set_count; j++) {
read_leb_uint32(p_code, buf_code_end, sub_local_count);
if (local_type_index + sub_local_count <= local_type_index
|| local_type_index + sub_local_count > local_count) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: "
"invalid local count.");
return false;
}
read_leb_uint8(p_code, buf_code_end, type);
if (type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: "
"invalid local type.");
return false;
}
for (k = 0; k < sub_local_count; k++) {
func->local_types[local_type_index++] = type;
}
@ -734,7 +758,8 @@ load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->table_count = table_count;
if (!(module->tables = wasm_malloc(sizeof(WASMTable) * table_count))) {
set_error_buf(error_buf, error_buf_size,
"Load table section failed: alloc memory failed.");
"Load table section failed: "
"allocate memory failed.");
return false;
}
@ -776,7 +801,8 @@ load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->memory_count = memory_count;
if (!(module->memories = wasm_malloc(sizeof(WASMMemory) * memory_count))) {
set_error_buf(error_buf, error_buf_size,
"Load memory section failed: alloc memory failed.");
"Load memory section failed: "
"allocate memory failed.");
return false;
}
@ -813,7 +839,8 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->global_count = global_count;
if (!(module->globals = wasm_malloc(sizeof(WASMGlobal) * global_count))) {
set_error_buf(error_buf, error_buf_size,
"Load global section failed: alloc memory failed.");
"Load global section failed: "
"allocate memory failed.");
return false;
}
@ -858,7 +885,8 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->export_count = export_count;
if (!(module->exports = wasm_malloc(sizeof(WASMExport) * export_count))) {
set_error_buf(error_buf, error_buf_size,
"Load export section failed: alloc memory failed.");
"Load export section failed: "
"allocate memory failed.");
return false;
}
@ -951,7 +979,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m
(sizeof(WASMTableSeg) * table_segment_count))) {
set_error_buf(error_buf, error_buf_size,
"Load table segment section failed: "
"alloc memory failed.");
"allocate memory failed.");
return false;
}
@ -973,7 +1001,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m
wasm_malloc(sizeof(uint32) * function_count))) {
set_error_buf(error_buf, error_buf_size,
"Load table segment section failed: "
"alloc memory failed.");
"allocate memory failed.");
return false;
}
for (j = 0; j < function_count; j++) {
@ -1011,8 +1039,8 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
if (!(module->data_segments =
wasm_malloc(sizeof(WASMDataSeg*) * data_seg_count))) {
set_error_buf(error_buf, error_buf_size,
"Load data segment section failed, "
"alloc memory failed.");
"Load data segment section failed: "
"allocate memory failed.");
return false;
}
@ -1030,7 +1058,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
wasm_malloc(sizeof(WASMDataSeg)))) {
set_error_buf(error_buf, error_buf_size,
"Load data segment section failed: "
"alloc memory failed.");
"allocate memory failed.");
return false;
}
@ -1116,12 +1144,6 @@ load_from_sections(WASMModule *module, WASMSection *sections,
section = section->next;
}
if (!buf_code) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: find code section failed.");
return false;
}
section = sections;
while (section) {
buf = section->section_body;
@ -1139,6 +1161,11 @@ load_from_sections(WASMModule *module, WASMSection *sections,
return false;
break;
case SECTION_TYPE_FUNC:
if (!buf_code) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: find code section failed.");
return false;
}
if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
module, error_buf, error_buf_size))
return false;
@ -1228,7 +1255,8 @@ create_module(char *error_buf, uint32 error_buf_size)
if (!module) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: alloc memory failed.");
"WASM module load failed: "
"allocate memory failed.");
return NULL;
}
@ -1308,7 +1336,8 @@ create_sections(const uint8 *buf, uint32 size,
if (!(section = wasm_malloc(sizeof(WASMSection)))) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: alloc memory failed.");
"WASM module load failed: "
"allocate memory failed.");
return false;
}
@ -1335,6 +1364,25 @@ create_sections(const uint8 *buf, uint32 size,
return true;
}
static void
exchange32(uint8* p_data)
{
uint8 value = *p_data;
*p_data = *(p_data + 3);
*(p_data + 3) = value;
value = *(p_data + 1);
*(p_data + 1) = *(p_data + 2);
*(p_data + 2) = value;
}
static union {
int a;
char b;
} __ue = { .a = 1 };
#define is_little_endian() (__ue.b == 1)
static bool
load(const uint8 *buf, uint32 size, WASMModule *module,
char *error_buf, uint32 error_buf_size)
@ -1345,13 +1393,21 @@ load(const uint8 *buf, uint32 size, WASMModule *module,
WASMSection *section_list = NULL;
CHECK_BUF(p, p_end, sizeof(uint32));
if ((magic_number = read_uint32(p)) != WASM_MAGIC_NUMBER) {
magic_number = read_uint32(p);
if (!is_little_endian())
exchange32((uint8*)&magic_number);
if (magic_number != WASM_MAGIC_NUMBER) {
set_error_buf(error_buf, error_buf_size, "magic header not detected");
return false;
}
CHECK_BUF(p, p_end, sizeof(uint32));
if ((version = read_uint32(p)) != WASM_CURRENT_VERSION) {
version = read_uint32(p);
if (!is_little_endian())
exchange32((uint8*)&version);
if (version != WASM_CURRENT_VERSION) {
set_error_buf(error_buf, error_buf_size, "unknown binary version");
return false;
}
@ -1373,7 +1429,7 @@ wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_bu
if (!module) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: alloc memory failed.");
"WASM module load failed: allocate memory failed.");
return NULL;
}
@ -1827,9 +1883,11 @@ wasm_loader_find_block_addr(WASMModule *module,
break;
default:
LOG_ERROR("WASM loader find block addr failed: invalid opcode %02x.\n",
opcode);
break;
if (error_buf)
snprintf(error_buf, error_buf_size,
"WASM loader find block addr failed: "
"invalid opcode %02x.", opcode);
return false;
}
}
@ -1874,7 +1932,7 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new)
if (!mem_new) { \
set_error_buf(error_buf, error_buf_size, \
"WASM loader prepare bytecode failed: " \
"alloc memory failed"); \
"allocate memory failed."); \
goto fail; \
} \
mem = mem_new; \
@ -2173,6 +2231,24 @@ pop_type(uint8 type, uint8 **p_frame_ref, uint32 *p_stack_cell_num,
} \
} while (0)
static bool
check_memory(WASMModule *module,
char *error_buf, uint32 error_buf_size)
{
if (module->memory_count == 0
&& module->import_memory_count == 0) {
set_error_buf(error_buf, error_buf_size,
"load or store in module without default memory");
return false;
}
return true;
}
#define CHECK_MEMORY() do { \
if (!check_memory(module, error_buf, error_buf_size)) \
goto fail; \
} while (0)
static bool
wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
char *error_buf, uint32 error_buf_size)
@ -2207,7 +2283,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
frame_ref_size = 32;
if (!(frame_ref_bottom = frame_ref = wasm_malloc(frame_ref_size))) {
set_error_buf(error_buf, error_buf_size,
"WASM loader prepare bytecode failed: alloc memory failed");
"WASM loader prepare bytecode failed: "
"allocate memory failed");
goto fail;
}
memset(frame_ref_bottom, 0, frame_ref_size);
@ -2216,7 +2293,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
frame_csp_size = sizeof(BranchBlock) * 8;
if (!(frame_csp_bottom = frame_csp = wasm_malloc(frame_csp_size))) {
set_error_buf(error_buf, error_buf_size,
"WASM loader prepare bytecode failed: alloc memory failed");
"WASM loader prepare bytecode failed: "
"allocate memory failed");
goto fail;
}
@ -2309,7 +2387,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
if (!block) {
set_error_buf(error_buf, error_buf_size,
"WASM loader prepare bytecode failed: "
"alloc memory failed");
"allocate memory failed.");
goto fail;
}
@ -2322,7 +2400,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
block)) {
set_error_buf(error_buf, error_buf_size,
"WASM loader prepare bytecode failed: "
"alloc memory failed");
"allocate memory failed.");
wasm_free(block);
goto fail;
}
@ -2454,12 +2532,20 @@ handle_op_br:
WASMType *func_type;
uint32 type_idx;
if (module->table_count == 0
&& module->import_table_count == 0) {
set_error_buf(error_buf, error_buf_size,
"call indirect without default table");
goto fail;
}
read_leb_uint32(p, p_end, type_idx);
read_leb_uint8(p, p_end, u8); /* 0x00 */
POP_I32();
if (type_idx >= module->type_count) {
set_error_buf(error_buf, error_buf_size, "function index is overflow");
set_error_buf(error_buf, error_buf_size,
"function index is overflow");
goto fail;
}
@ -2590,6 +2676,7 @@ handle_op_br:
case WASM_OP_I32_LOAD8_U:
case WASM_OP_I32_LOAD16_S:
case WASM_OP_I32_LOAD16_U:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_I32();
@ -2603,6 +2690,7 @@ handle_op_br:
case WASM_OP_I64_LOAD16_U:
case WASM_OP_I64_LOAD32_S:
case WASM_OP_I64_LOAD32_U:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_I32();
@ -2610,6 +2698,7 @@ handle_op_br:
break;
case WASM_OP_F32_LOAD:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_I32();
@ -2617,6 +2706,7 @@ handle_op_br:
break;
case WASM_OP_F64_LOAD:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_I32();
@ -2626,6 +2716,7 @@ handle_op_br:
case WASM_OP_I32_STORE:
case WASM_OP_I32_STORE8:
case WASM_OP_I32_STORE16:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_I32();
@ -2636,6 +2727,7 @@ handle_op_br:
case WASM_OP_I64_STORE8:
case WASM_OP_I64_STORE16:
case WASM_OP_I64_STORE32:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_I64();
@ -2643,6 +2735,7 @@ handle_op_br:
break;
case WASM_OP_F32_STORE:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_F32();
@ -2650,6 +2743,7 @@ handle_op_br:
break;
case WASM_OP_F64_STORE:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* align */
read_leb_uint32(p, p_end, u32); /* offset */
POP_F64();
@ -2657,11 +2751,13 @@ handle_op_br:
break;
case WASM_OP_MEMORY_SIZE:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* 0x00 */
PUSH_I32();
break;
case WASM_OP_MEMORY_GROW:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* 0x00 */
POP_I32();
PUSH_I32();
@ -2943,15 +3039,24 @@ handle_op_br:
break;
default:
LOG_ERROR("WASM loader find block addr failed: invalid opcode %02x.\n",
opcode);
break;
if (error_buf != NULL)
snprintf(error_buf, error_buf_size,
"WASM module load failed: "
"invalid opcode %02x.", opcode);
goto fail;
}
if (opcode != WASM_OP_I32_CONST)
is_i32_const = false;
}
if (csp_num > 0) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: "
"function body must end with END opcode.");
goto fail;
}
func->max_stack_cell_num = max_stack_cell_num;
func->max_block_num = max_csp_num;
return_value = true;

View File

@ -35,7 +35,7 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
bool
wasm_runtime_init()
{
if (wasm_platform_init() != 0)
if (bh_platform_init() != 0)
return false;
if (wasm_log_init() != 0)
@ -877,8 +877,9 @@ wasm_runtime_instantiate(WASMModule *module,
length = data_seg->data_length;
memory_size = NumBytesPerPage * module_inst->default_memory->cur_page_count;
if (base_offset >= memory_size
|| base_offset + length > memory_size) {
if (length > 0
&& (base_offset >= memory_size
|| base_offset + length > memory_size)) {
set_error_buf(error_buf, error_buf_size,
"Instantiate module failed: data segment out of range.");
wasm_runtime_deinstantiate(module_inst);
@ -946,18 +947,11 @@ wasm_runtime_instantiate(WASMModule *module,
wasm_runtime_set_tlr(&module_inst->main_tlr);
module_inst->main_tlr.handle = ws_self_thread();
/* Execute __post_instantiate function */
if (!execute_post_inst_function(module_inst)) {
const char *exception = wasm_runtime_get_exception(module_inst);
wasm_printf("%s\n", exception);
wasm_runtime_deinstantiate(module_inst);
return NULL;
}
/* Execute start function */
if (!execute_start_function(module_inst)) {
const char *exception = wasm_runtime_get_exception(module_inst);
wasm_printf("%s\n", exception);
/* Execute __post_instantiate and start function */
if (!execute_post_inst_function(module_inst)
|| !execute_start_function(module_inst)) {
set_error_buf(error_buf, error_buf_size,
module_inst->cur_exception);
wasm_runtime_deinstantiate(module_inst);
return NULL;
}
@ -991,6 +985,37 @@ wasm_runtime_deinstantiate(WASMModuleInstance *module_inst)
wasm_free(module_inst);
}
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
bool
wasm_runtime_set_ext_memory(WASMModuleInstance *module_inst,
uint8 *ext_mem_data, uint32 ext_mem_size,
char *error_buf, uint32 error_buf_size)
{
if (module_inst->ext_mem_data) {
set_error_buf(error_buf, error_buf_size,
"Set external memory failed: "
"an external memory has been set.");
return false;
}
if (!ext_mem_data
|| ext_mem_size > 1 * BH_GB
|| ext_mem_data + ext_mem_size < ext_mem_data) {
set_error_buf(error_buf, error_buf_size,
"Set external memory failed: "
"invalid input.");
return false;
}
module_inst->ext_mem_data = ext_mem_data;
module_inst->ext_mem_data_end = ext_mem_data + ext_mem_size;
module_inst->ext_mem_size = ext_mem_size;
module_inst->ext_mem_base_offset = DEFAULT_EXT_MEM_BASE_OFFSET;
return true;
}
#endif
bool
wasm_runtime_enlarge_memory(WASMModuleInstance *module, int inc_page_count)
{
@ -1165,24 +1190,40 @@ wasm_runtime_validate_app_addr(WASMModuleInstance *module_inst,
uint8 *addr;
/* integer overflow check */
if(app_offset < 0 ||
app_offset + size < app_offset) {
if(app_offset + size < app_offset) {
goto fail;
}
memory = module_inst->default_memory;
if (app_offset < memory->heap_base_offset) {
if (0 <= app_offset
&& app_offset < memory->heap_base_offset) {
addr = memory->memory_data + app_offset;
if (!(memory->base_addr <= addr && addr + size <= memory->end_addr))
goto fail;
return true;
}
else {
else if (memory->heap_base_offset < app_offset
&& app_offset < memory->heap_base_offset
+ (memory->heap_data_end - memory->heap_data)) {
addr = memory->heap_data + (app_offset - memory->heap_base_offset);
if (!(memory->heap_data <= addr && addr + size <= memory->heap_data_end))
goto fail;
return true;
}
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_base_offset <= app_offset
&& app_offset < module_inst->ext_mem_base_offset
+ module_inst->ext_mem_size) {
addr = module_inst->ext_mem_data
+ (app_offset - module_inst->ext_mem_base_offset);
if (!(module_inst->ext_mem_data <= addr
&& addr + size <= module_inst->ext_mem_data_end))
goto fail;
return true;
}
#endif
fail:
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
@ -1201,7 +1242,13 @@ wasm_runtime_validate_native_addr(WASMModuleInstance *module_inst,
}
if ((memory->base_addr <= addr && addr + size <= memory->end_addr)
|| (memory->heap_data <= addr && addr + size <= memory->heap_data_end))
|| (memory->heap_data <= addr && addr + size <= memory->heap_data_end)
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
|| (module_inst->ext_mem_data
&& module_inst->ext_mem_data <= addr
&& addr + size <= module_inst->ext_mem_data_end)
#endif
)
return true;
fail:
@ -1214,10 +1261,22 @@ wasm_runtime_addr_app_to_native(WASMModuleInstance *module_inst,
int32 app_offset)
{
WASMMemoryInstance *memory = module_inst->default_memory;
if (app_offset < memory->heap_base_offset)
if (0 <= app_offset && app_offset < memory->heap_base_offset)
return memory->memory_data + app_offset;
else
else if (memory->heap_base_offset < app_offset
&& app_offset < memory->heap_base_offset
+ (memory->heap_data_end - memory->heap_data))
return memory->heap_data + (app_offset - memory->heap_base_offset);
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_base_offset <= app_offset
&& app_offset < module_inst->ext_mem_base_offset
+ module_inst->ext_mem_size)
return module_inst->ext_mem_data
+ (app_offset - module_inst->ext_mem_base_offset);
#endif
else
return NULL;
}
int32
@ -1225,13 +1284,102 @@ wasm_runtime_addr_native_to_app(WASMModuleInstance *module_inst,
void *native_ptr)
{
WASMMemoryInstance *memory = module_inst->default_memory;
if ((uint8*)native_ptr < memory->heap_data)
if (memory->base_addr <= (uint8*)native_ptr
&& (uint8*)native_ptr < memory->end_addr)
return (uint8*)native_ptr - memory->memory_data;
else
else if (memory->heap_data <= (uint8*)native_ptr
&& (uint8*)native_ptr < memory->heap_data_end)
return memory->heap_base_offset
+ ((uint8*)native_ptr - memory->heap_data);
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_data <= (uint8*)native_ptr
&& (uint8*)native_ptr < module_inst->ext_mem_data_end)
return module_inst->ext_mem_base_offset
+ ((uint8*)native_ptr - module_inst->ext_mem_data);
#endif
else
return 0;
}
bool
wasm_runtime_get_app_addr_range(WASMModuleInstance *module_inst,
int32 app_offset,
int32 *p_app_start_offset,
int32 *p_app_end_offset)
{
int32 app_start_offset, app_end_offset;
WASMMemoryInstance *memory = module_inst->default_memory;
if (0 <= app_offset && app_offset < memory->heap_base_offset) {
app_start_offset = 0;
app_end_offset = NumBytesPerPage * memory->cur_page_count;
}
else if (memory->heap_base_offset < app_offset
&& app_offset < memory->heap_base_offset
+ (memory->heap_data_end - memory->heap_data)) {
app_start_offset = memory->heap_base_offset;
app_end_offset = memory->heap_base_offset
+ (memory->heap_data_end - memory->heap_data);
}
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_base_offset <= app_offset
&& app_offset < module_inst->ext_mem_base_offset
+ module_inst->ext_mem_size) {
app_start_offset = module_inst->ext_mem_base_offset;
app_end_offset = app_start_offset + module_inst->ext_mem_size;
}
#endif
else
return false;
if (p_app_start_offset)
*p_app_start_offset = app_start_offset;
if (p_app_end_offset)
*p_app_end_offset = app_end_offset;
return true;
}
bool
wasm_runtime_get_native_addr_range(WASMModuleInstance *module_inst,
uint8 *native_ptr,
uint8 **p_native_start_addr,
uint8 **p_native_end_addr)
{
uint8 *native_start_addr, *native_end_addr;
WASMMemoryInstance *memory = module_inst->default_memory;
if (memory->base_addr <= (uint8*)native_ptr
&& (uint8*)native_ptr < memory->end_addr) {
native_start_addr = memory->memory_data;
native_end_addr = memory->memory_data
+ NumBytesPerPage * memory->cur_page_count;
}
else if (memory->heap_data <= (uint8*)native_ptr
&& (uint8*)native_ptr < memory->heap_data_end) {
native_start_addr = memory->heap_data;
native_end_addr = memory->heap_data_end;
}
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_data <= (uint8*)native_ptr
&& (uint8*)native_ptr < module_inst->ext_mem_data_end) {
native_start_addr = module_inst->ext_mem_data;
native_end_addr = module_inst->ext_mem_data_end;
}
#endif
else
return false;
if (p_native_start_addr)
*p_native_start_addr = native_start_addr;
if (p_native_end_addr)
*p_native_end_addr = native_end_addr;
return true;
}
uint32
wasm_runtime_get_temp_ret(WASMModuleInstance *module_inst)
{

View File

@ -148,6 +148,13 @@ typedef struct WASMModuleInstance {
uint32 temp_ret;
uint32 llvm_stack;
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
int32 ext_mem_base_offset;
uint8 *ext_mem_data;
uint8 *ext_mem_data_end;
uint32 ext_mem_size;
#endif
/* Default WASM stack size of threads of this Module instance. */
uint32 wasm_stack_size;

Some files were not shown because too many files have changed in this diff Show More