From 11b1fc2ff089f08e9c4d350c1343ac04bdc68caa Mon Sep 17 00:00:00 2001 From: Spotandjake <40705786+spotandjake@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:21:27 -0400 Subject: [PATCH] fix(stdlib): Properly print `Range` values (#2184) --- compiler/test/suites/strings.re | 7 +++++++ stdlib/runtime/debugPrint.gr | 1 + stdlib/runtime/string.gr | 24 +++++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/compiler/test/suites/strings.re b/compiler/test/suites/strings.re index ba1748757..164d39867 100644 --- a/compiler/test/suites/strings.re +++ b/compiler/test/suites/strings.re @@ -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", + ); }); diff --git a/stdlib/runtime/debugPrint.gr b/stdlib/runtime/debugPrint.gr index 194a7d1af..d1445a4c4 100644 --- a/stdlib/runtime/debugPrint.gr +++ b/stdlib/runtime/debugPrint.gr @@ -1,3 +1,4 @@ +@noPervasives module DebugPrint from "runtime/numberUtils" include NumberUtils as Utils diff --git a/stdlib/runtime/string.gr b/stdlib/runtime/string.gr index 630df2128..a1836dacf 100644 --- a/stdlib/runtime/string.gr +++ b/stdlib/runtime/string.gr @@ -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: <...data> @unsafe let _VEC_LEN_OFFSET = 0n @@ -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 @@ -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