From 2ecaf0ec48fcd9f7eaa482eaee6473db8fdd48da Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Sat, 24 Aug 2024 13:47:26 +0800 Subject: [PATCH] examples: Add wamr_module This example demonstrates how to register a external module into WAMR runtime. Signed-off-by: Huang Qi --- examples/wamr_module/CMakeLists.txt | 27 +++++++++++ examples/wamr_module/Kconfig | 11 +++++ examples/wamr_module/Make.defs | 23 +++++++++ examples/wamr_module/Makefile | 34 +++++++++++++ examples/wamr_module/module_hello.c | 75 +++++++++++++++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 examples/wamr_module/CMakeLists.txt create mode 100644 examples/wamr_module/Kconfig create mode 100644 examples/wamr_module/Make.defs create mode 100644 examples/wamr_module/Makefile create mode 100644 examples/wamr_module/module_hello.c diff --git a/examples/wamr_module/CMakeLists.txt b/examples/wamr_module/CMakeLists.txt new file mode 100644 index 000000000..540ce7f40 --- /dev/null +++ b/examples/wamr_module/CMakeLists.txt @@ -0,0 +1,27 @@ +# ############################################################################## +# apps/examples/wamr_module/CMakeLists.txt +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +if(CONFIG_EXAMPLES_WAMR_MODULE) + + target_sources(apps PRIVATE module_hello.c) + + # register WAMR mod + nuttx_add_wamrmod(MODS hello) +endif() diff --git a/examples/wamr_module/Kconfig b/examples/wamr_module/Kconfig new file mode 100644 index 000000000..2f6e97aa5 --- /dev/null +++ b/examples/wamr_module/Kconfig @@ -0,0 +1,11 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_WAMR_MODULE + tristate "WAMR module example" + default n + ---help--- + This example demonstrates how to register a external module + into WAMR runtime. diff --git a/examples/wamr_module/Make.defs b/examples/wamr_module/Make.defs new file mode 100644 index 000000000..f4464946c --- /dev/null +++ b/examples/wamr_module/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/examples/wamr_module/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_WAMR_MODULE),) +CONFIGURED_APPS += $(APPDIR)/examples/wamr_module +endif diff --git a/examples/wamr_module/Makefile b/examples/wamr_module/Makefile new file mode 100644 index 000000000..56777bd4e --- /dev/null +++ b/examples/wamr_module/Makefile @@ -0,0 +1,34 @@ +############################################################################ +# apps/examples/wamr_module/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# A Hello World WAMR C module + +CSRCS = module_hello.c + +# Set WAMR_MODULE_NAME and include Module.mk to add this module to the list of +# builtin modules for the WAMR runtime. WAMR_MODULE_NAME must be unique across all +# modules in system. + +WAMR_MODULE_NAME = hello + +include $(APPDIR)/interpreters/wamr/Module.mk +include $(APPDIR)/Application.mk diff --git a/examples/wamr_module/module_hello.c b/examples/wamr_module/module_hello.c new file mode 100644 index 000000000..dde411f0b --- /dev/null +++ b/examples/wamr_module/module_hello.c @@ -0,0 +1,75 @@ +/**************************************************************************** + * apps/examples/wamr_module/module_hello.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include "wasm_export.h" + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void hello_wrapper(wasm_exec_env_t env); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static NativeSymbol g_hello_symbols[] = +{ + EXPORT_WASM_API_WITH_SIG2(hello, "()") +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: hello_wrapper + ****************************************************************************/ + +static void hello_wrapper(wasm_exec_env_t env) +{ + printf("Hello World from WAMR module!\n"); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wamr_module_hello_register + * + * The function prototype for the module must be: + * `bool wamr_module__register(void)` + * + ****************************************************************************/ + +bool wamr_module_hello_register(void) +{ + return wasm_runtime_register_natives("hello", g_hello_symbols, + nitems(g_hello_symbols)); +}