Skip to content

Instantly share code, notes, and snippets.

@MatthijsKamstra
Last active August 31, 2021 15:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthijsKamstra/cbae06ae1dc3561621e489ae4d7a8aab to your computer and use it in GitHub Desktop.
Save MatthijsKamstra/cbae06ae1dc3561621e489ae4d7a8aab to your computer and use it in GitHub Desktop.
Haxe and Webassemply
# build.hxml:
# Windows maybe needs this define
-D EMSCRIPTEN_SDK
# If you want the .html file showing how to embed the wasm
-D HXCPP_LINK_EMSCRIPTEN_EXT=.html
# Tell hxcpp to use emscripten-toolchain.xml
-D emscripten
-cpp out
-main Main

I didn't write this, but wanted to make sure the information didn't disappear. Currently the Haxe community doesn't use the google group anymore and this information was written down on that group.

I have tested it on OsX, and at first I tried brew but that failed (have no clue why) And after that I just followed the instructions on getting started on webassembly.

git clone https://github.com/juj/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest

and wait... a ... while ...

source ./emsdk_env.sh

and then it worked.

Probably you will need to install hxcpp

haxelib install hxcpp

The rest is written by Russ (that's all I know about the writer)

Here's a simple haxe 'Hello World' using wasm and the latest haxe+hxcpp.

First, visit http://webassembly.org/getting-started/developers-guide/ and follow the instructions there to setup Emscripten. Ensure the setup is working by doing the "Compile and run a simple program" step.

Then, add these two files to a project folder somewhere:

@:buildXml("
<linker id='exe' exe='emcc'>
<flag value='-s' />
<flag value='WASM=1' />
</linker>
")
class Main {
static function main() {
trace('Hello Web Assembly');
}
}

Next, setup Emscripten environment vars in your shell:

source /path/to/emsdk_env.sh

Then compile:

haxe build.hxml

And finally, run in the browser:

emrun out/Main.html

Or run in Node JS 8:

cd out
/path/to/nodejs8/node Main.js

Also check out the emscripten-toolchain.xml file in hxcpp for more Emscripten specific defines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment