diff --git a/wasmer/include/libwasmer_c_api.dylib b/wasmer/include/libwasmer_c_api.dylib index 702062bf..68a33dc9 100755 Binary files a/wasmer/include/libwasmer_c_api.dylib and b/wasmer/include/libwasmer_c_api.dylib differ diff --git a/wasmer/module.go b/wasmer/module.go index f190f882..2230a6e9 100644 --- a/wasmer/module.go +++ b/wasmer/module.go @@ -13,6 +13,14 @@ package wasmer // // return wasm_module_new(store, &wasm_bytes); // } +// +// bool to_wasm_module_validate(wasm_store_t *store, uint8_t *bytes, size_t bytes_length) { +// wasm_byte_vec_t wasm_bytes; +// wasm_bytes.size = bytes_length; +// wasm_bytes.data = (wasm_byte_t*) bytes; +// +// return wasm_module_validate(store, &wasm_bytes); +// } import "C" import ( "runtime" @@ -38,9 +46,6 @@ type Module struct { } func NewModule(store *Store, bytes []byte) (*Module, error) { - // If `bytes` contains a Wasm module with the WAT format, - // compile it to Wasm bytes. - // If it does not, it will return the same bytes. wasmBytes, err := Wat2Wasm(string(bytes)) if err != nil { @@ -71,6 +76,32 @@ func NewModule(store *Store, bytes []byte) (*Module, error) { return module, nil } +func ValidateModule(store *Store, bytes []byte) error { + wasmBytes, err := Wat2Wasm(string(bytes)) + + if err != nil { + return err + } + + var wasmBytesPtr *C.uint8_t + wasmBytesLength := len(wasmBytes) + + if wasmBytesLength > 0 { + wasmBytesPtr = (*C.uint8_t)(unsafe.Pointer(&wasmBytes[0])) + } + + isValid := C.to_wasm_module_validate(store.inner(), wasmBytesPtr, C.size_t(wasmBytesLength)) + + runtime.KeepAlive(bytes) + runtime.KeepAlive(wasmBytes) + + if !isValid { + return newErrorFromWasmer() + } + + return nil +} + func (module *Module) inner() *C.wasm_module_t { return module._inner } diff --git a/wasmer/module_test.go b/wasmer/module_test.go index 552d681f..32c5adcb 100644 --- a/wasmer/module_test.go +++ b/wasmer/module_test.go @@ -12,3 +12,11 @@ func TestModule(t *testing.T) { assert.NoError(t, err) } + +func TestValidateModule(t *testing.T) { + engine := NewEngine() + store := NewStore(engine) + err := ValidateModule(store, []byte("(module)")) + + assert.NoError(t, err) +}