Skip to content

Commit

Permalink
fix(stdlib): Properly print Range values (#2184)
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake authored Oct 30, 2024
1 parent 6a78502 commit 11b1fc2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 7 additions & 0 deletions compiler/test/suites/strings.re
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,11 @@ bar", 1))|},
{|print(b"abc😂")|},
"Byte literals may not contain non-ascii unicode characters",
);

// Range
assertRun(
"range_printing",
{|print({ rangeStart: 1, rangeEnd: 2 })|},
"{\n rangeStart: 1,\n rangeEnd: 2\n}\n",
);
});
1 change: 1 addition & 0 deletions stdlib/runtime/debugPrint.gr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@noPervasives
module DebugPrint

from "runtime/numberUtils" include NumberUtils as Utils
Expand Down
24 changes: 19 additions & 5 deletions stdlib/runtime/string.gr
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ let _LIST_ID = untagSimpleNumber(builtinId("List"))
let _OPTION_ID = untagSimpleNumber(builtinId("Option"))
@unsafe
let _RESULT_ID = untagSimpleNumber(builtinId("Result"))
@unsafe
let _RANGE_ID = untagSimpleNumber(builtinId("Range"))

let _SOME = "Some"
let _NONE = "None"
let _OK = "Ok"
let _ERR = "Err"

let _RANGE_FIELDS = [> "rangeStart", "rangeEnd"]

// Resizable arrays: <num items> <capacity> <...data>
@unsafe
let _VEC_LEN_OFFSET = 0n
Expand Down Expand Up @@ -163,6 +167,12 @@ let isListVariant = variant => {
typeId == _LIST_ID
}

@unsafe
let isRangeRecord = record_ => {
let typeId = WasmI32.load(record_, 8n) >> 1n
typeId == _RANGE_ID
}

@unsafe
let getBuiltinVariantName = variant => {
let typeId = WasmI32.load(variant, 8n) >> 1n
Expand Down Expand Up @@ -231,14 +241,18 @@ let getVariantMetadata = variant => {
@unsafe
let getRecordFieldNames = record_ => {
let typeHash = WasmI32.load(record_, 4n) >> 1n
let arity = WasmI32.load(record_, 12n)
if (isRangeRecord(record_)) {
return Memory.incRef(WasmI32.fromGrain(_RANGE_FIELDS))
} else {
let arity = WasmI32.load(record_, 12n)

let mut fields = findTypeMetadata(typeHash)
let mut fields = findTypeMetadata(typeHash)

if (fields == -1n) return -1n
if (fields == -1n) return -1n

fields += 4n
return getFieldArray(fields, arity)
fields += 4n
return getFieldArray(fields, arity)
}
}

@unsafe
Expand Down

0 comments on commit 11b1fc2

Please sign in to comment.