forked from wasmerio/wasmer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It depends on wasmerio/wasmer#1635. The `include/` and `wasmer_wasm.h` files are already from this PR.
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package wasmer | ||
|
||
// #include <wasmer_wasm.h> | ||
// | ||
// void to_wat2wasm(uint8_t *wat_data, size_t wat_length, wasm_byte_vec_t *wasm) { | ||
// wasm_byte_vec_t wat; | ||
// wat.size = wat_length; | ||
// wat.data = (wasm_byte_t*) wat_data; | ||
// | ||
// wat2wasm(&wat, wasm); | ||
// } | ||
import "C" | ||
import ( | ||
"runtime" | ||
"unsafe" | ||
) | ||
|
||
func Wat2Wasm(wat string) ([]byte, error) { | ||
watAsBytes := []byte(wat) | ||
var watPtr *C.uint8_t | ||
var watLength = len(wat) | ||
|
||
if watLength > 0 { | ||
watPtr = (*C.uint8_t)(unsafe.Pointer(&watAsBytes[0])) | ||
} | ||
|
||
wasm := C.wasm_byte_vec_t{} | ||
|
||
C.to_wat2wasm(watPtr, C.size_t(watLength), &wasm) | ||
runtime.KeepAlive(wat) | ||
|
||
if wasm.data == nil { | ||
return nil, newErrorFromWasmer() | ||
} | ||
|
||
wasmBytes := C.GoBytes(unsafe.Pointer(wasm.data), C.int(wasm.size)) | ||
C.wasm_byte_vec_delete(&wasm) | ||
|
||
return wasmBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package wasmer | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestWat2Wasm(t *testing.T) { | ||
wasm, err := Wat2Wasm("(module)") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, wasm, []byte("\x00asm\x01\x00\x00\x00")) | ||
} | ||
|
||
func TestWasm2Wasm(t *testing.T) { | ||
wasm, err := Wat2Wasm(string([]byte("\x00asm\x01\x00\x00\x00"))) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, wasm, []byte("\x00asm\x01\x00\x00\x00")) | ||
} | ||
|
||
func TestBadWat2Wasm(t *testing.T) { | ||
_, err := Wat2Wasm("(module") | ||
|
||
assert.EqualError(t, err, "expected `)`\n --> <anon>:1:8\n |\n 1 | (module\n | ^") | ||
} |