Skip to content

Commit b9e874a

Browse files
committed
WARM
1 parent 198d3c8 commit b9e874a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+64639
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ templates/
1616
*.exp
1717
*.lib
1818
__pycache__
19+
compile_commands.json

SConstruct

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from utils import download_wasmer, download_wasmtime, WASMER_VER_DEFAULT, WASMTI
55
opts = Variables([], ARGUMENTS)
66

77
# Define options
8-
opts.Add(EnumVariable("wasm_runtime", "Wasm runtime used", "wasmtime", ["wasmer", "wasmtime"]))
8+
opts.Add(EnumVariable("wasm_runtime", "Wasm runtime used", "wasmtime", ["wasmer", "wasmtime", "wamr"]))
99
opts.Add(BoolVariable("download_runtime", "(Re)download runtime library", "no"))
1010
opts.Add("runtime_version", "Runtime library version", None)
1111

@@ -40,21 +40,26 @@ if env["platform"] == "windows":
4040
# Defines for GDExtension specific API
4141
env.Append(CPPDEFINES=["GDEXTENSION", "LIBWASM_STATIC"])
4242

43+
env.Append(CPPDEFINES=["WASM_RUNTIME_" + env["wasm_runtime"]])
44+
45+
# Godot Wasm sources
46+
source = ["register_types.cpp", env.Glob("src/*.cpp")]
47+
4348
# Explicit static libraries
44-
runtime_lib = env.File(
45-
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
49+
if env["wasm_runtime"] != "wamr":
50+
runtime_lib = env.File(
51+
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
4652
runtime=env["wasm_runtime"],
4753
prefix=env["LIBPREFIX"],
4854
suffix=env.get("LIBRUNTIMESUFFIX", env["LIBSUFFIX"]),
55+
)
4956
)
50-
)
57+
env.Append(LIBS=[runtime_lib])
58+
else:
59+
source += [env.Glob("wamr/src/*.c"), "wamr/stub.cpp"]
5160

5261
# CPP includes and libraries
5362
env.Append(CPPPATH=[".", "{}/include".format(env["wasm_runtime"])])
54-
env.Append(LIBS=[runtime_lib])
55-
56-
# Godot Wasm sources
57-
source = ["register_types.cpp", env.Glob("src/*.cpp")]
5863

5964
# Builders
6065
library = env.SharedLibrary(target="addons/godot-wasm/bin/{}/godot-wasm".format(env["platform"]), source=source)

SCsub

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
from utils import download_wasmer, download_wasmtime, WASMER_VER_DEFAULT, WASMTIME_VER_DEFAULT
22

3+
# Import env and create module-specific clone
4+
Import("env")
5+
6+
default_runtime = "wasmtime"
7+
if env["platform"] in ["web"]:
8+
default_runtime = "wamr"
9+
310
opts = Variables([], ARGUMENTS)
411

5-
opts.Add(EnumVariable("wasm_runtime", "Wasm runtime used", "wasmtime", ["wasmer", "wasmtime"]))
12+
opts.Add(EnumVariable("wasm_runtime", "Wasm runtime used", default_runtime, ["wasmer", "wasmtime", "wamr"]))
613
opts.Add(BoolVariable("download_runtime", "(Re)download runtime library", "no"))
714
opts.Add("runtime_version", "Runtime library version", None)
815

9-
# Import env and create module-specific clone
10-
Import("env")
16+
17+
1118
module_env = env.Clone()
1219
opts.Update(module_env)
1320

@@ -33,21 +40,29 @@ elif env["platform"] == "windows":
3340
env.Append(LINKFLAGS=["bcrypt.lib", "userenv.lib", "ws2_32.lib", "advapi32.lib", "ntdll.lib"])
3441

3542
# Explicit static libraries
36-
runtime_lib = env.File(
37-
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
38-
runtime=module_env["wasm_runtime"],
39-
prefix=env["LIBPREFIX"],
40-
suffix=env.get("LIBRUNTIMESUFFIX", env["LIBSUFFIX"]),
43+
if module_env["wasm_runtime"] != "wamr":
44+
runtime_lib = env.File(
45+
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
46+
runtime=module_env["wasm_runtime"],
47+
prefix=env["LIBPREFIX"],
48+
suffix=env.get("LIBRUNTIMESUFFIX", env["LIBSUFFIX"]),
49+
)
50+
)
51+
env.Append(LIBS=[runtime_lib])
52+
else:
53+
module_env.add_source_files(
54+
env.modules_sources, [env.Glob("wamr/src/*.c"), "wamr/stub.cpp"]
4155
)
42-
)
4356

4457
# Linked libraries (global env) and includes (cloned env)
45-
env.Append(LIBPATH=[env.Dir("{}/lib".format(module_env["wasm_runtime"])).abspath])
46-
env.Append(LIBS=[runtime_lib])
58+
if module_env["wasm_runtime"] != "wamr":
59+
env.Append(LIBPATH=[env.Dir("{}/lib".format(module_env["wasm_runtime"])).abspath])
60+
4761
module_env.Append(CPPPATH=[env.Dir("{}/include".format(module_env["wasm_runtime"])).abspath])
4862

4963
# Defines for module agnosticism
5064
module_env.Append(CPPDEFINES=["GODOT_MODULE", "LIBWASM_STATIC"])
65+
module_env.Append(CPPDEFINES=["WASM_RUNTIME_" + module_env["wasm_runtime"]])
5166

5267
# Module sources
5368
module_env.add_source_files(

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def can_build(env, platform):
2-
return platform in ["linux", "linuxbsd", "x11", "windows", "osx", "macos"]
2+
return platform in ["linux", "linuxbsd", "x11", "windows", "osx", "macos", "web"]
33

44

55
def configure(env):

src/store.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Singleton Wasm C API store
66
The same store is used between all compiled Wasm modules
77
*/
88

9-
#include <wasm.h>
9+
#include "wasm.h"
1010

1111
#define STORE ::godot_wasm::Store::instance().store
1212

src/wasi-shim.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
#define WASI_SHIM_H
33

44
#include <functional>
5-
#include <wasm.h>
5+
#if defined(WASM_RUNTIME_wamr)
6+
#include "wasm_c_api.h"
7+
#else
8+
#include "wasm.h"
9+
#endif
610
#include "defs.h"
711

812
namespace godot {

src/wasm-memory.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <wasm.h>
1+
#include "wasm.h"
22
#include "wasm-memory.h"
33
#include "store.h"
44

@@ -88,8 +88,9 @@ namespace godot {
8888

8989
godot_error WasmMemory::INTERFACE_GET_DATA {
9090
FAIL_IF(memory == NULL, "Invalid memory", ERR_INVALID_DATA);
91-
byte_t* data = wasm_memory_data(memory) + pointer;
92-
memcpy(buffer, data, bytes);
91+
byte_t* data = wasm_memory_data(memory);
92+
FAIL_IF(data == NULL, "Invalid memory state", ERR_INVALID_DATA);
93+
memcpy(buffer, data + pointer, bytes);
9394
pointer += bytes;
9495
#ifndef GODOT_MODULE
9596
*received = bytes;
@@ -109,8 +110,9 @@ namespace godot {
109110
godot_error WasmMemory::INTERFACE_PUT_DATA {
110111
FAIL_IF(memory == NULL, "Invalid memory", ERR_INVALID_DATA);
111112
if (bytes <= 0) return OK;
112-
byte_t* data = wasm_memory_data(memory) + pointer;
113-
memcpy(data, buffer, bytes);
113+
byte_t* data = wasm_memory_data(memory);
114+
FAIL_IF(data == NULL, "Invalid memory state", ERR_INVALID_DATA);
115+
memcpy(data + pointer, buffer, bytes);
114116
pointer += bytes;
115117
#ifndef GODOT_MODULE
116118
*sent = bytes;

src/wasm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,12 @@ namespace godot {
455455
}
456456
wasm_val_vec_t f_args;
457457
DEFER(wasm_val_vec_delete(&f_args));
458-
wasm_val_vec_new(&f_args, args_vec.size(), args_vec.data());
458+
wasm_val_vec_new(&f_args, args_vec.size(), args_vec.data());
459459

460460
// Construct return values
461461
wasm_val_vec_t f_results;
462462
DEFER(wasm_val_vec_delete(&f_results));
463-
wasm_val_vec_new_uninitialized(&f_results, context.return_count);
463+
wasm_val_vec_new_uninitialized(&f_results, context.return_count);
464464

465465
// Call function
466466
FAIL_IF(wasm_func_call(func, &f_args, &f_results), "Failed calling function " + name, NULL_VARIANT);

src/wasm.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
#define GODOT_WASM_H
33

44
#include <map>
5+
#if defined(WASM_RUNTIME_wamr)
6+
#include "wasm_c_api.h"
7+
#else
58
#include <wasm.h>
9+
#endif
610
#include "defs.h"
711
#include "wasm-memory.h"
812

utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
WASMTIME_BASE_URL = "https://github.com/bytecodealliance/wasmtime/releases/download/{0}/wasmtime-{0}-{1}-c-api.{2}"
1111
WASMTIME_VER_DEFAULT = "v36.0.2"
1212

13-
1413
def _validate_version(v):
1514
"""Validate semver string"""
1615
if not re.fullmatch(r"v\d+\.\d+\.\d+(-.+)?", v):

0 commit comments

Comments
 (0)