Skip to content

Commit 11b1fc2

Browse files
authored
fix(stdlib): Properly print Range values (#2184)
1 parent 6a78502 commit 11b1fc2

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

compiler/test/suites/strings.re

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,11 @@ bar", 1))|},
401401
{|print(b"abc😂")|},
402402
"Byte literals may not contain non-ascii unicode characters",
403403
);
404+
405+
// Range
406+
assertRun(
407+
"range_printing",
408+
{|print({ rangeStart: 1, rangeEnd: 2 })|},
409+
"{\n rangeStart: 1,\n rangeEnd: 2\n}\n",
410+
);
404411
});

stdlib/runtime/debugPrint.gr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@noPervasives
12
module DebugPrint
23

34
from "runtime/numberUtils" include NumberUtils as Utils

stdlib/runtime/string.gr

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ let _LIST_ID = untagSimpleNumber(builtinId("List"))
7171
let _OPTION_ID = untagSimpleNumber(builtinId("Option"))
7272
@unsafe
7373
let _RESULT_ID = untagSimpleNumber(builtinId("Result"))
74+
@unsafe
75+
let _RANGE_ID = untagSimpleNumber(builtinId("Range"))
7476

7577
let _SOME = "Some"
7678
let _NONE = "None"
7779
let _OK = "Ok"
7880
let _ERR = "Err"
7981

82+
let _RANGE_FIELDS = [> "rangeStart", "rangeEnd"]
83+
8084
// Resizable arrays: <num items> <capacity> <...data>
8185
@unsafe
8286
let _VEC_LEN_OFFSET = 0n
@@ -163,6 +167,12 @@ let isListVariant = variant => {
163167
typeId == _LIST_ID
164168
}
165169

170+
@unsafe
171+
let isRangeRecord = record_ => {
172+
let typeId = WasmI32.load(record_, 8n) >> 1n
173+
typeId == _RANGE_ID
174+
}
175+
166176
@unsafe
167177
let getBuiltinVariantName = variant => {
168178
let typeId = WasmI32.load(variant, 8n) >> 1n
@@ -231,14 +241,18 @@ let getVariantMetadata = variant => {
231241
@unsafe
232242
let getRecordFieldNames = record_ => {
233243
let typeHash = WasmI32.load(record_, 4n) >> 1n
234-
let arity = WasmI32.load(record_, 12n)
244+
if (isRangeRecord(record_)) {
245+
return Memory.incRef(WasmI32.fromGrain(_RANGE_FIELDS))
246+
} else {
247+
let arity = WasmI32.load(record_, 12n)
235248

236-
let mut fields = findTypeMetadata(typeHash)
249+
let mut fields = findTypeMetadata(typeHash)
237250

238-
if (fields == -1n) return -1n
251+
if (fields == -1n) return -1n
239252

240-
fields += 4n
241-
return getFieldArray(fields, arity)
253+
fields += 4n
254+
return getFieldArray(fields, arity)
255+
}
242256
}
243257

244258
@unsafe

0 commit comments

Comments
 (0)