Swiftpack.co -  flavio/wapc-guest-swift as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
flavio/wapc-guest-swift
SDK for creating waPC WebAssembly Guest Modules in Swift
.package(url: "https://github.com/flavio/wapc-guest-swift.git", from: "0.0.2")

waPC Guest Library for Swift

This is the Swift implementation of the waPC standard for WebAssembly guest modules. It allows any waPC-compliant WebAssembly host to invoke to procedures inside a Swift compiled guest and similarly for the guest to invoke procedures exposed by the host.

The Swift compiler provided by the Swiftwasm project is required.

Example

The following is a simple example of synchronous, bi-directional procedure calls between a WebAssembly host runtime and the guest module.

import wapc

@_cdecl("__guest_call")
func __guest_call(operation_size: UInt, payload_size: UInt) -> Bool {
  return wapc.handleCall(operation_size: operation_size, payload_size: payload_size)
}

func hello(payload: String) -> String {
  // call the host from the Wasm module
  let hostMsg = wapc.hostCall(
    binding: "binding_name",
    namespace: "namespace_name",
    operation: "operation_name",
    payload: "that's the payload"
  )

  if hostMsg == nil {
    return "the validate function called the host but something went wrong"
  }
  return "the validate function called the host and received back: '\(hostMsg!)'"
}

// register the function
wapc.registerFunction(name: "hello", fn: hello)

The Wasm module must declare a __guest_call function and export it. The linker must be instructed to export this function (see the official documentation).

When using SwiftPM to manage a package, the linker can be instruted by adding few lines to the Packages.swift file:

.target(
    name: "demo",
    dependencies: ["wapc"],
    linkerSettings: [
        .unsafeFlags(
            [
                "-Xlinker",
                "--export=__guest_call",
            ]
        )
    ]
),

Working example

This GitHub repository contains a full working example.

Call for help

I'm new to Swift, I would appreciate help on the following topics from some more seasoned Swift develper:

  • Generic code review, ensure I'm not doing something stupid
  • Question about the API to expose, this is tracked on this issue

GitHub

link
Stars: 3
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API