Skip to content

Commit

Permalink
Update to CWasm3 v0.4.9 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
atdrendel authored Mar 14, 2021
1 parent 09e0c1d commit eb82bff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 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": "d1c37d03c68f54ee46bb8d49003b741c635cc705",
"version": "0.4.8"
"revision": "10c6530a759b2a0f46be76a34b6b9a65fd079616",
"version": "0.4.9"
}
},
{
Expand Down
34 changes: 24 additions & 10 deletions Sources/WasmInterpreter/WasmInterpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,20 @@ extension WasmInterpreter {
signature: String,
handler: @escaping ImportedFunctionSignature
) throws {
guard let context = UnsafeMutableRawPointer(bitPattern: (namespace + name).hashValue) else {
throw WasmInterpreterError.couldNotGenerateFunctionContext
}
guard let context = UnsafeMutableRawPointer(bitPattern: (namespace + name).hashValue)
else { throw WasmInterpreterError.couldNotGenerateFunctionContext }

do {
setImportedFunction(handler, for: context)
try WasmInterpreter.check(
m3_LinkRawFunctionEx(_module, namespace, name, signature, handleImportedFunction, context)
m3_LinkRawFunctionEx(
_module,
namespace,
name,
signature,
handleImportedFunction,
context
)
)
_lock.locked { _importedFunctionContexts.append(context) }
} catch {
Expand All @@ -226,7 +232,8 @@ extension WasmInterpreter {
} else {
var f: IM3Function?
try WasmInterpreter.check(m3_FindFunction(&f, _runtime, name))
guard let function = f else { throw WasmInterpreterError.couldNotFindFunction(name) }
guard let function = f
else { throw WasmInterpreterError.couldNotFindFunction(name) }
_functionCache[name] = function
return function
}
Expand Down Expand Up @@ -288,17 +295,24 @@ private func removeImportedFunctions(for contexts: [UnsafeMutableRawPointer]) {
importedFunctionLock.locked { contexts.forEach { contextToImportedFunction.removeValue(forKey: $0) } }
}

private func importedFunction(for context: UnsafeMutableRawPointer?) -> ImportedFunctionSignature? {
guard let context = context else { return nil }
private func importedFunction(
for userData: UnsafeMutableRawPointer?
) -> ImportedFunctionSignature? {
guard let context = userData else { return nil }
return importedFunctionLock.locked { contextToImportedFunction[context] }
}

private func handleImportedFunction(
_ runtime: UnsafeMutablePointer<M3Runtime>?,
_ context: UnsafeMutablePointer<M3ImportContext>?,
_ stackPointer: UnsafeMutablePointer<UInt64>?,
_ heap: UnsafeMutableRawPointer?,
_ context: UnsafeMutableRawPointer?
_ heap: UnsafeMutableRawPointer?
) -> UnsafeRawPointer? {
guard let function = importedFunction(for: context) else { return UnsafeRawPointer(m3Err_trapUnreachable) }
guard let userData = context?.pointee.userdata
else { return UnsafeRawPointer(m3Err_trapUnreachable) }

guard let function = importedFunction(for: userData)
else { return UnsafeRawPointer(m3Err_trapUnreachable) }

return function(stackPointer, heap)
}

0 comments on commit eb82bff

Please sign in to comment.