Skip to content

Commit

Permalink
Fixed new C pointer mismatch errors now compile with modern Nim and C
Browse files Browse the repository at this point in the history
  • Loading branch information
beef331 committed Nov 13, 2024
1 parent e25bb72 commit 8bead46
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/wasm3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ template getResult*[T: WasmTuple or WasmTypes](theFunc: PFunction): untyped =
var
res: T
ptrArray = res.ptrArrayTo
checkWasmRes m3_GetResults(theFunc, uint32 ptrArray.len, cast[ptr pointer](ptrArray.addr))
checkWasmRes m3_GetResults(theFunc, uint32 ptrArray.len, cast[ConstVoidStar](ptrArray.addr))
res

macro call*(theFunc: PFunction, returnType: typedesc[WasmTuple or WasmTypes or void], args: varargs[typed]): untyped =
Expand All @@ -150,7 +150,7 @@ macro call*(theFunc: PFunction, returnType: typedesc[WasmTuple or WasmTypes or v
result.add:
genast(returnType, theFunc, arrVals, callProc = bindsym"m3_Call"):
var arrVal = arrVals
checkWasmRes callProc(theFunc, uint32 len arrVal, cast[ptr pointer](arrVal.addr))
checkWasmRes callProc(theFunc, uint32 len arrVal, cast[ConstVoidStar](arrVal.addr))
getResult[returnType](theFunc)
else:
result.add:
Expand Down Expand Up @@ -194,7 +194,7 @@ template toWasmHostProc*(p: static proc, modul, nam, ty: string): WasmHostProc =
module: modul,
name: nam,
typ: ty,
prc: proc (runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
prc: proc (runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
var sp = sp.stackPtrToUint()
callHost(p, sp, mem)
)
Expand Down
63 changes: 42 additions & 21 deletions src/wasm3/wasm3c.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,46 @@ type
ValueKind* = enum
None = 0, I32 = 1, I64 = 2, F32 = 3,
F64 = 4, Unknown
WasmVal* {.bycopy.} = object
case kind*: ValueKind
of I32:
i32*: int32
of I64:
i64*: int64
of F32:
f32*: float32
of F64:
f64*: float64
else:
discard

WasmValUnion {.union, header: "wasm3.h".} = object
i32*: int32
i64*: int64
f32*: float32
f64*: float64



WasmVal* {.bycopy, importc: "struct M3TaggedValue".} = object
kind* {.importc: "type".}: ValueKind
value: WasmValUnion

template accessor(name: untyped, knd: ValueKind, typ: typedesc): untyped =
proc name*(wasmVal: WasmVal): typ =
assert wasmVal.kind == knd
wasmVal.value.name

proc name*(wasmVal: var WasmVal): var typ =
assert wasmVal.kind == knd
wasmVal.value.name

proc `name=`(wasmVal: var WasmVal, val: typ) =
assert wasmVal.kind == knd
wasmVal.value.name = val

accessor(i32, I32, int32)
accessor(i64, I64, int64)
accessor(f32, F32, float32)
accessor(f64, F64, float64)


{. push header: "wasm3.h".}
type
Result* = cstring

Environment* = object
Runtime* = object
Module* = object
Function* = object
Environment* {.bycopy, importc: "struct M3Environment".} = object
Runtime* {.bycopy, importc: "struct M3Runtime".} = object
Module* {.bycopy, importc: "struct M3Module".} = object
Function* {.bycopy, importc: "struct M3Function".} = object

PEnv* = ptr Environment

Expand All @@ -93,7 +111,7 @@ type

PFunction* = ptr Function

Global* = object
Global* {.bycopy, importc:"struct M3Global".} = object
PGlobal* = ptr Global
ErrorInfo* {.bycopy.} = object
result*: Result
Expand Down Expand Up @@ -126,8 +144,11 @@ type
userdata*: pointer
function*: PFunction

PConstVoid* {.importc: "const void*".} = object
ConstvoidStar* = ptr PConstVoid

PImportContext* = ptr ImportContext
WasmProc* = proc (runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.}
WasmProc* = proc (runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.}
M3SectionHandler* = proc (i_module: PModule; name: cstring; start: ptr uint8; `end`: ptr uint8): Result {.cdecl.}

const
Expand Down Expand Up @@ -181,7 +202,7 @@ const
trapUnreachable*{.importc: "m3Err_trapUnreachable".}: Result = ""
trapStackOverflow* {.importc: "m3Err_trapStackOverflow".}: Result = ""

{.push importc.}
{.push importc, cdecl.}

proc m3_NewEnvironment*(): PEnv
proc m3_FreeEnvironment*(i_environment: PEnv)
Expand Down Expand Up @@ -226,11 +247,11 @@ proc m3_GetArgType*(i_function: PFunction; i_index: uint32): ValueKind
proc m3_GetRetType*(i_function: PFunction; i_index: uint32): ValueKind
proc m3_CallV*(i_function: PFunction): Result {.varargs.}
#proc m3_CallVL*(i_function: PFunction; i_args: va_list): Result
proc m3_Call*(i_function: PFunction; i_argc: uint32; i_argptrs: ptr pointer): Result
proc m3_Call*(i_function: PFunction; i_argc: uint32; i_argptrs: ConstVoidStar): Result
proc m3_CallArgv*(i_function: PFunction; i_argc: uint32; i_argv: ptr cstring): Result
proc m3_GetResultsV*(i_function: PFunction): Result {.varargs.}
#proc m3_GetResultsVL*(i_function: PFunction; o_rets: va_list): Result
proc m3_GetResults*(i_function: PFunction; i_retc: uint32; o_retptrs: ptr pointer): Result
proc m3_GetResults*(i_function: PFunction; i_retc: uint32; o_retptrs: ConstVoidStar): Result
proc m3_GetErrorInfo*(i_runtime: PRuntime; o_info: ptr ErrorInfo)
proc m3_ResetErrorInfo*(i_runtime: PRuntime)
proc m3_GetFunctionName*(i_function: PFunction): cstring
Expand Down
14 changes: 7 additions & 7 deletions tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ suite "Raw C wrapping":
runtime = env.m3_NewRuntime(uint16.high.uint32, nil)
module: PModule

proc doThing(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
proc doThing(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
var sp = sp.stackPtrToUint
proc doStuff(a, b: int32): int32 = a * b
callHost(doStuff, sp, mem)

proc arrPassTest(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
proc arrPassTest(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
var sp = sp.stackPtrToUint()
proc arrPass(a: array[4, int32]) = check a == [10i32, 20, 30, 40]
callHost(arrPass, sp, mem)
Expand Down Expand Up @@ -99,7 +99,7 @@ suite "Idiomtic Nim Wrapping":
w: float32


proc doThing(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
proc doThing(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
var sp = sp.stackPtrToUint()
proc doStuff(a, b: int32): int32 = a * b
callHost(doStuff, sp, mem)
Expand Down Expand Up @@ -133,14 +133,14 @@ suite "Idiomtic Nim Wrapping":
w: float32


proc doThing(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
proc doThing(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
var sp = sp.stackPtrToUint()
extractAs(res, ptr int32, sp, mem)
extractAs(a, int32, sp, mem)
extractAs(b, int32, sp, mem)
res[] = a * b

proc arrPassTest(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
proc arrPassTest(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
var sp = sp.stackPtrToUint()
extractAs(val, array[4, int32], sp, mem)
check val == [10i32, 20, 30, 40]
Expand All @@ -167,7 +167,7 @@ suite "Idiomtic Nim Wrapping":

test "Setup log hook function and call it":

proc logProc(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
proc logProc(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
var sp = sp.stackPtrToUint()
extractAs(msg, cstring, sp, mem)
echo msg
Expand All @@ -180,7 +180,7 @@ suite "Idiomtic Nim Wrapping":
test "Setup log hook function and call it, using callHost":


proc logProc(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): pointer {.cdecl.} =
proc logProc(runtime: PRuntime; ctx: PImportContext; sp: ptr uint64; mem: pointer): PConstVoid {.cdecl.} =
proc logProcImpl(c: cstring) =
echo c
var sp = sp.stackPtrToUint()
Expand Down
2 changes: 1 addition & 1 deletion wasm3.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.12"
version = "0.1.13"
author = "jason"
description = "A new awesome nimble package"
license = "MIT"
Expand Down

0 comments on commit 8bead46

Please sign in to comment.