Skip to content

Commit

Permalink
Merge pull request #56 from suborbital/connor/swift-pointers
Browse files Browse the repository at this point in the history
Swift library improvements
  • Loading branch information
cohix committed Feb 13, 2021
2 parents 3129001 + d8a058e commit ee788c9
Show file tree
Hide file tree
Showing 27 changed files with 42 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -23,4 +23,6 @@ test
target

.build
Suborbital.wasm
Suborbital.wasm

.swiftpm
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -4,10 +4,10 @@ jobs:
include:
- os: linux
arch: amd64
- os: linux
arch: arm64
- os: osx
arch: amd64
- os: linux
arch: arm64-graviton2

go:
- "1.15.x"
Expand Down
3 changes: 3 additions & 0 deletions Makefile
@@ -1,6 +1,9 @@
test:
go test -v --count=1 -p=1 ./...

testdata:
subo build ./rwasm/testdata/ --bundle --native

wasm:
wasm-pack build
cp ./pkg/wasm_runner_bg.wasm ./wasm/
Expand Down
2 changes: 1 addition & 1 deletion api/rust/suborbital/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/rust/suborbital/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "suborbital"
version = "0.6.0"
version = "0.6.3"
authors = ["cohix <connorjhicks@gmail.com>"]
edition = "2018"
description = "Suborbital Wasm Runnable API"
Expand Down
18 changes: 11 additions & 7 deletions api/swift/Sources/suborbital/lib.swift
Expand Up @@ -246,7 +246,7 @@ public func GetStaticFile(name: String) -> String {
}

@_cdecl("run_e")
func run_e(pointer: UnsafeRawPointer, size: Int32, ident: Int32) {
func run_e(pointer: UnsafeMutablePointer<Int8>, size: Int32, ident: Int32) {
CURRENT_IDENT = ident

let inString = fromFFI(ptr: pointer, size: size)
Expand All @@ -261,13 +261,14 @@ func run_e(pointer: UnsafeRawPointer, size: Int32, ident: Int32) {
}

@_cdecl("allocate")
func allocate(size: Int32) -> UnsafeMutableRawPointer {
return UnsafeMutableRawPointer.allocate(byteCount: Int(size), alignment: MemoryLayout<UInt8>.alignment)
func allocate(size: Int32) -> UnsafeMutablePointer<Int8> {
return UnsafeMutablePointer<Int8>.allocate(capacity: Int(size) + 1)
}

@_cdecl("deallocate")
func deallocate(pointer: UnsafeRawPointer, size: Int32) {
let ptr: UnsafePointer<UInt8> = pointer.bindMemory(to: UInt8.self, capacity: Int(size))
func deallocate(ptr: UnsafeRawPointer, size: Int32) {
let ptr: UnsafeMutablePointer<Int8> = UnsafeMutablePointer(mutating: ptr.bindMemory(to: Int8.self, capacity: Int(size) + 1))
ptr.deinitialize(count: Int(size) + 1)
ptr.deallocate()
}

Expand All @@ -283,8 +284,11 @@ func toFFI(val: String, use: (UnsafePointer<Int8>, Int32) -> Void) {
}

func fromFFI(ptr: UnsafeRawPointer, size: Int32) -> String {
let typed: UnsafePointer<UInt8> = ptr.bindMemory(to: UInt8.self, capacity: Int(size))
let typed: UnsafeMutablePointer<Int8> = UnsafeMutablePointer(mutating: ptr.bindMemory(to: Int8.self, capacity: Int(size) + 1))
let term = typed + Int(size)
term.pointee = 0

let val = String(cString: typed)

return val
}
}
Binary file modified rwasm/testdata/fetch-swift/fetch-swift.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion rwasm/testdata/fetch/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified rwasm/testdata/fetch/fetch.wasm
Binary file not shown.
Binary file modified rwasm/testdata/get-static-swift/get-static-swift.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion rwasm/testdata/get-static/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified rwasm/testdata/get-static/get-static.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion rwasm/testdata/hello-echo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified rwasm/testdata/hello-echo/hello-echo.wasm
Binary file not shown.
Binary file modified rwasm/testdata/hello-swift/hello-swift.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion rwasm/testdata/log/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified rwasm/testdata/log/log.wasm
Binary file not shown.
Binary file modified rwasm/testdata/runnables.wasm.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion rwasm/testdata/rust-get/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified rwasm/testdata/rust-get/rust-get.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion rwasm/testdata/rust-set/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified rwasm/testdata/rust-set/rust-set.wasm
Binary file not shown.
Binary file modified rwasm/testdata/swift-get/swift-get.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions rwasm/testdata/swift-log/Sources/swift-log/main.swift
@@ -1,4 +1,5 @@
import Suborbital
import Foundation

class SwiftEcho: Suborbital.Runnable {
func run(input: String) -> String {
Expand Down
Binary file modified rwasm/testdata/swift-log/swift-log.wasm
Binary file not shown.
Binary file modified rwasm/testdata/swift-set/swift-set.wasm
Binary file not shown.
14 changes: 14 additions & 0 deletions rwasm/wasm_test.go
Expand Up @@ -68,6 +68,20 @@ func TestWasmRunnerWithFetchSwift(t *testing.T) {
}
}

func TestWasmRunnerEchoSwift(t *testing.T) {
job := rt.NewJob("hello-swift", "Connor")

res, err := sharedRT.Do(job).Then()
if err != nil {
t.Error(errors.Wrap(err, "failed to Then"))
return
}

if string(res.([]byte)) != "hello Connor" {
t.Error(fmt.Errorf("hello Connor, got %s", string(res.([]byte))))
}
}

func TestWasmRunnerWithRequest(t *testing.T) {
r := rt.New()

Expand Down

0 comments on commit ee788c9

Please sign in to comment.