From 8bead46c581cfd11cea4dd4712b5a0ccbbd70f47 Mon Sep 17 00:00:00 2001 From: Jason Beetham Date: Wed, 13 Nov 2024 13:33:36 -0700 Subject: [PATCH] Fixed new C pointer mismatch errors now compile with modern Nim and C --- src/wasm3.nim | 6 ++--- src/wasm3/wasm3c.nim | 63 +++++++++++++++++++++++++++++--------------- tests/test1.nim | 14 +++++----- wasm3.nimble | 2 +- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/wasm3.nim b/src/wasm3.nim index 58a7a19..7490e71 100644 --- a/src/wasm3.nim +++ b/src/wasm3.nim @@ -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 = @@ -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: @@ -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) ) diff --git a/src/wasm3/wasm3c.nim b/src/wasm3/wasm3c.nim index 69db1e2..88bc239 100644 --- a/src/wasm3/wasm3c.nim +++ b/src/wasm3/wasm3c.nim @@ -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 @@ -93,7 +111,7 @@ type PFunction* = ptr Function - Global* = object + Global* {.bycopy, importc:"struct M3Global".} = object PGlobal* = ptr Global ErrorInfo* {.bycopy.} = object result*: Result @@ -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 @@ -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) @@ -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 diff --git a/tests/test1.nim b/tests/test1.nim index 98d61f2..40b2b02 100644 --- a/tests/test1.nim +++ b/tests/test1.nim @@ -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) @@ -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) @@ -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] @@ -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 @@ -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() diff --git a/wasm3.nimble b/wasm3.nimble index 453a5b4..ccf792c 100644 --- a/wasm3.nimble +++ b/wasm3.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.12" +version = "0.1.13" author = "jason" description = "A new awesome nimble package" license = "MIT"