Skip to content

Commit

Permalink
Use hoodmane/hiwire library instead of our own implementation (pyodid…
Browse files Browse the repository at this point in the history
…e#4128)

This change switches to my external implementation of hiwire. This is the
minimal change set to do this, it uses some hacks to avoid changing any files
outside of `hiwire.{c,h,js}`. In followups, I will gradually switch to using
the new APIs rather than compatibility shims.
  • Loading branch information
hoodmane authored Sep 24, 2023
1 parent a24d69e commit b460383
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 473 deletions.
3 changes: 3 additions & 0 deletions Makefile.envs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) \
-lworkerfs.js \
-lwebsocket.js \
-leventloop.js \
-lhiwire \
\
-lGL \
-legl.js \
Expand All @@ -165,7 +166,9 @@ export MAIN_MODULE_CFLAGS= $(CFLAGS_BASE) \
-Werror=int-conversion \
-Werror=incompatible-pointer-types \
-Werror=unused-result \
-mreference-types \
-I$(PYTHONINCLUDE) \
-I$(PYTHONINCLUDE)/.. \
-s EXCEPTION_CATCHING_ALLOWED=['we only want to allow exception handling in side modules']

export STDLIB_MODULE_CFLAGS= $(SIDE_MODULE_CFLAGS) -I Include/ -I . -I Include/internal/
Expand Down
20 changes: 19 additions & 1 deletion cpython/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ FFIBUILD=$(ROOT)/build/libffi
LIBFFIREPO=https://github.com/libffi/libffi
LIBFFI_COMMIT=f08493d249d2067c8b3207ba46693dd858f95db3

HIWIREBUILD=$(ROOT)/build/hiwire
HIWIREREPO=https://github.com/hoodmane/hiwire
HIWIRE_COMMIT=49f3450e34f3f50d4b8296e782dc321bb2e3264e

ifdef CPYTHON_DEBUG
MAYBE_WITH_PYDEBUG=--with-pydebug
endif

all: $(INSTALL)/lib/$(LIB) $(INSTALL)/lib/libffi.a
all: $(INSTALL)/lib/$(LIB) $(INSTALL)/lib/libffi.a $(INSTALL)/lib/libhiwire.a


$(INSTALL)/lib/$(LIB): $(BUILD)/$(LIB)
Expand Down Expand Up @@ -84,6 +88,20 @@ $(INSTALL)/lib/libffi.a :
mkdir -p $(INSTALL)/lib
cp $(FFIBUILD)/target/lib/libffi.a $(INSTALL)/lib/

$(INSTALL)/lib/libhiwire.a :
rm -rf $(HIWIREBUILD)
mkdir $(HIWIREBUILD)
(\
cd $(HIWIREBUILD) \
&& git init \
&& git fetch --depth 1 $(HIWIREREPO) $(HIWIRE_COMMIT) \
&& git checkout FETCH_HEAD \
&& . $(PYODIDE_ROOT)/emsdk/emsdk/emsdk_env.sh \
&& CC=emcc EMSCRIPTEN_DEDUPLICATE=1 EXTERN_FAIL=1 make \
)
cp -r $(HIWIREBUILD)/dist/lib $(INSTALL)/
cp -r $(HIWIREBUILD)/dist/include $(INSTALL)/include/hiwire

$(BUILD)/Makefile: $(BUILD)/.patched
# --enable-big-digits=30 :
# Python integers have "digits" of size 15 by default on systems with 32
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
From 04dd736fe3ea0d469b84800585eaa4ca6648b9f9 Mon Sep 17 00:00:00 2001
From: Hood Chatham <[email protected]>
Date: Thu, 31 Aug 2023 13:53:39 +0200
Subject: [PATCH] Support externref in EM_JS functions with dynamic linking

---
emscripten.py | 1 +
src/library_addfunction.js | 1 +
test/core/test_externref2.c | 22 ++++++++++++++++++++++
test/core/test_externref2.out | 2 ++
test/test_core.py | 14 ++++++++++++++
5 files changed, 40 insertions(+)
create mode 100644 test/core/test_externref2.c
create mode 100644 test/core/test_externref2.out

diff --git a/emscripten.py b/emscripten.py
index f011a58b8..74ea6f525 100644
--- a/emscripten.py
+++ b/emscripten.py
@@ -601,6 +601,7 @@ def type_to_sig(type):
webassembly.Type.I64: 'j',
webassembly.Type.F32: 'f',
webassembly.Type.F64: 'd',
+ webassembly.Type.EXTERNREF: 'e',
webassembly.Type.VOID: 'v'
}[type]

