Skip to content

Commit 403e1d2

Browse files
authored
feat(stdlib): Convert runtime printing utils to @unsafe (#1135)
1 parent 5a44e93 commit 403e1d2

File tree

4 files changed

+172
-223
lines changed

4 files changed

+172
-223
lines changed

stdlib/runtime/dataStructures.gr

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,6 @@ export let allocateString = size => {
9999
str
100100
}
101101

102-
/**
103-
* Creates a new Grain string containing the given character
104-
*
105-
* @param {WasmI32} c The character for which to allocate a string
106-
* @returns {WasmI32} The pointer to the string
107-
*/
108-
export let singleByteString = c => {
109-
let str = Memory.malloc(9n)
110-
111-
WasmI32.store(str, Tags._GRAIN_STRING_HEAP_TAG, 0n)
112-
WasmI32.store(str, 1n, 4n)
113-
WasmI32.store8(str, c, 8n)
114-
115-
str
116-
}
117-
118102
/**
119103
* Allocates a new Grain char.
120104
*

stdlib/runtime/gc.gr

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,18 @@ primitive unbox: Box<a> -> a = "@unbox"
4545

4646
exception DecRefError
4747

48-
export let decimalCount32 = box((n: WasmI32) => 0n)
49-
export let utoa32Buffered = box((a: WasmI32, b: WasmI32, c: WasmI32) => void)
48+
let decimalCount32Dummy = (n: WasmI32) => 0n
49+
let utoa32BufferedDummy = (a: WasmI32, b: WasmI32, c: WasmI32) => void
50+
51+
// When these boxes are backpatched, the reference count of each function will
52+
// fall to zero which would cause them to be freed. We can't free anything that
53+
// got allocated in runtime mode (since that memory space is not managed by the
54+
// GC, so here we prevent that by manually setting a higher refcount.
55+
WasmI32.store(WasmI32.fromGrain(decimalCount32Dummy) - 8n, 2n, 0n)
56+
WasmI32.store(WasmI32.fromGrain(utoa32BufferedDummy) - 8n, 2n, 0n)
57+
58+
export let decimalCount32 = box(decimalCount32Dummy)
59+
export let utoa32Buffered = box(utoa32BufferedDummy)
5060

5161
let mut _DEBUG = false
5262

0 commit comments

Comments
 (0)