diff --git a/init_tinygowasm.go b/init_tinygowasm.go new file mode 100644 index 0000000..2594a84 --- /dev/null +++ b/init_tinygowasm.go @@ -0,0 +1,8 @@ +//go:build tinygo.wasm + +package wasilibs + +/* +#cgo LDFLAGS: -Linternal/wasm -lcre2 -lre2 -lc++ -lc++abi -lclang_rt.builtins-wasm32 +*/ +import "C" diff --git a/internal/wasm/libc++.a b/internal/wasm/libc++.a new file mode 100644 index 0000000..5f64756 Binary files /dev/null and b/internal/wasm/libc++.a differ diff --git a/internal/wasm/libc++abi.a b/internal/wasm/libc++abi.a new file mode 100644 index 0000000..fbe1ec1 Binary files /dev/null and b/internal/wasm/libc++abi.a differ diff --git a/internal/wasm/libclang_rt.builtins-wasm32.a b/internal/wasm/libclang_rt.builtins-wasm32.a new file mode 100644 index 0000000..be09105 Binary files /dev/null and b/internal/wasm/libclang_rt.builtins-wasm32.a differ diff --git a/internal/wasm/libcre2.a b/internal/wasm/libcre2.a new file mode 100644 index 0000000..c5f3de5 Binary files /dev/null and b/internal/wasm/libcre2.a differ diff --git a/internal/wasm/libre2.a b/internal/wasm/libre2.a new file mode 100644 index 0000000..9f95c13 Binary files /dev/null and b/internal/wasm/libre2.a differ diff --git a/malloc_tinygowasm.go b/malloc_tinygowasm.go new file mode 100644 index 0000000..3de3eb1 --- /dev/null +++ b/malloc_tinygowasm.go @@ -0,0 +1,54 @@ +//go:build tinygo.wasm && !gc.custom && !custommalloc + +package wasilibs + +import ( + "unsafe" +) + +/* +#include +void* malloc(size_t size); +void free(void* ptr); +*/ +import "C" + +// TinyGo currently only includes a subset of malloc functions by default, so we +// reimplement the remaining here. + +//export posix_memalign +func posix_memalign(memptr *uint32, align uint32, size uint32) int32 { + if align < 4 { + return 22 /* EINVAL */ + } + + // Ignore alignment and hope for best, TinyGo by default does not + // provide a way to allocate aligned memory. + mem := uint32(uintptr(C.malloc(C.size_t(size)))) + if mem == 0 { + // TODO(anuraaga): Needs to read errno to be precise + return 1 + } + + *memptr = mem + + return 0 +} + +//export calloc +func calloc(num uint32, size uint32) uint32 + +//export __libc_calloc +func __libc_calloc(num uint32, size uint32) uint32 { + return calloc(num, size) +} + +//export __libc_malloc +func __libc_malloc(size uint32) uint32 { + return uint32(uintptr(C.malloc(C.size_t(size)))) +} + +//export __libc_free +func __libc_free(ptr uint32) { + C.free(unsafe.Pointer(uintptr(ptr))) +}