Giter Site home page Giter Site logo

swiftwasm / wasmkit Goto Github PK

View Code? Open in Web Editor NEW
119.0 6.0 5.0 822 KB

WebAssembly Runtime written in Swift

Home Page: https://swiftpackageindex.com/swiftwasm/WasmKit/main/documentation/wasmkit

License: MIT License

Swift 98.40% Makefile 0.12% Shell 0.32% Python 0.10% C 0.03% CMake 1.04%
swift swiftwasm webassembly webassembly-runtime

wasmkit's Introduction

WAKit Icon

WasmKit

A WebAssembly runtime written in Swift. Originally developed and maintained by @akkyie.

Implements all of WebAssembly 2.0 binary parsing and execution core spec, with an exclusion of SIMD instructions. The validation and text format parts of the spec are not implemented yet.

It also has rudimentary support for WASI with only a few WASI imports implemented currently, with a goal of eventual full support for wasi_snapshot_preview1. See WASI module for more details.

Usage

Command Line Tool

$ # Usage: wasmkit-cli run <path> <functionName> [<arguments>] ...
$ swift run wasmkit-cli run Examples/wasm/fib.wasm fib i32:10
[I32(89)]

As a Library

Swift Package Manager

Add the URL of this repository to your Package.swift manifest. Then add the WasmKit library product as dependency to the target you'd like to use it with.

Testing

To run the core spec test suite run this:

$ make spectest   # Prepare core spec tests and check their assertions with WasmKit

wasmkit's People

Contributors

akkyie avatar dependabot[bot] avatar kabiroberai avatar kateinoigakukun avatar maxdesiatov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

wasmkit's Issues

Import object

I'm skimming through the project trying to figure out how to use the library. One thing I have not found how to do, which is the import object.

I have the following:

let modInstance = try runtime.instantiate(module: module, externalValues: [
    ExternalValue.function(/*What do I put here?*/)
])

What I want to accomplish is calling a Swift function from wasm.

Any help on this?

Thank you in advance,
Jomy

stackTypeMismatch when using `drop` instruction and when using globals when there is a local

I'm running wasm code with the following:

guard let fileHandle = FileHandle(forReadingAtPath: "./test.wasm") else {
    throw InterpreterError.interpreterReadError
}

defer { fileHandle.closeFile() }

let stream = FileHandleStream(fileHandle: fileHandle)

let module = try WasmParser.parse(stream: stream)
let runtime = Runtime()
let instance = try runtime.instantiate(module: module, externalValues: [])

let result = try runtime.invoke(instance, function: "test")

print(result)

Consider the following wasm code:

(module
    (func $test
        i32.const 1
        drop
    )
    
    (export "test" (func $test))
)

when I run the swift code, invoking the "test" function. It return the following error:

stackTypeMismatch(expected: WAKit.Value, actual: WAKit.Label

Make WASI deterministic

At the moment it seems like there are two sources of nondeterminism in WASI:

  1. The random number generator, which is hardcoded to swift_stdlib_random
  2. The clocks, hardcoded to the real system clocks

What if we allowed these to be overridden by user-provided Swift.RandomNumberGenerator and Swift.Clock types respectively? We could default to SystemRandomNumberGenerator and ContinuousClock + SuspendingClock.

To my understanding, enabling these to be overridden would allow for fully deterministic execution of WASI binaries while also serving as Dependency Injection points for solid testability.

Fatal error when running: Examples/wasm/fib.wasm

the example from README doesn't seem to work

WasmKit on main ❯ swift run wasmkit-cli run Examples/wasm/fib.wasm fib i32:10
Building for debugging...
[1/1] Write swift-version--58304C5D6DBC2206.txt
Build complete! (0.11s)
CLI/Run.swift:129: Fatal error: unknown type
zsh: trace trap  swift run wasmkit-cli run Examples/wasm/fib.wasm fib i32:10

Why memory is empty

I imported the wasm file to parse the wasm output data. The memory is empty.

wat file

(module
 (type $0 (func (result i32)))
 (memory $0 1)
 (data $0 (i32.const 1036) ",")
 (data $0.1 (i32.const 1048) "\02\00\00\00\16\00\00\00h\00e\00l\00l\00o\00 \00w\00o\00r\00l\00d")
 (export "getHelloWorld" (func $assembly/index/getHelloWorld))
 (export "memory" (memory $0))
 (func $assembly/index/getHelloWorld (result i32)
  i32.const 1056
 )
)
Module(types: [WasmKit.FunctionType(parameters: [], results: [i32])], functions: [WasmKit.GuestFunction(type: 0, defaultLocals: UnsafeBufferPointer(start: 0x00006000022583a0, count: 0), _bodyStorage: nil, materializer: (Function))], tables: [], memories: [WasmKit.Memory(type: WasmKit.Limits(min: 1, max: nil, isMemory64: false))], globals: [], elements: [], dataCount: Optional(2), data: [WasmKit.DataSegment.active(WasmKit.DataSegment.Active(index: 0, offset: [WasmKit.Instruction.numericConst(I32(1036))], initializer: ArraySlice([44]))), WasmKit.DataSegment.active(WasmKit.DataSegment.Active(index: 0, offset: [WasmKit.Instruction.numericConst(I32(1048))], initializer: ArraySlice([2, 0, 0, 0, 22, 0, 0, 0, 104, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 119, 0, 111, 0, 114, 0, 108, 0, 100])))], start: nil, imports: [], exports: [WasmKit.Export(name: "hello_world", descriptor: WasmKit.ExportDescriptor.function(0)), WasmKit.Export(name: "memory", descriptor: WasmKit.ExportDescriptor.memory(0))], customSections: [WasmKit.CustomSection(name: "sourceMappingURL", bytes: ArraySlice([18, 46, 47, 114, 101, 108, 101, 97, 115, 101, 46, 119, 97, 115, 109, 46, 109, 97, 112]))])

Validate all functions ahead of time

We currently validate only executed functions at runtime, but to catch this kind of signature mismatch, it might be worth adding an option to enforce full validation ahead of time.

No LICENSE file

Hi - I'm interested in using WAKit to make a QuickLook extension for .wasm files, but I don't see a LICENSE file in the project so it's not clear whether that's allowed.

Could you please add a LICENSE file? Preferably a non-restrictive license if you're ok with it being reused. Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.