Skip to content

Commit

Permalink
[WIP] Experiment with a WebAssembly distribution
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Oct 13, 2024
1 parent 325d8d5 commit c5c515f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ _deps
Brewfile.lock.json
.DS_Store
.cache
/bindings/*/build
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ endif()

if(PROJECT_IS_TOP_LEVEL)
noa_target_clang_format(SOURCES
bindings/*.cc
src/*.h src/*.cc
benchmark/*.h benchmark/*.cc
test/*.h test/*.cc)
Expand Down
26 changes: 26 additions & 0 deletions bindings/webassembly/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
EMCC = emcc
MKDIR = mkdir
NODE = node

CXX_FLAGS = -O3 -std=c++20 -flto

all: compile test
compile: build/jsontoolkit.js .always
test: test.js .always
$(NODE) $<

build:
$(MKDIR) $@
build/json: | build
$(MKDIR) $@

build/json/%.o: ../../src/json/%.cc | build/json
$(EMCC) $(CXX_FLAGS) -o $@ $< -I../../src/json/include -c

build/jsontoolkit.js: module.cc build/json/json.o build/json/json_value.o | build
$(EMCC) $(CXX_FLAGS) -o $@ $^ -I../../src/json/include -lembind \
-s EVAL_CTORS=2 -s EXPORT_NAME=jsontoolkit \
-s WASM=1 -s NO_EXIT_RUNTIME=1 -s MODULARIZE=1 \
-s EXPORT_ES6=1 -s ENVIRONMENT=node

.always:
17 changes: 17 additions & 0 deletions bindings/webassembly/module.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <sourcemeta/jsontoolkit/json.h>

// See https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html
#include <emscripten/bind.h>

#include <cstdlib> // EXIT_SUCCESS
#include <iostream> // std::cout

static auto hello() -> void {
const sourcemeta::jsontoolkit::JSON document{"Hello World"};
sourcemeta::jsontoolkit::stringify(document, std::cout);
std::cout << std::endl;
}

EMSCRIPTEN_BINDINGS(jsontoolkit) {
emscripten::function("hello", &hello);
}
4 changes: 4 additions & 0 deletions bindings/webassembly/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import wrapper from './build/jsontoolkit.js';
wrapper().then((jsontoolkit) => {
jsontoolkit.hello();
});
4 changes: 4 additions & 0 deletions src/json/include/sourcemeta/jsontoolkit/json.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef SOURCEMETA_JSONTOOLKIT_JSON_H_
#define SOURCEMETA_JSONTOOLKIT_JSON_H_

#ifdef __EMSCRIPTEN__
#define SOURCEMETA_JSONTOOLKIT_JSON_EXPORT
#else
#include "json_export.h"
#endif

#include <sourcemeta/jsontoolkit/json_error.h>
#include <sourcemeta/jsontoolkit/json_value.h>
Expand Down
4 changes: 4 additions & 0 deletions src/json/include/sourcemeta/jsontoolkit/json_error.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef SOURCEMETA_JSONTOOLKIT_JSON_ERROR_H_
#define SOURCEMETA_JSONTOOLKIT_JSON_ERROR_H_

#ifdef __EMSCRIPTEN__
#define SOURCEMETA_JSONTOOLKIT_JSON_EXPORT
#else
#include "json_export.h"
#endif

#include <cstdint> // std::uint64_t
#include <exception> // std::exception
Expand Down
4 changes: 4 additions & 0 deletions src/json/include/sourcemeta/jsontoolkit/json_value.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef SOURCEMETA_JSONTOOLKIT_JSON_VALUE_H_
#define SOURCEMETA_JSONTOOLKIT_JSON_VALUE_H_

#ifdef __EMSCRIPTEN__
#define SOURCEMETA_JSONTOOLKIT_JSON_EXPORT
#else
#include "json_export.h"
#endif

#include <sourcemeta/jsontoolkit/json_array.h>
#include <sourcemeta/jsontoolkit/json_object.h>
Expand Down

0 comments on commit c5c515f

Please sign in to comment.