Skip to content

Commit

Permalink
enable gcsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Aug 1, 2023
1 parent d78cca9 commit c8b7a88
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/msgpack4nim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type
MsgStream* = ref object of StringStreamObj
encodingMode: EncodingMode

{.push gcsafe.}

proc init*(x: typedesc[MsgStream], data: sink string, encodingMode = MSGPACK_OBJ_TO_DEFAULT): MsgStream =
result = new x
# Initialize StringStream base by copying fields from a new StringStream:
Expand Down Expand Up @@ -1284,3 +1286,5 @@ proc stringify*(data: string): string =
stringify(s, zz)
zz.write(" ")
result = zz.data

{.pop.}
4 changes: 4 additions & 0 deletions src/msgpack4nim/msgpack2any.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type
of msgUint: uintVal*: uint64
of msgNull: nil

{.push gcsafe.}

proc newMsgAny*(kind: AnyType): MsgAny =
result = MsgAny(kind: kind)

Expand Down Expand Up @@ -473,3 +475,5 @@ proc toPretty(result: var string, n: MsgAny, indent = 2, ml = true,
proc pretty*(n: MsgAny, indent = 2): string =
result = ""
toPretty(result, n, indent)

{.pop.}
4 changes: 4 additions & 0 deletions src/msgpack4nim/msgpack2json.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ../msgpack4nim, json, tables, math, base64, streams

{.push gcsafe.}

proc toJsonNode*(s: Stream): JsonNode =
let c = ord(s.peekChar)
case c
Expand Down Expand Up @@ -94,3 +96,5 @@ proc fromJsonNode*(n: JsonNode): string =
var s = MsgStream.init()
fromJsonNode(s, n)
result = s.data

{.pop.}
4 changes: 4 additions & 0 deletions src/msgpack4nim/msgpack4collection.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sequtils, math, ../msgpack4nim
import tables, intsets, lists, deques, sets, strtabs, critbits, streams

{.push gcsafe.}

proc pack_type*(s: Stream, val: IntSet) =
var ss = MsgStream.init()
var count = 0
Expand Down Expand Up @@ -201,3 +203,5 @@ proc unpack_type*(s: Stream, val: var IntSet) =
for i in 0..len-1:
x = s.unpack_imp_int32()
val.incl(x)

{.pop.}
14 changes: 13 additions & 1 deletion tests/test_codec.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[algorithm, critbits, deques, endians, hashes, intsets, lists,
import std/[algorithm, critbits, deques, hashes, intsets, lists,
math, sequtils, sets, streams, strtabs, strutils, tables, unittest]

import msgpack4nim
Expand Down Expand Up @@ -991,3 +991,15 @@ suite "msgpack encoder-decoder":
for sub in nodes:
check sub.name notin names
names.add(sub.name)

test "unpack in gcsafe proc":
proc gcsafeProc(): carrier {.gcsafe.} =
let cc = initCarrier()
let buf = pack(cc)
var dd: carrier
unpack(buf, dd)
result = dd

let cc = initCarrier()
let dd = gcsafeProc()
check $cc == $dd

0 comments on commit c8b7a88

Please sign in to comment.