diff --git a/src/library_addfunction.js b/src/library_addfunction.js
index 39e00b772..1537dca66 100644
--- a/src/library_addfunction.js
+++ b/src/library_addfunction.js
@@ -29,6 +29,7 @@ addToLibrary({
'j': 'i64',
'f': 'f32',
'd': 'f64',
+ 'e': 'externref',
#if MEMORY64
'p': 'i64',
#else
diff --git a/test/core/test_externref2.c b/test/core/test_externref2.c
new file mode 100644
index 000000000..47451c260
--- /dev/null
+++ b/test/core/test_externref2.c
@@ -0,0 +1,22 @@
+#include "emscripten.h"
+
+
+EM_JS(__externref_t, get_ref, (), {
+ return {a: 7, b: 9};
+});
+
+EM_JS(void, modify_ref, (__externref_t arg), {
+ arg.a += 3;
+ arg.b -= 3;
+});
+
+EM_JS(void, log_ref, (__externref_t arg), {
+ console.log(arg);
+});
+
+int main() {
+ __externref_t a = get_ref();
+ log_ref(a);
+ modify_ref(a);
+ log_ref(a);
+}
diff --git a/test/core/test_externref2.out b/test/core/test_externref2.out
new file mode 100644
index 000000000..eaceb4e73
--- /dev/null
+++ b/test/core/test_externref2.out
@@ -0,0 +1,2 @@
+{ a: 7, b: 9 }
+{ a: 10, b: 6 }
diff --git a/test/test_core.py b/test/test_core.py
index 2f776068d..8b1933bf9 100644
--- a/test/test_core.py
+++ b/test/test_core.py
@@ -9699,6 +9699,20 @@ NODEFS is no longer included by default; build with -lnodefs.js
self.emcc_args += ['-mreference-types']
self.do_core_test('test_externref.c', libraries=['asm.o'])

+ @parameterized({
+ '': [False],
+ 'dynlink': [True]
+ })
+ @requires_node
+ @no_wasm2js('wasm2js does not support reference types')
+ def test_externref2(self, dynlink):
+ self.emcc_args += ['-mreference-types']
+ self.node_args.append("--experimental-wasm-reftypes")
+ if dynlink:
+ self.set_setting('MAIN_MODULE', 2)
+ self.do_core_test('test_externref2.c')
+
+
def test_syscall_intercept(self):
self.do_core_test('test_syscall_intercept.c')

--
2.25.1

8 changes: 4 additions & 4 deletions src/core/error_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ console_error_obj(JsRef obj);
#endif

// Need an extra layer to expand LOG_EM_JS_ERROR.
#define EM_JS_DEFER(ret, func_name, args, body...) \
#define EM_JS_MACROS(ret, func_name, args, body...) \
EM_JS(ret, func_name, args, body)

#define EM_JS_UNCHECKED(ret, func_name, args, body...) \
Expand All @@ -101,7 +101,7 @@ console_error_obj(JsRef obj);
#define WARN_UNUSED __attribute__((warn_unused_result))

#define EM_JS_REF(ret, func_name, args, body...) \
EM_JS_DEFER(ret WARN_UNUSED, func_name, args, { \
EM_JS_MACROS(ret WARN_UNUSED, func_name, args, { \
try /* intentionally no braces, body already has them */ \
body /* <== body of func */ \
catch (e) { \
Expand All @@ -115,7 +115,7 @@ console_error_obj(JsRef obj);
})

#define EM_JS_NUM(ret, func_name, args, body...) \
EM_JS_DEFER(ret WARN_UNUSED, func_name, args, { \
EM_JS_MACROS(ret WARN_UNUSED, func_name, args, { \
try /* intentionally no braces, body already has them */ \
body /* <== body of func */ \
catch (e) { \
Expand All @@ -128,7 +128,7 @@ console_error_obj(JsRef obj);

// If there is a Js error, catch it and return false.
#define EM_JS_BOOL(ret, func_name, args, body...) \
EM_JS_DEFER(ret WARN_UNUSED, func_name, args, { \
EM_JS_MACROS(ret WARN_UNUSED, func_name, args, { \
try /* intentionally no braces, body already has them */ \
body /* <== body of func */ \
catch (e) { \
Expand Down
Loading

0 comments on commit b460383

Please sign in to comment.