Skip to content

Commit 2ecaf0e

Browse files
no1wudixiaoxiang781216
authored andcommitted
examples: Add wamr_module
This example demonstrates how to register a external module into WAMR runtime. Signed-off-by: Huang Qi <[email protected]>
1 parent 9320597 commit 2ecaf0e

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

examples/wamr_module/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ##############################################################################
2+
# apps/examples/wamr_module/CMakeLists.txt
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
if(CONFIG_EXAMPLES_WAMR_MODULE)
22+
23+
target_sources(apps PRIVATE module_hello.c)
24+
25+
# register WAMR mod
26+
nuttx_add_wamrmod(MODS hello)
27+
endif()

examples/wamr_module/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_WAMR_MODULE
7+
tristate "WAMR module example"
8+
default n
9+
---help---
10+
This example demonstrates how to register a external module
11+
into WAMR runtime.

examples/wamr_module/Make.defs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/wamr_module/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
ifneq ($(CONFIG_EXAMPLES_WAMR_MODULE),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/wamr_module
23+
endif

examples/wamr_module/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
############################################################################
2+
# apps/examples/wamr_module/Makefile
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(APPDIR)/Make.defs
22+
23+
# A Hello World WAMR C module
24+
25+
CSRCS = module_hello.c
26+
27+
# Set WAMR_MODULE_NAME and include Module.mk to add this module to the list of
28+
# builtin modules for the WAMR runtime. WAMR_MODULE_NAME must be unique across all
29+
# modules in system.
30+
31+
WAMR_MODULE_NAME = hello
32+
33+
include $(APPDIR)/interpreters/wamr/Module.mk
34+
include $(APPDIR)/Application.mk

examples/wamr_module/module_hello.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/****************************************************************************
2+
* apps/examples/wamr_module/module_hello.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <stdio.h>
26+
#include <stddef.h>
27+
#include <sys/param.h>
28+
29+
#include "wasm_export.h"
30+
31+
/****************************************************************************
32+
* Private Function Prototypes
33+
****************************************************************************/
34+
35+
static void hello_wrapper(wasm_exec_env_t env);
36+
37+
/****************************************************************************
38+
* Private Data
39+
****************************************************************************/
40+
41+
static NativeSymbol g_hello_symbols[] =
42+
{
43+
EXPORT_WASM_API_WITH_SIG2(hello, "()")
44+
};
45+
46+
/****************************************************************************
47+
* Private Functions
48+
****************************************************************************/
49+
50+
/****************************************************************************
51+
* Name: hello_wrapper
52+
****************************************************************************/
53+
54+
static void hello_wrapper(wasm_exec_env_t env)
55+
{
56+
printf("Hello World from WAMR module!\n");
57+
}
58+
59+
/****************************************************************************
60+
* Public Functions
61+
****************************************************************************/
62+
63+
/****************************************************************************
64+
* Name: wamr_module_hello_register
65+
*
66+
* The function prototype for the <WAMR_MODULE_NAME> module must be:
67+
* `bool wamr_module_<WAMR_MODULE_NAME>_register(void)`
68+
*
69+
****************************************************************************/
70+
71+
bool wamr_module_hello_register(void)
72+
{
73+
return wasm_runtime_register_natives("hello", g_hello_symbols,
74+
nitems(g_hello_symbols));
75+
}

0 commit comments

Comments
 (0)