Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stdlib): Properly print Range values #2184

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading