Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ templates/
*.exp
*.lib
__pycache__
compile_commands.json
21 changes: 13 additions & 8 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from utils import download_wasmer, download_wasmtime, WASMER_VER_DEFAULT, WASMTI
opts = Variables([], ARGUMENTS)

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

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

env.Append(CPPDEFINES=["WASM_RUNTIME_" + env["wasm_runtime"]])

# Godot Wasm sources
source = ["register_types.cpp", env.Glob("src/*.cpp")]

# Explicit static libraries
runtime_lib = env.File(
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
if env["wasm_runtime"] != "wamr":
runtime_lib = env.File(
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
runtime=env["wasm_runtime"],
prefix=env["LIBPREFIX"],
suffix=env.get("LIBRUNTIMESUFFIX", env["LIBSUFFIX"]),
)
)
)
env.Append(LIBS=[runtime_lib])
else:
source += [env.Glob("wamr/src/*.c"), "wamr/stub.cpp"]

# CPP includes and libraries
env.Append(CPPPATH=[".", "{}/include".format(env["wasm_runtime"])])
env.Append(LIBS=[runtime_lib])

# Godot Wasm sources
source = ["register_types.cpp", env.Glob("src/*.cpp")]

# Builders
library = env.SharedLibrary(target="addons/godot-wasm/bin/{}/godot-wasm".format(env["platform"]), source=source)
Expand Down
37 changes: 26 additions & 11 deletions SCsub
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from utils import download_wasmer, download_wasmtime, WASMER_VER_DEFAULT, WASMTIME_VER_DEFAULT

# Import env and create module-specific clone
Import("env")

default_runtime = "wasmtime"
if env["platform"] in ["web"]:
default_runtime = "wamr"

opts = Variables([], ARGUMENTS)

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

# Import env and create module-specific clone
Import("env")


module_env = env.Clone()
opts.Update(module_env)

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

# Explicit static libraries
runtime_lib = env.File(
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
runtime=module_env["wasm_runtime"],
prefix=env["LIBPREFIX"],
suffix=env.get("LIBRUNTIMESUFFIX", env["LIBSUFFIX"]),
if module_env["wasm_runtime"] != "wamr":
runtime_lib = env.File(
"{runtime}/lib/{prefix}{runtime}{suffix}".format(
runtime=module_env["wasm_runtime"],
prefix=env["LIBPREFIX"],
suffix=env.get("LIBRUNTIMESUFFIX", env["LIBSUFFIX"]),
)
)
env.Append(LIBS=[runtime_lib])
else:
module_env.add_source_files(
env.modules_sources, [env.Glob("wamr/src/*.c"), "wamr/stub.cpp"]
)
)

# Linked libraries (global env) and includes (cloned env)
env.Append(LIBPATH=[env.Dir("{}/lib".format(module_env["wasm_runtime"])).abspath])
env.Append(LIBS=[runtime_lib])
if module_env["wasm_runtime"] != "wamr":
env.Append(LIBPATH=[env.Dir("{}/lib".format(module_env["wasm_runtime"])).abspath])

module_env.Append(CPPPATH=[env.Dir("{}/include".format(module_env["wasm_runtime"])).abspath])

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

# Module sources
module_env.add_source_files(
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def can_build(env, platform):
return platform in ["linux", "linuxbsd", "x11", "windows", "osx", "macos"]
return platform in ["linux", "linuxbsd", "x11", "windows", "osx", "macos", "web"]


def configure(env):
Expand Down
2 changes: 1 addition & 1 deletion src/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Singleton Wasm C API store
The same store is used between all compiled Wasm modules
*/

#include <wasm.h>
#include "wasm.h"

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

Expand Down
6 changes: 5 additions & 1 deletion src/wasi-shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#define WASI_SHIM_H

#include <functional>
#include <wasm.h>
#if defined(WASM_RUNTIME_wamr)
#include "wasm_c_api.h"
#else
#include "wasm.h"
#endif
#include "defs.h"

namespace godot {
Expand Down
12 changes: 7 additions & 5 deletions src/wasm-memory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <wasm.h>
#include "wasm.h"
#include "wasm-memory.h"
#include "store.h"

Expand Down Expand Up @@ -88,8 +88,9 @@ namespace godot {

godot_error WasmMemory::INTERFACE_GET_DATA {
FAIL_IF(memory == NULL, "Invalid memory", ERR_INVALID_DATA);
byte_t* data = wasm_memory_data(memory) + pointer;
memcpy(buffer, data, bytes);
byte_t* data = wasm_memory_data(memory);
FAIL_IF(data == NULL, "Invalid memory state", ERR_INVALID_DATA);
memcpy(buffer, data + pointer, bytes);
pointer += bytes;
#ifndef GODOT_MODULE
*received = bytes;
Expand All @@ -109,8 +110,9 @@ namespace godot {
godot_error WasmMemory::INTERFACE_PUT_DATA {
FAIL_IF(memory == NULL, "Invalid memory", ERR_INVALID_DATA);
if (bytes <= 0) return OK;
byte_t* data = wasm_memory_data(memory) + pointer;
memcpy(data, buffer, bytes);
byte_t* data = wasm_memory_data(memory);
FAIL_IF(data == NULL, "Invalid memory state", ERR_INVALID_DATA);
memcpy(data + pointer, buffer, bytes);
pointer += bytes;
#ifndef GODOT_MODULE
*sent = bytes;
Expand Down
4 changes: 2 additions & 2 deletions src/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,12 @@ namespace godot {
}
wasm_val_vec_t f_args;
DEFER(wasm_val_vec_delete(&f_args));
wasm_val_vec_new(&f_args, args_vec.size(), args_vec.data());
wasm_val_vec_new(&f_args, args_vec.size(), args_vec.data());

// Construct return values
wasm_val_vec_t f_results;
DEFER(wasm_val_vec_delete(&f_results));
wasm_val_vec_new_uninitialized(&f_results, context.return_count);
wasm_val_vec_new_uninitialized(&f_results, context.return_count);

// Call function
FAIL_IF(wasm_func_call(func, &f_args, &f_results), "Failed calling function " + name, NULL_VARIANT);
Expand Down
4 changes: 4 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#define GODOT_WASM_H

#include <map>
#if defined(WASM_RUNTIME_wamr)
#include "wasm_c_api.h"
#else
#include <wasm.h>
#endif
#include "defs.h"
#include "wasm-memory.h"

Expand Down
1 change: 0 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
WASMTIME_BASE_URL = "https://github.com/bytecodealliance/wasmtime/releases/download/{0}/wasmtime-{0}-{1}-c-api.{2}"
WASMTIME_VER_DEFAULT = "v36.0.2"


def _validate_version(v):
"""Validate semver string"""
if not re.fullmatch(r"v\d+\.\d+\.\d+(-.+)?", v):
Expand Down
Loading