Skip to content

Commit

Permalink
Add support for functions with shared implementations (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
atdrendel authored Nov 28, 2020
1 parent 60c123b commit 91e4046
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/shareup/cwasm3.git",
"state": {
"branch": null,
"revision": "ba06c95a9dac8837ceaae3ace06d2149feb04e27",
"version": "0.4.7"
"revision": "93bee1c35cee166098cf290fe7b745d44730afce",
"version": "0.4.8"
}
},
{
Expand Down
6 changes: 6 additions & 0 deletions Tests/WasmInterpreterTests/Resources/constant.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
i32.const 65536)
(export "constant_1" (func 0))
(export "constant_2" (func 0)))
24 changes: 24 additions & 0 deletions Tests/WasmInterpreterTests/Wasm Modules/ConstantModule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation
import WasmInterpreter

public struct ConstantModule {
private let _vm: WasmInterpreter

init() throws {
_vm = try WasmInterpreter(module: ConstantModule.wasm)
}

func constant1() throws -> Int {
return Int(try _vm.call("constant_1") as Int32)
}

func constant2() throws -> Int {
return Int(try _vm.call("constant_2") as Int32)
}

// `wat2wasm -o >(base64) Tests/WasmInterpreterTests/Resources/constant.wat | pbcopy`
private static var wasm: [UInt8] {
let base64 = "AGFzbQEAAAABBQFgAAF/AwIBAAcbAgpjb25zdGFudF8xAAAKY29uc3RhbnRfMgAACggBBgBBgIAECw=="
return Array<UInt8>(Data(base64Encoded: base64)!)
}
}
7 changes: 7 additions & 0 deletions Tests/WasmInterpreterTests/WasmInterpreterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import XCTest
@testable import WasmInterpreter

final class WasmInterpreterTests: XCTestCase {
func testCallingTwoFunctionsWithSameImplementation() throws {
let mod = try ConstantModule()
XCTAssertEqual(65536, try mod.constant1())
XCTAssertEqual(65536, try mod.constant2())
}

func testPassingAndReturning32BitValues() throws {
let mod = try AddModule()
XCTAssertEqual(0, try mod.add(-1, 1))
Expand Down Expand Up @@ -31,6 +37,7 @@ final class WasmInterpreterTests: XCTestCase {
}

static var allTests = [
("testCallingTwoFunctionsWithSameImplementation", testCallingTwoFunctionsWithSameImplementation),
("testPassingAndReturning32BitValues", testPassingAndReturning32BitValues),
("testPassingAndReturning64BitValues", testPassingAndReturning64BitValues),
("testUsingImportedFunction", testUsingImportedFunction),
Expand Down

0 comments on commit 91e4046

Please sign in to comment.