Skip to content

Commit

Permalink
Merge #38
Browse files Browse the repository at this point in the history
38: fix(instance) Handle exported functions that return nothing r=Hywan a=Hywan

WebAssembly “void” is mapped to `None` in Python.

Co-authored-by: Ivan Enderlin <ivan.enderlin@hoa-project.net>
  • Loading branch information
bors[bot] and Hywan committed May 13, 2019
2 parents 1f8fac4 + 4d1fb48 commit a31e422
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -85,7 +85,7 @@ from wasmer import Instance
# Get the Wasm module as bytes.
wasm_bytes = open('my_program.wasm', 'rb').read()

# Instantiates the Wasm module.
# Instantiate the Wasm module.
instance = Instance(wasm_bytes)

# Call a function on it.
Expand Down Expand Up @@ -193,7 +193,7 @@ from wasmer import Instance
# Get the Wasm module as bytes.
wasm_bytes = open('my_program.wasm', 'rb').read()

# Instantiates the Wasm module.
# Instantiate the Wasm module.
instance = Instance(wasm_bytes)

# Call a function that returns a pointer to a string for instance.
Expand Down
16 changes: 10 additions & 6 deletions src/instance.rs
Expand Up @@ -111,12 +111,16 @@ impl ExportedFunction {
};

// Map the WebAssembly first result to a Python value.
Ok(match results[0] {
WasmValue::I32(result) => result.to_object(py),
WasmValue::I64(result) => result.to_object(py),
WasmValue::F32(result) => result.to_object(py),
WasmValue::F64(result) => result.to_object(py),
})
if results.len() > 0 {
Ok(match results[0] {
WasmValue::I32(result) => result.to_object(py),
WasmValue::I64(result) => result.to_object(py),
WasmValue::F32(result) => result.to_object(py),
WasmValue::F64(result) => result.to_object(py),
})
} else {
Ok(py.None())
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/test_instance.py
Expand Up @@ -65,6 +65,9 @@ def test_call_bool_casted_to_i32():
def test_call_string():
assert Instance(TEST_BYTES).exports.string() == 1048576

def test_call_void():
assert Instance(TEST_BYTES).exports.void() == None

def test_validate():
assert validate(TEST_BYTES)

Expand Down
3 changes: 3 additions & 0 deletions tests/tests.rs
Expand Up @@ -42,3 +42,6 @@ pub extern fn bool_casted_to_i32() -> bool {
pub extern fn string() -> *const u8 {
b"Hello, World!\0".as_ptr()
}

#[no_mangle]
pub extern fn void() {}
Binary file modified tests/tests.wasm
Binary file not shown.

0 comments on commit a31e422

Please sign in to comment.