Skip to content

Commit

Permalink
Repo cleanup (#56)
Browse files Browse the repository at this point in the history
* Fix Wasm consume example memory

* Remove media from repo

* Update README
  • Loading branch information
ashtonmeuser authored Oct 14, 2023
1 parent 2bebc26 commit 6cefb04
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Godot Wasm

<p align="center">
<img width="200" src="https://raw.githubusercontent.com/ashtonmeuser/godot-wasm/master/media/Icon.png" alt="Godot Wasm Logo">
<img width="200" src="https://github.com/ashtonmeuser/godot-wasm/assets/7253863/201300b3-41bc-4c54-b649-01d325ff8b69" alt="Godot Wasm Logo">
</p>
<p align="center">
<a href="https://github.com/ashtonmeuser/godot-wasm/actions/workflows/addon.yml">
Expand All @@ -20,18 +20,34 @@
A Godot extension allowing for loading and interacting with [WebAssembly (Wasm)](https://webassembly.org) modules from GDScript via the [Wasmer](https://wasmer.io) and [Wasmtime](https://wasmtime.dev) WebAssembly runtimes.

## Features

- Compile and instantiate WebAssembly modules
- Access exported Wasm functions and variables
- Write to and read from Wasm memory
- Wasmer and Wasmtime runtime support
- Install as Godot module or GDExtension addon
- Limited WASI support
- External (shared) Wasm memory support

## Motivation

- **Language Agnosticism.** With [dozens of supported languages](https://github.com/appcypher/awesome-wasm-langs), WebAssembly makes a versatile common build target. Create modules in Rust, Go, TypeScript, C++, etc. and seamlessly integrate them into your Godot project. A single Wasm module runs on multiple platforms and architectures e.g. Windows x86, Windows x64, macOS ARM, macOS x86, Linux, etc.
- **Sandboxed Environment.** WebAssembly operates within a [sandboxed VM](https://webassembly.org/docs/security/), allowing you to safely run modules from untrusted sources without jeopardizing your users' safety. This opens the door for zero-trust mods and plugins for your Godot project.
- **Speed.** WebAssembly runs incredibly fast at near-native speeds. Extensions become remarkably resource-efficient without the need to recompile the Godot engine. Mods can approach native speed regardless of source language. Compute-intensive operations can run orders of magnitude [faster in Wasm than in GDScript](https://github.com/ashtonmeuser/godot-wasm/wiki/Benchmarks).

## Documentation

Please refer to the [Godot Wasm wiki](https://github.com/ashtonmeuser/godot-wasm/wiki) for guides, FAQs, class documentation, etc.

## Getting Started
## Quick Start

Godot Wasm can be used as a [GDExtension/GDNative addon](https://docs.godotengine.org/en/4.0/) or [Godot module](https://docs.godotengine.org/en/4.0/contributing/development/core_and_modules/custom_modules_in_cpp.html). See the [Installation wiki page](https://github.com/ashtonmeuser/godot-wasm/wiki/Getting-Started#installation) for full instructions.

Using Godot Wasm involves the following.
1. Create a WebAssembly (Wasm) module using a language of your choice. See [FAQ](https://github.com/ashtonmeuser/godot-wasm/wiki/FAQs#how-do-i-build-a-wasm-module) for more information.
Using Godot Wasm involves the following. See the [Usage wiki page](https://github.com/ashtonmeuser/godot-wasm/wiki/Getting-Started#usage) for full instructions.
1. Create a WebAssembly (Wasm) module using a language of your choice. See [FAQ](https://github.com/ashtonmeuser/godot-wasm/wiki/FAQs#how-do-i-build-a-wasm-module) for more information. Alternatively, a simple [test module](https://github.com/ashtonmeuser/godot-wasm/blob/master/examples/wasm-test/wasm/simple.wasm) can be used.
1. Create a new Godot Wasm instance, read your Wasm module bytecode, and instantiate the Godot Wasm module. The following assumes a Wasm module that requires no imports.
```
```gdscript
var wasm = Wasm.new()
var file = FileAccess.open("res://my_module.wasm", FileAccess.READ)
var bytecode = file.get_buffer(file.get_length())
Expand All @@ -43,7 +59,7 @@ See the [Usage wiki page](https://github.com/ashtonmeuser/godot-wasm/wiki/Gettin
## Known Issues
1. A small subset of [WASI](https://wasmbyexample.dev/examples/wasi-introduction/wasi-introduction.all.en-us.html) bindings are provided to the Wasm module by default. These can be overridden by the imports supplied on module instantiation. The guest Wasm module has no access to the host machines filesystem, etc. Pros for this are simplicity and increased security. Cons include not being able to generate truly random numbers (without a workaround) or run Wasm modules created in ways that require a larger set of WASI bindings e.g. [TinyGo](https://tinygo.org/docs/guides/webassembly/) (see relevant [issue](https://github.com/tinygo-org/tinygo/issues/3068)).
1. A small subset of [WASI](https://wasmbyexample.dev/examples/wasi-introduction/wasi-introduction.all.en-us.html) bindings are provided to the Wasm module by default. These can be overridden by the imports supplied on module instantiation. The guest Wasm module has no access to the host machines filesystem, etc. Pros for this are simplicity and increased security. Cons include more work required to run Wasm modules created in ways that require a larger set of WASI bindings e.g. [TinyGo](https://tinygo.org/docs/guides/webassembly/) (see relevant [issue](https://github.com/tinygo-org/tinygo/issues/3068)).
1. Only `int` and `float` return values are supported. While workarounds could be used, this limitation is because the only [concrete types supported by Wasm](https://webassembly.github.io/spec/core/syntax/types.html#number-types) are integers and floating point.
1. A default empty `args` parameter for `function(name, args)` can not be supplied. Default `Array` parameters in GDNative seem to retain values between calls. Calling methods of this addon without expected arguments produces undefined behaviour. This is reliant on [godotengine/godot-cpp#209](https://github.com/godotengine/godot-cpp/issues/209).
1. Web/HTML5 export is not supported (see [#15](https://github.com/ashtonmeuser/godot-wasm/issues/15) and [#18](https://github.com/ashtonmeuser/godot-wasm/issues/18)).
Expand All @@ -53,7 +69,7 @@ See the [Usage wiki page](https://github.com/ashtonmeuser/godot-wasm/wiki/Gettin
There have been numerous discussions around modding/sandboxing support for Godot. Some of those are included below.
- [Proposal](https://github.com/godotengine/godot-proposals/issues/5010): Implement a sandbox mode
- [Issue](https://github.com/godotengine/godot/issues/28303) Add support for WebAssembly plugins and scripts
- [Proposal](https://github.com/godotengine/godot-proposals/issues/147) Add WASM (WASI) host support (including, but not limited to, the HTML5 target)
- [Issue](https://github.com/godotengine/godot/issues/28303): Add support for WebAssembly plugins and scripts
- [Proposal](https://github.com/godotengine/godot-proposals/issues/147): Add WASM (WASI) host support (including, but not limited to, the HTML5 target)
- [Proposal](https://github.com/godotengine/godot-proposals/issues/4642): Add a method to disallow using all global classes in a particular GDScript
- [Pull Request](https://github.com/godotengine/godot/pull/61831): Added globals disabled feature to GDScript class
7 changes: 4 additions & 3 deletions examples/wasm-consume/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func _update_info():
if info.is_empty():
$"%InfoText".set("text", "Error")
return
if !info.has("memory"): info["memory"] = {}
var memory_info = ""
if info.has("memory_min"): memory_info += "\nMin %s" % _pretty_bytes(info.memory_min)
if info.has("memory_max"): memory_info += "\nMax %s" % _pretty_bytes(info.memory_max)
if info.has("memory_current"): memory_info += "\nCurrent %s" % _pretty_bytes(info.memory_current)
if info.memory.has("min"): memory_info += "\nMin %s" % _pretty_bytes(info.memory.min)
if info.memory.has("max"): memory_info += "\nMax %s" % _pretty_bytes(info.memory.max)
if info.memory.has("current"): memory_info += "\nCurrent %s" % _pretty_bytes(info.memory.current)
$"%InfoText".text = info_template % [
_pretty_signatures({}),
_pretty_signatures(info.import_functions),
Expand Down
9 changes: 0 additions & 9 deletions examples/wasm-consume/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

[sub_resource type="FontFile" id="4"]
fallbacks = Array[Font]([ExtResource("2")])
face_index = null
embolden = null
transform = null
cache/0/16/0/ascent = 0.0
cache/0/16/0/descent = 0.0
cache/0/16/0/underline_position = 0.0
Expand All @@ -27,9 +24,6 @@ expand_margin_right = 10.0

[sub_resource type="FontFile" id="6"]
fallbacks = Array[Font]([ExtResource("3")])
face_index = null
embolden = null
transform = null
cache/0/16/0/ascent = 0.0
cache/0/16/0/descent = 0.0
cache/0/16/0/underline_position = 0.0
Expand All @@ -55,9 +49,6 @@ expand_margin_right = 2.0

[sub_resource type="FontFile" id="7"]
fallbacks = Array[Font]([ExtResource("3")])
face_index = null
embolden = null
transform = null
cache/0/16/0/ascent = 0.0
cache/0/16/0/descent = 0.0
cache/0/16/0/underline_position = 0.0
Expand Down
43 changes: 31 additions & 12 deletions examples/wasm-consume/Theme.tres

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/wasm-consume/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ config_version=5

config/name="Wasm Consume"
run/main_scene="res://Main.tscn"
config/features=PackedStringArray("4.0")
config/features=PackedStringArray("4.1")

[debug]

Expand Down
5 changes: 2 additions & 3 deletions examples/wasm-visualizations/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ corner_detail = 5

[sub_resource type="FontFile" id="5"]
fallbacks = Array[Font]([ExtResource("2")])
face_index = null
embolden = null
transform = null
cache/0/16/0/ascent = 0.0
cache/0/16/0/descent = 0.0
cache/0/16/0/underline_position = 0.0
Expand All @@ -38,6 +35,8 @@ layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1")

[node name="TextureRect" type="TextureRect" parent="."]
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm-visualizations/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ config_version=5

config/name="Wasm Visualizations"
run/main_scene="res://Main.tscn"
config/features=PackedStringArray("4.0")
config/features=PackedStringArray("4.1")

[debug]

Expand Down
Binary file removed media/Icon.png
Binary file not shown.
Binary file removed media/Screenshot.png
Binary file not shown.

0 comments on commit 6cefb04

Please sign in to comment.