-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generalize WASI to other Wasm engines #79
Conversation
evidently I hadn't run the tests myself yet 😶 @kateinoigakukun tests should now be green |
@@ -47,3 +49,23 @@ public struct CanonicalCallContext { | |||
return UnsafeGuestRawPointer(memorySpace: guestMemory, offset: new) | |||
} | |||
} | |||
|
|||
public struct GuestMemory: BaseGuestMemory { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public struct GuestMemory: BaseGuestMemory { | |
public struct WasmKitGuestMemory: GuestMemory { |
I personally prefer avoiding Base
here
Package.swift
Outdated
@@ -15,6 +15,10 @@ let package = Package( | |||
name: "WASI", | |||
targets: ["WASI"] | |||
), | |||
.library( | |||
name: "WASIBase", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name: "WASIBase", | |
name: "WASI", |
I'd like to make this engine agnostic target as bare WASI
, and the engine dependent one as WasmKitWASI
this is ready from my end! The only thing I haven't been able to test yet is the performance of using an existential |
I'll merge this once I check on my local 👍 |
let (name, type) = entry | ||
functions[name] = HostFunction(type: type) { _, _ in | ||
functions[name] = WASIHostFunction(type: type) { _, _ in | ||
print("\"\(name)\" not implemented yet") | ||
return [.i32(WASIAbi.Errno.ENOSYS.rawValue)] | ||
} | ||
} | ||
|
||
func withMemoryBuffer<T>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should note that this function is basically now a no-op since this work is offloaded to WasmKitWASI
. I wanted to minimize the diff to begin with for ease of review but it might make sense to remove this now. @kateinoigakukun would you prefer that I do it in this PR or in a follow-up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for minimizing diff! I prefer a following up way
This PR enables layering the WASI module on top of other Wasm engines, such as JavaScriptCore.
To achieve this, it's worth first noting that the only real dependency that WASI had on WasmKit was in reading/writing host memory (via
UnsafeGuestPointer
) as well as the small set of core WasmValue
types. Accordingly, I've split these bits out into a separateWasmTypes
module and created aWASIBase
package that depends only onWasmTypes
. I created aBaseGuestMemory
protocol that can be implemented by engines, and kept theGuestMemory
type as WasmKit's concrete implementation.I believe this maintains full source compatibility with previous versions, but I'm not sure if the API design is ideal. Certainly open to feedback on how this could be improvededit: there is some minor API breakage in that UnsafeGuest[Raw]Pointer now uses the protocol-ized BaseGuestMemory and requires a
count
when obtaining a pointer to host memory. If we really wanted full source compat we could create more pointer variants that take the concrete WasmKit.GuestMemory but I’m not sure it’s worth it.