diff --git a/compiler/src/utils/warnings.re b/compiler/src/utils/warnings.re index c0957a9765..1878449a61 100644 --- a/compiler/src/utils/warnings.re +++ b/compiler/src/utils/warnings.re @@ -173,11 +173,11 @@ let message = | PrintUnsafe(typ) => "it looks like you are using `print` on an unsafe Wasm value here.\nThis is generally unsafe and will cause errors. Use `DebugPrint.print`" ++ typ - ++ " from the `runtime/debugPrint` module instead." + ++ " from the `runtime/unsafe/debugPrint` module instead." | ToStringUnsafe(typ) => "it looks like you are using `toString` on an unsafe Wasm value here.\nThis is generally unsafe and will cause errors. Use `DebugPrint.toString`" ++ typ - ++ " from the `runtime/debugPrint` module instead." + ++ " from the `runtime/unsafe/debugPrint` module instead." | ArrayIndexNonInteger(idx) => "Array index should be an integer, but found `" ++ idx ++ "`." | PrintUnsafeRef => "it looks like you are using `print` on a `WasmRef` value here.\nThis is generally unsafe and will cause errors." diff --git a/compiler/test/__snapshots__/basic_functionality.1bf5759c.0.snapshot b/compiler/test/__snapshots__/basic_functionality.1bf5759c.0.snapshot index 009dcb3db7..59fb58f90e 100644 --- a/compiler/test/__snapshots__/basic_functionality.1bf5759c.0.snapshot +++ b/compiler/test/__snapshots__/basic_functionality.1bf5759c.0.snapshot @@ -7,11 +7,11 @@ basic functionality › unsafe_wasm_globals (mimp_type (MGlobalImport (WasmValue WasmF64) true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)) ((mimp_id ((stamp 1082) (name printF64))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printF64) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printF64) (mimp_type (MFuncImport (GrainValue (WasmValue WasmF64)) (GrainValue))) (mimp_kind MImportGrain) (mimp_setup MSetupNone) (mimp_used true)) ((mimp_id ((stamp 1082) (name printF64))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printF64) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printF64) (mimp_type (MGlobalImport GrainValue true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)) ((mimp_id ((stamp 1081) (name _F32_VAL))) @@ -19,11 +19,11 @@ basic functionality › unsafe_wasm_globals (mimp_type (MGlobalImport (WasmValue WasmF32) true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)) ((mimp_id ((stamp 1080) (name printF32))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printF32) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printF32) (mimp_type (MFuncImport (GrainValue (WasmValue WasmF32)) (GrainValue))) (mimp_kind MImportGrain) (mimp_setup MSetupNone) (mimp_used true)) ((mimp_id ((stamp 1080) (name printF32))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printF32) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printF32) (mimp_type (MGlobalImport GrainValue true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)) ((mimp_id ((stamp 1079) (name _I64_VAL))) @@ -31,11 +31,11 @@ basic functionality › unsafe_wasm_globals (mimp_type (MGlobalImport (WasmValue WasmI64) true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)) ((mimp_id ((stamp 1078) (name printI64))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printI64) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printI64) (mimp_type (MFuncImport (GrainValue (WasmValue WasmI64)) (GrainValue))) (mimp_kind MImportGrain) (mimp_setup MSetupNone) (mimp_used true)) ((mimp_id ((stamp 1078) (name printI64))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printI64) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printI64) (mimp_type (MGlobalImport GrainValue true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)) ((mimp_id ((stamp 1077) (name _I32_VAL))) @@ -43,11 +43,11 @@ basic functionality › unsafe_wasm_globals (mimp_type (MGlobalImport (WasmValue WasmI32) true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)) ((mimp_id ((stamp 1076) (name printI32))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printI32) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printI32) (mimp_type (MFuncImport (GrainValue (WasmValue WasmI32)) (GrainValue))) (mimp_kind MImportGrain) (mimp_setup MSetupNone) (mimp_used true)) ((mimp_id ((stamp 1076) (name printI32))) - (mimp_mod runtime/debugPrint.gr) (mimp_name printI32) + (mimp_mod runtime/unsafe/debugPrint.gr) (mimp_name printI32) (mimp_type (MGlobalImport GrainValue true)) (mimp_kind MImportGrain) (mimp_setup MCallGetter) (mimp_used true)))) (exports ()) diff --git a/compiler/test/input/letMutForLoop.gr b/compiler/test/input/letMutForLoop.gr index 4ad92a2dc5..88c9a3d792 100644 --- a/compiler/test/input/letMutForLoop.gr +++ b/compiler/test/input/letMutForLoop.gr @@ -1,7 +1,7 @@ module LetMutForLoop from "runtime/unsafe/wasmi64" include WasmI64 -from "runtime/debugPrint" include DebugPrint +from "runtime/unsafe/debugPrint" include DebugPrint @unsafe let foo = () => { diff --git a/compiler/test/input/unsafeWasmGlobals.gr b/compiler/test/input/unsafeWasmGlobals.gr index 1d3b06952b..6ef609f0ce 100644 --- a/compiler/test/input/unsafeWasmGlobals.gr +++ b/compiler/test/input/unsafeWasmGlobals.gr @@ -1,6 +1,6 @@ module UnsafeWasmGlobals -from "runtime/debugPrint" include DebugPrint +from "runtime/unsafe/debugPrint" include DebugPrint from "unsafeWasmGlobalsExports" include UnsafeWasmGlobalsExports use UnsafeWasmGlobalsExports.{ _I32_VAL, _I64_VAL, _F32_VAL, _F64_VAL } diff --git a/compiler/test/runtime/unsafe/wasmf32.test.gr b/compiler/test/runtime/unsafe/wasmf32.test.gr index 3b8b883bb4..33929743e7 100644 --- a/compiler/test/runtime/unsafe/wasmf32.test.gr +++ b/compiler/test/runtime/unsafe/wasmf32.test.gr @@ -1,7 +1,7 @@ module WasmF32Test from "runtime/unsafe/wasmf32" include WasmF32 -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS @unsafe let test = () => { @@ -57,8 +57,8 @@ let test = () => { assert WasmF32.demoteF64(1.23W) == 1.23w // Grain conversion tests - assert Conv.fromFloat32(1.23f) == 1.23w - assert eq(Conv.toFloat32(1.23w), 1.23f) + assert DS.Float32.fromFloat32(1.23f) == 1.23w + assert eq(DS.Float32.toFloat32(1.23w), 1.23f) } test() diff --git a/compiler/test/runtime/unsafe/wasmf64.test.gr b/compiler/test/runtime/unsafe/wasmf64.test.gr index 5e9307e87d..04878804dd 100644 --- a/compiler/test/runtime/unsafe/wasmf64.test.gr +++ b/compiler/test/runtime/unsafe/wasmf64.test.gr @@ -1,7 +1,7 @@ module WasmF64Test from "runtime/unsafe/wasmf64" include WasmF64 -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS @unsafe let test = () => { @@ -57,8 +57,8 @@ let test = () => { assert WasmF64.promoteF32(1.5w) == 1.5W // Grain conversion tests - assert eq(Conv.toFloat64(1.23W), 1.23d) - assert Conv.fromFloat64(1.23d) == 1.23W + assert eq(DS.Float64.toFloat64(1.23W), 1.23d) + assert DS.Float64.fromFloat64(1.23d) == 1.23W } test() diff --git a/compiler/test/runtime/unsafe/wasmi32.test.gr b/compiler/test/runtime/unsafe/wasmi32.test.gr index d6d3b72005..c02cf015fb 100644 --- a/compiler/test/runtime/unsafe/wasmi32.test.gr +++ b/compiler/test/runtime/unsafe/wasmi32.test.gr @@ -1,7 +1,7 @@ module WasmI32Test from "runtime/unsafe/wasmi32" include WasmI32 -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS @unsafe let test = () => { @@ -85,27 +85,27 @@ let test = () => { assert WasmI32.extendS16(0x8000n) == 0xffff8000n // Grain conversion tests - assert eq(Conv.toInt32(45n), 45l) + assert eq(DS.Int32.toInt32(45n), 45l) - assert Conv.fromInt32(45l) == 45n + assert DS.Int32.fromInt32(45l) == 45n // within range of simple numbers - assert eq(Conv.wasmI32ToNumber(45n), 45) + assert eq(DS.wasmI32ToNumber(45n), 45) - assert eq(Conv.wasmI32ToNumber(0n), 0) + assert eq(DS.wasmI32ToNumber(0n), 0) - assert eq(Conv.wasmI32ToNumber(-1073741824n), -1073741824) + assert eq(DS.wasmI32ToNumber(-1073741824n), -1073741824) - assert eq(Conv.wasmI32ToNumber(1073741823n), 1073741823) + assert eq(DS.wasmI32ToNumber(1073741823n), 1073741823) // heap allocated - assert eq(Conv.wasmI32ToNumber(-1073741825n), -1073741825) + assert eq(DS.wasmI32ToNumber(-1073741825n), -1073741825) - assert eq(Conv.wasmI32ToNumber(1073741824n), 1073741824) + assert eq(DS.wasmI32ToNumber(1073741824n), 1073741824) - assert eq(Conv.wasmI32ToNumber(-2147483648n), -2147483648) + assert eq(DS.wasmI32ToNumber(-2147483648n), -2147483648) - assert eq(Conv.wasmI32ToNumber(2147483647n), 2147483647) + assert eq(DS.wasmI32ToNumber(2147483647n), 2147483647) } test() diff --git a/compiler/test/runtime/unsafe/wasmi64.test.gr b/compiler/test/runtime/unsafe/wasmi64.test.gr index 7552367ba3..ddf6eb4e5f 100644 --- a/compiler/test/runtime/unsafe/wasmi64.test.gr +++ b/compiler/test/runtime/unsafe/wasmi64.test.gr @@ -1,7 +1,7 @@ module WasmI64Test from "runtime/unsafe/wasmi64" include WasmI64 -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS @unsafe let test = () => { @@ -88,8 +88,8 @@ let test = () => { assert WasmI64.extendS32(0x80000000N) == 0xffffffff80000000N // Grain conversion tests - assert eq(Conv.toInt64(45N), 45L) - assert Conv.fromInt64(45L) == 45N + assert eq(DS.Int64.toInt64(45N), 45L) + assert DS.Int64.fromInt64(45L) == 45N } test() diff --git a/compiler/test/runtime/unsafe/wasmref.test.gr b/compiler/test/runtime/unsafe/wasmref.test.gr index 64b477a935..c459c6a5c7 100644 --- a/compiler/test/runtime/unsafe/wasmref.test.gr +++ b/compiler/test/runtime/unsafe/wasmref.test.gr @@ -4,7 +4,7 @@ module WasmRefTest from "runtime/unsafe/wasmref" include WasmRef from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures as DS primitive (!) = "@not" primitive (is) = "@is" @@ -101,9 +101,9 @@ module WasmArrayRef { primitive getCompoundValueArrayRef = "@compound_value.refarray" @unsafe let test = () => { - let arrRef = WasmRef.fromGrain([>]) - let unsafeArrData = getCompoundValueArrayRef(arrRef) - let arrData = DataStructures.getCompoundValueArrayRef(arrRef) + let arr = [>] + let unsafeArrData = getCompoundValueArrayRef(WasmRef.fromGrain(arr)) + let arrData = DS.Array.getData(DS.Array.fromGrain(arr)) assert WasmArrayRef.fromWasmRef(unsafeArrData) is arrData } test() @@ -111,8 +111,8 @@ module WasmArrayRef { @unsafe let test = () => { let str = "Hello World" - let strRef = WasmRef.fromGrain(str) - let strData = DataStructures.getStringArrayRef(strRef) + let strRef = DS.String.fromGrain(str) + let strData = DS.String.getData(strRef) use WasmI32.{ (==) } assert WasmArrayRef.getI8S(strData, 0n) == 0x48n // 'H' assert WasmArrayRef.getI8S(strData, 1n) == 0x65n // 'e' @@ -125,8 +125,8 @@ module WasmArrayRef { assert WasmArrayRef.getI8S(strData, 8n) == 0x72n // 'r' assert WasmArrayRef.getI8S(strData, 9n) == 0x6Cn // 'l' assert WasmArrayRef.getI8S(strData, 10n) == 0x64n // 'd' - let bytes = DataStructures.allocateBytes(1n) - let bytesData = DataStructures.getBytesArrayRef(bytes) + let bytes = DS.Bytes.make(1n) + let bytesData = DS.Bytes.getData(bytes) assert WasmArrayRef.getI8S(bytesData, 0n) == 0n WasmArrayRef.setI8(bytesData, 0n, 1n) assert WasmArrayRef.getI8S(bytesData, 0n) == 1n @@ -138,8 +138,8 @@ module WasmArrayRef { @unsafe let test = () => { let str = "Hello World" - let strRef = WasmRef.fromGrain(str) - let strData = DataStructures.getStringArrayRef(strRef) + let strRef = DS.String.fromGrain(str) + let strData = DS.String.getData(strRef) use WasmI32.{ (==) } assert WasmArrayRef.getI8U(strData, 0n) == 0x48n // 'H' assert WasmArrayRef.getI8U(strData, 1n) == 0x65n // 'e' @@ -152,8 +152,8 @@ module WasmArrayRef { assert WasmArrayRef.getI8U(strData, 8n) == 0x72n // 'r' assert WasmArrayRef.getI8U(strData, 9n) == 0x6Cn // 'l' assert WasmArrayRef.getI8U(strData, 10n) == 0x64n // 'd' - let bytes = DataStructures.allocateBytes(1n) - let bytesData = DataStructures.getBytesArrayRef(bytes) + let bytes = DS.Bytes.make(1n) + let bytesData = DS.Bytes.getData(bytes) assert WasmArrayRef.getI8U(bytesData, 0n) == 0n WasmArrayRef.setI8(bytesData, 0n, 1n) assert WasmArrayRef.getI8U(bytesData, 0n) == 1n @@ -166,7 +166,7 @@ module WasmArrayRef { let test = () => { let bigInt = 1t let bigIntRef = WasmRef.fromGrain(bigInt) - let bigIntData = DataStructures.getBigIntArrayRef(bigIntRef) + let bigIntData = DS.BigInt.getLimbData(bigIntRef) use WasmI64.{ (==) } assert WasmArrayRef.getI64(bigIntData, 0n) == 1N WasmArrayRef.setI64(bigIntData, 0n, -1N) @@ -177,8 +177,8 @@ module WasmArrayRef { @unsafe let test = () => { let arr = [> true, false] - let arrRef = WasmRef.fromGrain(arr) - let arrData = DataStructures.getCompoundValueArrayRef(arrRef) + let arrRef = DS.Array.fromGrain(arr) + let arrData = DS.Array.getData(arrRef) assert WasmArrayRef.getAny(arrData, 0n) is WasmRef.fromGrain(true) assert WasmArrayRef.getAny(arrData, 1n) is WasmRef.fromGrain(false) WasmArrayRef.setAny(arrData, 0n, WasmRef.fromGrain(false)) @@ -191,8 +191,8 @@ module WasmArrayRef { @unsafe let test = () => { use WasmI32.{ (==) } - let bytesRef = DataStructures.allocateBytes(4n) - let bytesData = DataStructures.getBytesArrayRef(bytesRef) + let bytesRef = DS.Bytes.make(4n) + let bytesData = DS.Bytes.getData(bytesRef) assert WasmArrayRef.getI8S(bytesData, 0n) == 0n assert WasmArrayRef.getI8S(bytesData, 1n) == 0n assert WasmArrayRef.getI8S(bytesData, 2n) == 0n @@ -213,11 +213,11 @@ module WasmArrayRef { @unsafe let test = () => { use WasmI32.{ (==) } - let bytes1Ref = DataStructures.allocateBytes(4n) - let bytes1Data = DataStructures.getBytesArrayRef(bytes1Ref) + let bytes1Ref = DS.Bytes.make(4n) + let bytes1Data = DS.Bytes.getData(bytes1Ref) WasmArrayRef.fillI8(bytes1Data, 0n, 3n, 4n) - let bytes2Ref = DataStructures.allocateBytes(4n) - let bytes2Data = DataStructures.getBytesArrayRef(bytes2Ref) + let bytes2Ref = DS.Bytes.make(4n) + let bytes2Data = DS.Bytes.getData(bytes2Ref) assert WasmArrayRef.getI8S(bytes2Data, 0n) == 0n assert WasmArrayRef.getI8S(bytes2Data, 1n) == 0n assert WasmArrayRef.getI8S(bytes2Data, 2n) == 0n @@ -233,14 +233,14 @@ module WasmArrayRef { @unsafe let test = () => { use WasmI64.{ (==) } - let bigInt1Ref = DataStructures.allocateBigInt(4n) - let bigInt1Data = DataStructures.getBigIntArrayRef(bigInt1Ref) + let bigInt1Ref = DS.BigInt.make(4n) + let bigInt1Data = DS.BigInt.getLimbData(bigInt1Ref) WasmArrayRef.setI64(bigInt1Data, 0n, 3N) WasmArrayRef.setI64(bigInt1Data, 1n, 3N) WasmArrayRef.setI64(bigInt1Data, 2n, 3N) WasmArrayRef.setI64(bigInt1Data, 3n, 3N) - let bigInt2Ref = DataStructures.allocateBigInt(4n) - let bigInt2Data = DataStructures.getBigIntArrayRef(bigInt2Ref) + let bigInt2Ref = DS.BigInt.make(4n) + let bigInt2Data = DS.BigInt.getLimbData(bigInt2Ref) assert WasmArrayRef.getI64(bigInt2Data, 0n) == 0N assert WasmArrayRef.getI64(bigInt2Data, 1n) == 0N assert WasmArrayRef.getI64(bigInt2Data, 2n) == 0N @@ -257,10 +257,10 @@ module WasmArrayRef { let test = () => { let trueRef = WasmRef.fromGrain(true) let falseRef = WasmRef.fromGrain(false) - let arr1Ref = DataStructures.allocateArray(4n, trueRef) - let arr1Data = DataStructures.getCompoundValueArrayRef(arr1Ref) - let arr2Ref = DataStructures.allocateArray(4n, falseRef) - let arr2Data = DataStructures.getCompoundValueArrayRef(arr2Ref) + let arr1Ref = DS.Array.make(4n, trueRef) + let arr1Data = DS.Array.getData(arr1Ref) + let arr2Ref = DS.Array.make(4n, falseRef) + let arr2Data = DS.Array.getData(arr2Ref) assert WasmArrayRef.getAny(arr2Data, 0n) is falseRef assert WasmArrayRef.getAny(arr2Data, 1n) is falseRef assert WasmArrayRef.getAny(arr2Data, 2n) is falseRef @@ -276,10 +276,10 @@ module WasmArrayRef { @unsafe let test = () => { use WasmI32.{ (==), (<), (>) } - let str1Ref = WasmRef.fromGrain("ABC") - let str1Data = DataStructures.getStringArrayRef(str1Ref) - let str2Ref = WasmRef.fromGrain("CBA") - let str2Data = DataStructures.getStringArrayRef(str2Ref) + let str1Ref = DS.String.fromGrain("ABC") + let str1Data = DS.String.getData(str1Ref) + let str2Ref = DS.String.fromGrain("CBA") + let str2Data = DS.String.getData(str2Ref) // No offset, full length comparisons assert WasmArrayRef.compareI8(str1Data, 0n, str2Data, 0n, 3n) < 0n assert WasmArrayRef.compareI8(str2Data, 0n, str1Data, 0n, 3n) > 0n diff --git a/docs/contributor/data_representations.md b/docs/contributor/data_representations.md index 78e8acd23c..6796a0fd14 100644 --- a/docs/contributor/data_representations.md +++ b/docs/contributor/data_representations.md @@ -2,7 +2,7 @@ As of Grain `v0.8` [Wasm GC](https://github.com/WebAssembly/gc) is used to represent all grain values. This means that all values are represented either as an `ref i31` or `struct`. We use `ref i31`s to represent "stack" values, which are values that can be stored directly in a WebAssembly `i32`, and we use `struct`s to represent "heap" values, which are values that require more than 32 bits of data to represent. The layout of the `struct`s is described in the section on heap-allocated data. -While this document describes the structure of grain values directly it is preferable to use helper functions provided by the runtime through `runtime/unsafe/conv` and `runtime/dataStructures` when manipulating grain values. +While this document describes the structure of grain values directly it is preferable to use helper functions provided by the runtime through `runtime/unsafe/dataStructures` when manipulating grain values. ## Value tagging diff --git a/docs/contributor/low_level_programming.md b/docs/contributor/low_level_programming.md index 5522353ca3..6cc06c9993 100644 --- a/docs/contributor/low_level_programming.md +++ b/docs/contributor/low_level_programming.md @@ -6,7 +6,7 @@ Grain is overwhelmingly a high-level programming language, but it provides a few Grain has five unsafe types, `WasmI32`, `WasmI64`, `WasmF32`, `WasmF64` and `WasmRef`. Unlike all other Grain types which are always represented as a `GrainValue`, these special types are represented by their corresponding WebAssembly types. This makes these types **incompatible** with regular Grain values, and they cannot be used with generic functions (like `print`) or appear in Grain's heap values (like in a `list`, `array`, `tuple` or any other high level dataStructure). While the typechecker will often prevent you from using them incorrectly, some uses may not be caught (#1153). -Libraries for working with these types can be found in `stdlib/runtime/unsafe` and map directly to WebAssembly instructions. `runtime/debugPrint` can be used to print values of these types for debugging purposes as they cannot be printed with the regular `print` function due to their incompatibility with regular Grain values. +Libraries for working with these types can be found in `stdlib/runtime/unsafe` and map directly to WebAssembly instructions. `runtime/unsafe/debugPrint` can be used to print values of these types for debugging purposes as they cannot be printed with the regular `print` function due to their incompatibility with regular Grain values. ## `@unsafe` diff --git a/stdlib/array.gr b/stdlib/array.gr index 295aae2b67..db3d12cf60 100644 --- a/stdlib/array.gr +++ b/stdlib/array.gr @@ -17,14 +17,7 @@ from "list" include List from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - allocateArray, - tagSimpleNumber, - untagSimpleNumber, - getStringArrayRef, - getCompoundValueArrayRef, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/exception" include Exception from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToWasmI32 } @@ -35,7 +28,7 @@ let checkLength = length => { if (WasmRef.isGrainHeapValue(WasmRef.fromGrain(length))) { throw Exception.InvalidArgument("Length argument must be an integer") } - let len = untagSimpleNumber(length) + let len = DS.untagSimpleNumber(length) if (len < 0n) { throw Exception.InvalidArgument("Length argument must be non-negative") } @@ -84,8 +77,8 @@ provide primitive length = "@array.length" provide let make = (length: Number, item: a) => { checkLength(length) let length = coerceNumberToWasmI32(length) - let array = allocateArray(length, WasmRef.fromGrain(item)) - WasmRef.toGrain(array): Array + let array = DS.Array.make(length, WasmRef.fromGrain(item)) + DS.Array.toGrain(array): Array } /** @@ -109,12 +102,16 @@ provide let init = (length: Number, fn: Number => a) => { use WasmI32.{ (+), (*), (<) } checkLength(length) let length = coerceNumberToWasmI32(length) - let arrayRef = allocateArray(length, WasmRef.fromGrain(void)) - let arrayData = getCompoundValueArrayRef(arrayRef) + let arrayRef = DS.Array.make(length, WasmRef.fromGrain(void)) + let arrayData = DS.Array.getData(arrayRef) for (let mut i = 0n; i < length; i += 1n) { - WasmArrayRef.setAny(arrayData, i, WasmRef.fromGrain(fn(tagSimpleNumber(i)))) + WasmArrayRef.setAny( + arrayData, + i, + WasmRef.fromGrain(fn(DS.tagSimpleNumber(i))) + ) } - WasmRef.toGrain(arrayRef): Array + DS.Array.toGrain(arrayRef): Array } /** @@ -140,10 +137,10 @@ provide let tryInit = (length: Number, fn: Number => Result) => { use WasmI32.{ (+), (*), (<) } checkLength(length) let length = coerceNumberToWasmI32(length) - let arrayRef = allocateArray(length, WasmRef.fromGrain(void)) - let arrayData = getCompoundValueArrayRef(arrayRef) + let arrayRef = DS.Array.make(length, WasmRef.fromGrain(void)) + let arrayData = DS.Array.getData(arrayRef) for (let mut i = 0n; i < length; i += 1n) { - let value = fn(tagSimpleNumber(i)) + let value = fn(DS.tagSimpleNumber(i)) match (value) { Ok(value) => { WasmArrayRef.setAny(arrayData, i, WasmRef.fromGrain(value)) @@ -153,7 +150,7 @@ provide let tryInit = (length: Number, fn: Number => Result) => { }, } } - return Ok(WasmRef.toGrain(arrayRef): Array) + return Ok(DS.Array.toGrain(arrayRef): Array) } /** @@ -995,30 +992,29 @@ provide let unzip = array => { @unsafe provide let join = (separator: String, items: Array) => { use WasmI32.{ (+), (==) } - use DataStructures.{ allocateString, getStringSize } let arrLen = length(items) - let sepRef = WasmRef.fromGrain(separator) - let sepData = getStringArrayRef(sepRef) - let sepSize = getStringSize(sepRef) + let sepRef = DS.String.fromGrain(separator) + let sepData = DS.String.getData(sepRef) + let sepSize = DS.String.getSize(sepRef) let mut strSize = 0n for (let mut i = 0; i < arrLen; i = incr(i)) { - strSize += getStringSize(WasmRef.fromGrain(items[i])) + strSize += DS.String.getSize(DS.String.fromGrain(items[i])) if (i != 0) strSize += sepSize } - let newStringRef = allocateString(strSize) - let newStringData = getStringArrayRef(newStringRef) + let newStringRef = DS.String.make(strSize) + let newStringData = DS.String.getData(newStringRef) let mut offset = 0n for (let mut i = 0; i < arrLen; i = incr(i)) { - let ref = WasmRef.fromGrain(items[i]) - let data = getStringArrayRef(ref) - let size = getStringSize(ref) + let ref = DS.String.fromGrain(items[i]) + let data = DS.String.getData(ref) + let size = DS.String.getSize(ref) WasmArrayRef.copyI8(newStringData, offset, data, 0n, size) offset += size if (i < arrLen - 1) WasmArrayRef.copyI8(newStringData, offset, sepData, 0n, sepSize) offset += sepSize } - WasmRef.toGrain(newStringRef): String + DS.String.toGrain(newStringRef) } /** diff --git a/stdlib/bigint.gr b/stdlib/bigint.gr index 631b6adc56..b0c32e2aaf 100644 --- a/stdlib/bigint.gr +++ b/stdlib/bigint.gr @@ -13,8 +13,8 @@ module BigInt from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmref" include WasmRef +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/bigint" include Bigint as BI -from "runtime/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ @@ -242,7 +242,7 @@ provide let gcd = (num1: BigInt, num2: BigInt) => { @unsafe provide let (<<) = (num: BigInt, places: Int32) => { let num = WasmRef.fromGrain(num) - let places = DS.getInt32Value(WasmRef.fromGrain(places)) + let places = DS.Int32.fromInt32(places) WasmRef.toGrain(BI.shl(num, places)): BigInt } @@ -263,7 +263,7 @@ provide let (<<) = (num: BigInt, places: Int32) => { @unsafe provide let (>>) = (num: BigInt, places: Int32) => { let num = WasmRef.fromGrain(num) - let places = DS.getInt32Value(WasmRef.fromGrain(places)) + let places = DS.Int32.fromInt32(places) WasmRef.toGrain(BI.shrS(num, places)): BigInt } @@ -512,8 +512,7 @@ provide let (^) = (num1: BigInt, num2: BigInt) => { */ @unsafe provide let clz = (num: BigInt) => { - WasmRef.toGrain(DS.newInt32(BI.countLeadingZeros(WasmRef.fromGrain(num)))): - Int32 + DS.Int32.toInt32(BI.countLeadingZeros(WasmRef.fromGrain(num))) } /** @@ -528,8 +527,7 @@ provide let clz = (num: BigInt) => { */ @unsafe provide let ctz = (num: BigInt) => { - WasmRef.toGrain(DS.newInt64(BI.countTrailingZeros(WasmRef.fromGrain(num)))): - Int64 + DS.Int64.toInt64(BI.countTrailingZeros(WasmRef.fromGrain(num))) } /** diff --git a/stdlib/buffer.gr b/stdlib/buffer.gr index be657ba0a4..456b324d18 100644 --- a/stdlib/buffer.gr +++ b/stdlib/buffer.gr @@ -15,15 +15,7 @@ from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/exception" include Exception -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - untagChar, - tagSimpleNumber, - getBytesSize, - getBytesArrayRef, - getStringSize, - getStringArrayRef, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS from "int32" include Int32 from "bytes" include Bytes from "string" include String @@ -226,9 +218,9 @@ provide let truncate = (length, buffer) => { if (length < 0 || length > buffer.len) throw IndexOutOfBounds use WasmI32.{ (-) } - let srcRef = WasmRef.fromGrain(buffer.data) - let srcSize = getBytesSize(srcRef) - let srcData = getBytesArrayRef(srcRef) + let srcRef = DS.Bytes.fromGrain(buffer.data) + let srcSize = DS.Bytes.getSize(srcRef) + let srcData = DS.Bytes.getData(srcRef) let off = coerceNumberToWasmI32(length) WasmArrayRef.fillI8(srcData, off, 0n, srcSize - off) buffer.len = length @@ -331,11 +323,11 @@ provide let addBytes = (bytes, buffer) => { autogrow(bytelen, buffer) let off = coerceNumberToWasmI32(buffer.len) - let srcRef = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(srcRef) - let srcData = getBytesArrayRef(srcRef) - let dstRef = WasmRef.fromGrain(buffer.data) - let dstData = getBytesArrayRef(dstRef) + let srcRef = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(srcRef) + let srcData = DS.Bytes.getData(srcRef) + let dstRef = DS.Bytes.fromGrain(buffer.data) + let dstData = DS.Bytes.getData(dstRef) WasmArrayRef.copyI8(dstData, off, srcData, 0n, srcSize) buffer.len += bytelen @@ -361,11 +353,11 @@ provide let addString = (string, buffer) => { autogrow(bytelen, buffer) let off = coerceNumberToWasmI32(buffer.len) - let srcRef = WasmRef.fromGrain(string) - let srcSize = getStringSize(srcRef) - let srcData = getStringArrayRef(srcRef) - let dstRef = WasmRef.fromGrain(buffer.data) - let dstData = getBytesArrayRef(dstRef) + let srcRef = DS.String.fromGrain(string) + let srcSize = DS.String.getSize(srcRef) + let srcData = DS.String.getData(srcRef) + let dstRef = DS.Bytes.fromGrain(buffer.data) + let dstData = DS.Bytes.getData(dstRef) WasmArrayRef.copyI8(dstData, off, srcData, 0n, srcSize) buffer.len += bytelen @@ -396,9 +388,9 @@ provide let getChar = (index, buffer) => { // Note: We do a raw check as we need the byte length before reading the full char let offset = coerceNumberToWasmI32(index) let bytes = buffer.data - let bytesRef = WasmRef.fromGrain(bytes) - let bytesSize = getBytesSize(bytesRef) - let bytesData = getBytesArrayRef(bytesRef) + let bytesRef = DS.Bytes.fromGrain(bytes) + let bytesSize = DS.Bytes.getSize(bytesRef) + let bytesData = DS.Bytes.getData(bytesRef) let byte = WasmArrayRef.getI8U(bytesData, offset) let charSize = utf8ByteCount(byte) if (offset + charSize > coerceNumberToWasmI32(buffer.len)) { @@ -422,8 +414,8 @@ provide let getChar = (index, buffer) => { */ @unsafe provide let addChar = (char, buffer) => { - let usv = untagChar(char) - let byteCount = tagSimpleNumber(usvEncodeLength(usv)) + let usv = DS.untagChar(char) + let byteCount = DS.tagSimpleNumber(usvEncodeLength(usv)) autogrow(byteCount, buffer) let index = buffer.len buffer.len += byteCount @@ -472,10 +464,10 @@ provide let addStringSlice = (start: Number, end, string, buffer) => { autogrow(bytelen, buffer) let dstOff = coerceNumberToWasmI32(buffer.len) - let srcRef = WasmRef.fromGrain(slice) - let srcData = getStringArrayRef(srcRef) - let dstRef = WasmRef.fromGrain(buffer.data) - let dstData = getBytesArrayRef(dstRef) + let srcRef = DS.String.fromGrain(slice) + let srcData = DS.String.getData(srcRef) + let dstRef = DS.Bytes.fromGrain(buffer.data) + let dstData = DS.Bytes.getData(dstRef) WasmArrayRef.copyI8( dstData, dstOff, @@ -518,9 +510,9 @@ provide let addBytesSlice = ( use WasmI32.{ (-), (<), (>), (>=) } // bounds check start - let srcRef = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(srcRef) - let srcData = getBytesArrayRef(srcRef) + let srcRef = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(srcRef) + let srcData = DS.Bytes.getData(srcRef) let srcOff = coerceNumberToWasmI32(start) if (srcOff < 0n || srcOff >= srcSize) { throw IndexOutOfBounds @@ -535,8 +527,8 @@ provide let addBytesSlice = ( autogrow(length, buffer) let dstOff = coerceNumberToWasmI32(buffer.len) - let dstRef = WasmRef.fromGrain(buffer.data) - let dstData = getBytesArrayRef(dstRef) + let dstRef = DS.Bytes.fromGrain(buffer.data) + let dstData = DS.Bytes.getData(dstRef) WasmArrayRef.copyI8(dstData, dstOff, srcData, srcOff, len) buffer.len += length diff --git a/stdlib/bytes.gr b/stdlib/bytes.gr index dbc41bac1c..138084091e 100644 --- a/stdlib/bytes.gr +++ b/stdlib/bytes.gr @@ -16,26 +16,7 @@ from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmf32" include WasmF32 from "runtime/unsafe/wasmf64" include WasmF64 -from "runtime/unsafe/conv" include Conv -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - tagChar, - tagInt8, - tagUint8, - tagInt16, - tagUint16, - untagChar, - untagInt8, - untagUint8, - untagInt16, - untagUint16, - allocateBytes, - getBytesSize, - getBytesArrayRef, - allocateString, - getStringSize, - getStringArrayRef, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/exception" include Exception from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToWasmI32 } @@ -88,8 +69,8 @@ let checkIndexIsInBounds = (i, byteSize, max) => { provide let make = (size: Number) => { use WasmI32.{ (+) } let size = coerceNumberToWasmI32(size) - let bytes = allocateBytes(size) - WasmRef.toGrain(bytes): Bytes + let bytes = DS.Bytes.make(size) + DS.Bytes.toGrain(bytes) } /** @@ -114,13 +95,13 @@ provide let empty = make(0) @unsafe provide let fromString = (string: String) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(string) - let srcSize = getStringSize(src) - let srcData = getStringArrayRef(src) - let dst = allocateBytes(srcSize) - let dstData = getBytesArrayRef(dst) + let src = DS.String.fromGrain(string) + let srcSize = DS.String.getSize(src) + let srcData = DS.String.getData(src) + let dst = DS.Bytes.make(srcSize) + let dstData = DS.Bytes.getData(dst) WasmArrayRef.copyI8(dstData, 0n, srcData, 0n, srcSize) - WasmRef.toGrain(dst): Bytes + DS.Bytes.toGrain(dst) } /** @@ -136,13 +117,13 @@ provide let fromString = (string: String) => { */ @unsafe provide let toString = (bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) - let dst = allocateString(srcSize) - let dstData = getStringArrayRef(dst) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) + let dst = DS.String.make(srcSize) + let dstData = DS.String.getData(dst) WasmArrayRef.copyI8(dstData, 0n, srcData, 0n, srcSize) - WasmRef.toGrain(dst): String + DS.String.toGrain(dst) } /** @@ -158,9 +139,9 @@ provide let toString = (bytes: Bytes) => { */ @unsafe provide let length = (bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - Conv.wasmI32ToNumber(srcSize) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + DS.wasmI32ToNumber(srcSize) } /** @@ -175,13 +156,13 @@ provide let length = (bytes: Bytes) => { */ @unsafe provide let copy = (bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) - let dst = allocateBytes(srcSize) - let dstData = getBytesArrayRef(dst) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) + let dst = DS.Bytes.make(srcSize) + let dstData = DS.Bytes.getData(dst) WasmArrayRef.copyI8(dstData, 0n, srcData, 0n, srcSize) - WasmRef.toGrain(dst): Bytes + DS.Bytes.toGrain(dst) } /** @@ -203,9 +184,9 @@ provide let copy = (bytes: Bytes) => { */ @unsafe provide let slice = (start: Number, length: Number, bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) use WasmI32.{ (+), (>) } let start = coerceNumberToWasmI32(start) @@ -216,10 +197,10 @@ provide let slice = (start: Number, length: Number, bytes: Bytes) => { ) } - let dst = allocateBytes(length) - let dstData = getBytesArrayRef(dst) + let dst = DS.Bytes.make(length) + let dstData = DS.Bytes.getData(dst) WasmArrayRef.copyI8(dstData, 0n, srcData, start, length) - WasmRef.toGrain(dst): Bytes + DS.Bytes.toGrain(dst) } /** @@ -240,9 +221,9 @@ provide let slice = (start: Number, length: Number, bytes: Bytes) => { */ @unsafe provide let resize = (left: Number, right: Number, bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) use WasmI32.{ (+), (-), (*), (<), (>) } let left = coerceNumberToWasmI32(left) @@ -252,8 +233,8 @@ provide let resize = (left: Number, right: Number, bytes: Bytes) => { throw Exception.InvalidArgument("The resulting length is less than 0") } - let dst = allocateBytes(newSize) - let dstData = getBytesArrayRef(dst) + let dst = DS.Bytes.make(newSize) + let dstData = DS.Bytes.getData(dst) let mut srcOffset = 0n let mut dstOffset = 0n if (left < 0n) { @@ -272,7 +253,7 @@ provide let resize = (left: Number, right: Number, bytes: Bytes) => { if (len > 0n) { WasmArrayRef.copyI8(dstData, dstOffset, srcData, srcOffset, len) } - WasmRef.toGrain(dst): Bytes + DS.Bytes.toGrain(dst) } /** @@ -304,13 +285,13 @@ provide let move = ( dst: Bytes, ) => { use WasmI32.{ (+), (>) } - let src = WasmRef.fromGrain(src) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(src) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) - let dst = WasmRef.fromGrain(dst) - let dstSize = getBytesSize(dst) - let dstData = getBytesArrayRef(dst) + let dst = DS.Bytes.fromGrain(dst) + let dstSize = DS.Bytes.getSize(dst) + let dstData = DS.Bytes.getData(dst) let srcIndex = coerceNumberToWasmI32(srcIndex) let dstIndex = coerceNumberToWasmI32(dstIndex) @@ -365,10 +346,10 @@ provide let concat = (bytes1: Bytes, bytes2: Bytes) => { @unsafe provide let fill = (value: Uint8, bytes: Bytes) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) - WasmArrayRef.fillI8(srcData, 0n, untagUint8(value), srcSize) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) + WasmArrayRef.fillI8(srcData, 0n, DS.untagUint8(value), srcSize) } /** @@ -409,9 +390,9 @@ provide let clear = (bytes: Bytes) => { @unsafe provide let getChar = (index: Number, bytes: Bytes) => { use WasmI32.{ (+), (&), (+), (==), (>) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, 1n, srcSize) @@ -421,7 +402,7 @@ provide let getChar = (index: Number, bytes: Bytes) => { throw MalformedUnicode } let codePoint = getCodePoint(srcData, offset) - tagChar(codePoint) + DS.tagChar(codePoint) } /** @@ -444,11 +425,11 @@ provide let getChar = (index: Number, bytes: Bytes) => { @unsafe provide let setChar = (index: Number, value: Char, bytes: Bytes) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) - let usv = untagChar(value) + let usv = DS.untagChar(value) let charSize = usvEncodeLength(usv) checkIndexIsInBounds(offset, charSize, srcSize) ignore(writeUtf8CodePoint(srcData, offset, usv)) @@ -475,13 +456,13 @@ provide let setChar = (index: Number, value: Char, bytes: Bytes) => { @unsafe provide let getInt8 = (index: Number, bytes: Bytes) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, srcSize) let n = WasmArrayRef.getI8S(srcData, offset) - tagInt8(n) + DS.tagInt8(n) } /** @@ -505,12 +486,12 @@ provide let getInt8 = (index: Number, bytes: Bytes) => { @unsafe provide let setInt8 = (index: Number, value: Int8, bytes: Bytes) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, srcSize) - let v = untagInt8(value) + let v = DS.untagInt8(value) WasmArrayRef.setI8(srcData, offset, v) } @@ -535,13 +516,13 @@ provide let setInt8 = (index: Number, value: Int8, bytes: Bytes) => { @unsafe provide let getUint8 = (index: Number, bytes: Bytes) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, srcSize) let n = WasmArrayRef.getI8U(srcData, offset) - tagUint8(n) + DS.tagUint8(n) } /** @@ -564,12 +545,12 @@ provide let getUint8 = (index: Number, bytes: Bytes) => { @unsafe provide let setUint8 = (index: Number, value: Uint8, bytes: Bytes) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, srcSize) - let v = untagUint8(value) + let v = DS.untagUint8(value) WasmArrayRef.setI8(srcData, offset, v) } @@ -594,15 +575,15 @@ provide let setUint8 = (index: Number, value: Uint8, bytes: Bytes) => { @unsafe provide let getInt16 = (index: Number, bytes: Bytes) => { use WasmI32.{ (+), (|), (<<), (>>) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, srcSize) let byte1 = WasmArrayRef.getI8U(srcData, offset) let byte2 = WasmArrayRef.getI8U(srcData, offset + 1n) let n = ((byte1 | byte2 << 8n) << 16n) >> 16n - tagInt16(n) + DS.tagInt16(n) } /** @@ -626,12 +607,12 @@ provide let getInt16 = (index: Number, bytes: Bytes) => { @unsafe provide let setInt16 = (index: Number, value: Int16, bytes: Bytes) => { use WasmI32.{ (+), (&), (>>), (<<) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, srcSize) - let v = untagInt16(value) + let v = DS.untagInt16(value) WasmArrayRef.setI8(srcData, offset + 0n, v) WasmArrayRef.setI8(srcData, offset + 1n, v >> 8n) } @@ -657,15 +638,15 @@ provide let setInt16 = (index: Number, value: Int16, bytes: Bytes) => { @unsafe provide let getUint16 = (index: Number, bytes: Bytes) => { use WasmI32.{ (+), (|), (<<), (>>) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, srcSize) let byte1 = WasmArrayRef.getI8U(srcData, offset) let byte2 = WasmArrayRef.getI8U(srcData, offset + 1n) let n = byte1 | byte2 << 8n - tagUint16(n) + DS.tagUint16(n) } /** @@ -688,12 +669,12 @@ provide let getUint16 = (index: Number, bytes: Bytes) => { @unsafe provide let setUint16 = (index: Number, value: Uint16, bytes: Bytes) => { use WasmI32.{ (+), (&), (>>) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, srcSize) - let v = untagUint16(value) + let v = DS.untagUint16(value) WasmArrayRef.setI8(srcData, offset + 0n, v) WasmArrayRef.setI8(srcData, offset + 1n, v >> 8n) } @@ -718,9 +699,9 @@ provide let setUint16 = (index: Number, value: Uint16, bytes: Bytes) => { @unsafe provide let getInt32 = (index: Number, bytes: Bytes) => { use WasmI32.{ (+), (|), (<<) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, srcSize) let byte1 = WasmArrayRef.getI8U(srcData, offset) @@ -728,7 +709,7 @@ provide let getInt32 = (index: Number, bytes: Bytes) => { let byte3 = WasmArrayRef.getI8U(srcData, offset + 2n) let byte4 = WasmArrayRef.getI8U(srcData, offset + 3n) let n = byte1 | byte2 << 8n | byte3 << 16n | byte4 << 24n - Conv.toInt32(n) + DS.Int32.toInt32(n) } /** @@ -751,12 +732,12 @@ provide let getInt32 = (index: Number, bytes: Bytes) => { @unsafe provide let setInt32 = (index: Number, value: Int32, bytes: Bytes) => { use WasmI32.{ (+), (&), (>>) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, srcSize) - let v = Conv.fromInt32(value) + let v = DS.Int32.fromInt32(value) WasmArrayRef.setI8(srcData, offset + 0n, v) WasmArrayRef.setI8(srcData, offset + 1n, v >> 8n) WasmArrayRef.setI8(srcData, offset + 2n, v >> 16n) @@ -783,9 +764,9 @@ provide let setInt32 = (index: Number, value: Int32, bytes: Bytes) => { @unsafe provide let getUint32 = (index: Number, bytes: Bytes) => { use WasmI32.{ (+), (|), (<<) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, srcSize) let byte1 = WasmArrayRef.getI8U(srcData, offset) @@ -793,7 +774,7 @@ provide let getUint32 = (index: Number, bytes: Bytes) => { let byte3 = WasmArrayRef.getI8U(srcData, offset + 2n) let byte4 = WasmArrayRef.getI8U(srcData, offset + 3n) let n = byte1 | byte2 << 8n | byte3 << 16n | byte4 << 24n - Conv.toUint32(n) + DS.Uint32.toUint32(n) } /** @@ -816,12 +797,12 @@ provide let getUint32 = (index: Number, bytes: Bytes) => { @unsafe provide let setUint32 = (index: Number, value: Uint32, bytes: Bytes) => { use WasmI32.{ (+), (&), (>>) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, srcSize) - let v = Conv.fromUint32(value) + let v = DS.Uint32.fromUint32(value) WasmArrayRef.setI8(srcData, offset + 0n, v) WasmArrayRef.setI8(srcData, offset + 1n, v >> 8n) WasmArrayRef.setI8(srcData, offset + 2n, v >> 16n) @@ -848,9 +829,9 @@ provide let setUint32 = (index: Number, value: Uint32, bytes: Bytes) => { @unsafe provide let getFloat32 = (index: Number, bytes: Bytes) => { use WasmI32.{ (+), (|), (<<) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _FLOAT32_BYTE_SIZE, srcSize) let byte1 = WasmArrayRef.getI8U(srcData, offset) @@ -858,7 +839,7 @@ provide let getFloat32 = (index: Number, bytes: Bytes) => { let byte3 = WasmArrayRef.getI8U(srcData, offset + 2n) let byte4 = WasmArrayRef.getI8U(srcData, offset + 3n) let n = byte1 | byte2 << 8n | byte3 << 16n | byte4 << 24n - Conv.toFloat32(WasmF32.reinterpretI32(n)) + DS.Float32.toFloat32(WasmF32.reinterpretI32(n)) } /** @@ -881,12 +862,12 @@ provide let getFloat32 = (index: Number, bytes: Bytes) => { @unsafe provide let setFloat32 = (index: Number, value: Float32, bytes: Bytes) => { use WasmI32.{ (+), (&), (>>) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _FLOAT32_BYTE_SIZE, srcSize) - let v = WasmI32.reinterpretF32(Conv.fromFloat32(value)) + let v = WasmI32.reinterpretF32(DS.Float32.fromFloat32(value)) WasmArrayRef.setI8(srcData, offset + 0n, v) WasmArrayRef.setI8(srcData, offset + 1n, v >> 8n) WasmArrayRef.setI8(srcData, offset + 2n, v >> 16n) @@ -912,9 +893,9 @@ provide let setFloat32 = (index: Number, value: Float32, bytes: Bytes) => { */ @unsafe provide let getInt64 = (index: Number, bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, srcSize) use WasmI32.{ (+), (|), (<<) } @@ -930,7 +911,7 @@ provide let getInt64 = (index: Number, bytes: Bytes) => { let hi32 = WasmI64.extendI32U(hi0 | hi1 << 8n | hi2 << 16n | hi3 << 24n) use WasmI64.{ (<<), (|) } let n = hi32 << 32N | lo32 - Conv.toInt64(n) + DS.Int64.toInt64(n) } /** @@ -952,12 +933,12 @@ provide let getInt64 = (index: Number, bytes: Bytes) => { */ @unsafe provide let setInt64 = (index: Number, value: Int64, bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, srcSize) - let v = Conv.fromInt64(value) + let v = DS.Int64.fromInt64(value) use WasmI64.{ (&), (>>) } let lo = WasmI32.wrapI64(v & 0xffffffffN) let hi = WasmI32.wrapI64(v >> 32N) @@ -991,9 +972,9 @@ provide let setInt64 = (index: Number, value: Int64, bytes: Bytes) => { */ @unsafe provide let getUint64 = (index: Number, bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, srcSize) use WasmI32.{ (+), (|), (<<) } @@ -1009,7 +990,7 @@ provide let getUint64 = (index: Number, bytes: Bytes) => { let hi32 = WasmI64.extendI32U(hi0 | hi1 << 8n | hi2 << 16n | hi3 << 24n) use WasmI64.{ (<<), (|) } let n = hi32 << 32N | lo32 - Conv.toUint64(n) + DS.Uint64.toUint64(n) } /** @@ -1031,12 +1012,12 @@ provide let getUint64 = (index: Number, bytes: Bytes) => { */ @unsafe provide let setUint64 = (index: Number, value: Uint64, bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, srcSize) - let v = Conv.fromUint64(value) + let v = DS.Uint64.fromUint64(value) use WasmI64.{ (&), (>>) } let lo = WasmI32.wrapI64(v & 0xffffffffN) let hi = WasmI32.wrapI64(v >> 32N) @@ -1070,9 +1051,9 @@ provide let setUint64 = (index: Number, value: Uint64, bytes: Bytes) => { */ @unsafe provide let getFloat64 = (index: Number, bytes: Bytes) => { - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _FLOAT64_BYTE_SIZE, srcSize) use WasmI32.{ (+), (|), (<<) } @@ -1088,7 +1069,7 @@ provide let getFloat64 = (index: Number, bytes: Bytes) => { let hi32 = WasmI64.extendI32U(hi0 | hi1 << 8n | hi2 << 16n | hi3 << 24n) use WasmI64.{ (<<), (|) } let n = hi32 << 32N | lo32 - Conv.toFloat64(WasmF64.reinterpretI64(n)) + DS.Float64.toFloat64(WasmF64.reinterpretI64(n)) } /** @@ -1111,12 +1092,12 @@ provide let getFloat64 = (index: Number, bytes: Bytes) => { @unsafe provide let setFloat64 = (index: Number, value: Float64, bytes: Bytes) => { use WasmI32.{ (+) } - let src = WasmRef.fromGrain(bytes) - let srcSize = getBytesSize(src) - let srcData = getBytesArrayRef(src) + let src = DS.Bytes.fromGrain(bytes) + let srcSize = DS.Bytes.getSize(src) + let srcData = DS.Bytes.getData(src) let offset = coerceNumberToWasmI32(index) checkIndexIsInBounds(offset, _FLOAT64_BYTE_SIZE, srcSize) - let v = WasmI64.reinterpretF64(Conv.fromFloat64(value)) + let v = WasmI64.reinterpretF64(DS.Float64.fromFloat64(value)) use WasmI64.{ (&), (>>) } let lo = WasmI32.wrapI64(v & 0xffffffffN) let hi = WasmI32.wrapI64(v >> 32N) diff --git a/stdlib/char.gr b/stdlib/char.gr index 735e7f785c..092b6aa3f3 100644 --- a/stdlib/char.gr +++ b/stdlib/char.gr @@ -15,15 +15,7 @@ module Char from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmref" include WasmRef -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - tagSimpleNumber, - untagSimpleNumber, - tagChar, - untagChar, - allocateString, - getStringArrayRef, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/utf8" include Utf8 use Utf8.{ usvEncodeLength, writeUtf8CodePoint } @@ -70,11 +62,11 @@ provide let isValid = charCode => { */ @unsafe provide let code = (char: Char) => { - let usv = untagChar(char) + let usv = DS.untagChar(char) // This could save an instruction by combining the two tagging operations, // though we stick with tagSimpleNumber for simplicity. - tagSimpleNumber(usv) + DS.tagSimpleNumber(usv) } /** @@ -99,7 +91,7 @@ provide let fromCode = (usv: Number) => { } // usv is now guaranteed to be a simple number - tagChar(untagSimpleNumber(usv)) + DS.tagChar(DS.untagSimpleNumber(usv)) } /** @@ -163,11 +155,11 @@ provide let pred = char => { */ @unsafe provide let toString = (char: Char) => { - let usv = untagChar(char) + let usv = DS.untagChar(char) let byteCount = usvEncodeLength(usv) - let string = allocateString(byteCount) - writeUtf8CodePoint(getStringArrayRef(string), 0n, usv) - WasmRef.toGrain(string): String + let string = DS.String.make(byteCount) + writeUtf8CodePoint(DS.String.getData(string), 0n, usv) + DS.String.toGrain(string) } /** @@ -196,9 +188,9 @@ provide enum Encoding { */ @unsafe provide let encodedLength = (encoding, char: Char) => { - let usv = untagChar(char) + let usv = DS.untagChar(char) let utf8ByteCount = usvEncodeLength(usv) - let utf8ByteCount = tagSimpleNumber(utf8ByteCount) + let utf8ByteCount = DS.tagSimpleNumber(utf8ByteCount) match (encoding) { UTF32 => 4, UTF16 => if (utf8ByteCount == 4) 2 else 1, @@ -225,8 +217,8 @@ provide let encodedLength = (encoding, char: Char) => { @unsafe provide let (<) = (x: Char, y: Char) => { use WasmI32.{ (<) } - let x = untagChar(x) - let y = untagChar(y) + let x = DS.untagChar(x) + let y = DS.untagChar(y) x < y } @@ -252,8 +244,8 @@ provide let (<) = (x: Char, y: Char) => { @unsafe provide let (<=) = (x: Char, y: Char) => { use WasmI32.{ (<=) } - let x = untagChar(x) - let y = untagChar(y) + let x = DS.untagChar(x) + let y = DS.untagChar(y) x <= y } @@ -276,8 +268,8 @@ provide let (<=) = (x: Char, y: Char) => { @unsafe provide let (>) = (x: Char, y: Char) => { use WasmI32.{ (>) } - let x = untagChar(x) - let y = untagChar(y) + let x = DS.untagChar(x) + let y = DS.untagChar(y) x > y } @@ -303,8 +295,8 @@ provide let (>) = (x: Char, y: Char) => { @unsafe provide let (>=) = (x: Char, y: Char) => { use WasmI32.{ (>=) } - let x = untagChar(x) - let y = untagChar(y) + let x = DS.untagChar(x) + let y = DS.untagChar(y) x >= y } diff --git a/stdlib/float32.gr b/stdlib/float32.gr index e5fa7b13ef..421599dbef 100644 --- a/stdlib/float32.gr +++ b/stdlib/float32.gr @@ -16,7 +16,7 @@ from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmf32" include WasmF32 from "runtime/unsafe/wasmf64" include WasmF64 use WasmF32.{ (+), (-), (*), (/), (<), (<=), (>), (>=) } -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToFloat32 as fromNumber, @@ -78,9 +78,9 @@ provide { fromNumber, toNumber } */ @unsafe provide let reinterpretInt32 = (value: Int32) => { - let value = Conv.fromInt32(value) + let value = DS.Int32.fromInt32(value) let value = WasmF32.reinterpretI32(value) - Conv.toFloat32(value) + DS.Float32.toFloat32(value) } /** @@ -96,9 +96,9 @@ provide let reinterpretInt32 = (value: Int32) => { */ @unsafe provide let reinterpretUint32 = (value: Uint32) => { - let value = Conv.fromUint32(value) + let value = DS.Uint32.fromUint32(value) let value = WasmF32.reinterpretI32(value) - Conv.toFloat32(value) + DS.Float32.toFloat32(value) } /** @@ -117,9 +117,9 @@ provide let reinterpretUint32 = (value: Uint32) => { */ @unsafe provide let (+) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) - Conv.toFloat32(xv + yv) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) + DS.Float32.toFloat32(xv + yv) } /** @@ -138,9 +138,9 @@ provide let (+) = (x: Float32, y: Float32) => { */ @unsafe provide let (-) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) - Conv.toFloat32(xv - yv) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) + DS.Float32.toFloat32(xv - yv) } /** @@ -159,9 +159,9 @@ provide let (-) = (x: Float32, y: Float32) => { */ @unsafe provide let (*) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) - Conv.toFloat32(xv * yv) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) + DS.Float32.toFloat32(xv * yv) } /** @@ -180,9 +180,9 @@ provide let (*) = (x: Float32, y: Float32) => { */ @unsafe provide let (/) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) - Conv.toFloat32(xv / yv) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) + DS.Float32.toFloat32(xv / yv) } /** @@ -200,13 +200,13 @@ provide let (/) = (x: Float32, y: Float32) => { */ @unsafe provide let (**) = (base: Float32, power: Float32) => { - let basev = Conv.fromFloat32(base) - let powerv = Conv.fromFloat32(power) + let basev = DS.Float32.fromFloat32(base) + let powerv = DS.Float32.fromFloat32(power) let value = Numbers.powf( WasmF64.promoteF32(basev), WasmF64.promoteF32(powerv) ) - Conv.toFloat32(WasmF32.demoteF64(value)) + DS.Float32.toFloat32(WasmF32.demoteF64(value)) } /** @@ -225,8 +225,8 @@ provide let (**) = (base: Float32, power: Float32) => { */ @unsafe provide let (<) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) xv < yv } @@ -246,8 +246,8 @@ provide let (<) = (x: Float32, y: Float32) => { */ @unsafe provide let (>) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) xv > yv } @@ -271,8 +271,8 @@ provide let (>) = (x: Float32, y: Float32) => { */ @unsafe provide let (<=) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) xv <= yv } @@ -296,8 +296,8 @@ provide let (<=) = (x: Float32, y: Float32) => { */ @unsafe provide let (>=) = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) xv >= yv } @@ -369,9 +369,9 @@ provide let isInfinite = (x: Float32) => x == Infinityf || x == -Infinityf */ @unsafe provide let min = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) - Conv.toFloat32(WasmF32.min(xv, yv)) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) + DS.Float32.toFloat32(WasmF32.min(xv, yv)) } /** @@ -387,9 +387,9 @@ provide let min = (x: Float32, y: Float32) => { */ @unsafe provide let max = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) - Conv.toFloat32(WasmF32.max(xv, yv)) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) + DS.Float32.toFloat32(WasmF32.max(xv, yv)) } /** @@ -405,8 +405,8 @@ provide let max = (x: Float32, y: Float32) => { */ @unsafe provide let abs = (x: Float32) => { - let xv = Conv.fromFloat32(x) - Conv.toFloat32(WasmF32.abs(xv)) + let xv = DS.Float32.fromFloat32(x) + DS.Float32.toFloat32(WasmF32.abs(xv)) } /** @@ -422,8 +422,8 @@ provide let abs = (x: Float32) => { */ @unsafe provide let neg = (x: Float32) => { - let xv = Conv.fromFloat32(x) - Conv.toFloat32(WasmF32.neg(xv)) + let xv = DS.Float32.fromFloat32(x) + DS.Float32.toFloat32(WasmF32.neg(xv)) } /** @@ -439,8 +439,8 @@ provide let neg = (x: Float32) => { */ @unsafe provide let ceil = (x: Float32) => { - let xv = Conv.fromFloat32(x) - Conv.toFloat32(WasmF32.ceil(xv)) + let xv = DS.Float32.fromFloat32(x) + DS.Float32.toFloat32(WasmF32.ceil(xv)) } /** @@ -456,8 +456,8 @@ provide let ceil = (x: Float32) => { */ @unsafe provide let floor = (x: Float32) => { - let xv = Conv.fromFloat32(x) - Conv.toFloat32(WasmF32.floor(xv)) + let xv = DS.Float32.fromFloat32(x) + DS.Float32.toFloat32(WasmF32.floor(xv)) } /** @@ -472,8 +472,8 @@ provide let floor = (x: Float32) => { */ @unsafe provide let trunc = (x: Float32) => { - let xv = Conv.fromFloat32(x) - Conv.toFloat32(WasmF32.trunc(xv)) + let xv = DS.Float32.fromFloat32(x) + DS.Float32.toFloat32(WasmF32.trunc(xv)) } /** @@ -491,8 +491,8 @@ provide let trunc = (x: Float32) => { */ @unsafe provide let round = (x: Float32) => { - let xv = Conv.fromFloat32(x) - Conv.toFloat32(WasmF32.nearest(xv)) + let xv = DS.Float32.fromFloat32(x) + DS.Float32.toFloat32(WasmF32.nearest(xv)) } /** @@ -507,8 +507,8 @@ provide let round = (x: Float32) => { */ @unsafe provide let sqrt = (x: Float32) => { - let xv = Conv.fromFloat32(x) - Conv.toFloat32(WasmF32.sqrt(xv)) + let xv = DS.Float32.fromFloat32(x) + DS.Float32.toFloat32(WasmF32.sqrt(xv)) } /** @@ -526,9 +526,9 @@ provide let sqrt = (x: Float32) => { */ @unsafe provide let copySign = (x: Float32, y: Float32) => { - let xv = Conv.fromFloat32(x) - let yv = Conv.fromFloat32(y) - Conv.toFloat32(WasmF32.copySign(xv, yv)) + let xv = DS.Float32.fromFloat32(x) + let yv = DS.Float32.fromFloat32(y) + DS.Float32.toFloat32(WasmF32.copySign(xv, yv)) } /** @@ -576,9 +576,9 @@ provide let isClose = (a, b, relativeTolerance=1e-9f, absoluteTolerance=0.0f) => @unsafe provide let sin = (radians: Float32) => { // TODO(#2167): Implement Float32 optimized trig functions - let xval = Conv.fromFloat32(radians) + let xval = DS.Float32.fromFloat32(radians) let value = sin(WasmF64.promoteF32(xval)) - Conv.toFloat32(WasmF32.demoteF64(value)) + DS.Float32.toFloat32(WasmF32.demoteF64(value)) } /** @@ -594,9 +594,9 @@ provide let sin = (radians: Float32) => { @unsafe provide let cos = (radians: Float32) => { // TODO(#2167): Implement Float32 optimized trig functions - let xval = Conv.fromFloat32(radians) + let xval = DS.Float32.fromFloat32(radians) let value = cos(WasmF64.promoteF32(xval)) - Conv.toFloat32(WasmF32.demoteF64(value)) + DS.Float32.toFloat32(WasmF32.demoteF64(value)) } /** @@ -612,7 +612,7 @@ provide let cos = (radians: Float32) => { @unsafe provide let tan = (radians: Float32) => { // TODO(#2167): Implement Float32 optimized trig functions - let xval = Conv.fromFloat32(radians) + let xval = DS.Float32.fromFloat32(radians) let value = tan(WasmF64.promoteF32(xval)) - Conv.toFloat32(WasmF32.demoteF64(value)) + DS.Float32.toFloat32(WasmF32.demoteF64(value)) } diff --git a/stdlib/float64.gr b/stdlib/float64.gr index 57386d898e..cc8d8fdb03 100644 --- a/stdlib/float64.gr +++ b/stdlib/float64.gr @@ -15,7 +15,7 @@ module Float64 from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmf64" include WasmF64 use WasmF64.{ (-), (+), (*), (/), (<), (<=), (>), (>=) } -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToFloat64 as fromNumber, @@ -78,9 +78,9 @@ provide { fromNumber, toNumber } */ @unsafe provide let reinterpretInt64 = (value: Int64) => { - let value = Conv.fromInt64(value) + let value = DS.Int64.fromInt64(value) let value = WasmF64.reinterpretI64(value) - Conv.toFloat64(value) + DS.Float64.toFloat64(value) } /** @@ -96,9 +96,9 @@ provide let reinterpretInt64 = (value: Int64) => { */ @unsafe provide let reinterpretUint64 = (value: Uint64) => { - let value = Conv.fromUint64(value) + let value = DS.Uint64.fromUint64(value) let value = WasmF64.reinterpretI64(value) - Conv.toFloat64(value) + DS.Float64.toFloat64(value) } /** @@ -117,9 +117,9 @@ provide let reinterpretUint64 = (value: Uint64) => { */ @unsafe provide let (+) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) - Conv.toFloat64(xv + yv) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) + DS.Float64.toFloat64(xv + yv) } /** @@ -138,9 +138,9 @@ provide let (+) = (x: Float64, y: Float64) => { */ @unsafe provide let (-) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) - Conv.toFloat64(xv - yv) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) + DS.Float64.toFloat64(xv - yv) } /** @@ -159,9 +159,9 @@ provide let (-) = (x: Float64, y: Float64) => { */ @unsafe provide let (*) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) - Conv.toFloat64(xv * yv) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) + DS.Float64.toFloat64(xv * yv) } /** @@ -180,9 +180,9 @@ provide let (*) = (x: Float64, y: Float64) => { */ @unsafe provide let (/) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) - Conv.toFloat64(xv / yv) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) + DS.Float64.toFloat64(xv / yv) } /** @@ -200,9 +200,9 @@ provide let (/) = (x: Float64, y: Float64) => { */ @unsafe provide let (**) = (base: Float64, power: Float64) => { - let basev = Conv.fromFloat64(base) - let powerv = Conv.fromFloat64(power) - Conv.toFloat64(Numbers.powf(basev, powerv)) + let basev = DS.Float64.fromFloat64(base) + let powerv = DS.Float64.fromFloat64(power) + DS.Float64.toFloat64(Numbers.powf(basev, powerv)) } /** @@ -221,8 +221,8 @@ provide let (**) = (base: Float64, power: Float64) => { */ @unsafe provide let (<) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) xv < yv } @@ -242,8 +242,8 @@ provide let (<) = (x: Float64, y: Float64) => { */ @unsafe provide let (>) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) xv > yv } @@ -267,8 +267,8 @@ provide let (>) = (x: Float64, y: Float64) => { */ @unsafe provide let (<=) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) xv <= yv } @@ -292,8 +292,8 @@ provide let (<=) = (x: Float64, y: Float64) => { */ @unsafe provide let (>=) = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) xv >= yv } @@ -365,9 +365,9 @@ provide let isInfinite = (x: Float64) => x == Infinityd || x == -Infinityd */ @unsafe provide let min = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) - Conv.toFloat64(WasmF64.min(xv, yv)) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) + DS.Float64.toFloat64(WasmF64.min(xv, yv)) } /** @@ -383,9 +383,9 @@ provide let min = (x: Float64, y: Float64) => { */ @unsafe provide let max = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) - Conv.toFloat64(WasmF64.max(xv, yv)) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) + DS.Float64.toFloat64(WasmF64.max(xv, yv)) } /** @@ -401,8 +401,8 @@ provide let max = (x: Float64, y: Float64) => { */ @unsafe provide let abs = (x: Float64) => { - let xv = Conv.fromFloat64(x) - Conv.toFloat64(WasmF64.abs(xv)) + let xv = DS.Float64.fromFloat64(x) + DS.Float64.toFloat64(WasmF64.abs(xv)) } /** @@ -418,8 +418,8 @@ provide let abs = (x: Float64) => { */ @unsafe provide let neg = (x: Float64) => { - let xv = Conv.fromFloat64(x) - Conv.toFloat64(WasmF64.neg(xv)) + let xv = DS.Float64.fromFloat64(x) + DS.Float64.toFloat64(WasmF64.neg(xv)) } /** @@ -435,8 +435,8 @@ provide let neg = (x: Float64) => { */ @unsafe provide let ceil = (x: Float64) => { - let xv = Conv.fromFloat64(x) - Conv.toFloat64(WasmF64.ceil(xv)) + let xv = DS.Float64.fromFloat64(x) + DS.Float64.toFloat64(WasmF64.ceil(xv)) } /** @@ -452,8 +452,8 @@ provide let ceil = (x: Float64) => { */ @unsafe provide let floor = (x: Float64) => { - let xv = Conv.fromFloat64(x) - Conv.toFloat64(WasmF64.floor(xv)) + let xv = DS.Float64.fromFloat64(x) + DS.Float64.toFloat64(WasmF64.floor(xv)) } /** @@ -468,8 +468,8 @@ provide let floor = (x: Float64) => { */ @unsafe provide let trunc = (x: Float64) => { - let xv = Conv.fromFloat64(x) - Conv.toFloat64(WasmF64.trunc(xv)) + let xv = DS.Float64.fromFloat64(x) + DS.Float64.toFloat64(WasmF64.trunc(xv)) } /** @@ -487,8 +487,8 @@ provide let trunc = (x: Float64) => { */ @unsafe provide let round = (x: Float64) => { - let xv = Conv.fromFloat64(x) - Conv.toFloat64(WasmF64.nearest(xv)) + let xv = DS.Float64.fromFloat64(x) + DS.Float64.toFloat64(WasmF64.nearest(xv)) } /** @@ -503,8 +503,8 @@ provide let round = (x: Float64) => { */ @unsafe provide let sqrt = (x: Float64) => { - let xv = Conv.fromFloat64(x) - Conv.toFloat64(WasmF64.sqrt(xv)) + let xv = DS.Float64.fromFloat64(x) + DS.Float64.toFloat64(WasmF64.sqrt(xv)) } /** @@ -522,9 +522,9 @@ provide let sqrt = (x: Float64) => { */ @unsafe provide let copySign = (x: Float64, y: Float64) => { - let xv = Conv.fromFloat64(x) - let yv = Conv.fromFloat64(y) - Conv.toFloat64(WasmF64.copySign(xv, yv)) + let xv = DS.Float64.fromFloat64(x) + let yv = DS.Float64.fromFloat64(y) + DS.Float64.toFloat64(WasmF64.copySign(xv, yv)) } /** @@ -571,9 +571,9 @@ provide let isClose = (a, b, relativeTolerance=1e-9d, absoluteTolerance=0.0d) => */ @unsafe provide let sin = (radians: Float64) => { - let xval = Conv.fromFloat64(radians) + let xval = DS.Float64.fromFloat64(radians) let value = sin(xval) - Conv.toFloat64(value) + DS.Float64.toFloat64(value) } /** @@ -588,9 +588,9 @@ provide let sin = (radians: Float64) => { */ @unsafe provide let cos = (radians: Float64) => { - let xval = Conv.fromFloat64(radians) + let xval = DS.Float64.fromFloat64(radians) let value = cos(xval) - Conv.toFloat64(value) + DS.Float64.toFloat64(value) } /** @@ -605,7 +605,7 @@ provide let cos = (radians: Float64) => { */ @unsafe provide let tan = (radians: Float64) => { - let xval = Conv.fromFloat64(radians) + let xval = DS.Float64.fromFloat64(radians) let value = tan(xval) - Conv.toFloat64(value) + DS.Float64.toFloat64(value) } diff --git a/stdlib/hash.gr b/stdlib/hash.gr index 019621c47f..a9d31ebefc 100644 --- a/stdlib/hash.gr +++ b/stdlib/hash.gr @@ -38,15 +38,7 @@ use WasmI32.{ } from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/tags" include Tags -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - tagSimpleNumber, - untagSimpleNumber, - getStringSize, - getStringArrayRef, - getBytesSize, - getBytesArrayRef, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToWasmI32 } from "runtime/bigint" include Bigint as BI @@ -119,11 +111,12 @@ let rec hashOne = (h, val: WasmRef, depth) => { hash32(h, WasmRef.i31GetU(val)) } else { // Handle heap values - let heapTag = DataStructures.loadValueTag(val) + let heapTag = DS.getHeapTag(val) match (heapTag) { t when t == Tags._GRAIN_STRING_HEAP_TAG => { - let length = getStringSize(val) - let data = getStringArrayRef(val) + let val = DS.String.fromGrain(WasmRef.toGrain(val): String) + let length = DS.String.getSize(val) + let data = DS.String.getData(val) let extra = length % 4n let l = length - extra let mut h = h @@ -144,8 +137,9 @@ let rec hashOne = (h, val: WasmRef, depth) => { finalize(h, length) }, t when t == Tags._GRAIN_BYTES_HEAP_TAG => { - let length = getBytesSize(val) - let data = getBytesArrayRef(val) + let val = DS.Bytes.fromGrain(WasmRef.toGrain(val): Bytes) + let length = DS.Bytes.getSize(val) + let data = DS.Bytes.getData(val) let extra = length % 4n let l = length - extra let mut h = h @@ -167,19 +161,13 @@ let rec hashOne = (h, val: WasmRef, depth) => { }, t when t == Tags._GRAIN_ADT_HEAP_TAG => { // moduleId - let h = hash32( - h, - untagSimpleNumber(DataStructures.loadVariantTypeHash(val)) - ) + let h = hash32(h, DS.untagSimpleNumber(DS.ADT.getTypeHash(val))) // typeId - let h = hash32( - h, - untagSimpleNumber(DataStructures.loadVariantTypeId(val)) - ) + let h = hash32(h, DS.untagSimpleNumber(DS.ADT.getTypeId(val))) // variantId - let h = hash32(h, untagSimpleNumber(DataStructures.loadAdtVariant(val))) + let h = hash32(h, DS.untagSimpleNumber(DS.ADT.getVariantId(val))) - let data = DataStructures.getCompoundValueArrayRef(val) + let data = DS.ADT.getData(val) let arity = WasmArrayRef.length(data) let mut h = h @@ -191,17 +179,11 @@ let rec hashOne = (h, val: WasmRef, depth) => { }, t when t == Tags._GRAIN_RECORD_HEAP_TAG => { // moduleId - let h = hash32( - h, - untagSimpleNumber(DataStructures.loadRecordTypeHash(val)) - ) + let h = hash32(h, DS.untagSimpleNumber(DS.Record.getTypeHash(val))) // typeId - let h = hash32( - h, - untagSimpleNumber(DataStructures.loadRecordTypeId(val)) - ) + let h = hash32(h, DS.untagSimpleNumber(DS.Record.getTypeId(val))) - let data = DataStructures.getCompoundValueArrayRef(val) + let data = DS.Record.getData(val) let arity = WasmArrayRef.length(data) let mut h = h @@ -211,7 +193,8 @@ let rec hashOne = (h, val: WasmRef, depth) => { finalize(h, arity) }, t when t == Tags._GRAIN_ARRAY_HEAP_TAG => { - let data = DataStructures.getCompoundValueArrayRef(val) + let val = DS.Array.fromGrain(WasmRef.toGrain(val): Array) + let data = DS.Array.getData(val) let arity = WasmArrayRef.length(data) let mut h = h @@ -221,7 +204,8 @@ let rec hashOne = (h, val: WasmRef, depth) => { finalize(h, arity) }, t when t == Tags._GRAIN_TUPLE_HEAP_TAG => { - let data = DataStructures.getCompoundValueArrayRef(val) + let val = DS.Tuple.fromGrain(WasmRef.toGrain(val)) + let data = DS.Tuple.getData(val) let arity = WasmArrayRef.length(data) let mut h = h for (let mut i = 0n; i < arity; i += 1n) { @@ -230,7 +214,7 @@ let rec hashOne = (h, val: WasmRef, depth) => { finalize(h, arity) }, t when t == Tags._GRAIN_LAMBDA_HEAP_TAG => { - let data = DataStructures.getCompoundValueArrayRef(val) + let data = DS.Closure.getData(val) let arity = WasmArrayRef.length(data) let mut h = h for (let mut i = 0n; i < arity; i += 1n) { @@ -239,10 +223,10 @@ let rec hashOne = (h, val: WasmRef, depth) => { finalize(h, arity) }, t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => { - let tag = DataStructures.getNumberTag(val) + let tag = DS.getBoxedNumberTag(val) match (tag) { t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG => { - hash64(h, DataStructures.getInt64Value(val)) + hash64(h, DS.Int64.getValue(val)) }, t when t == Tags._GRAIN_BIGINT_BOXED_NUM_TAG => { // TODO(#1187): should include fixint size once implemented @@ -255,22 +239,11 @@ let rec hashOne = (h, val: WasmRef, depth) => { h }, t when t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => { - hash64( - h, - WasmI64.reinterpretF64(DataStructures.getFloat64Value(val)) - ) + hash64(h, WasmI64.reinterpretF64(DS.Float64.getValue(val))) }, t when t == Tags._GRAIN_RATIONAL_BOXED_NUM_TAG => { - let h = hashOne( - h, - DataStructures.getRationalNumerator(val), - depth + 1n - ) - let h = hashOne( - h, - DataStructures.getRationalDenominator(val), - depth + 1n - ) + let h = hashOne(h, DS.Rational.getNumerator(val), depth + 1n) + let h = hashOne(h, DS.Rational.getDenominator(val), depth + 1n) h }, _ => { @@ -281,16 +254,16 @@ let rec hashOne = (h, val: WasmRef, depth) => { } }, t when t == Tags._GRAIN_INT32_HEAP_TAG => { - hash32(h, DataStructures.getInt32Value(val)) + hash32(h, DS.Int32.getValue(val)) }, t when t == Tags._GRAIN_FLOAT32_HEAP_TAG => { - hash32(h, WasmI32.reinterpretF32(DataStructures.getFloat32Value(val))) + hash32(h, WasmI32.reinterpretF32(DS.Float32.getValue(val))) }, t when t == Tags._GRAIN_UINT32_HEAP_TAG => { - hash32(h, DataStructures.getUint32Value(val)) + hash32(h, DS.Uint32.getValue(val)) }, t when t == Tags._GRAIN_UINT64_HEAP_TAG => { - hash64(h, DataStructures.getUint64Value(val)) + hash64(h, DS.Uint64.getValue(val)) }, _ => { // TODO(#1192): Just fail directly once typechecking is fixed @@ -311,7 +284,7 @@ abstract type HashInstance = Number let mut seed: HashInstance = 0 @unsafe let getSeed = () => { - if (WasmI32.eqz(untagSimpleNumber(seed))) { + if (WasmI32.eqz(DS.untagSimpleNumber(seed))) { // Delay initialization to the first call to `hash` to prevent WASI calls // during startup let random = Random.random() @@ -383,5 +356,5 @@ provide let hash = (hashInstance: HashInstance, anything) => { // Tag the number on the way out. // Since Grain has proper modulus, negative numbers are okay. - tagSimpleNumber(h) + DS.tagSimpleNumber(h) } diff --git a/stdlib/int16.gr b/stdlib/int16.gr index 9768ac18bb..5e6e7abb7d 100644 --- a/stdlib/int16.gr +++ b/stdlib/int16.gr @@ -33,7 +33,7 @@ use Numbers.{ coerceNumberToInt16 as fromNumber, coerceInt16ToNumber as toNumber, } -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures use DataStructures.{ untagUint16, tagInt16, untagInt16 } provide { fromNumber, toNumber } diff --git a/stdlib/int32.gr b/stdlib/int32.gr index bc5b76bc0b..8498954102 100644 --- a/stdlib/int32.gr +++ b/stdlib/int32.gr @@ -28,8 +28,8 @@ use WasmI32.{ (>), (>=), } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/exception" include Exception -from "runtime/unsafe/conv" include Conv from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToInt32 as fromNumber, @@ -50,8 +50,8 @@ provide { fromNumber, toNumber } */ @unsafe provide let fromUint32 = (number: Uint32) => { - let value = Conv.fromUint32(number) - Conv.toInt32(value) + let value = DS.Uint32.fromUint32(number) + DS.Int32.toInt32(value) } /** @@ -67,9 +67,9 @@ provide let fromUint32 = (number: Uint32) => { */ @unsafe provide let reinterpretFloat32 = (value: Float32) => { - let value = Conv.fromFloat32(value) + let value = DS.Float32.fromFloat32(value) let value = WasmI32.reinterpretF32(value) - Conv.toInt32(value) + DS.Int32.toInt32(value) } /** @@ -85,8 +85,8 @@ provide let reinterpretFloat32 = (value: Float32) => { */ @unsafe provide let incr = (value: Int32) => { - let value = Conv.fromInt32(value) - Conv.toInt32(value + 1n) + let value = DS.Int32.fromInt32(value) + DS.Int32.toInt32(value + 1n) } /** @@ -102,8 +102,8 @@ provide let incr = (value: Int32) => { */ @unsafe provide let decr = (value: Int32) => { - let value = Conv.fromInt32(value) - Conv.toInt32(value - 1n) + let value = DS.Int32.fromInt32(value) + DS.Int32.toInt32(value - 1n) } /** @@ -122,9 +122,9 @@ provide let decr = (value: Int32) => { */ @unsafe provide let (+) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(xv + yv) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(xv + yv) } /** @@ -143,9 +143,9 @@ provide let (+) = (x: Int32, y: Int32) => { */ @unsafe provide let (-) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(xv - yv) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(xv - yv) } /** @@ -164,9 +164,9 @@ provide let (-) = (x: Int32, y: Int32) => { */ @unsafe provide let (*) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(xv * yv) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(xv * yv) } /** @@ -185,9 +185,9 @@ provide let (*) = (x: Int32, y: Int32) => { */ @unsafe provide let (/) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(xv / yv) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(xv / yv) } /** @@ -203,9 +203,9 @@ provide let (/) = (x: Int32, y: Int32) => { */ @unsafe provide let rem = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(WasmI32.remS(xv, yv)) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(WasmI32.remS(xv, yv)) } @unsafe @@ -235,8 +235,8 @@ let abs = n => { @unsafe provide let (%) = (x: Int32, y: Int32) => { use WasmI32.{ (-) } - let xval = Conv.fromInt32(x) - let yval = Conv.fromInt32(y) + let xval = DS.Int32.fromInt32(x) + let yval = DS.Int32.fromInt32(y) if (WasmI32.eqz(yval)) { throw Exception.ModuloByZero @@ -247,9 +247,11 @@ provide let (%) = (x: Int32, y: Int32) => { let yabs = abs(yval) let mval = WasmI32.remS(xabs, yabs) let mres = yabs - mval - Conv.toInt32(if (mval != 0n) (if (yval < 0n) 0n - mres else mres) else 0n) + DS.Int32.toInt32( + if (mval != 0n) (if (yval < 0n) 0n - mres else mres) else 0n + ) } else { - Conv.toInt32(WasmI32.remS(xval, yval)) + DS.Int32.toInt32(WasmI32.remS(xval, yval)) } } @@ -267,9 +269,9 @@ provide let (%) = (x: Int32, y: Int32) => { */ @unsafe provide let rotl = (value: Int32, amount: Int32) => { - let value = Conv.fromInt32(value) - let amount = Conv.fromInt32(amount) - Conv.toInt32(WasmI32.rotl(value, amount)) + let value = DS.Int32.fromInt32(value) + let amount = DS.Int32.fromInt32(amount) + DS.Int32.toInt32(WasmI32.rotl(value, amount)) } /** @@ -286,9 +288,9 @@ provide let rotl = (value: Int32, amount: Int32) => { */ @unsafe provide let rotr = (value: Int32, amount: Int32) => { - let value = Conv.fromInt32(value) - let amount = Conv.fromInt32(amount) - Conv.toInt32(WasmI32.rotr(value, amount)) + let value = DS.Int32.fromInt32(value) + let amount = DS.Int32.fromInt32(amount) + DS.Int32.toInt32(WasmI32.rotr(value, amount)) } /** @@ -307,9 +309,9 @@ provide let rotr = (value: Int32, amount: Int32) => { */ @unsafe provide let (<<) = (value: Int32, amount: Int32) => { - let value = Conv.fromInt32(value) - let amount = Conv.fromInt32(amount) - Conv.toInt32(value << amount) + let value = DS.Int32.fromInt32(value) + let amount = DS.Int32.fromInt32(amount) + DS.Int32.toInt32(value << amount) } /** @@ -328,9 +330,9 @@ provide let (<<) = (value: Int32, amount: Int32) => { */ @unsafe provide let (>>) = (value: Int32, amount: Int32) => { - let value = Conv.fromInt32(value) - let amount = Conv.fromInt32(amount) - Conv.toInt32(value >> amount) + let value = DS.Int32.fromInt32(value) + let amount = DS.Int32.fromInt32(amount) + DS.Int32.toInt32(value >> amount) } /** @@ -349,8 +351,8 @@ provide let (>>) = (value: Int32, amount: Int32) => { */ @unsafe provide let (==) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) xv == yv } @@ -370,8 +372,8 @@ provide let (==) = (x: Int32, y: Int32) => { */ @unsafe provide let (!=) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) xv != yv } @@ -388,7 +390,7 @@ provide let (!=) = (x: Int32, y: Int32) => { */ @unsafe provide let eqz = (value: Int32) => { - let value = Conv.fromInt32(value) + let value = DS.Int32.fromInt32(value) WasmI32.eqz(value) } @@ -408,8 +410,8 @@ provide let eqz = (value: Int32) => { */ @unsafe provide let (<) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) xv < yv } @@ -429,8 +431,8 @@ provide let (<) = (x: Int32, y: Int32) => { */ @unsafe provide let (>) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) xv > yv } @@ -453,8 +455,8 @@ provide let (>) = (x: Int32, y: Int32) => { */ @unsafe provide let (<=) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) xv <= yv } @@ -477,8 +479,8 @@ provide let (<=) = (x: Int32, y: Int32) => { */ @unsafe provide let (>=) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) xv >= yv } @@ -494,8 +496,8 @@ provide let (>=) = (x: Int32, y: Int32) => { */ @unsafe provide let lnot = (value: Int32) => { - let value = Conv.fromInt32(value) - Conv.toInt32(value ^ 0xffffffffn) + let value = DS.Int32.fromInt32(value) + DS.Int32.toInt32(value ^ 0xffffffffn) } /** @@ -514,9 +516,9 @@ provide let lnot = (value: Int32) => { */ @unsafe provide let (&) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(xv & yv) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(xv & yv) } /** @@ -535,9 +537,9 @@ provide let (&) = (x: Int32, y: Int32) => { */ @unsafe provide let (|) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(xv | yv) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(xv | yv) } /** @@ -556,9 +558,9 @@ provide let (|) = (x: Int32, y: Int32) => { */ @unsafe provide let (^) = (x: Int32, y: Int32) => { - let xv = Conv.fromInt32(x) - let yv = Conv.fromInt32(y) - Conv.toInt32(xv ^ yv) + let xv = DS.Int32.fromInt32(x) + let yv = DS.Int32.fromInt32(y) + DS.Int32.toInt32(xv ^ yv) } /** @@ -574,8 +576,8 @@ provide let (^) = (x: Int32, y: Int32) => { */ @unsafe provide let clz = (value: Int32) => { - let value = Conv.fromInt32(value) - Conv.toInt32(WasmI32.clz(value)) + let value = DS.Int32.fromInt32(value) + DS.Int32.toInt32(WasmI32.clz(value)) } /** @@ -591,8 +593,8 @@ provide let clz = (value: Int32) => { */ @unsafe provide let ctz = (value: Int32) => { - let value = Conv.fromInt32(value) - Conv.toInt32(WasmI32.ctz(value)) + let value = DS.Int32.fromInt32(value) + DS.Int32.toInt32(WasmI32.ctz(value)) } /** @@ -608,8 +610,8 @@ provide let ctz = (value: Int32) => { */ @unsafe provide let popcnt = (value: Int32) => { - let value = Conv.fromInt32(value) - Conv.toInt32(WasmI32.popcnt(value)) + let value = DS.Int32.fromInt32(value) + DS.Int32.toInt32(WasmI32.popcnt(value)) } // Exponentiation by squaring https://en.wikipedia.org/wiki/Exponentiation_by_squaring special path for int^int diff --git a/stdlib/int64.gr b/stdlib/int64.gr index 6123780553..6e8d36f08d 100644 --- a/stdlib/int64.gr +++ b/stdlib/int64.gr @@ -28,8 +28,8 @@ use WasmI64.{ (>), (>=), } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/exception" include Exception -from "runtime/unsafe/conv" include Conv from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToInt64 as fromNumber, @@ -50,8 +50,8 @@ provide { fromNumber, toNumber } */ @unsafe provide let fromUint64 = (number: Uint64) => { - let value = Conv.fromUint64(number) - Conv.toInt64(value) + let value = DS.Uint64.fromUint64(number) + DS.Int64.toInt64(value) } /** @@ -67,9 +67,9 @@ provide let fromUint64 = (number: Uint64) => { */ @unsafe provide let reinterpretFloat64 = (value: Float64) => { - let value = Conv.fromFloat64(value) + let value = DS.Float64.fromFloat64(value) let value = WasmI64.reinterpretF64(value) - Conv.toInt64(value) + DS.Int64.toInt64(value) } /** @@ -85,8 +85,8 @@ provide let reinterpretFloat64 = (value: Float64) => { */ @unsafe provide let incr = (value: Int64) => { - let value = Conv.fromInt64(value) - Conv.toInt64(value + 1N) + let value = DS.Int64.fromInt64(value) + DS.Int64.toInt64(value + 1N) } /** @@ -102,8 +102,8 @@ provide let incr = (value: Int64) => { */ @unsafe provide let decr = (value: Int64) => { - let value = Conv.fromInt64(value) - Conv.toInt64(value - 1N) + let value = DS.Int64.fromInt64(value) + DS.Int64.toInt64(value - 1N) } /** @@ -122,9 +122,9 @@ provide let decr = (value: Int64) => { */ @unsafe provide let (+) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(xv + yv) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(xv + yv) } /** @@ -143,9 +143,9 @@ provide let (+) = (x: Int64, y: Int64) => { */ @unsafe provide let (-) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(xv - yv) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(xv - yv) } /** @@ -164,9 +164,9 @@ provide let (-) = (x: Int64, y: Int64) => { */ @unsafe provide let (*) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(xv * yv) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(xv * yv) } /** @@ -185,9 +185,9 @@ provide let (*) = (x: Int64, y: Int64) => { */ @unsafe provide let (/) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(xv / yv) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(xv / yv) } /** @@ -203,9 +203,9 @@ provide let (/) = (x: Int64, y: Int64) => { */ @unsafe provide let rem = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(WasmI64.remS(xv, yv)) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(WasmI64.remS(xv, yv)) } @unsafe @@ -235,8 +235,8 @@ let abs = n => { @unsafe provide let (%) = (x: Int64, y: Int64) => { use WasmI64.{ (-) } - let xval = Conv.fromInt64(x) - let yval = Conv.fromInt64(y) + let xval = DS.Int64.fromInt64(x) + let yval = DS.Int64.fromInt64(y) if (WasmI64.eqz(yval)) { throw Exception.ModuloByZero @@ -247,9 +247,11 @@ provide let (%) = (x: Int64, y: Int64) => { let yabs = abs(yval) let mval = WasmI64.remS(xabs, yabs) let mres = yabs - mval - Conv.toInt64(if (mval != 0N) (if (yval < 0N) 0N - mres else mres) else 0N) + DS.Int64.toInt64( + if (mval != 0N) (if (yval < 0N) 0N - mres else mres) else 0N + ) } else { - Conv.toInt64(WasmI64.remS(xval, yval)) + DS.Int64.toInt64(WasmI64.remS(xval, yval)) } } @@ -267,9 +269,9 @@ provide let (%) = (x: Int64, y: Int64) => { */ @unsafe provide let rotl = (value: Int64, amount: Int64) => { - let value = Conv.fromInt64(value) - let amount = Conv.fromInt64(amount) - Conv.toInt64(WasmI64.rotl(value, amount)) + let value = DS.Int64.fromInt64(value) + let amount = DS.Int64.fromInt64(amount) + DS.Int64.toInt64(WasmI64.rotl(value, amount)) } /** @@ -286,9 +288,9 @@ provide let rotl = (value: Int64, amount: Int64) => { */ @unsafe provide let rotr = (value: Int64, amount: Int64) => { - let value = Conv.fromInt64(value) - let amount = Conv.fromInt64(amount) - Conv.toInt64(WasmI64.rotr(value, amount)) + let value = DS.Int64.fromInt64(value) + let amount = DS.Int64.fromInt64(amount) + DS.Int64.toInt64(WasmI64.rotr(value, amount)) } /** @@ -307,9 +309,9 @@ provide let rotr = (value: Int64, amount: Int64) => { */ @unsafe provide let (<<) = (value: Int64, amount: Int64) => { - let value = Conv.fromInt64(value) - let amount = Conv.fromInt64(amount) - Conv.toInt64(value << amount) + let value = DS.Int64.fromInt64(value) + let amount = DS.Int64.fromInt64(amount) + DS.Int64.toInt64(value << amount) } /** @@ -328,9 +330,9 @@ provide let (<<) = (value: Int64, amount: Int64) => { */ @unsafe provide let (>>) = (value: Int64, amount: Int64) => { - let value = Conv.fromInt64(value) - let amount = Conv.fromInt64(amount) - Conv.toInt64(value >> amount) + let value = DS.Int64.fromInt64(value) + let amount = DS.Int64.fromInt64(amount) + DS.Int64.toInt64(value >> amount) } /** @@ -349,8 +351,8 @@ provide let (>>) = (value: Int64, amount: Int64) => { */ @unsafe provide let (==) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) xv == yv } @@ -370,8 +372,8 @@ provide let (==) = (x: Int64, y: Int64) => { */ @unsafe provide let (!=) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) xv != yv } @@ -388,7 +390,7 @@ provide let (!=) = (x: Int64, y: Int64) => { */ @unsafe provide let eqz = (value: Int64) => { - let value = Conv.fromInt64(value) + let value = DS.Int64.fromInt64(value) WasmI64.eqz(value) } @@ -408,8 +410,8 @@ provide let eqz = (value: Int64) => { */ @unsafe provide let (<) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) xv < yv } @@ -429,8 +431,8 @@ provide let (<) = (x: Int64, y: Int64) => { */ @unsafe provide let (>) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) xv > yv } @@ -453,8 +455,8 @@ provide let (>) = (x: Int64, y: Int64) => { */ @unsafe provide let (<=) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) xv <= yv } @@ -477,8 +479,8 @@ provide let (<=) = (x: Int64, y: Int64) => { */ @unsafe provide let (>=) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) xv >= yv } @@ -494,8 +496,8 @@ provide let (>=) = (x: Int64, y: Int64) => { */ @unsafe provide let lnot = (value: Int64) => { - let value = Conv.fromInt64(value) - Conv.toInt64(value ^ 0xffffffffffffffffN) + let value = DS.Int64.fromInt64(value) + DS.Int64.toInt64(value ^ 0xffffffffffffffffN) } /** @@ -514,9 +516,9 @@ provide let lnot = (value: Int64) => { */ @unsafe provide let (&) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(xv & yv) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(xv & yv) } /** @@ -535,9 +537,9 @@ provide let (&) = (x: Int64, y: Int64) => { */ @unsafe provide let (|) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(xv | yv) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(xv | yv) } /** @@ -556,9 +558,9 @@ provide let (|) = (x: Int64, y: Int64) => { */ @unsafe provide let (^) = (x: Int64, y: Int64) => { - let xv = Conv.fromInt64(x) - let yv = Conv.fromInt64(y) - Conv.toInt64(xv ^ yv) + let xv = DS.Int64.fromInt64(x) + let yv = DS.Int64.fromInt64(y) + DS.Int64.toInt64(xv ^ yv) } /** @@ -574,8 +576,8 @@ provide let (^) = (x: Int64, y: Int64) => { */ @unsafe provide let clz = (value: Int64) => { - let value = Conv.fromInt64(value) - Conv.toInt64(WasmI64.clz(value)) + let value = DS.Int64.fromInt64(value) + DS.Int64.toInt64(WasmI64.clz(value)) } /** @@ -591,8 +593,8 @@ provide let clz = (value: Int64) => { */ @unsafe provide let ctz = (value: Int64) => { - let value = Conv.fromInt64(value) - Conv.toInt64(WasmI64.ctz(value)) + let value = DS.Int64.fromInt64(value) + DS.Int64.toInt64(WasmI64.ctz(value)) } /** @@ -608,8 +610,8 @@ provide let ctz = (value: Int64) => { */ @unsafe provide let popcnt = (value: Int64) => { - let value = Conv.fromInt64(value) - Conv.toInt64(WasmI64.popcnt(value)) + let value = DS.Int64.fromInt64(value) + DS.Int64.toInt64(WasmI64.popcnt(value)) } // Exponentiation by squaring https://en.wikipedia.org/wiki/Exponentiation_by_squaring special path for int^int diff --git a/stdlib/int8.gr b/stdlib/int8.gr index 9291c9014e..0f3dea0da8 100644 --- a/stdlib/int8.gr +++ b/stdlib/int8.gr @@ -32,7 +32,7 @@ use WasmI32.{ from "runtime/exception" include Exception from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToInt8 as fromNumber, coerceInt8ToNumber as toNumber } -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures use DataStructures.{ untagUint8, tagInt8, untagInt8 } provide { fromNumber, toNumber } diff --git a/stdlib/json.gr b/stdlib/json.gr index 03edcc3a83..1ee482d212 100644 --- a/stdlib/json.gr +++ b/stdlib/json.gr @@ -14,7 +14,7 @@ module Json from "runtime/bigint" include Bigint as BI -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers from "runtime/numberUtils" include NumberUtils from "runtime/utf8" include Utf8 @@ -33,7 +33,6 @@ from "list" include List from "uint8" include Uint8 use RuntimeString.{ toString as runtimeToString } use Numbers.{ coerceNumberToWasmI32 } -use DataStructures.{ tagSimpleNumber, untagSimpleNumber } // Primitive offsets // TODO(#703): Get these offsets from the runtime @@ -609,17 +608,17 @@ let printNumber = (value: Number, buffer: Buffer.Buffer) => { let ref = WasmRef.fromGrain(value) if (WasmRef.isRefI31(ref)) { - printNumberWasmI32(untagSimpleNumber(value), buffer) + printNumberWasmI32(DS.untagSimpleNumber(value), buffer) None } else { - let tag = DataStructures.loadValueTag(ref) + let tag = DS.getHeapTag(ref) if (tag != Tags._GRAIN_BOXED_NUM_HEAP_TAG) { fail "Impossible: Json.toString encountered a non-number value in a printNumber" } - let numberTag = DataStructures.getNumberTag(ref) + let numberTag = DS.getBoxedNumberTag(ref) match (numberTag) { t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG => { - let asWasmI64 = DataStructures.getInt64Value(ref) + let asWasmI64 = DS.Int64.getValue(ref) printNumberWasmI64(asWasmI64, buffer) None }, @@ -633,11 +632,11 @@ let printNumber = (value: Number, buffer: Buffer.Buffer) => { // this means there is a slight loss in precision let asFloat64 = Numbers.coerceNumberToFloat64(value) let ref = WasmRef.fromGrain(asFloat64) - let asWasmF64 = DataStructures.getFloat64Value(ref) + let asWasmF64 = DS.Float64.getValue(ref) printNumberWasmF64(asWasmF64, buffer) }, t when t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => { - let asWasmF64 = DataStructures.getFloat64Value(ref) + let asWasmF64 = DS.Float64.getValue(ref) printNumberWasmF64(asWasmF64, buffer) }, _ => { @@ -1305,16 +1304,16 @@ let _END_OF_INPUT = -1 let rec readCodePoint = (bytePosition: Number, string: String) => { use WasmI32.{ (<) } - let strRef = WasmRef.fromGrain(string) - let byteSize = DataStructures.getStringSize(strRef) - let strData = DataStructures.getStringArrayRef(strRef) + let strRef = DS.String.fromGrain(string) + let byteSize = DS.String.getSize(strRef) + let strData = DS.String.getData(strRef) let bytePositionW32 = coerceNumberToWasmI32(bytePosition) if (bytePositionW32 < byteSize) { let codePoint = getCodePoint(strData, bytePositionW32) - tagSimpleNumber(codePoint) + DS.tagSimpleNumber(codePoint) } else { _END_OF_INPUT } diff --git a/stdlib/list.gr b/stdlib/list.gr index 7ecf12b8e1..6315a56968 100644 --- a/stdlib/list.gr +++ b/stdlib/list.gr @@ -12,7 +12,7 @@ module List from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures as DS /** * Creates a new list with all elements in reverse order. @@ -927,12 +927,11 @@ provide let sub = (start, length, list) => { @unsafe let rec computeJoinSize = (sepSize: WasmI32, size: WasmI32, lst: List) => { use WasmI32.{ (+) } - use DataStructures.{ getStringSize } match (lst) { [] => size, - [hd] => size + getStringSize(WasmRef.fromGrain(hd)), + [hd] => size + DS.String.getSize(DS.String.fromGrain(hd)), [hd, ...tl] => { - let size = size + getStringSize(WasmRef.fromGrain(hd)) + sepSize + let size = size + DS.String.getSize(DS.String.fromGrain(hd)) + sepSize computeJoinSize(sepSize, size, tl) }, } @@ -946,30 +945,17 @@ let rec buildJoinedString = ( lst: List, ) => { use WasmI32.{ (+) } - use DataStructures.{ getStringSize } match (lst) { [] => void, [hd] => { - let ref = WasmRef.fromGrain(hd) - let size = getStringSize(ref) - WasmArrayRef.copyI8( - strRef, - offset, - DataStructures.getStringArrayRef(ref), - 0n, - size - ) + let ref = DS.String.fromGrain(hd) + let size = DS.String.getSize(ref) + WasmArrayRef.copyI8(strRef, offset, DS.String.getData(ref), 0n, size) }, [hd, ...tl] => { - let ref = WasmRef.fromGrain(hd) - let size = getStringSize(ref) - WasmArrayRef.copyI8( - strRef, - offset, - DataStructures.getStringArrayRef(ref), - 0n, - size - ) + let ref = DS.String.fromGrain(hd) + let size = DS.String.getSize(ref) + WasmArrayRef.copyI8(strRef, offset, DS.String.getData(ref), 0n, size) let offset = offset + size WasmArrayRef.copyI8(strRef, offset, sepRef, 0n, sepSize) buildJoinedString(strRef, offset + sepSize, sepRef, sepSize, tl) @@ -989,19 +975,18 @@ let rec buildJoinedString = ( @unsafe provide let join = (separator: String, list: List) => { use WasmI32.{ (+), (-), (<=) } - use DataStructures.{ allocateString, getStringSize } - let sepRef = WasmRef.fromGrain(separator) - let sepSize = getStringSize(sepRef) + let sepRef = DS.String.fromGrain(separator) + let sepSize = DS.String.getSize(sepRef) let strSize = computeJoinSize(sepSize, 0n, list) - let newString = allocateString(strSize) + let newString = DS.String.make(strSize) buildJoinedString( - DataStructures.getStringArrayRef(newString), + DS.String.getData(newString), 0n, - DataStructures.getStringArrayRef(sepRef), + DS.String.getData(sepRef), sepSize, list ) - return WasmRef.toGrain(newString): String + return DS.String.toGrain(newString) } /** diff --git a/stdlib/map.gr b/stdlib/map.gr index 578c986ac5..19eda0912e 100644 --- a/stdlib/map.gr +++ b/stdlib/map.gr @@ -9,8 +9,7 @@ module Map from "runtime/unsafe/wasmref" include WasmRef -from "runtime/dataStructures" include DataStructures -use DataStructures.{ allocateArray, untagSimpleNumber } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "list" include List from "array" include Array from "option" include Option @@ -435,8 +434,8 @@ provide let fromList = list => { */ @unsafe provide let toArray = (map: Map) => { - let length = untagSimpleNumber(map.size) - let arr = WasmRef.toGrain(allocateArray(length, WasmRef.fromGrain(void))): + let length = DS.untagSimpleNumber(map.size) + let arr = DS.Array.toGrain(DS.Array.make(length, WasmRef.fromGrain(void))): Array<(a, b)> @unsafe let reducer = (i, key, value) => { diff --git a/stdlib/marshal.gr b/stdlib/marshal.gr index 48f2cc66fb..8fb231fc14 100644 --- a/stdlib/marshal.gr +++ b/stdlib/marshal.gr @@ -45,28 +45,8 @@ from "runtime/unsafe/wasmf64" include WasmF64 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/tags" include Tags -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - allocateBytes, - allocateArray, - allocateTuple, - allocateString, - allocateAdt, - allocateRecord, - allocateBigInt, - newInt32, - newUint32, - newFloat32, - newInt64, - newUint64, - newFloat64, - newRational, - setBigIntFlags, - tagSimpleNumber, - untagSimpleNumber, - loadCycleMarker, - storeCycleMarker, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS +from "runtime/numberUtils" include NumberUtils primitive (!) = "@not" primitive (&&) = "@and" @@ -125,17 +105,17 @@ let vecData = (vec: Vec) => { @unsafe let vecPush = (vec: Vec, val: WasmRef) => { - let size = untagSimpleNumber(vec.size) - let capacity = untagSimpleNumber(vec.capacity) + let size = DS.untagSimpleNumber(vec.size) + let capacity = DS.untagSimpleNumber(vec.capacity) if (size == capacity) { let newCapacity = capacity * 2n let newArray = WasmArrayRef.makeAny(newCapacity, WasmRef.fromGrain(void)) WasmArrayRef.copyAny(newArray, 0n, vecData(vec), 0n, capacity) vec.data = WasmRef.toGrain(WasmRef.fromGrain(newArray)): a - vec.capacity = tagSimpleNumber(newCapacity) + vec.capacity = DS.tagSimpleNumber(newCapacity) } WasmArrayRef.setAny(vecData(vec), size, val) - vec.size = tagSimpleNumber(size + 1n) + vec.size = DS.tagSimpleNumber(size + 1n) } @unsafe @@ -145,7 +125,7 @@ let vecGet = (vec: Vec, idx: WasmI32) => { @unsafe let vecFindRef = (vec: Vec, val: WasmRef) => { - let len = untagSimpleNumber(vec.size) + let len = DS.untagSimpleNumber(vec.size) for (let mut i = 0n; i < len; i += 1n) { if (WasmArrayRef.getAny(vecData(vec), i) is val) { return i @@ -156,7 +136,7 @@ let vecFindRef = (vec: Vec, val: WasmRef) => { @unsafe let vecFindI32 = (vec: Vec, val: WasmI32) => { - let len = untagSimpleNumber(vec.size) + let len = DS.untagSimpleNumber(vec.size) for (let mut i = 0n; i < len; i += 1n) { if (WasmRef.i31GetS(WasmArrayRef.getAny(vecData(vec), i)) == val) { return i @@ -216,23 +196,31 @@ let rec computeSize = (ref, currentOffset) => { if (WasmRef.isRefI31(ref)) { 5n // 1 tag + 4 value } else { - let tag = DataStructures.loadValueTag(ref) + let tag = DS.getHeapTag(ref) match (tag) { t when t == Tags._GRAIN_STRING_HEAP_TAG => { - 5n + DataStructures.getStringSize(ref) + 5n + DS.String.getSize(DS.String.fromGrain(WasmRef.toGrain(ref))) }, t when t == Tags._GRAIN_BYTES_HEAP_TAG => { - 5n + DataStructures.getBytesSize(ref) + 5n + DS.Bytes.getSize(DS.Bytes.fromGrain(WasmRef.toGrain(ref))) }, t when t == Tags._GRAIN_ADT_HEAP_TAG || t == Tags._GRAIN_RECORD_HEAP_TAG || t == Tags._GRAIN_ARRAY_HEAP_TAG || t == Tags._GRAIN_TUPLE_HEAP_TAG => { - if (loadCycleMarker(ref) != 0n) { + if (DS.loadCycleMarker(ref) != 0n) { 5n } else { - storeCycleMarker(ref, 1n) - let arr = DataStructures.getCompoundValueArrayRef(ref) + DS.storeCycleMarker(ref, 1n) + let arr = if (t == Tags._GRAIN_RECORD_HEAP_TAG) { + DS.Record.getData(ref) + } else if (t == Tags._GRAIN_ARRAY_HEAP_TAG) { + DS.Array.getData(DS.Array.fromGrain(WasmRef.toGrain(ref))) + } else if (t == Tags._GRAIN_TUPLE_HEAP_TAG) { + DS.Tuple.getData(DS.Tuple.fromGrain(WasmRef.toGrain(ref))) + } else { + DS.ADT.getData(ref) + } let arity = WasmArrayRef.length(arr) let headerSize = if (tag == Tags._GRAIN_ADT_HEAP_TAG) { 17n @@ -251,7 +239,7 @@ let rec computeSize = (ref, currentOffset) => { totalSize += childSize childOffset += childSize } - storeCycleMarker(ref, 0n) + DS.storeCycleMarker(ref, 0n) totalSize } }, @@ -268,23 +256,23 @@ let rec computeSize = (ref, currentOffset) => { 9n }, t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => { - let numberTag = DataStructures.getNumberTag(ref) + let numberTag = DS.getBoxedNumberTag(ref) match (numberTag) { t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG => 9n, t when t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => 9n, t when t == Tags._GRAIN_RATIONAL_BOXED_NUM_TAG => { let numSize = computeSize( - DataStructures.getRationalNumerator(ref), + DS.Rational.getNumerator(ref), currentOffset + 1n ) let denomSize = computeSize( - DataStructures.getRationalDenominator(ref), + DS.Rational.getDenominator(ref), currentOffset + 1n + numSize ) 1n + numSize + denomSize }, t when t == Tags._GRAIN_BIGINT_BOXED_NUM_TAG => { - 9n + DataStructures.getBigIntSize(ref) * 8n + 9n + DS.BigInt.getSize(ref) * 8n }, _ => { ignore(fail "Unknown number type") @@ -307,19 +295,21 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { writeI32(buf, offset + 1n, WasmRef.i31GetS(ref)) offset + 5n } else { - let tag = DataStructures.loadValueTag(ref) + let tag = DS.getHeapTag(ref) match (tag) { t when t == Tags._GRAIN_STRING_HEAP_TAG => { - let data = DataStructures.getStringArrayRef(ref) - let size = DataStructures.getStringSize(ref) + let ref = DS.String.fromGrain(WasmRef.toGrain(ref)) + let data = DS.String.getData(ref) + let size = DS.String.getSize(ref) writeU8(buf, offset, _TAG_STRING) writeI32(buf, offset + 1n, size) WasmArrayRef.copyI8(buf, offset + 5n, data, 0n, size) offset + 5n + size }, t when t == Tags._GRAIN_BYTES_HEAP_TAG => { - let data = DataStructures.getBytesArrayRef(ref) - let size = DataStructures.getBytesSize(ref) + let ref = DS.Bytes.fromGrain(WasmRef.toGrain(ref)) + let data = DS.Bytes.getData(ref) + let size = DS.Bytes.getSize(ref) writeU8(buf, offset, _TAG_BYTES) writeI32(buf, offset + 1n, size) WasmArrayRef.copyI8(buf, offset + 5n, data, 0n, size) @@ -329,28 +319,30 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { || t == Tags._GRAIN_RECORD_HEAP_TAG || t == Tags._GRAIN_ARRAY_HEAP_TAG || t == Tags._GRAIN_TUPLE_HEAP_TAG => { - if (loadCycleMarker(ref) != 0n) { + if (DS.loadCycleMarker(ref) != 0n) { let idx = vecFindRef(pathRefs, ref) let origOffset = WasmRef.i31GetS(vecGet(pathOffsets, idx)) writeU8(buf, offset, _TAG_BACKREF) writeI32(buf, offset + 1n, origOffset) offset + 5n } else { - storeCycleMarker(ref, 1n) + DS.storeCycleMarker(ref, 1n) vecPush(pathRefs, ref) vecPush(pathOffsets, WasmRef.i31Make(offset)) - let arr = DataStructures.getCompoundValueArrayRef(ref) + let arr = if (t == Tags._GRAIN_RECORD_HEAP_TAG) { + DS.Record.getData(ref) + } else if (t == Tags._GRAIN_ARRAY_HEAP_TAG) { + DS.Array.getData(DS.Array.fromGrain(WasmRef.toGrain(ref))) + } else if (t == Tags._GRAIN_TUPLE_HEAP_TAG) { + DS.Tuple.getData(DS.Tuple.fromGrain(WasmRef.toGrain(ref))) + } else { + DS.ADT.getData(ref) + } let arity = WasmArrayRef.length(arr) let mut childOffset = if (tag == Tags._GRAIN_ADT_HEAP_TAG) { - let typeHash = untagSimpleNumber( - DataStructures.loadVariantTypeHash(ref) - ) - let typeId = untagSimpleNumber( - DataStructures.loadVariantTypeId(ref) - ) - let variantId = untagSimpleNumber( - DataStructures.loadAdtVariant(ref) - ) + let typeHash = DS.untagSimpleNumber(DS.ADT.getTypeHash(ref)) + let typeId = DS.untagSimpleNumber(DS.ADT.getTypeId(ref)) + let variantId = DS.untagSimpleNumber(DS.ADT.getVariantId(ref)) writeU8(buf, offset, _TAG_ADT) writeI32(buf, offset + 1n, typeHash) writeI32(buf, offset + 5n, typeId) @@ -358,10 +350,8 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { writeI32(buf, offset + 13n, arity) offset + 17n } else if (tag == Tags._GRAIN_RECORD_HEAP_TAG) { - let typeHash = untagSimpleNumber( - DataStructures.loadRecordTypeHash(ref) - ) - let typeId = untagSimpleNumber(DataStructures.loadRecordTypeId(ref)) + let typeHash = DS.untagSimpleNumber(DS.Record.getTypeHash(ref)) + let typeId = DS.untagSimpleNumber(DS.Record.getTypeId(ref)) writeU8(buf, offset, _TAG_RECORD) writeI32(buf, offset + 1n, typeHash) writeI32(buf, offset + 5n, typeId) @@ -385,7 +375,7 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { pathOffsets ) } - storeCycleMarker(ref, 0n) + DS.storeCycleMarker(ref, 0n) childOffset } }, @@ -395,7 +385,7 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { }, t when t == Tags._GRAIN_INT32_HEAP_TAG => { writeU8(buf, offset, _TAG_INT32) - writeI32(buf, offset + 1n, DataStructures.getInt32Value(ref)) + writeI32(buf, offset + 1n, DS.Int32.getValue(ref)) offset + 5n }, t when t == Tags._GRAIN_FLOAT32_HEAP_TAG => { @@ -403,26 +393,26 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { writeI32( buf, offset + 1n, - WasmI32.reinterpretF32(DataStructures.getFloat32Value(ref)) + WasmI32.reinterpretF32(DS.Float32.getValue(ref)) ) offset + 5n }, t when t == Tags._GRAIN_UINT32_HEAP_TAG => { writeU8(buf, offset, _TAG_UINT32) - writeI32(buf, offset + 1n, DataStructures.getUint32Value(ref)) + writeI32(buf, offset + 1n, DS.Uint32.getValue(ref)) offset + 5n }, t when t == Tags._GRAIN_UINT64_HEAP_TAG => { writeU8(buf, offset, _TAG_UINT64) - writeI64(buf, offset + 1n, DataStructures.getUint64Value(ref)) + writeI64(buf, offset + 1n, DS.Uint64.getValue(ref)) offset + 9n }, t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => { - let numberTag = DataStructures.getNumberTag(ref) + let numberTag = DS.getBoxedNumberTag(ref) match (numberTag) { t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG => { writeU8(buf, offset, _TAG_INT64) - writeI64(buf, offset + 1n, DataStructures.getInt64Value(ref)) + writeI64(buf, offset + 1n, DS.Int64.getValue(ref)) offset + 9n }, t when t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => { @@ -430,7 +420,7 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { writeI64( buf, offset + 1n, - WasmI64.reinterpretF64(DataStructures.getFloat64Value(ref)) + WasmI64.reinterpretF64(DS.Float64.getValue(ref)) ) offset + 9n }, @@ -438,14 +428,14 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { writeU8(buf, offset, _TAG_RATIONAL) let mut childOffset = offset + 1n childOffset = marshalValue( - DataStructures.getRationalNumerator(ref), + DS.Rational.getNumerator(ref), buf, childOffset, pathRefs, pathOffsets ) childOffset = marshalValue( - DataStructures.getRationalDenominator(ref), + DS.Rational.getDenominator(ref), buf, childOffset, pathRefs, @@ -454,9 +444,9 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { childOffset }, t when t == Tags._GRAIN_BIGINT_BOXED_NUM_TAG => { - let flags = DataStructures.getBigIntFlags(ref) - let numLimbs = DataStructures.getBigIntSize(ref) - let limbs = DataStructures.getBigIntArrayRef(ref) + let flags = DS.BigInt.getFlags(ref) + let numLimbs = DS.BigInt.getSize(ref) + let limbs = DS.BigInt.getLimbData(ref) writeU8(buf, offset, _TAG_BIGINT) writeI32(buf, offset + 1n, flags) writeI32(buf, offset + 5n, numLimbs) @@ -501,17 +491,17 @@ let rec marshalValue = (ref, buf, offset, pathRefs, pathOffsets) => { provide let marshal = value => { let ref = WasmRef.fromGrain(value) let totalSize = computeSize(ref, 0n) - let bytesRef = allocateBytes(totalSize) - let buf = DataStructures.getBytesArrayRef(bytesRef) + let bytesRef = DS.Bytes.make(totalSize) + let buf = DS.Bytes.getData(bytesRef) let pathRefs = makeVec() let pathOffsets = makeVec() marshalValue(ref, buf, 0n, pathRefs, pathOffsets) - WasmRef.toGrain(bytesRef): Bytes + DS.Bytes.toGrain(bytesRef) } @unsafe let reportError = (message, offset) => { - Some(message ++ " at offset " ++ toString(WasmRef.toGrain(newInt32(offset)))) + Some(message ++ " at offset " ++ NumberUtils.itoa32(offset, 10n)) } @unsafe @@ -708,17 +698,17 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { }, t when t == _TAG_STRING => { let size = readI32(buf, offset + 1n) - let str = allocateString(size) - let strArr = DataStructures.getStringArrayRef(str) + let str = DS.String.make(size) + let strArr = DS.String.getData(str) WasmArrayRef.copyI8(strArr, 0n, buf, offset + 5n, size) - (str, WasmRef.i31Make(offset + 5n + size)) + (WasmRef.fromGrain(str), WasmRef.i31Make(offset + 5n + size)) }, t when t == _TAG_BYTES => { let size = readI32(buf, offset + 1n) - let bytes = allocateBytes(size) - let bytesArr = DataStructures.getBytesArrayRef(bytes) + let bytes = DS.Bytes.make(size) + let bytesArr = DS.Bytes.getData(bytes) WasmArrayRef.copyI8(bytesArr, 0n, buf, offset + 5n, size) - (bytes, WasmRef.i31Make(offset + 5n + size)) + (WasmRef.fromGrain(bytes), WasmRef.i31Make(offset + 5n + size)) }, t when t == _TAG_ADT => { let typeHash = readI32(buf, offset + 1n) << 1n | 1n @@ -726,8 +716,8 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { let variantId = readI32(buf, offset + 9n) << 1n | 1n let arity = readI32(buf, offset + 13n) let valuesArr = WasmArrayRef.makeAny(arity, WasmRef.fromGrain(void)) - let adtRef = allocateAdt( - WasmRef.fromGrain(valuesArr): WasmRef, + let adtRef = DS.ADT.make( + valuesArr, WasmRef.i31Make(typeHash), WasmRef.i31Make(typeId), WasmRef.i31Make(variantId) @@ -752,8 +742,8 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { let typeId = readI32(buf, offset + 5n) << 1n | 1n let arity = readI32(buf, offset + 9n) let valuesArr = WasmArrayRef.makeAny(arity, WasmRef.fromGrain(void)) - let recordRef = allocateRecord( - WasmRef.fromGrain(valuesArr): WasmRef, + let recordRef = DS.Record.make( + valuesArr, WasmRef.i31Make(typeHash), WasmRef.i31Make(typeId) ) @@ -774,10 +764,10 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { }, t when t == _TAG_ARRAY => { let length = readI32(buf, offset + 1n) - let arrayRef = allocateArray(length, WasmRef.fromGrain(void)) + let arrayRef = DS.Array.make(length, WasmRef.fromGrain(void)) vecPush(seenOffsets, WasmRef.i31Make(offset)) - vecPush(seenValues, arrayRef) - let arr = DataStructures.getCompoundValueArrayRef(arrayRef) + vecPush(seenValues, WasmRef.fromGrain(arrayRef)) + let arr = DS.Array.getData(arrayRef) let mut childOffset = offset + 5n for (let mut i = 0n; i < length; i += 1n) { let (childValue, nextOffsetRef) = unmarshalNode( @@ -789,14 +779,14 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { WasmArrayRef.setAny(arr, i, childValue) childOffset = WasmRef.i31GetS(nextOffsetRef) } - (arrayRef, WasmRef.i31Make(childOffset)) + (WasmRef.fromGrain(arrayRef), WasmRef.i31Make(childOffset)) }, t when t == _TAG_TUPLE => { let length = readI32(buf, offset + 1n) - let tupleRef = allocateTuple(length) + let tupleRef = DS.Tuple.make(length) vecPush(seenOffsets, WasmRef.i31Make(offset)) - vecPush(seenValues, tupleRef) - let arr = DataStructures.getCompoundValueArrayRef(tupleRef) + vecPush(seenValues, WasmRef.fromGrain(tupleRef)) + let arr = DS.Tuple.getData(tupleRef) let mut childOffset = offset + 5n for (let mut i = 0n; i < length; i += 1n) { let (childValue, nextOffsetRef) = unmarshalNode( @@ -808,29 +798,29 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { WasmArrayRef.setAny(arr, i, childValue) childOffset = WasmRef.i31GetS(nextOffsetRef) } - (tupleRef, WasmRef.i31Make(childOffset)) + (WasmRef.fromGrain(tupleRef), WasmRef.i31Make(childOffset)) }, t when t == _TAG_INT32 => { - (newInt32(readI32(buf, offset + 1n)), WasmRef.i31Make(offset + 5n)) + (DS.Int32.make(readI32(buf, offset + 1n)), WasmRef.i31Make(offset + 5n)) }, t when t == _TAG_UINT32 => { - (newUint32(readI32(buf, offset + 1n)), WasmRef.i31Make(offset + 5n)) + (DS.Uint32.make(readI32(buf, offset + 1n)), WasmRef.i31Make(offset + 5n)) }, t when t == _TAG_FLOAT32 => { ( - newFloat32(WasmF32.reinterpretI32(readI32(buf, offset + 1n))), + DS.Float32.make(WasmF32.reinterpretI32(readI32(buf, offset + 1n))), WasmRef.i31Make(offset + 5n), ) }, t when t == _TAG_INT64 => { - (newInt64(readI64(buf, offset + 1n)), WasmRef.i31Make(offset + 9n)) + (DS.Int64.make(readI64(buf, offset + 1n)), WasmRef.i31Make(offset + 9n)) }, t when t == _TAG_UINT64 => { - (newUint64(readI64(buf, offset + 1n)), WasmRef.i31Make(offset + 9n)) + (DS.Uint64.make(readI64(buf, offset + 1n)), WasmRef.i31Make(offset + 9n)) }, t when t == _TAG_FLOAT64 => { ( - newFloat64(WasmF64.reinterpretI64(readI64(buf, offset + 1n))), + DS.Float64.make(WasmF64.reinterpretI64(readI64(buf, offset + 1n))), WasmRef.i31Make(offset + 9n), ) }, @@ -850,14 +840,14 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { seenValues ) childOffset = WasmRef.i31GetS(nextOffsetRef) - (newRational(numRef, denomRef), WasmRef.i31Make(childOffset)) + (DS.Rational.make(numRef, denomRef), WasmRef.i31Make(childOffset)) }, t when t == _TAG_BIGINT => { let flags = readI32(buf, offset + 1n) let numLimbs = readI32(buf, offset + 5n) - let bigintRef = allocateBigInt(numLimbs) - setBigIntFlags(bigintRef, flags) - let limbs = DataStructures.getBigIntArrayRef(bigintRef) + let bigintRef = DS.BigInt.make(numLimbs) + DS.BigInt.setFlags(bigintRef, flags) + let limbs = DS.BigInt.getLimbData(bigintRef) let mut limbOffset = offset + 9n for (let mut i = 0n; i < numLimbs; i += 1n) { WasmArrayRef.setI64(limbs, i, readI64(buf, limbOffset)) @@ -889,9 +879,9 @@ let rec unmarshalNode = (buf, offset, seenOffsets, seenValues) => { */ @unsafe provide let unmarshal = (bytes: Bytes) => { - let bytesRef = WasmRef.fromGrain(bytes) - let buf = DataStructures.getBytesArrayRef(bytesRef) - let bufSize = DataStructures.getBytesSize(bytesRef) + let bytesRef = DS.Bytes.fromGrain(bytes) + let buf = DS.Bytes.getData(bytesRef) + let bufSize = DS.Bytes.getSize(bytesRef) match (validateNode(buf, bufSize, 0n, makeVec())) { Some(error) => Err(error), None => { diff --git a/stdlib/number.gr b/stdlib/number.gr index a166680e63..344aaeca29 100644 --- a/stdlib/number.gr +++ b/stdlib/number.gr @@ -33,14 +33,7 @@ from "runtime/unsafe/wasmf32" include WasmF32 from "runtime/unsafe/wasmf64" include WasmF64 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - newFloat64, - allocateString, - getStringArrayRef, - getNumberTag, - getFloat64Value, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ coerceNumberToWasmF64, @@ -310,7 +303,7 @@ provide let sqrt = (x: Number) => { if (!isFloat(x) && sqrtd == WasmF64.trunc(sqrtd)) { WasmRef.toGrain(reducedInteger(WasmI64.truncF64S(sqrtd))): Number } else { - WasmRef.toGrain(newFloat64(sqrtd)): Number + WasmRef.toGrain(DS.Float64.make(sqrtd)): Number } } @@ -572,13 +565,13 @@ provide let isFinite = (x: Number) => { let asRef = WasmRef.fromGrain(x) if (isBoxedNumber(asRef)) { // Boxed numbers can have multiple subtypes, of which float32 and float64 can be infinite. - let tag = getNumberTag(asRef) + let tag = DS.getBoxedNumberTag(asRef) if (tag == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG) { use WasmF64.{ (-), (==) } // uses the fact that all finite floats minus themselves are zero // (NaN - NaN == NaN, inf - inf == NaN, // -inf - -inf == NaN, inf - -inf == inf, -inf - inf == -inf) - let wf64 = getFloat64Value(asRef) + let wf64 = DS.Float64.getValue(asRef) wf64 - wf64 == 0.0W } else { // Neither rational numbers nor boxed integers can be infinite or NaN. @@ -637,10 +630,10 @@ provide let isInfinite = (x: Number) => { // so see those functions to understand what's going on here. let asRef = WasmRef.fromGrain(x) if (isBoxedNumber(asRef)) { - let tag = getNumberTag(asRef) + let tag = DS.getBoxedNumberTag(asRef) if (tag == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG) { use WasmF64.{ (-), (==), (!=) } - let wf64 = getFloat64Value(asRef) + let wf64 = DS.Float64.getValue(asRef) wf64 - wf64 != 0.0W && wf64 == wf64 } else { false @@ -744,7 +737,7 @@ provide let parse = input => { use WasmI32.{ (+), (-), ltU as (<), (==) } // Search for `/` - let input = getStringArrayRef(WasmRef.fromGrain(input)) + let input = DS.String.getData(DS.String.fromGrain(input)) let len = WasmArrayRef.length(input) let mut slashIdx = -1n for (let mut i = 0n; i < len; i += 1n) { @@ -760,25 +753,25 @@ provide let parse = input => { let numeratorLen = slashIdx let denominatorLen = len - slashIdx - 1n - let numerator = allocateString(numeratorLen) + let numerator = DS.String.make(numeratorLen) WasmArrayRef.copyI8( - getStringArrayRef(numerator), + DS.String.getData(numerator), 0n, input, 0n, numeratorLen ) - let numerator = WasmRef.toGrain(numerator): String + let numerator = DS.String.toGrain(numerator) - let denominator = allocateString(denominatorLen) + let denominator = DS.String.make(denominatorLen) WasmArrayRef.copyI8( - getStringArrayRef(denominator), + DS.String.getData(denominator), 0n, input, slashIdx + 1n, denominatorLen ) - let denominator = WasmRef.toGrain(denominator): String + let denominator = DS.String.toGrain(denominator) match ((parseInt(numerator, 10), parseInt(denominator, 10))) { (Ok(numerator), Ok(denominator)) => Ok(numerator / denominator), @@ -844,8 +837,8 @@ provide let asin = angle => { use WasmI32.{ (-) } /* asin(1) = +-pi/2 with inexact */ if (WasmI32.eqz(ix - 0x3ff00000n | lx)) - return WasmRef.toGrain(newFloat64(x * pio2_hi + 0x1p-120W)): Number - return WasmRef.toGrain(newFloat64(NaNW)): Number + return WasmRef.toGrain(DS.Float64.make(x * pio2_hi + 0x1p-120W)): Number + return WasmRef.toGrain(DS.Float64.make(NaNW)): Number } /* |x| < 0.5 */ if (ix < 0x3fe00000n) { @@ -854,7 +847,7 @@ provide let asin = angle => { x else x + x * rf(x * x) - return WasmRef.toGrain(newFloat64(output)): Number + return WasmRef.toGrain(DS.Float64.make(output)): Number } /* 1 > |x| >= 0.5 */ let z = (1.0W - WasmF64.abs(x)) * 0.5W @@ -874,7 +867,7 @@ provide let asin = angle => { - (2.0W * s * r - (pio2_lo - 2.0W * c) - (0.5W * pio2_hi - 2.0W * f)) } x = WasmF64.copySign(x, origAngle) - return WasmRef.toGrain(newFloat64(x)): Number + return WasmRef.toGrain(DS.Float64.make(x)): Number } /** @@ -911,11 +904,12 @@ provide let acos = angle => { if (WasmI32.eqz(ix - 0x3ff00000n | lx)) { /* acos(1)=0, acos(-1)=pi */ if (hx >> 31n != 0n) - return WasmRef.toGrain(newFloat64(2.0W * pio2_hi + 0x1p-120W)): Number + return WasmRef.toGrain(DS.Float64.make(2.0W * pio2_hi + 0x1p-120W)): + Number else return 0 } - return WasmRef.toGrain(newFloat64(NaNW)): Number + return WasmRef.toGrain(DS.Float64.make(NaNW)): Number } /* |x| < 0.5 */ if (ix < 0x3fe00000n) { @@ -924,14 +918,14 @@ provide let acos = angle => { pio2_hi + 0x1p-120W else pio2_hi - (x - (pio2_lo - x * rf(x * x))) - return WasmRef.toGrain(newFloat64(output)): Number + return WasmRef.toGrain(DS.Float64.make(output)): Number } /* x < -0.5 */ if (hx >> 31n != 0n) { let z = (1.0W + x) * 0.5W let s = WasmF64.sqrt(z) let w = rf(z) * s - pio2_lo - return WasmRef.toGrain(newFloat64(2.0W * (pio2_hi - (s + w)))): Number + return WasmRef.toGrain(DS.Float64.make(2.0W * (pio2_hi - (s + w)))): Number } /* x > 0.5 */ @@ -943,7 +937,7 @@ provide let acos = angle => { ) let c = (z - df * df) / (s + df) let w = rf(z) * s + c - return WasmRef.toGrain(newFloat64(2.0W * (df + w))): Number + return WasmRef.toGrain(DS.Float64.make(2.0W * (df + w))): Number } /** @@ -994,7 +988,7 @@ provide let atan = angle => { if (ix >= 0x44100000n) { if (isNaN(angle)) return NaN let z = atanhi3 + 0x1p-120W - return WasmRef.toGrain(newFloat64(WasmF64.copySign(z, sx))): Number + return WasmRef.toGrain(DS.Float64.make(WasmF64.copySign(z, sx))): Number } let mut id = 3n /* |x| < 0.4375 */ @@ -1031,7 +1025,8 @@ provide let atan = angle => { let s3 = x * (s1 + s2) use WasmI32.{ (<) } if (id < 0n) - return WasmRef.toGrain(newFloat64(WasmF64.copySign(x - s3, sx))): Number + return WasmRef.toGrain(DS.Float64.make(WasmF64.copySign(x - s3, sx))): + Number let mut z = 0.0W match (id) { 0n => z = atanhi0 - (s3 - atanlo0 - x), @@ -1040,7 +1035,7 @@ provide let atan = angle => { 3n => z = atanhi3 - (s3 + atanlo3 - x), _ => fail "Unreachable", } - return WasmRef.toGrain(newFloat64(WasmF64.copySign(z, sx))): Number + return WasmRef.toGrain(DS.Float64.make(WasmF64.copySign(z, sx))): Number } /** @@ -1200,7 +1195,7 @@ provide let sin = (radians: Number) => { return if (!isFloat(radians) && value == WasmF64.trunc(value)) { WasmRef.toGrain(reducedInteger(WasmI64.truncF64S(value))): Number } else { - WasmRef.toGrain(newFloat64(value)): Number + WasmRef.toGrain(DS.Float64.make(value)): Number } } @@ -1222,7 +1217,7 @@ provide let cos = (radians: Number) => { return if (!isFloat(radians) && value == WasmF64.trunc(value)) { WasmRef.toGrain(reducedInteger(WasmI64.truncF64S(value))): Number } else { - WasmRef.toGrain(newFloat64(value)): Number + WasmRef.toGrain(DS.Float64.make(value)): Number } } @@ -1244,7 +1239,7 @@ provide let tan = (radians: Number) => { return if (!isFloat(radians) && value == WasmF64.trunc(value)) { WasmRef.toGrain(reducedInteger(WasmI64.truncF64S(value))): Number } else { - WasmRef.toGrain(newFloat64(value)): Number + WasmRef.toGrain(DS.Float64.make(value)): Number } } diff --git a/stdlib/random.gr b/stdlib/random.gr index f0e19a97fa..aab0ef8cc7 100644 --- a/stdlib/random.gr +++ b/stdlib/random.gr @@ -12,7 +12,7 @@ from "uint32" include Uint32 from "uint64" include Uint64 from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS abstract record Random { seed: Uint64, @@ -30,8 +30,8 @@ let squares = (ctr: Uint64, key: Uint64) => { use WasmI64.{ (+), (*), (|), (<<), (>>>) } // Implemented with @unsafe to boost efficiency // and have fine-grained control over overflow semantics - let ctr = Conv.fromUint64(ctr) - let key = Conv.fromUint64(key) + let ctr = DS.Uint64.fromUint64(ctr) + let key = DS.Uint64.fromUint64(key) let mut x = ctr * key let mut y = x let mut z = y + key @@ -45,7 +45,7 @@ let squares = (ctr: Uint64, key: Uint64) => { x = x * x + y x = x >>> 32N | x << 32N let ret = WasmI32.wrapI64((x * x + z) >>> 32N) - Conv.toUint32(ret) + DS.Uint32.toUint32(ret) } /** diff --git a/stdlib/runtime/atof/common.gr b/stdlib/runtime/atof/common.gr index 3e36b916c3..582b3f3be6 100644 --- a/stdlib/runtime/atof/common.gr +++ b/stdlib/runtime/atof/common.gr @@ -33,8 +33,7 @@ from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmf64" include WasmF64 from "runtime/unsafe/wasmref" include WasmRef -from "runtime/dataStructures" include DataStructures -use DataStructures.{ newInt32, newInt64, newFloat64 } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numberUtils" include NumberUtils use NumberUtils.{ get_POWERS10 } from "runtime/atof/table" include Table @@ -114,8 +113,7 @@ provide let _CHAR_CODE_y = 0x79n provide let fpZero = () => { f: 0L, e: 0l } @unsafe -provide let fpInf = () => - { f: 0L, e: WasmRef.toGrain(newInt32(_INFINITE_POWER)): Int32 } +provide let fpInf = () => { f: 0L, e: DS.Int32.toInt32(_INFINITE_POWER) } provide let fpErr = () => { f: 0L, e: -1l } provide let fpNan = () => { f: 1L, e: -1l } @@ -178,20 +176,18 @@ provide let fullMultiplication = (a: WasmI64, b: WasmI64) => { let hi = aHi * bHi + w1 + k let lo = (mid << 32N) + w3 - (WasmRef.toGrain(newInt64(lo)): Int64, WasmRef.toGrain(newInt64(hi)): Int64) + (DS.Int64.toInt64(lo), DS.Int64.toInt64(hi)) } @unsafe provide let biasedFpToNumber = (fp, negative) => { use WasmI64.{ (|), (<<) } - let f = DataStructures.getInt64Value(WasmRef.fromGrain(fp.f)) - let e = WasmI64.extendI32S( - DataStructures.getInt32Value(WasmRef.fromGrain(fp.e)) - ) + let f = DS.Int64.fromInt64(fp.f) + let e = WasmI64.extendI32S(DS.Int32.fromInt32(fp.e)) let word = f | e << _MANTISSA_EXPLICIT_BITS_64 let mut float = WasmF64.reinterpretI64(word) if (negative) { float = WasmF64.neg(float) } - WasmRef.toGrain(newFloat64(float)): Number + WasmRef.toGrain(DS.Float64.make(float)): Number } diff --git a/stdlib/runtime/atof/decimal.gr b/stdlib/runtime/atof/decimal.gr index 03bd79c866..1cd79d2146 100644 --- a/stdlib/runtime/atof/decimal.gr +++ b/stdlib/runtime/atof/decimal.gr @@ -33,8 +33,7 @@ from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } -from "runtime/dataStructures" include DataStructures -use DataStructures.{ newInt32, allocateBytes } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/atof/common" include Common use Common.{ @@ -108,36 +107,36 @@ provide let _DECIMAL_POINT_RANGE = 2047n @unsafe let new = () => { - let digits = allocateBytes(_MAX_DIGITS) - let digits = WasmRef.toGrain(digits): Bytes + let digits = DS.Bytes.make(_MAX_DIGITS) + let digits = DS.Bytes.toGrain(digits) { numDigits: 0l, decimalPoint: 0l, truncated: false, digits } } @unsafe let loadDigit = (digits, index) => { - WasmArrayRef.getI8U(DataStructures.getBytesArrayRef(digits), index) + WasmArrayRef.getI8U(DS.Bytes.getData(digits), index) } @unsafe let storeDigit = (digits, index, digit) => { - WasmArrayRef.setI8(DataStructures.getBytesArrayRef(digits), index, digit) + WasmArrayRef.setI8(DS.Bytes.getData(digits), index, digit) } @unsafe let loadStringChar = (string, index) => { - WasmArrayRef.getI8U(DataStructures.getStringArrayRef(string), index) + WasmArrayRef.getI8U(DS.String.getData(string), index) } // Append a digit to the buffer. @unsafe provide let tryAddDigit = (d, digit) => { use WasmI32.{ (+) } - let numDigits = DataStructures.getInt32Value(WasmRef.fromGrain(d.numDigits)) + let numDigits = DS.Int32.fromInt32(d.numDigits) if (WasmI32.ltU(numDigits, _MAX_DIGITS)) { - let digits = WasmRef.fromGrain(d.digits) + let digits = DS.Bytes.fromGrain(d.digits) storeDigit(digits, numDigits, digit) } - d.numDigits = WasmRef.toGrain(newInt32(numDigits + 1n)) + d.numDigits = DS.Int32.toInt32(numDigits + 1n) } // Trim trailing zeros from the buffer. @@ -151,27 +150,21 @@ let trim = d => { // 3. `leftShift` `numDigits` to a max of `_MAX_DIGITS`. // // Trim is only called in `rightShift` and `leftShift`. - let digits = WasmRef.fromGrain(d.digits) - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) + let digits = DS.Bytes.fromGrain(d.digits) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) while (numDigits != 0n && WasmI32.eqz(loadDigit(digits, numDigits - 1n))) { numDigits -= 1n } - d.numDigits = WasmRef.toGrain(newInt32(numDigits)) + d.numDigits = DS.Int32.toInt32(numDigits) } @unsafe provide let round = d => { use WasmI32.{ (+), (-), (&), (<), (>), (>=), (==), (!=) } use WasmI64.{ (+) as addWasmI64, (*) } - let digits = WasmRef.fromGrain(d.digits) - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) - let mut decimalPoint = DataStructures.getInt32Value( - WasmRef.fromGrain(d.decimalPoint) - ) + let digits = DS.Bytes.fromGrain(d.digits) + let numDigits = DS.Int32.fromInt32(d.numDigits) + let decimalPoint = DS.Int32.fromInt32(d.decimalPoint) if (numDigits == 0n || decimalPoint < 0n) { 0N } else if (decimalPoint > 18n) { @@ -211,7 +204,7 @@ provide let get_TABLE = (index: WasmI32) => { ) } use WasmI32.{ (+), (|), (<<) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) let lower = WasmArrayRef.getI8U(data, index) let upper = WasmArrayRef.getI8U(data, index + 1n) upper << 8n | lower @@ -229,7 +222,7 @@ provide let get_TABLE_POW5 = (index: WasmI32) => { b"\x05\x02\x05\x01\x02\x05\x06\x02\x05\x03\x01\x02\x05\x01\x05\x06\x02\x05\x07\x08\x01\x02\x05\x03\x09\x00\x06\x02\x05\x01\x09\x05\x03\x01\x02\x05\x09\x07\x06\x05\x06\x02\x05\x04\x08\x08\x02\x08\x01\x02\x05\x02\x04\x04\x01\x04\x00\x06\x02\x05\x01\x02\x02\x00\x07\x00\x03\x01\x02\x05\x06\x01\x00\x03\x05\x01\x05\x06\x02\x05\x03\x00\x05\x01\x07\x05\x07\x08\x01\x02\x05\x01\x05\x02\x05\x08\x07\x08\x09\x00\x06\x02\x05\x07\x06\x02\x09\x03\x09\x04\x05\x03\x01\x02\x05\x03\x08\x01\x04\x06\x09\x07\x02\x06\x05\x06\x02\x05\x01\x09\x00\x07\x03\x04\x08\x06\x03\x02\x08\x01\x02\x05\x09\x05\x03\x06\x07\x04\x03\x01\x06\x04\x00\x06\x02\x05\x04\x07\x06\x08\x03\x07\x01\x05\x08\x02\x00\x03\x01\x02\x05\x02\x03\x08\x04\x01\x08\x05\x07\x09\x01\x00\x01\x05\x06\x02\x05\x01\x01\x09\x02\x00\x09\x02\x08\x09\x05\x05\x00\x07\x08\x01\x02\x05\x05\x09\x06\x00\x04\x06\x04\x04\x07\x07\x05\x03\x09\x00\x06\x02\x05\x02\x09\x08\x00\x02\x03\x02\x02\x03\x08\x07\x06\x09\x05\x03\x01\x02\x05\x01\x04\x09\x00\x01\x01\x06\x01\x01\x09\x03\x08\x04\x07\x06\x05\x06\x02\x05\x07\x04\x05\x00\x05\x08\x00\x05\x09\x06\x09\x02\x03\x08\x02\x08\x01\x02\x05\x03\x07\x02\x05\x02\x09\x00\x02\x09\x08\x04\x06\x01\x09\x01\x04\x00\x06\x02\x05\x01\x08\x06\x02\x06\x04\x05\x01\x04\x09\x02\x03\x00\x09\x05\x07\x00\x03\x01\x02\x05\x09\x03\x01\x03\x02\x02\x05\x07\x04\x06\x01\x05\x04\x07\x08\x05\x01\x05\x06\x02\x05\x04\x06\x05\x06\x06\x01\x02\x08\x07\x03\x00\x07\x07\x03\x09\x02\x05\x07\x08\x01\x02\x05\x02\x03\x02\x08\x03\x00\x06\x04\x03\x06\x05\x03\x08\x06\x09\x06\x02\x08\x09\x00\x06\x02\x05\x01\x01\x06\x04\x01\x05\x03\x02\x01\x08\x02\x06\x09\x03\x04\x08\x01\x04\x04\x05\x03\x01\x02\x05\x05\x08\x02\x00\x07\x06\x06\x00\x09\x01\x03\x04\x06\x07\x04\x00\x07\x02\x02\x06\x05\x06\x02\x05\x02\x09\x01\x00\x03\x08\x03\x00\x04\x05\x06\x07\x03\x03\x07\x00\x03\x06\x01\x03\x02\x08\x01\x02\x05\x01\x04\x05\x05\x01\x09\x01\x05\x02\x02\x08\x03\x06\x06\x08\x05\x01\x08\x00\x06\x06\x04\x00\x06\x02\x05\x07\x02\x07\x05\x09\x05\x07\x06\x01\x04\x01\x08\x03\x04\x02\x05\x09\x00\x03\x03\x02\x00\x03\x01\x02\x05\x03\x06\x03\x07\x09\x07\x08\x08\x00\x07\x00\x09\x01\x07\x01\x02\x09\x05\x01\x06\x06\x00\x01\x05\x06\x02\x05\x01\x08\x01\x08\x09\x08\x09\x04\x00\x03\x05\x04\x05\x08\x05\x06\x04\x07\x05\x08\x03\x00\x00\x07\x08\x01\x02\x05\x09\x00\x09\x04\x09\x04\x07\x00\x01\x07\x07\x02\x09\x02\x08\x02\x03\x07\x09\x01\x05\x00\x03\x09\x00\x06\x02\x05\x04\x05\x04\x07\x04\x07\x03\x05\x00\x08\x08\x06\x04\x06\x04\x01\x01\x08\x09\x05\x07\x05\x01\x09\x05\x03\x01\x02\x05\x02\x02\x07\x03\x07\x03\x06\x07\x05\x04\x04\x03\x02\x03\x02\x00\x05\x09\x04\x07\x08\x07\x05\x09\x07\x06\x05\x06\x02\x05\x01\x01\x03\x06\x08\x06\x08\x03\x07\x07\x02\x01\x06\x01\x06\x00\x02\x09\x07\x03\x09\x03\x07\x09\x08\x08\x02\x08\x01\x02\x05\x05\x06\x08\x04\x03\x04\x01\x08\x08\x06\x00\x08\x00\x08\x00\x01\x04\x08\x06\x09\x06\x08\x09\x09\x04\x01\x04\x00\x06\x02\x05\x02\x08\x04\x02\x01\x07\x00\x09\x04\x03\x00\x04\x00\x04\x00\x00\x07\x04\x03\x04\x08\x04\x04\x09\x07\x00\x07\x00\x03\x01\x02\x05\x01\x04\x02\x01\x00\x08\x05\x04\x07\x01\x05\x02\x00\x02\x00\x00\x03\x07\x01\x07\x04\x02\x02\x04\x08\x05\x03\x05\x01\x05\x06\x02\x05\x07\x01\x00\x05\x04\x02\x07\x03\x05\x07\x06\x00\x01\x00\x00\x01\x08\x05\x08\x07\x01\x01\x02\x04\x02\x06\x07\x05\x07\x08\x01\x02\x05\x03\x05\x05\x02\x07\x01\x03\x06\x07\x08\x08\x00\x00\x05\x00\x00\x09\x02\x09\x03\x05\x05\x06\x02\x01\x03\x03\x07\x08\x09\x00\x06\x02\x05\x01\x07\x07\x06\x03\x05\x06\x08\x03\x09\x04\x00\x00\x02\x05\x00\x04\x06\x04\x06\x07\x07\x08\x01\x00\x06\x06\x08\x09\x04\x05\x03\x01\x02\x05\x08\x08\x08\x01\x07\x08\x04\x01\x09\x07\x00\x00\x01\x02\x05\x02\x03\x02\x03\x03\x08\x09\x00\x05\x03\x03\x04\x04\x07\x02\x06\x05\x06\x02\x05\x04\x04\x04\x00\x08\x09\x02\x00\x09\x08\x05\x00\x00\x06\x02\x06\x01\x06\x01\x06\x09\x04\x05\x02\x06\x06\x07\x02\x03\x06\x03\x02\x08\x01\x02\x05\x02\x02\x02\x00\x04\x04\x06\x00\x04\x09\x02\x05\x00\x03\x01\x03\x00\x08\x00\x08\x04\x07\x02\x06\x03\x03\x03\x06\x01\x08\x01\x06\x04\x00\x06\x02\x05\x01\x01\x01\x00\x02\x02\x03\x00\x02\x04\x06\x02\x05\x01\x05\x06\x05\x04\x00\x04\x02\x03\x06\x03\x01\x06\x06\x08\x00\x09\x00\x08\x02\x00\x03\x01\x02\x05\x05\x05\x05\x01\x01\x01\x05\x01\x02\x03\x01\x02\x05\x07\x08\x02\x07\x00\x02\x01\x01\x08\x01\x05\x08\x03\x04\x00\x04\x05\x04\x01\x00\x01\x05\x06\x02\x05\x02\x07\x07\x05\x05\x05\x07\x05\x06\x01\x05\x06\x02\x08\x09\x01\x03\x05\x01\x00\x05\x09\x00\x07\x09\x01\x07\x00\x02\x02\x07\x00\x05\x00\x07\x08\x01\x02\x05\x01\x03\x08\x07\x07\x07\x08\x07\x08\x00\x07\x08\x01\x04\x04\x05\x06\x07\x05\x05\x02\x09\x05\x03\x09\x05\x08\x05\x01\x01\x03\x05\x02\x05\x03\x09\x00\x06\x02\x05\x06\x09\x03\x08\x08\x09\x03\x09\x00\x03\x09\x00\x07\x02\x02\x08\x03\x07\x07\x06\x04\x07\x06\x09\x07\x09\x02\x05\x05\x06\x07\x06\x02\x06\x09\x05\x03\x01\x02\x05\x03\x04\x06\x09\x04\x04\x06\x09\x05\x01\x09\x05\x03\x06\x01\x04\x01\x08\x08\x08\x02\x03\x08\x04\x08\x09\x06\x02\x07\x08\x03\x08\x01\x03\x04\x07\x06\x05\x06\x02\x05\x01\x07\x03\x04\x07\x02\x03\x04\x07\x05\x09\x07\x06\x08\x00\x07\x00\x09\x04\x04\x01\x01\x09\x02\x04\x04\x08\x01\x03\x09\x01\x09\x00\x06\x07\x03\x08\x02\x08\x01\x02\x05\x08\x06\x07\x03\x06\x01\x07\x03\x07\x09\x08\x08\x04\x00\x03\x05\x04\x07\x02\x00\x05\x09\x06\x02\x02\x04\x00\x06\x09\x05\x09\x05\x03\x03\x06\x09\x01\x04\x00\x06\x02\x05" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) WasmArrayRef.getI8U(data, index) } @@ -237,10 +230,8 @@ provide let get_TABLE_POW5 = (index: WasmI32) => { let numberOfDigitsDecimalLeftShift = (d, shift) => { use WasmI32.{ (+), (-), (&), ltU as (<), geU as (>=), (==), (>>>), (<<) } - let digits = WasmRef.fromGrain(d.digits) - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) + let digits = DS.Bytes.fromGrain(d.digits) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) let shift = (shift & 63n) << 1n let x_a = get_TABLE(shift) @@ -281,13 +272,9 @@ provide let leftShift = (d, shift) => { (>), } - let digits = WasmRef.fromGrain(d.digits) - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) - let mut decimalPoint = DataStructures.getInt32Value( - WasmRef.fromGrain(d.decimalPoint) - ) + let digits = DS.Bytes.fromGrain(d.digits) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) + let mut decimalPoint = DS.Int32.fromInt32(d.decimalPoint) if (numDigits != 0n) { let numNewDigits = numberOfDigitsDecimalLeftShift(d, shift) @@ -329,8 +316,8 @@ provide let leftShift = (d, shift) => { } decimalPoint += numNewDigits - d.numDigits = WasmRef.toGrain(newInt32(numDigits)) - d.decimalPoint = WasmRef.toGrain(newInt32(decimalPoint)) + d.numDigits = DS.Int32.toInt32(numDigits) + d.decimalPoint = DS.Int32.toInt32(decimalPoint) trim(d) } } @@ -342,13 +329,9 @@ provide let rightShift = (d, shift) => { use WasmI64.{ (+) as addWasmI64, (-) as subWasmI64, (*), (&), (<<), (>>>) } - let digits = WasmRef.fromGrain(d.digits) - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) - let mut decimalPoint = DataStructures.getInt32Value( - WasmRef.fromGrain(d.decimalPoint) - ) + let digits = DS.Bytes.fromGrain(d.digits) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) + let mut decimalPoint = DS.Int32.fromInt32(d.decimalPoint) let mut readIndex = 0n let mut writeIndex = 0n @@ -376,8 +359,8 @@ provide let rightShift = (d, shift) => { decimalPoint = 0n d.truncated = false - d.numDigits = WasmRef.toGrain(newInt32(numDigits)) - d.decimalPoint = WasmRef.toGrain(newInt32(decimalPoint)) + d.numDigits = DS.Int32.toInt32(numDigits) + d.decimalPoint = DS.Int32.toInt32(decimalPoint) } else { let mask = subWasmI64(1N << WasmI64.extendI32U(shift), 1N) while (readIndex < numDigits) { @@ -403,8 +386,8 @@ provide let rightShift = (d, shift) => { } numDigits = writeIndex - d.numDigits = WasmRef.toGrain(newInt32(numDigits)) - d.decimalPoint = WasmRef.toGrain(newInt32(decimalPoint)) + d.numDigits = DS.Int32.toInt32(numDigits) + d.decimalPoint = DS.Int32.toInt32(decimalPoint) trim(d) } } @@ -416,9 +399,9 @@ provide let parseDecimal = (s: String) => { use WasmI32.{ (+), (-), (*), (&), (<), (>), (>=), (==), (!=) } let d = new() - let s = WasmRef.fromGrain(s) - let digits = WasmRef.fromGrain(d.digits) - let len = DataStructures.getStringSize(s) + let s = DS.String.fromGrain(s) + let digits = DS.Bytes.fromGrain(d.digits) + let len = DS.String.getSize(s) let mut i = 0n let c = loadStringChar(s, i) if (c == _CHAR_CODE_PLUS || c == _CHAR_CODE_MINUS) { @@ -450,9 +433,7 @@ provide let parseDecimal = (s: String) => { let c = loadStringChar(s, i) if (c == _CHAR_CODE_DOT) { i += 1n - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) let numDigitsBeforeDecimal = numDigits // Skip leading zeros. if (numDigits == 0n) { @@ -477,15 +458,11 @@ provide let parseDecimal = (s: String) => { break } } - numDigits = DataStructures.getInt32Value(WasmRef.fromGrain(d.numDigits)) - d.decimalPoint = WasmRef.toGrain( - newInt32(numDigitsBeforeDecimal - numDigits) - ) + numDigits = DS.Int32.fromInt32(d.numDigits) + d.decimalPoint = DS.Int32.toInt32(numDigitsBeforeDecimal - numDigits) } } - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) if (numDigits != 0n) { // Ignore the trailing zeros if there are any let mut nTrailingZeros = 0n @@ -499,9 +476,7 @@ provide let parseDecimal = (s: String) => { break } } - let mut decimalPoint = DataStructures.getInt32Value( - WasmRef.fromGrain(d.decimalPoint) - ) + let mut decimalPoint = DS.Int32.fromInt32(d.decimalPoint) decimalPoint += nTrailingZeros numDigits -= nTrailingZeros decimalPoint += numDigits @@ -509,8 +484,8 @@ provide let parseDecimal = (s: String) => { d.truncated = true numDigits = _MAX_DIGITS } - d.numDigits = WasmRef.toGrain(newInt32(numDigits)) - d.decimalPoint = WasmRef.toGrain(newInt32(decimalPoint)) + d.numDigits = DS.Int32.toInt32(numDigits) + d.decimalPoint = DS.Int32.toInt32(decimalPoint) } if (i < len) { let c = loadStringChar(s, i) @@ -539,20 +514,16 @@ provide let parseDecimal = (s: String) => { break } } - let mut decimalPoint = DataStructures.getInt32Value( - WasmRef.fromGrain(d.decimalPoint) - ) + let mut decimalPoint = DS.Int32.fromInt32(d.decimalPoint) decimalPoint += if (negExp) { 0n - expNum } else { expNum } - d.decimalPoint = WasmRef.toGrain(newInt32(decimalPoint)) + d.decimalPoint = DS.Int32.toInt32(decimalPoint) } } - let mut numDigits = DataStructures.getInt32Value( - WasmRef.fromGrain(d.numDigits) - ) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) for (let mut i = numDigits; i < _MAX_DIGITS_WITHOUT_OVERFLOW; i += 1n) { storeDigit(digits, i, 0n) } diff --git a/stdlib/runtime/atof/lemire.gr b/stdlib/runtime/atof/lemire.gr index 629055c1d7..7847e75aee 100644 --- a/stdlib/runtime/atof/lemire.gr +++ b/stdlib/runtime/atof/lemire.gr @@ -32,9 +32,7 @@ module Lemire from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmref" include WasmRef - -from "runtime/dataStructures" include DataStructures -use DataStructures.{ newInt32, newInt64, newFloat64, getInt64Value } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/atof/table" include Table use Table.{ get_POWERS5 } @@ -93,8 +91,8 @@ let computeProductApprox = (q: WasmI64, w: WasmI64, precision: WasmI64) => { // determine the rounding direction, +1 for if the computed // product has a leading zero. let (firstLo, firstHi) = fullMultiplication(w, lo5) - let mut firstLo = getInt64Value(WasmRef.fromGrain(firstLo)) - let mut firstHi = getInt64Value(WasmRef.fromGrain(firstHi)) + let mut firstLo = DS.Int64.fromInt64(firstLo) + let mut firstHi = DS.Int64.fromInt64(firstHi) if ((firstHi & mask) == mask) { // From Rust: // Need to do a second multiplication to get better precision @@ -102,17 +100,14 @@ let computeProductApprox = (q: WasmI64, w: WasmI64, precision: WasmI64) => { // where q is < 55, since 5^55 < 2^128. If this wraps, // then we need to need to round up the hi product. let (_, secondHi) = fullMultiplication(w, hi5) - let secondHi = getInt64Value(WasmRef.fromGrain(secondHi)) + let secondHi = DS.Int64.fromInt64(secondHi) firstLo += secondHi if (secondHi > firstLo) { firstHi += 1N } } - ( - WasmRef.toGrain(newInt64(firstLo)): Int64, - WasmRef.toGrain(newInt64(firstHi)): Int64, - ) + (DS.Int64.toInt64(firstLo), DS.Int64.toInt64(firstHi)) } // Returns the significant bits and a biased binary exponent that can be directly shifted into exponent bits: @@ -132,8 +127,8 @@ provide let computeFloat = (exponent: WasmI64, mantissa: WasmI64) => { let lz = WasmI64.clz(w) let w = w << lz let (lo, hi) = computeProductApprox(q, w, _MANTISSA_EXPLICIT_BITS_64 + 3N) - let lo = getInt64Value(WasmRef.fromGrain(lo)) - let hi = getInt64Value(WasmRef.fromGrain(hi)) + let lo = DS.Int64.fromInt64(lo) + let hi = DS.Int64.fromInt64(hi) // From Rust: // If we have failed to approximate w x 5^-q with our 128-bit value. // Since the addition of 1 could lead to an overflow which could then @@ -185,10 +180,7 @@ provide let computeFloat = (exponent: WasmI64, mantissa: WasmI64) => { 0n } - { - f: WasmRef.toGrain(newInt64(mantissa)): Int64, - e: WasmRef.toGrain(newInt32(power2)): Int32, - } + { f: DS.Int64.toInt64(mantissa), e: DS.Int32.toInt32(power2) } } } else { use WasmI64.{ (<=) } @@ -238,10 +230,7 @@ provide let computeFloat = (exponent: WasmI64, mantissa: WasmI64) => { // Exponent is above largest normal value, must be infinite. fpInf() } else { - { - f: WasmRef.toGrain(newInt64(mantissa)): Int64, - e: WasmRef.toGrain(newInt32(power2)): Int32, - } + { f: DS.Int64.toInt64(mantissa), e: DS.Int32.toInt32(power2) } } } }, diff --git a/stdlib/runtime/atof/parse.gr b/stdlib/runtime/atof/parse.gr index 14e0b20726..01a973bd28 100644 --- a/stdlib/runtime/atof/parse.gr +++ b/stdlib/runtime/atof/parse.gr @@ -33,8 +33,7 @@ from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmf64" include WasmF64 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } -from "runtime/dataStructures" include DataStructures -use DataStructures.{ newInt32, newInt64, newFloat64 } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/atof/common" include Common use Common.{ @@ -135,9 +134,9 @@ let parseScientificExponent = ( if (offset != len) { Err("Invalid exponent") } else if (negative) { - Ok(WasmRef.toGrain(newInt32(0n - exponent)): Int32) + Ok(DS.Int32.toInt32(0n - exponent)) } else { - Ok(WasmRef.toGrain(newInt32(exponent)): Int32) + Ok(DS.Int32.toInt32(exponent)) } } } @@ -147,7 +146,7 @@ let parseFloatToParts = (string: String) => { use WasmI32.{ (+), (-), gtU as (>), geU as (>=), ltU as (<), (==), (!=) } use WasmI64.{ (+) as addWasmI64, (*) } - let dataRef = DataStructures.getStringArrayRef(WasmRef.fromGrain(string)) + let dataRef = DS.String.getData(DS.String.fromGrain(string)) match (WasmArrayRef.length(dataRef)) { // Invalid string 0n => Err("Invalid string"), @@ -246,9 +245,7 @@ let parseFloatToParts = (string: String) => { match (exponentResult) { Ok(exponentNumber) => { - let exponentNumber = DataStructures.getInt32Value( - WasmRef.fromGrain(exponentNumber) - ) + let exponentNumber = DS.Int32.fromInt32(exponentNumber) exponent += exponentNumber // Check to see if we need to truncate @@ -344,8 +341,8 @@ let parseFloatToParts = (string: String) => { } Ok( ( - WasmRef.toGrain(newInt32(exponent)): Int32, - WasmRef.toGrain(newInt64(mantissa)): Int64, + DS.Int32.toInt32(exponent), + DS.Int64.toInt64(mantissa), negative, manyDigits, ), @@ -353,8 +350,8 @@ let parseFloatToParts = (string: String) => { } else { Ok( ( - WasmRef.toGrain(newInt32(exponent)): Int32, - WasmRef.toGrain(newInt64(mantissa)): Int64, + DS.Int32.toInt32(exponent), + DS.Int64.toInt64(mantissa), negative, manyDigits, ), @@ -373,7 +370,7 @@ let parseFloatToParts = (string: String) => { let parseInfNan = (s: String) => { use WasmI32.{ (+), (-), (<), (>=), (<=), (==) } - let dataRef = DataStructures.getStringArrayRef(WasmRef.fromGrain(s)) + let dataRef = DS.String.getData(DS.String.fromGrain(s)) match (WasmArrayRef.length(dataRef)) { // Invalid string 0n => Err("Invalid string"), @@ -496,8 +493,8 @@ provide let parseFloat = (string: String) => { match (parseFloatToParts(string)) { Ok((exponent, mantissa, negative, manyDigits)) => { use WasmI64.{ (+), (>) } - let exponent = DataStructures.getInt32Value(WasmRef.fromGrain(exponent)) - let mantissa = DataStructures.getInt64Value(WasmRef.fromGrain(mantissa)) + let exponent = DS.Int32.fromInt32(exponent) + let mantissa = DS.Int64.fromInt64(mantissa) let floatOpt = if (isFastPath(exponent, mantissa, negative, manyDigits)) { use WasmI32.{ (<=) } if (exponent <= _MAX_EXPONENT_FAST_PATH) { @@ -512,9 +509,9 @@ provide let parseFloat = (string: String) => { mantissa * getPowers10FastPath(exponent) } if (negative) { - Some(WasmRef.toGrain(newFloat64(n * -1.0W)): Number) + Some(WasmRef.toGrain(DS.Float64.make(n * -1.0W)): Number) } else { - Some(WasmRef.toGrain(newFloat64(n)): Number) + Some(WasmRef.toGrain(DS.Float64.make(n)): Number) } } else { // disguised fast path @@ -532,9 +529,9 @@ provide let parseFloat = (string: String) => { let mantissa = WasmF64.convertI64U(mantissa) let n = mantissa * getPowers10FastPath(_MAX_EXPONENT_FAST_PATH) if (negative) { - Some(WasmRef.toGrain(newFloat64(n * -1.0W)): Number) + Some(WasmRef.toGrain(DS.Float64.make(n * -1.0W)): Number) } else { - Some(WasmRef.toGrain(newFloat64(n)): Number) + Some(WasmRef.toGrain(DS.Float64.make(n)): Number) } } } @@ -553,13 +550,13 @@ provide let parseFloat = (string: String) => { // correctly round on the first pass. let mut fp = computeFloat(WasmI64.extendI32S(exponent), mantissa) - let f = DataStructures.getInt64Value(WasmRef.fromGrain(fp.f)) - let e = DataStructures.getInt32Value(WasmRef.fromGrain(fp.e)) + let f = DS.Int64.fromInt64(fp.f) + let e = DS.Int32.fromInt32(fp.e) if (manyDigits && e >= 0n) { use WasmI64.{ (!=) as neWasmI64 } let fp2 = computeFloat(WasmI64.extendI32S(exponent), mantissa + 1N) - let f2 = DataStructures.getInt64Value(WasmRef.fromGrain(fp2.f)) - let e2 = DataStructures.getInt32Value(WasmRef.fromGrain(fp2.e)) + let f2 = DS.Int64.fromInt64(fp2.f) + let e2 = DS.Int32.fromInt32(fp2.e) if (e != e2 || neWasmI64(f, f2)) { fp.e = -1l } @@ -567,7 +564,7 @@ provide let parseFloat = (string: String) => { // Unable to correctly round the float using the Eisel-Lemire algorithm. // Fallback to a slower, but always correct algorithm. - let e = DataStructures.getInt32Value(WasmRef.fromGrain(fp.e)) + let e = DS.Int32.fromInt32(fp.e) if (e < 0n) { fp = parseLongMantissa(string) } diff --git a/stdlib/runtime/atof/slow.gr b/stdlib/runtime/atof/slow.gr index 11efefa53a..43fad00015 100644 --- a/stdlib/runtime/atof/slow.gr +++ b/stdlib/runtime/atof/slow.gr @@ -34,8 +34,7 @@ from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } -from "runtime/dataStructures" include DataStructures -use DataStructures.{ newInt32, newInt64, getInt32Value, getBytesArrayRef } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/atof/common" include Common use Common.{ @@ -65,7 +64,7 @@ let get_POWERS = (index: WasmI32) => { b"\x00\x03\x06\x09\x0d\x10\x13\x17\x1a\x1d\x21\x24\x27\x2b\x2e\x31\x35\x38\x3b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) WasmArrayRef.getI8U(data, index) } @@ -106,9 +105,9 @@ provide let parseLongMantissa = (s: String) => { use WasmI64.{ (+) as addWasmI64, (<<) } let mut d = parseDecimal(s) - let digits = getBytesArrayRef(WasmRef.fromGrain(d.digits)) - let mut numDigits = getInt32Value(WasmRef.fromGrain(d.numDigits)) - let mut decimalPoint = getInt32Value(WasmRef.fromGrain(d.decimalPoint)) + let digits = DS.Bytes.getData(DS.Bytes.fromGrain(d.digits)) + let mut numDigits = DS.Int32.fromInt32(d.numDigits) + let mut decimalPoint = DS.Int32.fromInt32(d.decimalPoint) // Short-circuit if the value can only be a literal 0 or infinity. if (numDigits == 0n || decimalPoint < -324n) { @@ -123,7 +122,7 @@ provide let parseLongMantissa = (s: String) => { let n = decimalPoint let shift = getShift(n) Decimal.rightShift(d, shift) - decimalPoint = getInt32Value(WasmRef.fromGrain(d.decimalPoint)) + decimalPoint = DS.Int32.fromInt32(d.decimalPoint) if (decimalPoint < 0n - _DECIMAL_POINT_RANGE) { done = true break @@ -150,7 +149,7 @@ provide let parseLongMantissa = (s: String) => { getShift(0n - decimalPoint) } Decimal.leftShift(d, shift) - decimalPoint = getInt32Value(WasmRef.fromGrain(d.decimalPoint)) + decimalPoint = DS.Int32.fromInt32(d.decimalPoint) if (decimalPoint > _DECIMAL_POINT_RANGE) { done = true break @@ -203,10 +202,7 @@ provide let parseLongMantissa = (s: String) => { use WasmI64.{ (-), (&) } // Zero out all the bits above the explicit mantissa bits. mantissa = mantissa & (1N << _MANTISSA_EXPLICIT_BITS_64) - 1N - { - f: WasmRef.toGrain(newInt64(mantissa)): Int64, - e: WasmRef.toGrain(newInt32(power2)): Int32, - } + { f: DS.Int64.toInt64(mantissa), e: DS.Int32.toInt32(power2) } } } } diff --git a/stdlib/runtime/atof/table.gr b/stdlib/runtime/atof/table.gr index e88e857c5e..5b12aff721 100644 --- a/stdlib/runtime/atof/table.gr +++ b/stdlib/runtime/atof/table.gr @@ -6,7 +6,7 @@ use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmf64" include WasmF64 -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures as DS @unsafe let mut tableRef = WasmRef.fromGrain(void) @@ -19,7 +19,7 @@ provide let get_F64_POWERS10_FAST_PATH = (index: WasmI32) => { ) } use WasmI32.{ (+), (<<), (|) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) let lo = WasmArrayRef.getI8U(data, index + 3n) << 24n | WasmArrayRef.getI8U(data, index + 2n) << 16n | WasmArrayRef.getI8U(data, index + 1n) << 8n @@ -43,7 +43,7 @@ provide let get_POWERS5 = (index: WasmI32) => { ) } use WasmI32.{ (+), (<<), (|) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) let lo = WasmArrayRef.getI8U(data, index + 3n) << 24n | WasmArrayRef.getI8U(data, index + 2n) << 16n | WasmArrayRef.getI8U(data, index + 1n) << 8n diff --git a/stdlib/runtime/atoi/parse.gr b/stdlib/runtime/atoi/parse.gr index 9dbb5a5cbe..13251b3705 100644 --- a/stdlib/runtime/atoi/parse.gr +++ b/stdlib/runtime/atoi/parse.gr @@ -18,7 +18,7 @@ from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/tags" include Tags from "runtime/bigint" include Bigint as BI -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ reducedInteger } @@ -66,21 +66,21 @@ provide let parseInt = (string: String, radix: Number) => { let _INT_MIN = -9223372036854775808N - let stringRef = WasmRef.fromGrain(string) + let stringRef = DS.String.fromGrain(string) // Don't need to process Unicode length since if the string // contains non-ascii characters, it's not a valid integer - let strLen = DataStructures.getStringSize(stringRef) + let strLen = DS.String.getSize(stringRef) - let strData = DataStructures.getStringArrayRef(stringRef) + let strData = DS.String.getData(stringRef) // Our offset within the string we're parsing let mut offset = 0n if ( WasmRef.isGrainHeapValue(WasmRef.fromGrain(radix)) - || DataStructures.untagSimpleNumber(radix) < 2n - || DataStructures.untagSimpleNumber(radix) > 36n + || DS.untagSimpleNumber(radix) < 2n + || DS.untagSimpleNumber(radix) > 36n ) { return Err(ParseIntInvalidRadix) } @@ -102,7 +102,7 @@ provide let parseInt = (string: String, radix: Number) => { char = WasmArrayRef.getI8U(strData, offset) } - let mut radix = WasmI64.extendI32U(DataStructures.untagSimpleNumber(radix)) + let mut radix = WasmI64.extendI32U(DS.untagSimpleNumber(radix)) // Check if we should override the supplied radix if (char == _CHAR_0 && strLen > 2n) { diff --git a/stdlib/runtime/bigint.gr b/stdlib/runtime/bigint.gr index 22bc99663b..c1aeaf374c 100644 --- a/stdlib/runtime/bigint.gr +++ b/stdlib/runtime/bigint.gr @@ -24,8 +24,8 @@ from "runtime/unsafe/wasmf32" include WasmF32 from "runtime/unsafe/wasmf64" include WasmF64 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/exception" include Exception -from "runtime/dataStructures" include DataStructures as DS // things we need which are missing due to --no-pervasives: primitive (!) = "@not" @@ -102,15 +102,15 @@ let lnot = n => { } @unsafe -let init = DS.allocateBigInt +let init = DS.BigInt.make @unsafe -provide let getSize = DS.getBigIntSize +provide let getSize = DS.BigInt.getSize @unsafe -provide let getFlags = DS.getBigIntFlags +provide let getFlags = DS.BigInt.getFlags @unsafe -provide let setFlags = DS.setBigIntFlags +provide let setFlags = DS.BigInt.setFlags @unsafe let flagIsSet = (ref, flag) => { @@ -141,12 +141,12 @@ let setFlag = (ref, flag, value) => { @unsafe provide let getLimb = (ref, i) => { - WasmArrayRef.getI64(DS.getBigIntArrayRef(ref), i) + WasmArrayRef.getI64(DS.BigInt.getLimbData(ref), i) } @unsafe let setLimb = (ref, i, v) => { - WasmArrayRef.setI64(DS.getBigIntArrayRef(ref), i, v) + WasmArrayRef.setI64(DS.BigInt.getLimbData(ref), i, v) } @unsafe @@ -204,9 +204,9 @@ let clone = num => { let size = getSize(num) let ret = init(size) WasmArrayRef.copyI64( - DS.getBigIntArrayRef(ret), + DS.BigInt.getLimbData(ret), 0n, - DS.getBigIntArrayRef(num), + DS.BigInt.getLimbData(num), 0n, size ) @@ -219,9 +219,9 @@ let cloneWithLen = (num, len) => { let oldlen = getSize(num) let ret = init(len) WasmArrayRef.copyI64( - DS.getBigIntArrayRef(ret), + DS.BigInt.getLimbData(ret), 0n, - DS.getBigIntArrayRef(num), + DS.BigInt.getLimbData(num), 0n, if (WasmI32.ltU(oldlen, len)) oldlen else len ) @@ -737,7 +737,7 @@ let _SIZES = [> provide let bigIntToString = (num, base) => { use WasmI32.{ (+), (<), (>) } let getDigit = n => - WasmArrayRef.getI8U(DS.getStringArrayRef(WasmRef.fromGrain(_DIGITS)), n) + WasmArrayRef.getI8U(DS.String.getData(DS.String.fromGrain(_DIGITS)), n) if (base < 2n || base > 32n) { throw Exception.InvalidArgument("toString base must be in range [2,32]") } @@ -827,8 +827,8 @@ provide let bigIntToString = (num, base) => { } } let length = computeLength(result, 0n) - let ret = DS.allocateString(length) - let retArray = DS.getStringArrayRef(ret) + let ret = DS.String.make(length) + let retArray = DS.String.getData(ret) @unsafe let rec populateString = (lst, idx, str) => { match (lst) { @@ -840,7 +840,7 @@ provide let bigIntToString = (num, base) => { } } populateString(result, 0n, retArray) - WasmRef.toGrain(ret): String + DS.String.toGrain(ret) } } @@ -915,9 +915,9 @@ let unsignedAddInt = (num1, int) => { if (WasmI64.eqz(carry) && i < n - 1n) { // if we're not carrying, then there's nothing left to do but copy WasmArrayRef.copyI64( - DS.getBigIntArrayRef(dest), + DS.BigInt.getLimbData(dest), i + 1n, - DS.getBigIntArrayRef(num1), + DS.BigInt.getLimbData(num1), i + 1n, num1Limbs - (i + 1n) ) @@ -1014,9 +1014,9 @@ let unsignedSubIntImpl = (num1, int) => { if (WasmI64.eqz(carry) && i < n - 1n) { // if we're not carrying, then there's nothing left to do but copy WasmArrayRef.copyI64( - DS.getBigIntArrayRef(dest), + DS.BigInt.getLimbData(dest), i + 1n, - DS.getBigIntArrayRef(num1), + DS.BigInt.getLimbData(num1), i + 1n, num1Limbs - (i + 1n) ) @@ -1590,7 +1590,7 @@ provide let popcnt = num => { let limbtz = WasmI64.popcnt(limb) ret += limbtz } - Some(WasmRef.toGrain(DS.newInt64(ret)): Int64) + Some(DS.Int64.toInt64(ret)) } } diff --git a/stdlib/runtime/compare.gr b/stdlib/runtime/compare.gr index ce2fe6eb00..7a0ac76f34 100644 --- a/stdlib/runtime/compare.gr +++ b/stdlib/runtime/compare.gr @@ -22,8 +22,7 @@ from "runtime/unsafe/wasmf32" include WasmF32 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/tags" include Tags -from "runtime/dataStructures" include DataStructures -use DataStructures.{ tagSimpleNumber, untagSimpleNumber } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ isNumber, cmp as numberCompare } @@ -37,14 +36,14 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { match (heapTag) { t when t == Tags._GRAIN_ADT_HEAP_TAG => { // Check if the same constructor variant - let xvariant = untagSimpleNumber(DataStructures.loadAdtVariant(xRef)) - let yvariant = untagSimpleNumber(DataStructures.loadAdtVariant(yRef)) + let xvariant = DS.untagSimpleNumber(DS.ADT.getVariantId(xRef)) + let yvariant = DS.untagSimpleNumber(DS.ADT.getVariantId(yRef)) if (xvariant != yvariant) { - return tagSimpleNumber(xvariant - yvariant) + return DS.tagSimpleNumber(xvariant - yvariant) } - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.ADT.getData(xRef) + let yarray = DS.ADT.getData(yRef) let xarity = WasmArrayRef.length(xarray) let yarity = WasmArrayRef.length(yarray) @@ -54,7 +53,7 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { WasmArrayRef.getAny(xarray, i), WasmArrayRef.getAny(yarray, i) ) - if (untagSimpleNumber(sub) != 0n) { + if (DS.untagSimpleNumber(sub) != 0n) { return sub } } @@ -62,8 +61,8 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { return 0 }, t when t == Tags._GRAIN_RECORD_HEAP_TAG => { - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.Record.getData(xRef) + let yarray = DS.Record.getData(yRef) let xlength = WasmArrayRef.length(xarray) let ylength = WasmArrayRef.length(yarray) @@ -73,7 +72,7 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { WasmArrayRef.getAny(xarray, i), WasmArrayRef.getAny(yarray, i) ) - if (untagSimpleNumber(sub) != 0n) { + if (DS.untagSimpleNumber(sub) != 0n) { return sub } } @@ -81,22 +80,22 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { return 0 }, t when t == Tags._GRAIN_ARRAY_HEAP_TAG => { - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.Array.getData(WasmRef.toGrain(xRef): DS.Array.ArrayRef) + let yarray = DS.Array.getData(WasmRef.toGrain(yRef): DS.Array.ArrayRef) let xlength = WasmArrayRef.length(xarray) let ylength = WasmArrayRef.length(yarray) // Check if the same length if (xlength != ylength) { - return tagSimpleNumber(xlength - ylength) + return DS.tagSimpleNumber(xlength - ylength) } for (let mut i = 0n; i < xlength; i += 1n) { let sub = compareHelp( WasmArrayRef.getAny(xarray, i), WasmArrayRef.getAny(yarray, i) ) - if (untagSimpleNumber(sub) != 0n) { + if (DS.untagSimpleNumber(sub) != 0n) { return sub } } @@ -104,52 +103,52 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { return 0 }, t when t == Tags._GRAIN_STRING_HEAP_TAG => { - let xarray = DataStructures.getStringArrayRef(xRef) - let yarray = DataStructures.getStringArrayRef(yRef) + let xarray = DS.String.getData(WasmRef.toGrain(xRef): DS.String.StringRef) + let yarray = DS.String.getData(WasmRef.toGrain(yRef): DS.String.StringRef) let xlength = WasmArrayRef.length(xarray) let ylength = WasmArrayRef.length(yarray) if (xlength == ylength) { - return tagSimpleNumber( + return DS.tagSimpleNumber( WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, xlength) ) } if (xlength < ylength) { let sub = WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, xlength) // The shorter one comes first - return if (sub == 0n) -1 else tagSimpleNumber(sub) + return if (sub == 0n) -1 else DS.tagSimpleNumber(sub) } else { let sub = WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, ylength) // The shorter one comes first - return if (sub == 0n) 1 else tagSimpleNumber(sub) + return if (sub == 0n) 1 else DS.tagSimpleNumber(sub) } }, t when t == Tags._GRAIN_BYTES_HEAP_TAG => { - let xarray = DataStructures.getBytesArrayRef(xRef) - let yarray = DataStructures.getBytesArrayRef(yRef) + let xarray = DS.Bytes.getData(WasmRef.toGrain(xRef): DS.Bytes.BytesRef) + let yarray = DS.Bytes.getData(WasmRef.toGrain(yRef): DS.Bytes.BytesRef) let xlength = WasmArrayRef.length(xarray) let ylength = WasmArrayRef.length(yarray) if (xlength == ylength) { - return tagSimpleNumber( + return DS.tagSimpleNumber( WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, xlength) ) } if (xlength < ylength) { let sub = WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, xlength) // The shorter one comes first - return if (sub == 0n) -1 else tagSimpleNumber(sub) + return if (sub == 0n) -1 else DS.tagSimpleNumber(sub) } else { let sub = WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, ylength) // The shorter one comes first - return if (sub == 0n) 1 else tagSimpleNumber(sub) + return if (sub == 0n) 1 else DS.tagSimpleNumber(sub) } }, t when t == Tags._GRAIN_TUPLE_HEAP_TAG => { - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.Tuple.getData(WasmRef.toGrain(xRef): DS.Tuple.TupleRef) + let yarray = DS.Tuple.getData(WasmRef.toGrain(yRef): DS.Tuple.TupleRef) let xsize = WasmArrayRef.length(xarray) let ysize = WasmArrayRef.length(yarray) @@ -159,7 +158,7 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { WasmArrayRef.getAny(xarray, i), WasmArrayRef.getAny(yarray, i) ) - if (untagSimpleNumber(sub) != 0n) { + if (DS.untagSimpleNumber(sub) != 0n) { return sub } } @@ -167,8 +166,8 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { return 0 }, t when t == Tags._GRAIN_INT32_HEAP_TAG => { - let xval = DataStructures.getInt32Value(xRef) - let yval = DataStructures.getInt32Value(yRef) + let xval = DS.Int32.getValue(xRef) + let yval = DS.Int32.getValue(yRef) return if (xval < yval) { -1 } else if (xval > yval) { @@ -179,8 +178,8 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { }, // Float32 is handled by compareHelp directly t when t == Tags._GRAIN_UINT32_HEAP_TAG => { - let xval = DataStructures.getUint32Value(xRef) - let yval = DataStructures.getUint32Value(yRef) + let xval = DS.Uint32.getValue(xRef) + let yval = DS.Uint32.getValue(yRef) return if (WasmI32.ltU(xval, yval)) { -1 } else if (WasmI32.gtU(xval, yval)) { @@ -190,8 +189,8 @@ let rec heapCompareHelp = (heapTag, xRef: WasmRef, yRef: WasmRef) => { } }, t when t == Tags._GRAIN_UINT64_HEAP_TAG => { - let xval = DataStructures.getUint64Value(xRef) - let yval = DataStructures.getUint64Value(yRef) + let xval = DS.Uint64.getValue(xRef) + let yval = DS.Uint64.getValue(yRef) return if (WasmI64.ltU(xval, yval)) { -1 } else if (WasmI64.gtU(xval, yval)) { @@ -228,7 +227,7 @@ and compareHelp = (x: WasmRef, y: WasmRef) => { shortValTag == Tags._GRAIN_INT8_TAG_MASK || shortValTag == Tags._GRAIN_INT16_TAG_MASK ) { - tagSimpleNumber(xi32 - yi32) + DS.tagSimpleNumber(xi32 - yi32) } else { if (WasmI32.ltU(xi32, yi32)) { -1 @@ -250,11 +249,11 @@ and compareHelp = (x: WasmRef, y: WasmRef) => { } } else if (isNumber(x)) { // Numbers have special comparison rules, e.g. NaN == NaN - tagSimpleNumber(numberCompare(x, y)) - } else if (DataStructures.loadValueTag(x) == Tags._GRAIN_FLOAT32_HEAP_TAG) { + DS.tagSimpleNumber(numberCompare(x, y)) + } else if (DS.getHeapTag(x) == Tags._GRAIN_FLOAT32_HEAP_TAG) { // Short circuit for Float32 to correctly handle NaN comparisons - let xval = DataStructures.getFloat32Value(x) - let yval = DataStructures.getFloat32Value(y) + let xval = DS.Float32.getValue(x) + let yval = DS.Float32.getValue(y) use WasmF32.{ (!=) } let xIsNaN = xval != xval let yIsNaN = yval != yval @@ -279,7 +278,7 @@ and compareHelp = (x: WasmRef, y: WasmRef) => { if (x is y) { 0 } else { - heapCompareHelp(DataStructures.loadValueTag(x), x, y) + heapCompareHelp(DS.getHeapTag(x), x, y) } } } diff --git a/stdlib/runtime/dataStructures.gr b/stdlib/runtime/dataStructures.gr deleted file mode 100644 index 8bfd6f99cc..0000000000 --- a/stdlib/runtime/dataStructures.gr +++ /dev/null @@ -1,447 +0,0 @@ -@runtimeMode -module DataStructures - -from "runtime/unsafe/wasmref" include WasmRef -use WasmRef.{ module WasmArrayRef } - -/** - * Allocates a new Grain array. - * - * @param size: The number of elements to be contained in this array - * @param initial: The initial value to fill the array with - * @returns The reference to the array - */ -provide primitive allocateArray = "@allocate.array" - -/** - * Allocates a new Grain tuple. - * - * @param size: The number of elements to be contained in this tuple - * @returns The reference to the tuple - */ -provide primitive allocateTuple = "@allocate.tuple" - -/** - * Allocates a new Grain bytes. - * - * @param size: The number of bytes to be contained in this buffer - * @returns The reference to the bytes - */ -provide primitive allocateBytes = "@allocate.bytes" - -primitive getBytesArrayRef = "@bytes.refarray" -/** - * Gets the array backing a Grain Bytes. - * - * @param ref: The Bytes ref - * @returns The reference to the array - */ -provide let getBytesArrayRef = ref => - WasmArrayRef.fromWasmRef(getBytesArrayRef(ref)) - -/** - * Gets the size of the array backing a Grain Bytes. - * - * @param ref: The Bytes - * @returns The size of the Bytes - */ -provide primitive getBytesSize = "@bytes.size" - -/** - * Allocates a new Grain string. - * - * @param size: The size (in bytes) of the string to allocate - * @returns The reference to the string - */ -provide primitive allocateString = "@allocate.string" - -primitive getStringArrayRef = "@string.refarray" -/** - * Gets the array backing a Grain string. - * - * @param ref: The string ref - * @returns The reference to the array - */ -provide let getStringArrayRef = ref => - WasmArrayRef.fromWasmRef(getStringArrayRef(ref)) - -/** - * Gets the size of the array backing a Grain string. - * - * @param ref: The string - * @returns The size of the string - */ -provide primitive getStringSize = "@string.size" - -primitive getCompoundValueArrayRef = "@compound_value.refarray" -/** - * Gets the array backing a compound Grain value. - * - * @param ref: The value ref - * @returns The reference to the array - */ -provide let getCompoundValueArrayRef = ref => - WasmArrayRef.fromWasmRef(getCompoundValueArrayRef(ref)) - -/** - * Gets a compound Grain value's cycle marker. - * - * @param ref: The value ref - * @returns The cycle marker - */ -provide primitive loadCycleMarker = "@grain_value.load_cycle_marker" - -/** - * Sets a compound Grain value's cycle marker. - * - * @param ref: The value ref - * @param value: The cycle marker to set - */ -provide primitive storeCycleMarker = "@grain_value.store_cycle_marker" - -/** - * Gets a record's type hash. - * - * @param ref: The value ref - * @returns: The type hash - */ -provide primitive loadRecordTypeHash = "@record.load_typehash" - -/** - * Gets a variant's type hash. - * - * @param ref: The value ref - * @returns: The type hash - */ -provide primitive loadVariantTypeHash = "@adt.load_typehash" - -/** - * Gets a record's type id. - * - * @param ref: The value ref - * @returns: The type id - */ -provide primitive loadRecordTypeId = "@record.load_typeid" - -/** - * Gets a variant's type id. - * - * @param ref: The value ref - * @returns: The type id - */ -provide primitive loadVariantTypeId = "@adt.load_typeid" - -// ADT/RECORD - -/** - * Allocates a new ADT (enum variant) value. - * - * @param valuesArray: The WasmArrayRef backing the ADT's values - * @param typeHash: The type hash - * @param typeId: The type id - * @param variantId: The variant id - * @returns The reference to the ADT - */ -provide primitive allocateAdt = "@allocate.adt" - -/** - * Allocates a new Record value. - * - * @param valuesArray: The WasmArrayRef backing the record's fields - * @param typeHash: The type hash - * @param typeId: The type id - * @returns The reference to the record - */ -provide primitive allocateRecord = "@allocate.record" - -// INT32/INT64 - -/** - * Allocates a new Int32 with a prepopulated value - * - * @param int: The value to store - * @returns The reference to the Int32 - */ -provide primitive newInt32 = "@new.int32" - -/** - * Allocates a new Uint32 with a prepopulated value - * - * @param int: The value to store - * @returns The reference to the Uint32 - */ -provide primitive newUint32 = "@new.uint32" - -/** - * Allocates a new Int64 with a prepopulated value - * - * @param int: The value to store - * @returns The reference to the Int64 - */ -provide primitive newInt64 = "@new.int64" - -/** - * Allocates a new Uint64 with a prepopulated value - * - * @param int: The value to store - * @returns The reference to the Uint64 - */ -provide primitive newUint64 = "@new.uint64" - -// FLOATS - -/** - * Allocates a new Float32 with a prepopulated value - * - * @param float: The value to store - * @returns the reference to the Float32 - */ -provide primitive newFloat32 = "@new.float32" - -/** - * Allocates a new Float64 with a prepopulated value - * - * @param float: The value to store - * @returns The reference to the Float64 - */ -provide primitive newFloat64 = "@new.float64" - -// RATIONALS - -/** - * Allocates a new Rational with a prepopulated value - * - * @param numerator: The numerator value to store - * @param denominator: The denominator value to store - * @returns The reference to the Rational - */ -provide primitive newRational = "@new.rational" - -// BIGINTS - -/** - * Allocates a new BigInt with the specified number of limbs - * - * @param size: The number of limbs - * @returns The reference to the BigInt - */ -provide primitive allocateBigInt = "@allocate.bigint" - -/** - * Gets number of limbs of a BigInt. - * - * @param ref: The BigInt ref - * @returns The number of BigInt limbs - */ -provide primitive getBigIntSize = "@bigint.size" - -/** - * Gets the BigInt flags. - * - * @param ref: The BigInt ref - * @returns The BigInt flags - */ -provide primitive getBigIntFlags = "@bigint.flags" - -/** - * Sets the BigInt flags. - * - * @param ref: The BigInt ref - * @param flags: The BigInt flags to set - */ -provide primitive setBigIntFlags = "@bigint.set_flags" - -primitive getBigIntArrayRef = "@bigint.refarray" -/** - * Gets the array backing a BigInt. - * - * @param ref: The BigInt ref - * @returns The reference to the BigInt limbs - */ -provide let getBigIntArrayRef = ref => - WasmArrayRef.fromWasmRef(getBigIntArrayRef(ref)) - -// BOXED NUMBERS - -/** - * Gets the tag of a Number. - * - * @param ref: The Number ref - * @returns The boxed Number tag - */ -provide primitive getNumberTag = "@boxed_number.get_tag" - -/** - * Gets the value of a boxed Int32 - * - * @param ref: The Number ref - * @returns The inner i32 - */ -provide primitive getInt32Value = "@boxed_number.get_int32" - -/** - * Gets the value of a boxed Uint32 - * - * @param ref: The Number ref - * @returns The inner i32 - */ -provide primitive getUint32Value = "@boxed_number.get_uint32" - -/** - * Gets the value of a boxed Float32 - * - * @param ref: The Number ref - * @returns The inner f32 - */ -provide primitive getFloat32Value = "@boxed_number.get_float32" - -/** - * Gets the value of a boxed Int64 - * - * @param ref: The Number ref - * @returns The inner i64 - */ -provide primitive getInt64Value = "@boxed_number.get_int64" -/** - * Gets the value of a boxed Uint64 - * - * @param ref: The Number ref - * @returns The inner i64 - */ -provide primitive getUint64Value = "@boxed_number.get_uint64" - -/** - * Gets the value of a boxed Float64 - * - * @param ref: The Number ref - * @returns The inner f64 - */ -provide primitive getFloat64Value = "@boxed_number.get_float64" - -/** - * Gets the numerator of a boxed Rational - * - * @param ref: The Number ref - * @returns The rational numerator - */ -provide primitive getRationalNumerator = "@boxed_number.get_rational_numerator" - -/** - * Gets the denominator of a boxed Rational - * - * @param ref: The Number ref - * @returns The rational denominator - */ -provide primitive getRationalDenominator = "@boxed_number.get_rational_denominator" - -/** - * Load the variant id of an ADT. - * - * @param ref: The reference to the ADT - * @returns The ADT variant id - */ -provide primitive loadAdtVariant = "@adt.load_variant" - -/** - * Load Grain heap value's tag. - * - * @param ref: The reference to the value - * @returns The value tag - */ -provide primitive loadValueTag = "@grain_value.load_tag" - -/** - * Tag a simple number. - * - * @param num: The number to tag - * @returns The tagged number - */ -provide primitive tagSimpleNumber = "@tag.simple_number" - -/** - * Untag a simple number. - * - * @param num: The number to untag - * @returns The untagged number - */ -provide primitive untagSimpleNumber = "@untag.simple_number" - -/** - * Tag a char. - * - * @param char: The usv to tag - * @returns The tagged char - */ -provide primitive tagChar = "@tag.char" - -/** - * Untag a char. - * - * @param char: The char to untag - * @returns The untagged usv - */ -provide primitive untagChar = "@untag.char" - -/** - * Tag an int8. - * - * @param int: The int8 to tag - * @returns The tagged int8 - */ -provide primitive tagInt8 = "@tag.int8" - -/** - * Untag an int8. - * - * @param int: The int8 to untag - * @returns The untagged int8 - */ -provide primitive untagInt8 = "@untag.int8" - -/** - * Tag an int16. - * - * @param int: The int16 to tag - * @returns The tagged int16 - */ -provide primitive tagInt16 = "@tag.int16" - -/** - * Untag an int16. - * - * @param int: The int16 to untag - * @returns The untagged int16 - */ -provide primitive untagInt16 = "@untag.int16" - -/** - * Tag a uint8. - * - * @param int: The uint8 to tag - * @returns The tagged uint8 - */ -provide primitive tagUint8 = "@tag.uint8" - -/** - * Untag a uint8. - * - * @param int: The uint8 to untag - * @returns The untagged uint8 - */ -provide primitive untagUint8 = "@untag.uint8" - -/** - * Tag a uint16. - * - * @param int: The uint16 to tag - * @returns The tagged uint16 - */ -provide primitive tagUint16 = "@tag.uint16" - -/** - * Untag a uint16. - * - * @param int: The uint16 to untag - * @returns The untagged uint16 - */ -provide primitive untagUint16 = "@untag.uint16" diff --git a/stdlib/runtime/dataStructures.md b/stdlib/runtime/dataStructures.md deleted file mode 100644 index 0dab17e7ee..0000000000 --- a/stdlib/runtime/dataStructures.md +++ /dev/null @@ -1,1048 +0,0 @@ ---- -title: DataStructures ---- - -## Values - -Functions and constants included in the DataStructures module. - -### DataStructures.**allocateArray** - -```grain -allocateArray: (size: WasmI32, initial: WasmRef) => WasmRef -``` - -Allocates a new Grain array. - -Parameters: - -| param | type | description | -| --------- | --------- | ---------------------------------------------------- | -| `size` | `WasmI32` | The number of elements to be contained in this array | -| `initial` | `WasmRef` | The initial value to fill the array with | - -Returns: - -| type | description | -| --------- | -------------------------- | -| `WasmRef` | The reference to the array | - -### DataStructures.**allocateTuple** - -```grain -allocateTuple: (size: WasmI32) => WasmRef -``` - -Allocates a new Grain tuple. - -Parameters: - -| param | type | description | -| ------ | --------- | ---------------------------------------------------- | -| `size` | `WasmI32` | The number of elements to be contained in this tuple | - -Returns: - -| type | description | -| --------- | -------------------------- | -| `WasmRef` | The reference to the tuple | - -### DataStructures.**allocateBytes** - -```grain -allocateBytes: (size: WasmI32) => WasmRef -``` - -Allocates a new Grain bytes. - -Parameters: - -| param | type | description | -| ------ | --------- | -------------------------------------------------- | -| `size` | `WasmI32` | The number of bytes to be contained in this buffer | - -Returns: - -| type | description | -| --------- | -------------------------- | -| `WasmRef` | The reference to the bytes | - -### DataStructures.**getBytesArrayRef** - -```grain -getBytesArrayRef: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef -``` - -Gets the array backing a Grain Bytes. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------- | -| `ref` | `WasmRef` | The Bytes ref | - -Returns: - -| type | description | -| ----------------------------------- | -------------------------- | -| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the array | - -### DataStructures.**getBytesSize** - -```grain -getBytesSize: (ref: WasmRef) => WasmI32 -``` - -Gets the size of the array backing a Grain Bytes. - -Parameters: - -| param | type | description | -| ----- | --------- | ----------- | -| `ref` | `WasmRef` | The Bytes | - -Returns: - -| type | description | -| --------- | --------------------- | -| `WasmI32` | The size of the Bytes | - -### DataStructures.**allocateString** - -```grain -allocateString: (size: WasmI32) => WasmRef -``` - -Allocates a new Grain string. - -Parameters: - -| param | type | description | -| ------ | --------- | --------------------------------------------- | -| `size` | `WasmI32` | The size (in bytes) of the string to allocate | - -Returns: - -| type | description | -| --------- | --------------------------- | -| `WasmRef` | The reference to the string | - -### DataStructures.**getStringArrayRef** - -```grain -getStringArrayRef: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef -``` - -Gets the array backing a Grain string. - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The string ref | - -Returns: - -| type | description | -| ----------------------------------- | -------------------------- | -| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the array | - -### DataStructures.**getStringSize** - -```grain -getStringSize: (ref: WasmRef) => WasmI32 -``` - -Gets the size of the array backing a Grain string. - -Parameters: - -| param | type | description | -| ----- | --------- | ----------- | -| `ref` | `WasmRef` | The string | - -Returns: - -| type | description | -| --------- | ---------------------- | -| `WasmI32` | The size of the string | - -### DataStructures.**getCompoundValueArrayRef** - -```grain -getCompoundValueArrayRef: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef -``` - -Gets the array backing a compound Grain value. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------- | -| `ref` | `WasmRef` | The value ref | - -Returns: - -| type | description | -| ----------------------------------- | -------------------------- | -| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the array | - -### DataStructures.**loadCycleMarker** - -```grain -loadCycleMarker: (ref: WasmRef) => WasmI32 -``` - -Gets a compound Grain value's cycle marker. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------- | -| `ref` | `WasmRef` | The value ref | - -Returns: - -| type | description | -| --------- | ---------------- | -| `WasmI32` | The cycle marker | - -### DataStructures.**storeCycleMarker** - -```grain -storeCycleMarker: (ref: WasmRef, value: WasmI32) => Void -``` - -Sets a compound Grain value's cycle marker. - -Parameters: - -| param | type | description | -| ------- | --------- | ----------------------- | -| `ref` | `WasmRef` | The value ref | -| `value` | `WasmI32` | The cycle marker to set | - -### DataStructures.**loadRecordTypeHash** - -```grain -loadRecordTypeHash: (ref: WasmRef) => Number -``` - -Gets a record's type hash. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------- | -| `ref` | `WasmRef` | The value ref | - -Returns: - -| type | description | -| -------- | --------------- | -| `Number` | : The type hash | - -### DataStructures.**loadVariantTypeHash** - -```grain -loadVariantTypeHash: (ref: WasmRef) => Number -``` - -Gets a variant's type hash. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------- | -| `ref` | `WasmRef` | The value ref | - -Returns: - -| type | description | -| -------- | --------------- | -| `Number` | : The type hash | - -### DataStructures.**loadRecordTypeId** - -```grain -loadRecordTypeId: (ref: WasmRef) => Number -``` - -Gets a record's type id. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------- | -| `ref` | `WasmRef` | The value ref | - -Returns: - -| type | description | -| -------- | ------------- | -| `Number` | : The type id | - -### DataStructures.**loadVariantTypeId** - -```grain -loadVariantTypeId: (ref: WasmRef) => Number -``` - -Gets a variant's type id. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------- | -| `ref` | `WasmRef` | The value ref | - -Returns: - -| type | description | -| -------- | ------------- | -| `Number` | : The type id | - -### DataStructures.**allocateAdt** - -```grain -allocateAdt: - (valuesArray: WasmRef, typeHash: WasmRef, typeId: WasmRef, - variantId: WasmRef) => WasmRef -``` - -Allocates a new ADT (enum variant) value. - -Parameters: - -| param | type | description | -| ------------- | --------- | ----------------------------------------- | -| `valuesArray` | `WasmRef` | The WasmArrayRef backing the ADT's values | -| `typeHash` | `WasmRef` | The type hash | -| `typeId` | `WasmRef` | The type id | -| `variantId` | `WasmRef` | The variant id | - -Returns: - -| type | description | -| --------- | ------------------------ | -| `WasmRef` | The reference to the ADT | - -### DataStructures.**allocateRecord** - -```grain -allocateRecord: - (valuesArray: WasmRef, typeHash: WasmRef, typeId: WasmRef) => WasmRef -``` - -Allocates a new Record value. - -Parameters: - -| param | type | description | -| ------------- | --------- | -------------------------------------------- | -| `valuesArray` | `WasmRef` | The WasmArrayRef backing the record's fields | -| `typeHash` | `WasmRef` | The type hash | -| `typeId` | `WasmRef` | The type id | - -Returns: - -| type | description | -| --------- | --------------------------- | -| `WasmRef` | The reference to the record | - -### DataStructures.**newInt32** - -```grain -newInt32: (int: WasmI32) => WasmRef -``` - -Allocates a new Int32 with a prepopulated value - -Parameters: - -| param | type | description | -| ----- | --------- | ------------------ | -| `int` | `WasmI32` | The value to store | - -Returns: - -| type | description | -| --------- | -------------------------- | -| `WasmRef` | The reference to the Int32 | - -### DataStructures.**newUint32** - -```grain -newUint32: (int: WasmI32) => WasmRef -``` - -Allocates a new Uint32 with a prepopulated value - -Parameters: - -| param | type | description | -| ----- | --------- | ------------------ | -| `int` | `WasmI32` | The value to store | - -Returns: - -| type | description | -| --------- | --------------------------- | -| `WasmRef` | The reference to the Uint32 | - -### DataStructures.**newInt64** - -```grain -newInt64: (int: WasmI64) => WasmRef -``` - -Allocates a new Int64 with a prepopulated value - -Parameters: - -| param | type | description | -| ----- | --------- | ------------------ | -| `int` | `WasmI64` | The value to store | - -Returns: - -| type | description | -| --------- | -------------------------- | -| `WasmRef` | The reference to the Int64 | - -### DataStructures.**newUint64** - -```grain -newUint64: (int: WasmI64) => WasmRef -``` - -Allocates a new Uint64 with a prepopulated value - -Parameters: - -| param | type | description | -| ----- | --------- | ------------------ | -| `int` | `WasmI64` | The value to store | - -Returns: - -| type | description | -| --------- | --------------------------- | -| `WasmRef` | The reference to the Uint64 | - -### DataStructures.**newFloat32** - -```grain -newFloat32: (float: WasmF32) => WasmRef -``` - -Allocates a new Float32 with a prepopulated value - -Parameters: - -| param | type | description | -| ------- | --------- | ------------------ | -| `float` | `WasmF32` | The value to store | - -Returns: - -| type | description | -| --------- | ---------------------------- | -| `WasmRef` | the reference to the Float32 | - -### DataStructures.**newFloat64** - -```grain -newFloat64: (float: WasmF64) => WasmRef -``` - -Allocates a new Float64 with a prepopulated value - -Parameters: - -| param | type | description | -| ------- | --------- | ------------------ | -| `float` | `WasmF64` | The value to store | - -Returns: - -| type | description | -| --------- | ---------------------------- | -| `WasmRef` | The reference to the Float64 | - -### DataStructures.**newRational** - -```grain -newRational: (numerator: WasmRef, denominator: WasmRef) => WasmRef -``` - -Allocates a new Rational with a prepopulated value - -Parameters: - -| param | type | description | -| ------------- | --------- | ------------------------------ | -| `numerator` | `WasmRef` | The numerator value to store | -| `denominator` | `WasmRef` | The denominator value to store | - -Returns: - -| type | description | -| --------- | ----------------------------- | -| `WasmRef` | The reference to the Rational | - -### DataStructures.**allocateBigInt** - -```grain -allocateBigInt: (size: WasmI32) => WasmRef -``` - -Allocates a new BigInt with the specified number of limbs - -Parameters: - -| param | type | description | -| ------ | --------- | ------------------- | -| `size` | `WasmI32` | The number of limbs | - -Returns: - -| type | description | -| --------- | --------------------------- | -| `WasmRef` | The reference to the BigInt | - -### DataStructures.**getBigIntSize** - -```grain -getBigIntSize: (ref: WasmRef) => WasmI32 -``` - -Gets number of limbs of a BigInt. - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The BigInt ref | - -Returns: - -| type | description | -| --------- | -------------------------- | -| `WasmI32` | The number of BigInt limbs | - -### DataStructures.**getBigIntFlags** - -```grain -getBigIntFlags: (ref: WasmRef) => WasmI32 -``` - -Gets the BigInt flags. - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The BigInt ref | - -Returns: - -| type | description | -| --------- | ---------------- | -| `WasmI32` | The BigInt flags | - -### DataStructures.**setBigIntFlags** - -```grain -setBigIntFlags: (ref: WasmRef, flags: WasmI32) => Void -``` - -Sets the BigInt flags. - -Parameters: - -| param | type | description | -| ------- | --------- | ----------------------- | -| `ref` | `WasmRef` | The BigInt ref | -| `flags` | `WasmI32` | The BigInt flags to set | - -### DataStructures.**getBigIntArrayRef** - -```grain -getBigIntArrayRef: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef -``` - -Gets the array backing a BigInt. - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The BigInt ref | - -Returns: - -| type | description | -| ----------------------------------- | --------------------------------- | -| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the BigInt limbs | - -### DataStructures.**getNumberTag** - -```grain -getNumberTag: (ref: WasmRef) => WasmI32 -``` - -Gets the tag of a Number. - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | -------------------- | -| `WasmI32` | The boxed Number tag | - -### DataStructures.**getInt32Value** - -```grain -getInt32Value: (ref: WasmRef) => WasmI32 -``` - -Gets the value of a boxed Int32 - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ------------- | -| `WasmI32` | The inner i32 | - -### DataStructures.**getUint32Value** - -```grain -getUint32Value: (ref: WasmRef) => WasmI32 -``` - -Gets the value of a boxed Uint32 - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ------------- | -| `WasmI32` | The inner i32 | - -### DataStructures.**getFloat32Value** - -```grain -getFloat32Value: (ref: WasmRef) => WasmF32 -``` - -Gets the value of a boxed Float32 - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ------------- | -| `WasmF32` | The inner f32 | - -### DataStructures.**getInt64Value** - -```grain -getInt64Value: (ref: WasmRef) => WasmI64 -``` - -Gets the value of a boxed Int64 - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ------------- | -| `WasmI64` | The inner i64 | - -### DataStructures.**getUint64Value** - -```grain -getUint64Value: (ref: WasmRef) => WasmI64 -``` - -Gets the value of a boxed Uint64 - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ------------- | -| `WasmI64` | The inner i64 | - -### DataStructures.**getFloat64Value** - -```grain -getFloat64Value: (ref: WasmRef) => WasmF64 -``` - -Gets the value of a boxed Float64 - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ------------- | -| `WasmF64` | The inner f64 | - -### DataStructures.**getRationalNumerator** - -```grain -getRationalNumerator: (ref: WasmRef) => WasmRef -``` - -Gets the numerator of a boxed Rational - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ---------------------- | -| `WasmRef` | The rational numerator | - -### DataStructures.**getRationalDenominator** - -```grain -getRationalDenominator: (ref: WasmRef) => WasmRef -``` - -Gets the denominator of a boxed Rational - -Parameters: - -| param | type | description | -| ----- | --------- | -------------- | -| `ref` | `WasmRef` | The Number ref | - -Returns: - -| type | description | -| --------- | ------------------------ | -| `WasmRef` | The rational denominator | - -### DataStructures.**loadAdtVariant** - -```grain -loadAdtVariant: (ref: WasmRef) => Number -``` - -Load the variant id of an ADT. - -Parameters: - -| param | type | description | -| ----- | --------- | ------------------------ | -| `ref` | `WasmRef` | The reference to the ADT | - -Returns: - -| type | description | -| -------- | ------------------ | -| `Number` | The ADT variant id | - -### DataStructures.**loadValueTag** - -```grain -loadValueTag: (ref: WasmRef) => WasmI32 -``` - -Load Grain heap value's tag. - -Parameters: - -| param | type | description | -| ----- | --------- | -------------------------- | -| `ref` | `WasmRef` | The reference to the value | - -Returns: - -| type | description | -| --------- | ------------- | -| `WasmI32` | The value tag | - -### DataStructures.**tagSimpleNumber** - -```grain -tagSimpleNumber: (num: WasmI32) => Number -``` - -Tag a simple number. - -Parameters: - -| param | type | description | -| ----- | --------- | ----------------- | -| `num` | `WasmI32` | The number to tag | - -Returns: - -| type | description | -| -------- | ----------------- | -| `Number` | The tagged number | - -### DataStructures.**untagSimpleNumber** - -```grain -untagSimpleNumber: (num: Number) => WasmI32 -``` - -Untag a simple number. - -Parameters: - -| param | type | description | -| ----- | -------- | ------------------- | -| `num` | `Number` | The number to untag | - -Returns: - -| type | description | -| --------- | ------------------- | -| `WasmI32` | The untagged number | - -### DataStructures.**tagChar** - -```grain -tagChar: (char: WasmI32) => Char -``` - -Tag a char. - -Parameters: - -| param | type | description | -| ------ | --------- | -------------- | -| `char` | `WasmI32` | The usv to tag | - -Returns: - -| type | description | -| ------ | --------------- | -| `Char` | The tagged char | - -### DataStructures.**untagChar** - -```grain -untagChar: (char: Char) => WasmI32 -``` - -Untag a char. - -Parameters: - -| param | type | description | -| ------ | ------ | ----------------- | -| `char` | `Char` | The char to untag | - -Returns: - -| type | description | -| --------- | ---------------- | -| `WasmI32` | The untagged usv | - -### DataStructures.**tagInt8** - -```grain -tagInt8: (int: WasmI32) => Int8 -``` - -Tag an int8. - -Parameters: - -| param | type | description | -| ----- | --------- | --------------- | -| `int` | `WasmI32` | The int8 to tag | - -Returns: - -| type | description | -| ------ | --------------- | -| `Int8` | The tagged int8 | - -### DataStructures.**untagInt8** - -```grain -untagInt8: (int: Int8) => WasmI32 -``` - -Untag an int8. - -Parameters: - -| param | type | description | -| ----- | ------ | ----------------- | -| `int` | `Int8` | The int8 to untag | - -Returns: - -| type | description | -| --------- | ----------------- | -| `WasmI32` | The untagged int8 | - -### DataStructures.**tagInt16** - -```grain -tagInt16: (int: WasmI32) => Int16 -``` - -Tag an int16. - -Parameters: - -| param | type | description | -| ----- | --------- | ---------------- | -| `int` | `WasmI32` | The int16 to tag | - -Returns: - -| type | description | -| ------- | ---------------- | -| `Int16` | The tagged int16 | - -### DataStructures.**untagInt16** - -```grain -untagInt16: (int: Int16) => WasmI32 -``` - -Untag an int16. - -Parameters: - -| param | type | description | -| ----- | ------- | ------------------ | -| `int` | `Int16` | The int16 to untag | - -Returns: - -| type | description | -| --------- | ------------------ | -| `WasmI32` | The untagged int16 | - -### DataStructures.**tagUint8** - -```grain -tagUint8: (int: WasmI32) => Uint8 -``` - -Tag a uint8. - -Parameters: - -| param | type | description | -| ----- | --------- | ---------------- | -| `int` | `WasmI32` | The uint8 to tag | - -Returns: - -| type | description | -| ------- | ---------------- | -| `Uint8` | The tagged uint8 | - -### DataStructures.**untagUint8** - -```grain -untagUint8: (int: Uint8) => WasmI32 -``` - -Untag a uint8. - -Parameters: - -| param | type | description | -| ----- | ------- | ------------------ | -| `int` | `Uint8` | The uint8 to untag | - -Returns: - -| type | description | -| --------- | ------------------ | -| `WasmI32` | The untagged uint8 | - -### DataStructures.**tagUint16** - -```grain -tagUint16: (int: WasmI32) => Uint16 -``` - -Tag a uint16. - -Parameters: - -| param | type | description | -| ----- | --------- | ----------------- | -| `int` | `WasmI32` | The uint16 to tag | - -Returns: - -| type | description | -| -------- | ----------------- | -| `Uint16` | The tagged uint16 | - -### DataStructures.**untagUint16** - -```grain -untagUint16: (int: Uint16) => WasmI32 -``` - -Untag a uint16. - -Parameters: - -| param | type | description | -| ----- | -------- | ------------------- | -| `int` | `Uint16` | The uint16 to untag | - -Returns: - -| type | description | -| --------- | ------------------- | -| `WasmI32` | The untagged uint16 | - diff --git a/stdlib/runtime/equal.gr b/stdlib/runtime/equal.gr index e61b8fa9a1..e5ef589bb2 100644 --- a/stdlib/runtime/equal.gr +++ b/stdlib/runtime/equal.gr @@ -20,8 +20,7 @@ from "runtime/unsafe/wasmf32" include WasmF32 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/tags" include Tags -from "runtime/dataStructures" include DataStructures -use DataStructures.{ tagSimpleNumber, untagSimpleNumber } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/numbers" include Numbers use Numbers.{ isNumber, numberEqual } @@ -42,17 +41,17 @@ let rec heapEqualHelp = (heapTag, xRef, yRef) => { match (heapTag) { t when t == Tags._GRAIN_ADT_HEAP_TAG => { // Check if the same constructor variant - let xVariantTag = untagSimpleNumber(DataStructures.loadAdtVariant(xRef)) - let yVariantTag = untagSimpleNumber(DataStructures.loadAdtVariant(yRef)) + let xVariantTag = DS.untagSimpleNumber(DS.ADT.getVariantId(xRef)) + let yVariantTag = DS.untagSimpleNumber(DS.ADT.getVariantId(yRef)) if (xVariantTag != yVariantTag) { return false } - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.ADT.getData(xRef) + let yarray = DS.ADT.getData(yRef) // Handle lists separately to avoid stack overflow - if (DataStructures.loadVariantTypeId(xRef) is _LIST_ID) { + if (DS.ADT.getTypeId(xRef) is _LIST_ID) { if (xVariantTag == 1n) return true // End of list if ( @@ -73,12 +72,12 @@ let rec heapEqualHelp = (heapTag, xRef, yRef) => { let yarity = WasmArrayRef.length(yarray) // Cycle check - if (DataStructures.loadCycleMarker(xRef) == cycleMarker) { + if (DS.loadCycleMarker(xRef) == cycleMarker) { return true } - DataStructures.storeCycleMarker(xRef, cycleMarker) - DataStructures.storeCycleMarker(yRef, cycleMarker) + DS.storeCycleMarker(xRef, cycleMarker) + DS.storeCycleMarker(yRef, cycleMarker) for (let mut i = 0n; i < xarity; i += 1n) { if ( @@ -87,32 +86,32 @@ let rec heapEqualHelp = (heapTag, xRef, yRef) => { WasmArrayRef.getAny(yarray, i) ) ) { - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return false } } - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return true } }, t when t == Tags._GRAIN_RECORD_HEAP_TAG => { - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.Record.getData(xRef) + let yarray = DS.Record.getData(yRef) let xlength = WasmArrayRef.length(xarray) let ylength = WasmArrayRef.length(yarray) // Cycle check - if (DataStructures.loadCycleMarker(xRef) == cycleMarker) { + if (DS.loadCycleMarker(xRef) == cycleMarker) { return true } - DataStructures.storeCycleMarker(xRef, cycleMarker) - DataStructures.storeCycleMarker(yRef, cycleMarker) + DS.storeCycleMarker(xRef, cycleMarker) + DS.storeCycleMarker(yRef, cycleMarker) for (let mut i = 0n; i < xlength; i += 1n) { if ( @@ -121,20 +120,20 @@ let rec heapEqualHelp = (heapTag, xRef, yRef) => { WasmArrayRef.getAny(yarray, i) ) ) { - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return false } } - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return true }, t when t == Tags._GRAIN_ARRAY_HEAP_TAG => { - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.Array.getData(WasmRef.toGrain(xRef): DS.Array.ArrayRef) + let yarray = DS.Array.getData(WasmRef.toGrain(yRef): DS.Array.ArrayRef) let xlength = WasmArrayRef.length(xarray) let ylength = WasmArrayRef.length(yarray) @@ -145,12 +144,12 @@ let rec heapEqualHelp = (heapTag, xRef, yRef) => { } // Cycle check - if (DataStructures.loadCycleMarker(xRef) == cycleMarker) { + if (DS.loadCycleMarker(xRef) == cycleMarker) { return true } - DataStructures.storeCycleMarker(xRef, cycleMarker) - DataStructures.storeCycleMarker(yRef, cycleMarker) + DS.storeCycleMarker(xRef, cycleMarker) + DS.storeCycleMarker(yRef, cycleMarker) for (let mut i = 0n; i < xlength; i += 1n) { if ( @@ -159,57 +158,61 @@ let rec heapEqualHelp = (heapTag, xRef, yRef) => { WasmArrayRef.getAny(yarray, i) ) ) { - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return false } } - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return true }, t when t == Tags._GRAIN_STRING_HEAP_TAG => { - let xlength = DataStructures.getStringSize(xRef) - let ylength = DataStructures.getStringSize(yRef) + let xRef = WasmRef.toGrain(xRef): DS.String.StringRef + let yRef = WasmRef.toGrain(yRef): DS.String.StringRef + let xlength = DS.String.getSize(xRef) + let ylength = DS.String.getSize(yRef) // Check if the same length return if (xlength != ylength) { false } else { - let xarray = DataStructures.getStringArrayRef(xRef) - let yarray = DataStructures.getStringArrayRef(yRef) + let xarray = DS.String.getData(xRef) + let yarray = DS.String.getData(yRef) WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, xlength) == 0n } }, t when t == Tags._GRAIN_BYTES_HEAP_TAG => { - let xlength = DataStructures.getBytesSize(xRef) - let ylength = DataStructures.getBytesSize(yRef) + let xRef = WasmRef.toGrain(xRef): DS.Bytes.BytesRef + let yRef = WasmRef.toGrain(yRef): DS.Bytes.BytesRef + let xlength = DS.Bytes.getSize(xRef) + let ylength = DS.Bytes.getSize(yRef) // Check if the same length return if (xlength != ylength) { false } else { - let xarray = DataStructures.getBytesArrayRef(xRef) - let yarray = DataStructures.getBytesArrayRef(yRef) + let xarray = DS.Bytes.getData(xRef) + let yarray = DS.Bytes.getData(yRef) WasmArrayRef.compareI8(xarray, 0n, yarray, 0n, xlength) == 0n } }, t when t == Tags._GRAIN_TUPLE_HEAP_TAG => { - let xarray = DataStructures.getCompoundValueArrayRef(xRef) - let yarray = DataStructures.getCompoundValueArrayRef(yRef) + let xarray = DS.Tuple.getData(WasmRef.toGrain(xRef): DS.Tuple.TupleRef) + let yarray = DS.Tuple.getData(WasmRef.toGrain(yRef): DS.Tuple.TupleRef) let xsize = WasmArrayRef.length(xarray) let ysize = WasmArrayRef.length(yarray) // Cycle check - if (DataStructures.loadCycleMarker(xRef) == cycleMarker) { + if (DS.loadCycleMarker(xRef) == cycleMarker) { return true } - DataStructures.storeCycleMarker(xRef, cycleMarker) - DataStructures.storeCycleMarker(yRef, cycleMarker) + DS.storeCycleMarker(xRef, cycleMarker) + DS.storeCycleMarker(yRef, cycleMarker) for (let mut i = 0n; i < xsize; i += 1n) { if ( @@ -218,32 +221,32 @@ let rec heapEqualHelp = (heapTag, xRef, yRef) => { WasmArrayRef.getAny(yarray, i) ) ) { - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return false } } - DataStructures.storeCycleMarker(xRef, 0n) - DataStructures.storeCycleMarker(yRef, 0n) + DS.storeCycleMarker(xRef, 0n) + DS.storeCycleMarker(yRef, 0n) return true }, t when t == Tags._GRAIN_INT32_HEAP_TAG => { - let xval = DataStructures.getInt32Value(xRef) - let yval = DataStructures.getInt32Value(yRef) + let xval = DS.Int32.getValue(xRef) + let yval = DS.Int32.getValue(yRef) return xval == yval }, t when t == Tags._GRAIN_UINT32_HEAP_TAG => { - let xval = DataStructures.getUint32Value(xRef) - let yval = DataStructures.getUint32Value(yRef) + let xval = DS.Uint32.getValue(xRef) + let yval = DS.Uint32.getValue(yRef) return xval == yval }, // Float32 is handled by equalHelp directly t when t == Tags._GRAIN_UINT64_HEAP_TAG => { use WasmI64.{ (==) } - let xval = DataStructures.getUint64Value(xRef) - let yval = DataStructures.getUint64Value(yRef) + let xval = DS.Uint64.getValue(xRef) + let yval = DS.Uint64.getValue(yRef) return xval == yval }, // No other implementation @@ -257,16 +260,16 @@ and equalHelp = (x, y) => { } else if (isNumber(x)) { // Numbers have special equality rules, e.g. NaN != NaN numberEqual(x, y) - } else if (DataStructures.loadValueTag(x) == Tags._GRAIN_FLOAT32_HEAP_TAG) { + } else if (DS.getHeapTag(x) == Tags._GRAIN_FLOAT32_HEAP_TAG) { // Short circuit for Float32 to correctly handle NaN != NaN - let xval = DataStructures.getFloat32Value(x) - let yval = DataStructures.getFloat32Value(y) + let xval = DS.Float32.getValue(x) + let yval = DS.Float32.getValue(y) use WasmF32.{ (==) } xval == yval } else { // Handle all other heap allocated things // Can short circuit if references are the same - x is y || heapEqualHelp(DataStructures.loadValueTag(x), x, y) + x is y || heapEqualHelp(DS.getHeapTag(x), x, y) } } diff --git a/stdlib/runtime/math/rempio2.gr b/stdlib/runtime/math/rempio2.gr index 365ff7fa27..e2bac93837 100644 --- a/stdlib/runtime/math/rempio2.gr +++ b/stdlib/runtime/math/rempio2.gr @@ -15,9 +15,7 @@ use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmf64" include WasmF64 -from "runtime/unsafe/conv" include Conv -use Conv.{ toInt32, toUint64, toFloat64, fromInt32, fromUint64, fromFloat64 } -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/math/umuldi" include Umuldi use Umuldi.{ umuldi } @@ -36,8 +34,8 @@ let pio2_right = (q0: WasmI64, q1: WasmI64) => { // see: jdh8/metallic/blob/mast let q0 = WasmF64.convertI64U(q0 << shift) let (lo, hi) = umuldi(p1, q1) - let lo = fromUint64(lo) - let hi = fromUint64(hi) + let lo = DS.Uint64.fromUint64(lo) + let hi = DS.Uint64.fromUint64(hi) let ahi = hi >>> 11N let alo = lo >>> 11N | hi << 53N @@ -48,7 +46,11 @@ let pio2_right = (q0: WasmI64, q1: WasmI64) => { // see: jdh8/metallic/blob/mast let y0 = WasmF64.convertI64U(ahi + (if (lo < blo) 1N else 0N)) let y1 = 0x1p-64W * WasmF64.convertI64U(alo + blo) - return (toUint64(shift), toFloat64(y0), toFloat64(y1)) + return ( + DS.Uint64.toUint64(shift), + DS.Float64.toFloat64(y0), + DS.Float64.toFloat64(y1), + ) } @unsafe @@ -61,7 +63,7 @@ let get_PIO2_TABLE = (index: WasmI32) => { ) } use WasmI32.{ (+), (<<), (|) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) let lo = WasmArrayRef.getI8U(data, index + 3n) << 24n | WasmArrayRef.getI8U(data, index + 2n) << 16n | WasmArrayRef.getI8U(data, index + 1n) << 8n @@ -110,8 +112,8 @@ let pio2_large_quot = (x: WasmF64, i: WasmI64) => { // see: jdh8/metallic/blob/m // First 128 bits of fractional part of x/(2π) let (blo, bhi) = umuldi(s1, significand) - let blo = fromUint64(blo) - let bhi = fromUint64(bhi) + let blo = DS.Uint64.fromUint64(blo) + let bhi = DS.Uint64.fromUint64(bhi) let ahi = s0 * significand let clo = (s2 >>> 32N) * (significand >>> 32N) @@ -127,9 +129,9 @@ let pio2_large_quot = (x: WasmF64, i: WasmI64) => { // see: jdh8/metallic/blob/m let q = WasmI32.wrapI64((productHigh >> 62N) - sLow) let (right, y0, y1) = pio2_right(rLow ^ sLow, rHigh ^ sHigh) - let right = fromUint64(right) - let y0 = fromFloat64(y0) - let y1 = fromFloat64(y1) + let right = DS.Uint64.fromUint64(right) + let y0 = DS.Float64.fromFloat64(y0) + let y1 = DS.Float64.fromFloat64(y1) let shifter = 0x3CB0000000000000N - (right << 52N) let signbit = (i ^ rHigh) & 0x8000000000000000N @@ -139,7 +141,11 @@ let pio2_large_quot = (x: WasmF64, i: WasmI64) => { // see: jdh8/metallic/blob/m let y0 = y0 * coeff let y1 = y1 * coeff - return (toInt32(q), toFloat64(y0), toFloat64(y1)) + return ( + DS.Int32.toInt32(q), + DS.Float64.toFloat64(y0), + DS.Float64.toFloat64(y1), + ) } @unsafe @@ -171,7 +177,7 @@ provide let rempio2 = (x: WasmF64, i: WasmI64, sign: Bool) => { y0 = z - pio2_2t y1 = z - y0 - pio2_2t } - return (1l, toFloat64(y0), toFloat64(y1)) + return (1l, DS.Float64.toFloat64(y0), DS.Float64.toFloat64(y1)) } else { let z = x + pio2_1 let mut y0 = 0.0W @@ -184,7 +190,7 @@ provide let rempio2 = (x: WasmF64, i: WasmI64, sign: Bool) => { y0 = z + pio2_2t y1 = z - y0 + pio2_2t } - return (-1l, toFloat64(y0), toFloat64(y1)) + return (-1l, DS.Float64.toFloat64(y0), DS.Float64.toFloat64(y1)) } } @@ -222,12 +228,16 @@ provide let rempio2 = (x: WasmF64, i: WasmI64, sign: Bool) => { use WasmF64.{ (-) } let y1 = r - y0 - w let q = WasmI32.truncF64S(q) - return (toInt32(q), toFloat64(y0), toFloat64(y1)) + return ( + DS.Int32.toInt32(q), + DS.Float64.toFloat64(y0), + DS.Float64.toFloat64(y1), + ) } let (q, y0, y1) = pio2_large_quot(x, i) - let q = fromInt32(q) + let q = DS.Int32.fromInt32(q) use WasmI32.{ (*) } let q = if (sign) q * -1n else q - return (toInt32(q), y0, y1) + return (DS.Int32.toInt32(q), y0, y1) } diff --git a/stdlib/runtime/math/trig.gr b/stdlib/runtime/math/trig.gr index dd6f59d5e4..2e9b4ca082 100644 --- a/stdlib/runtime/math/trig.gr +++ b/stdlib/runtime/math/trig.gr @@ -6,8 +6,7 @@ module Trig from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/unsafe/wasmf64" include WasmF64 -from "runtime/unsafe/conv" include Conv -use Conv.{ fromInt32, fromFloat64 } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/math/kernel/sin" include Sine use Sine.{ sin as kernSin } from "runtime/math/kernel/cos" include Cosine @@ -43,9 +42,9 @@ provide let sin = (x: WasmF64) => { // see: musl/src/math/sin.c // argument reduction needed let (n, y0, y1) = rempio2(x, i, sign) - let n = fromInt32(n) - let y0 = fromFloat64(y0) - let y1 = fromFloat64(y1) + let n = DS.Int32.fromInt32(n) + let y0 = DS.Float64.fromFloat64(y0) + let y1 = DS.Float64.fromFloat64(y1) return match (n & 3n) { 0n => kernSin(y0, y1, true), @@ -81,9 +80,9 @@ provide let cos = (x: WasmF64) => { // see: musl/src/math/cos.c // argument reduction needed let (n, y0, y1) = rempio2(x, i, sign) - let n = fromInt32(n) - let y0 = fromFloat64(y0) - let y1 = fromFloat64(y1) + let n = DS.Int32.fromInt32(n) + let y0 = DS.Float64.fromFloat64(y0) + let y1 = DS.Float64.fromFloat64(y1) return match (n & 3n) { 0n => kernCos(y0, y1), @@ -117,9 +116,9 @@ provide let tan = (x: WasmF64) => { // see: musl/src/math/tan.c if (ix >= 0x7FF00000n) return x - x let (n, y0, y1) = rempio2(x, i, sign) - let n = fromInt32(n) - let y0 = fromFloat64(y0) - let y1 = fromFloat64(y1) + let n = DS.Int32.fromInt32(n) + let y0 = DS.Float64.fromFloat64(y0) + let y1 = DS.Float64.fromFloat64(y1) use WasmI32.{ (-), (&), (<<), (!=) } return match (n & 3n) { 0n => kernTan(y0, y1, true), diff --git a/stdlib/runtime/math/umuldi.gr b/stdlib/runtime/math/umuldi.gr index 20dd0b76e7..4f285fcfdc 100644 --- a/stdlib/runtime/math/umuldi.gr +++ b/stdlib/runtime/math/umuldi.gr @@ -1,8 +1,7 @@ module Umuldi from "runtime/unsafe/wasmi64" include WasmI64 -from "runtime/unsafe/conv" include Conv -use Conv.{ toUint64 } +from "runtime/unsafe/dataStructures" include DataStructures as DS @unsafe provide let umuldi = (u: WasmI64, v: WasmI64) => { // see: jdh8/metallic/blob/master/src/soft/integer/umulditi3.h @@ -21,5 +20,5 @@ provide let umuldi = (u: WasmI64, v: WasmI64) => { // see: jdh8/metallic/blob/ma let result = (t << 32N) + w0 let res128_hi = a1 * b1 + w1 + (t >>> 32N) - return (toUint64(result), toUint64(res128_hi)) + return (DS.Uint64.toUint64(result), DS.Uint64.toUint64(res128_hi)) } diff --git a/stdlib/runtime/numberUtils.gr b/stdlib/runtime/numberUtils.gr index f7e2512bb9..f7e1794728 100644 --- a/stdlib/runtime/numberUtils.gr +++ b/stdlib/runtime/numberUtils.gr @@ -21,8 +21,7 @@ from "runtime/unsafe/wasmf64" include WasmF64 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/exception" include Exception -from "runtime/dataStructures" include DataStructures -use DataStructures.{ allocateString } +from "runtime/unsafe/dataStructures" include DataStructures as DS primitive (!) = "@not" primitive (&&) = "@and" @@ -56,7 +55,7 @@ provide let get_POWERS10 = (index: WasmI32) => { b"\x01\x00\x00\x00\x0a\x00\x00\x00\x64\x00\x00\x00\xe8\x03\x00\x00\x10\x27\x00\x00\xa0\x86\x01\x00\x40\x42\x0f\x00\x80\x96\x98\x00\x00\xe1\xf5\x05\x00\xca\x9a\x3b" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) use WasmI32.{ (>) } if (index > 36n) { 1n @@ -90,7 +89,7 @@ let get_DIGITS = (index: WasmI32) => { b"\x30\x30\x31\x30\x32\x30\x33\x30\x34\x30\x35\x30\x36\x30\x37\x30\x38\x30\x39\x30\x30\x31\x31\x31\x32\x31\x33\x31\x34\x31\x35\x31\x36\x31\x37\x31\x38\x31\x39\x31\x30\x32\x31\x32\x32\x32\x33\x32\x34\x32\x35\x32\x36\x32\x37\x32\x38\x32\x39\x32\x30\x33\x31\x33\x32\x33\x33\x33\x34\x33\x35\x33\x36\x33\x37\x33\x38\x33\x39\x33\x30\x34\x31\x34\x32\x34\x33\x34\x34\x34\x35\x34\x36\x34\x37\x34\x38\x34\x39\x34\x30\x35\x31\x35\x32\x35\x33\x35\x34\x35\x35\x35\x36\x35\x37\x35\x38\x35\x39\x35\x30\x36\x31\x36\x32\x36\x33\x36\x34\x36\x35\x36\x36\x36\x37\x36\x38\x36\x39\x36\x30\x37\x31\x37\x32\x37\x33\x37\x34\x37\x35\x37\x36\x37\x37\x37\x38\x37\x39\x37\x30\x38\x31\x38\x32\x38\x33\x38\x34\x38\x35\x38\x36\x38\x37\x38\x38\x38\x39\x38\x30\x39\x31\x39\x32\x39\x33\x39\x34\x39\x35\x39\x36\x39\x37\x39\x38\x39\x39\x39" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) WasmArrayRef.getI8U(data, index) << 8n | WasmArrayRef.getI8U(data, index + 1n) } @@ -104,7 +103,7 @@ provide let get_HEX_DIGITS = (index: WasmI32) => { b"\x30\x30\x31\x30\x32\x30\x33\x30\x34\x30\x35\x30\x36\x30\x37\x30\x38\x30\x39\x30\x61\x30\x62\x30\x63\x30\x64\x30\x65\x30\x66\x30\x30\x31\x31\x31\x32\x31\x33\x31\x34\x31\x35\x31\x36\x31\x37\x31\x38\x31\x39\x31\x61\x31\x62\x31\x63\x31\x64\x31\x65\x31\x66\x31\x30\x32\x31\x32\x32\x32\x33\x32\x34\x32\x35\x32\x36\x32\x37\x32\x38\x32\x39\x32\x61\x32\x62\x32\x63\x32\x64\x32\x65\x32\x66\x32\x30\x33\x31\x33\x32\x33\x33\x33\x34\x33\x35\x33\x36\x33\x37\x33\x38\x33\x39\x33\x61\x33\x62\x33\x63\x33\x64\x33\x65\x33\x66\x33\x30\x34\x31\x34\x32\x34\x33\x34\x34\x34\x35\x34\x36\x34\x37\x34\x38\x34\x39\x34\x61\x34\x62\x34\x63\x34\x64\x34\x65\x34\x66\x34\x30\x35\x31\x35\x32\x35\x33\x35\x34\x35\x35\x35\x36\x35\x37\x35\x38\x35\x39\x35\x61\x35\x62\x35\x63\x35\x64\x35\x65\x35\x66\x35\x30\x36\x31\x36\x32\x36\x33\x36\x34\x36\x35\x36\x36\x36\x37\x36\x38\x36\x39\x36\x61\x36\x62\x36\x63\x36\x64\x36\x65\x36\x66\x36\x30\x37\x31\x37\x32\x37\x33\x37\x34\x37\x35\x37\x36\x37\x37\x37\x38\x37\x39\x37\x61\x37\x62\x37\x63\x37\x64\x37\x65\x37\x66\x37\x30\x38\x31\x38\x32\x38\x33\x38\x34\x38\x35\x38\x36\x38\x37\x38\x38\x38\x39\x38\x61\x38\x62\x38\x63\x38\x64\x38\x65\x38\x66\x38\x30\x39\x31\x39\x32\x39\x33\x39\x34\x39\x35\x39\x36\x39\x37\x39\x38\x39\x39\x39\x61\x39\x62\x39\x63\x39\x64\x39\x65\x39\x66\x39\x30\x61\x31\x61\x32\x61\x33\x61\x34\x61\x35\x61\x36\x61\x37\x61\x38\x61\x39\x61\x61\x61\x62\x61\x63\x61\x64\x61\x65\x61\x66\x61\x30\x62\x31\x62\x32\x62\x33\x62\x34\x62\x35\x62\x36\x62\x37\x62\x38\x62\x39\x62\x61\x62\x62\x62\x63\x62\x64\x62\x65\x62\x66\x62\x30\x63\x31\x63\x32\x63\x33\x63\x34\x63\x35\x63\x36\x63\x37\x63\x38\x63\x39\x63\x61\x63\x62\x63\x63\x63\x64\x63\x65\x63\x66\x63\x30\x64\x31\x64\x32\x64\x33\x64\x34\x64\x35\x64\x36\x64\x37\x64\x38\x64\x39\x64\x61\x64\x62\x64\x63\x64\x64\x64\x65\x64\x66\x64\x30\x65\x31\x65\x32\x65\x33\x65\x34\x65\x35\x65\x36\x65\x37\x65\x38\x65\x39\x65\x61\x65\x62\x65\x63\x65\x64\x65\x65\x65\x66\x65\x30\x66\x31\x66\x32\x66\x33\x66\x34\x66\x35\x66\x36\x66\x37\x66\x38\x66\x39\x66\x61\x66\x62\x66\x63\x66\x64\x66\x65\x66\x66\x66" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) WasmArrayRef.getI8U(data, index) << 8n | WasmArrayRef.getI8U(data, index + 1n) } @@ -118,7 +117,7 @@ let get_ANY_DIGITS = (index: WasmI32) => { b"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) WasmArrayRef.getI8U(data, index) } @@ -131,7 +130,7 @@ let get_EXP_POWERS = (index: WasmI32) => { b"\x3c\xfb\x57\xfb\x72\xfb\x8c\xfb\xa7\xfb\xc1\xfb\xdc\xfb\xf6\xfb\x11\xfc\x2c\xfc\x46\xfc\x61\xfc\x7b\xfc\x96\xfc\xb1\xfc\xcb\xfc\xe6\xfc\x00\xfd\x1b\xfd\x35\xfd\x50\xfd\x6b\xfd\x85\xfd\xa0\xfd\xba\xfd\xd5\xfd\xef\xfd\x0a\xfe\x25\xfe\x3f\xfe\x5a\xfe\x74\xfe\x8f\xfe\xa9\xfe\xc4\xfe\xdf\xfe\xf9\xfe\x14\xff\x2e\xff\x49\xff\x63\xff\x7e\xff\x99\xff\xb3\xff\xce\xff\xe8\xff\x03\x00\x1e\x00\x38\x00\x53\x00\x6d\x00\x88\x00\xa2\x00\xbd\x00\xd8\x00\xf2\x00\x0d\x01\x27\x01\x42\x01\x5c\x01\x77\x01\x92\x01\xac\x01\xc7\x01\xe1\x01\xfc\x01\x16\x02\x31\x02\x4c\x02\x66\x02\x81\x02\x9b\x02\xb6\x02\xd0\x02\xeb\x02\x06\x03\x20\x03\x3b\x03\x55\x03\x70\x03\x8b\x03\xa5\x03\xc0\x03\xda\x03\xf5\x03\x0f\x04\x2a\x04" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) let lower = WasmArrayRef.getI8U(data, index) let upper = WasmArrayRef.getI8U(data, index + 1n) WasmI32.extendS16(upper << 8n | lower) @@ -147,7 +146,7 @@ let get_FRC_POWERS = (index: WasmI32) => { b"\x88\x02\x1c\x08\xa0\xd5\x8f\xfa\x76\xbf\x3e\xa2\x7f\xe1\xae\xba\x76\xac\x55\x30\x20\xfb\x16\x8b\xea\x35\xce\x5d\x4a\x89\x42\xcf\x2d\x3b\x65\x55\xaa\xb0\x6b\x9a\xdf\x45\x1a\x3d\x03\xcf\x1a\xe6\xca\xc6\x9a\xc7\x17\xfe\x70\xab\x4f\xdc\xbc\xbe\xfc\xb1\x77\xff\x0c\xd6\x6b\x41\xef\x91\x56\xbe\x3c\xfc\x7f\x90\xad\x1f\xd0\x8d\x83\x9a\x55\x31\x28\x5c\x51\xd3\xb5\xc9\xa6\xad\x8f\xac\x71\x9d\xcb\x8b\xee\x23\x77\x22\x9c\xea\x6d\x53\x78\x40\x91\x49\xcc\xae\x57\xce\xb6\x5d\x79\x12\x3c\x82\x37\x56\xfb\x4d\x36\x94\x10\xc2\x4f\x98\x48\x38\x6f\xea\x96\x90\xc7\x3a\x82\x25\xcb\x85\x74\xd7\xf4\x97\xbf\x97\xcd\xcf\x86\xa0\xe5\xac\x2a\x17\x98\x0a\x34\xef\x8e\xb2\x35\x2a\xfb\x67\x38\xb2\x3b\x3f\xc6\xd2\xdf\xd4\xc8\x84\xba\xcd\xd3\x1a\x27\x44\xdd\xc5\x96\xc9\x25\xbb\xce\x9f\x6b\x93\x84\xa5\x62\x7d\x24\x6c\xac\xdb\xf6\xda\x5f\x0d\x58\x66\xab\xa3\x26\xf1\xc3\xde\x93\xf8\xe2\xf3\xb8\x80\xff\xaa\xa8\xad\xb5\xb5\x8b\x4a\x7c\x6c\x05\x5f\x62\x87\x53\x30\xc1\x34\x60\xff\xbc\xc9\x55\x26\xba\x91\x8c\x85\x4e\x96\xbd\x7e\x29\x70\x24\x77\xf9\xdf\x8f\xb8\xe5\xb8\x9f\xbd\xdf\xa6\x94\x7d\x74\x88\xcf\x5f\xa9\xf8\xcf\x9b\xa8\x8f\x93\x70\x44\xb9\x6b\x15\x0f\xbf\xf8\xf0\x08\x8a\xb6\x31\x31\x65\x55\x25\xb0\xcd\xac\x7f\x7b\xd0\xc6\xe2\x3f\x99\x06\x3b\x2b\x2a\xc4\x10\x5c\xe4\xd3\x92\x73\x69\x99\x24\x24\xaa\x0e\xca\x00\x83\xf2\xb5\x87\xfd\xeb\x1a\x11\x92\x64\x08\xe5\xbc\xcc\x88\x50\x6f\x09\xcc\xbc\x8c\x2c\x65\x19\xe2\x58\x17\xb7\xd1\x00\x00\x00\x00\x00\x00\x40\x9c\x00\x00\x00\x00\x10\xa5\xd4\xe8\x00\x00\x62\xac\xc5\xeb\x78\xad\x84\x09\x94\xf8\x78\x39\x3f\x81\xb3\x15\x07\xc9\x7b\xce\x97\xc0\x70\x5c\xea\x7b\xce\x32\x7e\x8f\x68\x80\xe9\xab\xa4\x38\xd2\xd5\x45\x22\x9a\x17\x26\x27\x4f\x9f\x27\xfb\xc4\xd4\x31\xa2\x63\xed\xa8\xad\xc8\x8c\x38\x65\xde\xb0\xdb\x65\xab\x1a\x8e\x08\xc7\x83\x9a\x1d\x71\x42\xf9\x1d\x5d\xc4\x58\xe7\x1b\xa6\x2c\x69\x4d\x92\xea\x8d\x70\x1a\x64\xee\x01\xda\x4a\x77\xef\x9a\x99\xa3\x6d\xa2\x85\x6b\x7d\xb4\x7b\x78\x09\xf2\x77\x18\xdd\x79\xa1\xe4\x54\xb4\xc2\xc5\x9b\x5b\x92\x86\x5b\x86\x3d\x5d\x96\xc8\xc5\x53\x35\xc8\xb3\xa0\x97\xfa\x5c\xb4\x2a\x95\xe3\x5f\xa0\x99\xbd\x9f\x46\xde\x25\x8c\x39\xdb\x34\xc2\x9b\xa5\x5c\x9f\x98\xa3\x72\x9a\xc6\xf6\xce\xbe\xe9\x54\x53\xbf\xdc\xb7\xe2\x41\x22\xf2\x17\xf3\xfc\x88\xa5\x78\x5c\xd3\x9b\xce\x20\xcc\xdf\x53\x21\x7b\xf3\x5a\x16\x98\x3a\x30\x1f\x97\xdc\xb5\xa0\xe2\x96\xb3\xe3\x5c\x53\xd1\xd9\xa8\x3c\x44\xa7\xa4\xd9\x7c\x9b\xfb\x10\x44\xa4\xa7\x4c\x4c\x76\xbb\x1a\x9c\x40\xb6\xef\x8e\xab\x8b\x2c\x84\x57\xa6\x10\xef\x1f\xd0\x29\x31\x91\xe9\xe5\xa4\x10\x9b\x9d\x0c\x9c\xa1\xfb\x9b\x10\xe7\x29\xf4\x3b\x62\xd9\x20\x28\xac\x85\xcf\xa7\x7a\x5e\x4b\x44\x80\x2d\xdd\xac\x03\x40\xe4\x21\xbf\x8f\xff\x44\x5e\x2f\x9c\x67\x8e\x41\xb8\x8c\x9c\x9d\x17\x33\xd4\xa9\x1b\xe3\xb4\x92\xdb\x19\x9e\xd9\x77\xdf\xba\x6e\xbf\x96\xeb\x6b\xee\xf0\x9b\x3b\x02\x87\xaf" ) } - let data = DataStructures.getBytesArrayRef(tableRef) + let data = DS.Bytes.getData(WasmRef.toGrain(tableRef): DS.Bytes.BytesRef) let lo = WasmArrayRef.getI8U(data, index + 3n) << 24n | WasmArrayRef.getI8U(data, index + 2n) << 16n | WasmArrayRef.getI8U(data, index + 1n) << 8n @@ -416,7 +415,6 @@ provide let utoa32Buffered = (buf, value, radix) => { utoa32_hex_core(buf, value, decimals) } else { let decimals = ulog_base(WasmI64.extendI32U(value), radix) - let out = allocateString(decimals) utoa64_any_core(buf, WasmI64.extendI32U(value), decimals, radix) } } @@ -433,23 +431,23 @@ provide let utoa32 = (value, radix) => { "0" } else if (radix == 10n) { let decimals = decimalCount32(value) - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa32_dec_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } else if (radix == 16n) { let decimals = ((31n - WasmI32.clz(value)) >>> 2n) + 1n - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa32_hex_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } else { let val64 = WasmI64.extendI32U(value) let decimals = ulog_base(val64, radix) - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_any_core(buf, val64, decimals, radix) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } } @@ -470,27 +468,27 @@ provide let itoa32 = (value, radix) => { "0" } else if (radix == 10n) { let decimals = decimalCount32(value) + sign - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa32_dec_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out): String } else if (radix == 16n) { let decimals = ((31n - WasmI32.clz(value)) >>> 2n) + 1n + sign - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa32_hex_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } else { let val64 = WasmI64.extendI32U(value) let decimals = ulog_base(val64, radix) + sign - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_any_core(buf, val64, decimals, radix) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } if (sign != 0n) { WasmArrayRef.setI8( - DataStructures.getStringArrayRef(WasmRef.fromGrain(out)), + DS.String.getData(DS.String.fromGrain(out)), 0n, _CHAR_CODE_MINUS ) @@ -512,29 +510,29 @@ provide let utoa64 = (value, radix) => { if (WasmI64.leU(value, _I32_MAX)) { let val32 = WasmI32.wrapI64(value) let decimals = decimalCount32(val32) - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa32_dec_core(buf, val32, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } else { let decimals = decimalCount64High(value) - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_dec_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } } else if (radix == 16n) { let decimals = ((63n - WasmI32.wrapI64(WasmI64.clz(value))) >>> 2n) + 1n - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_hex_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } else { let decimals = ulog_base(value, radix) - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_any_core(buf, value, decimals, radix) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } } @@ -562,35 +560,35 @@ provide let itoa64 = (value, radix) => { if (WasmI64.leU(value, _I32_MAX)) { let val32 = WasmI32.wrapI64(value) let decimals = decimalCount32(val32) + sign - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa32_dec_core(buf, val32, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } else { let decimals = decimalCount64High(value) + sign - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_dec_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } } else if (radix == 16n) { let decimals = ((63n - WasmI32.wrapI64(WasmI64.clz(value))) >>> 2n) + 1n + sign - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_hex_core(buf, value, decimals) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } else { let decimals = ulog_base(value, radix) + sign - let out = allocateString(decimals) - let buf = DataStructures.getStringArrayRef(out) + let out = DS.String.make(decimals) + let buf = DS.String.getData(out) utoa64_any_core(buf, value, decimals, radix) - WasmRef.toGrain(out): String + DS.String.toGrain(out) } if (sign != 0n) { WasmArrayRef.setI8( - DataStructures.getStringArrayRef(WasmRef.fromGrain(out)), + DS.String.getData(DS.String.fromGrain(out)), 0n, _CHAR_CODE_MINUS ) @@ -935,9 +933,9 @@ let mut _dtoa_buf = WasmRef.fromGrain(void) @unsafe let get_dtoa_buf = () => { if (_dtoa_buf is WasmRef.fromGrain(void)) { - _dtoa_buf = WasmRef.toGrain(allocateString(_MAX_DOUBLE_LENGTH)) + _dtoa_buf = WasmRef.fromGrain(DS.String.make(_MAX_DOUBLE_LENGTH)): WasmRef } - DataStructures.getStringArrayRef(_dtoa_buf) + DS.String.getData(WasmRef.toGrain(_dtoa_buf): DS.String.StringRef) } @unsafe @@ -969,9 +967,9 @@ provide let dtoa = value => { } else { let buf = get_dtoa_buf() let size = dtoa_core(buf, value) - let result = allocateString(size) - let resultData = DataStructures.getStringArrayRef(result) + let result = DS.String.make(size) + let resultData = DS.String.getData(result) WasmArrayRef.copyI8(resultData, 0n, buf, 0n, size) - WasmRef.toGrain(result) + DS.String.toGrain(result) } } diff --git a/stdlib/runtime/numbers.gr b/stdlib/runtime/numbers.gr index 042ae1f832..d6926003dd 100644 --- a/stdlib/runtime/numbers.gr +++ b/stdlib/runtime/numbers.gr @@ -41,33 +41,7 @@ primitive throw = "@throw" exception UnknownNumberTag exception InvariantViolation -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - newRational, - newInt32, - newInt64, - newFloat32, - newFloat64, - tagSimpleNumber, - tagInt8, - tagInt16, - tagUint8, - tagUint16, - untagSimpleNumber, - untagInt8, - untagInt16, - untagUint8, - untagUint16, - getNumberTag, - getInt32Value, - getUint32Value, - getFloat32Value, - getInt64Value, - getFloat64Value, - getRationalNumerator, - getRationalDenominator, - loadValueTag, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS @unsafe let _F32_MAX = 3.40282347e+38W @@ -82,12 +56,12 @@ use WasmI32.{ (==), (!=), (^), (<<), (>>) } @unsafe let tagSimple = x => { - WasmRef.fromGrain(tagSimpleNumber(x)) + WasmRef.fromGrain(DS.tagSimpleNumber(x)) } @unsafe let untagSimple = x => { - untagSimpleNumber(WasmRef.toGrain(x)) + DS.untagSimpleNumber(WasmRef.toGrain(x)) } // naive check; not meant to be used beyond this module @@ -101,7 +75,7 @@ provide let isBoxedNumber = WasmRef.isGrainHeapValue @unsafe provide let isFloat = x => { if (isBoxedNumber(x)) { - let tag = getNumberTag(x) + let tag = DS.getBoxedNumberTag(x) tag == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG } else { false @@ -111,7 +85,7 @@ provide let isFloat = x => { @unsafe provide let isInteger = x => { if (isBoxedNumber(x)) { - let tag = getNumberTag(x) + let tag = DS.getBoxedNumberTag(x) tag == Tags._GRAIN_INT64_BOXED_NUM_TAG || tag == Tags._GRAIN_BIGINT_BOXED_NUM_TAG } else { @@ -122,7 +96,7 @@ provide let isInteger = x => { @unsafe provide let isRational = x => { if (isBoxedNumber(x)) { - let tag = getNumberTag(x) + let tag = DS.getBoxedNumberTag(x) tag == Tags._GRAIN_RATIONAL_BOXED_NUM_TAG } else { false @@ -134,10 +108,10 @@ provide let isNaN = x => { use WasmF64.{ (!=) } if (isBoxedNumber(x)) { // Boxed numbers can have multiple subtypes, of which float64 can be NaN. - let tag = getNumberTag(x) + let tag = DS.getBoxedNumberTag(x) if (tag == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG) { // uses the fact that NaN is the only number not equal to itself - let wf64 = getFloat64Value(x) + let wf64 = DS.Float64.getValue(x) wf64 != wf64 } else { // Neither rational numbers nor boxed integers can be infinite or NaN. @@ -153,7 +127,7 @@ provide let isNaN = x => { @unsafe let isBigInt = x => { if (isBoxedNumber(x)) { - let tag = getNumberTag(x) + let tag = DS.getBoxedNumberTag(x) tag == Tags._GRAIN_BIGINT_BOXED_NUM_TAG } else { false @@ -167,7 +141,7 @@ provide let isNumber = x => { (WasmRef.i31GetS(x) & Tags._GRAIN_NUMBER_TAG_MASK) == Tags._GRAIN_NUMBER_TAG_TYPE } else { - loadValueTag(x) == Tags._GRAIN_BOXED_NUM_HEAP_TAG + DS.getHeapTag(x) == Tags._GRAIN_BOXED_NUM_HEAP_TAG } } @@ -253,7 +227,7 @@ provide let reducedInteger = x => { // TODO(#1736): Remove the formatter ignore when parsing is fixed //formatter-ignore if (x > (_I32_MAX >> 2N) || x < (_I32_MIN >> 2N)) { - newInt64(x) + DS.Int64.make(x) } else { tagSimple(WasmI32.wrapI64(x)) } @@ -265,7 +239,7 @@ provide let reducedUnsignedInteger = x => { if (WasmI64.gtU(x, _I64_MAX)) { BI.makeWrappedUint64(x) } else if (WasmI64.gtU(x, _I32_MAX >>> 2N)) { - newInt64(x) + DS.Int64.make(x) } else { tagSimple(WasmI32.wrapI64(x)) } @@ -306,7 +280,7 @@ let reducedFractionBigInt = (x, y, keepRational) => { let factor = BI.gcd(x, y) let xdiv = BI.div(x, factor) let ydiv = BI.div(y, factor) - newRational(xdiv, ydiv) + DS.Rational.make(xdiv, ydiv) } } @@ -331,26 +305,26 @@ let reducedFraction64 = (x, y) => { let factor = gcd(x, y) let xdiv = x / factor let ydiv = y / factor - newRational(BI.makeWrappedInt64(xdiv), BI.makeWrappedInt64(ydiv)) + DS.Rational.make(BI.makeWrappedInt64(xdiv), BI.makeWrappedInt64(ydiv)) } } // Accessor functions @unsafe -provide let boxedNumberTag = getNumberTag +provide let boxedNumberTag = DS.getBoxedNumberTag @unsafe -provide let boxedInt64Number = getInt64Value +provide let boxedInt64Number = DS.Int64.getValue @unsafe -provide let boxedFloat64Number = getFloat64Value +provide let boxedFloat64Number = DS.Float64.getValue @unsafe -provide let boxedRationalNumerator = getRationalNumerator +provide let boxedRationalNumerator = DS.Rational.getNumerator @unsafe -provide let boxedRationalDenominator = getRationalDenominator +provide let boxedRationalDenominator = DS.Rational.getDenominator @unsafe provide let coerceNumberToWasmF32 = (x: Number) => { @@ -854,7 +828,7 @@ let numberAddSubSimpleHelp = (x, y, isSub) => { let yBoxedVal = boxedFloat64Number(y) let xval = WasmF64.convertI32S(xval) let result = if (isSub) xval - yBoxedVal else xval + yBoxedVal - newFloat64(result) + DS.Float64.make(result) }, _ => { throw UnknownNumberTag @@ -929,7 +903,7 @@ let numberAddSubInt64Help = (xval, y, isSub) => { let xval = WasmF64.convertI64S(xval) let yBoxedVal = boxedFloat64Number(y) let result = if (isSub) xval - yBoxedVal else xval + yBoxedVal - newFloat64(result) + DS.Float64.make(result) }, _ => { throw UnknownNumberTag @@ -943,7 +917,7 @@ let numberAddSubFloat64Help = (xval, y, isSub) => { use WasmF64.{ (+), (-) } let yval = coerceNumberToWasmF64(WasmRef.toGrain(y): Number) let result = if (isSub) xval - yval else xval + yval - newFloat64(result) + DS.Float64.make(result) } @unsafe @@ -993,7 +967,7 @@ let numberAddSubBigIntHelp = (x, y, isSub) => { let xval = BI.toFloat64(x) let yBoxedVal = boxedFloat64Number(y) let result = if (isSub) xval - yBoxedVal else xval + yBoxedVal - newFloat64(result) + DS.Float64.make(result) }, _ => { throw UnknownNumberTag @@ -1152,7 +1126,7 @@ let numberAddSubRationalHelp = (x, y, isSub) => { let xval = xnumfloat / xdenfloat let yval = boxedFloat64Number(y) let result = if (isSub) xval - yval else xval + yval - newFloat64(result) + DS.Float64.make(result) }, _ => { throw UnknownNumberTag @@ -1265,9 +1239,9 @@ let numberTimesDivideInt64Help = (xval, y, isDivide) => { let xval = WasmF64.convertI64S(xval) let yBoxedVal = boxedFloat64Number(y) if (isDivide) { - newFloat64(xval / yBoxedVal) + DS.Float64.make(xval / yBoxedVal) } else { - newFloat64(xval * yBoxedVal) + DS.Float64.make(xval * yBoxedVal) } }, _ => { @@ -1323,9 +1297,9 @@ let numberTimesDivideBigIntHelp = (x, y, isDivide) => { let xval = BI.toFloat64(x) let yBoxedVal = boxedFloat64Number(y) if (isDivide) { - newFloat64(xval / yBoxedVal) + DS.Float64.make(xval / yBoxedVal) } else { - newFloat64(xval * yBoxedVal) + DS.Float64.make(xval * yBoxedVal) } }, _ => { @@ -1394,9 +1368,9 @@ let numberTimesDivideRationalHelp = (x, y, isDivide) => { // TODO(#1190): We should probably use something more accurate if possible here let asFloat = BI.toFloat64(xNumerator) / BI.toFloat64(xDenominator) if (isDivide) { - newFloat64(asFloat / boxedFloat64Number(y)) + DS.Float64.make(asFloat / boxedFloat64Number(y)) } else { - newFloat64(asFloat * boxedFloat64Number(y)) + DS.Float64.make(asFloat * boxedFloat64Number(y)) } }, _ => { @@ -1411,9 +1385,9 @@ let numberTimesDivideFloat64Help = (x, y, isDivide) => { use WasmF64.{ (/), (*) } let yAsFloat = coerceNumberToWasmF64(WasmRef.toGrain(y): Number) if (isDivide) { - newFloat64(x / yAsFloat) + DS.Float64.make(x / yAsFloat) } else { - newFloat64(x * yAsFloat) + DS.Float64.make(x * yAsFloat) } } @@ -1473,11 +1447,11 @@ let numberMod = (x, y) => { let yval = coerceNumberToWasmF64(WasmRef.toGrain(y): Number) let yInfinite = yval == InfinityW || yval == -InfinityW if (yval == 0.0W || yInfinite && (xval == InfinityW || xval == -InfinityW)) { - newFloat64(NaNW) + DS.Float64.make(NaNW) } else if (yInfinite) { - newFloat64(xval) + DS.Float64.make(xval) } else { - newFloat64(xval - WasmF64.trunc(xval / yval) * yval) + DS.Float64.make(xval - WasmF64.trunc(xval / yval) * yval) } } else { let xval = coerceNumberToWasmI64(WasmRef.toGrain(x): Number) @@ -2129,7 +2103,7 @@ provide let coerceNumberToInt8 = (number: Number) => { _SMAX8_I64, true ) - tagInt8(val) + DS.tagInt8(val) } /** @@ -2150,7 +2124,7 @@ provide let coerceNumberToInt16 = (number: Number) => { _SMAX16_I64, false ) - tagInt16(val) + DS.tagInt16(val) } /** @@ -2164,7 +2138,7 @@ provide let coerceNumberToInt16 = (number: Number) => { @unsafe provide let coerceNumberToUint8 = (number: Number) => { let val = coerceNumberToShortUint(number, _UMAX8_I32, _UMAX8_I64, true) - tagUint8(val) + DS.tagUint8(val) } /** @@ -2178,7 +2152,7 @@ provide let coerceNumberToUint8 = (number: Number) => { @unsafe provide let coerceNumberToUint16 = (number: Number) => { let val = coerceNumberToShortUint(number, _UMAX16_I32, _UMAX16_I64, false) - tagUint16(val) + DS.tagUint16(val) } /** @@ -2191,7 +2165,7 @@ provide let coerceNumberToUint16 = (number: Number) => { */ @unsafe provide let coerceNumberToInt32 = (number: Number) => { - let result = newInt32(coerceNumberToWasmI32(number)) + let result = DS.Int32.make(coerceNumberToWasmI32(number)) WasmRef.toGrain(result): Int32 } @@ -2213,7 +2187,7 @@ provide let coerceNumberToInt64 = (number: Number) => { // avoid extra alloc x } else { - newInt64(coerceNumberToWasmI64(WasmRef.toGrain(x): Number)) + DS.Int64.make(coerceNumberToWasmI64(WasmRef.toGrain(x): Number)) } WasmRef.toGrain(result): Int64 } @@ -2243,14 +2217,17 @@ provide let coerceNumberToBigInt = (number: Number) => { provide let coerceNumberToRational = (number: Number) => { let x = WasmRef.fromGrain(number) let result = if (isSimpleNumber(x)) { - newRational(BI.makeWrappedInt32(untagSimple(x)), BI.makeWrappedInt32(1n)) + DS.Rational.make( + BI.makeWrappedInt32(untagSimple(x)), + BI.makeWrappedInt32(1n) + ) } else { let tag = boxedNumberTag(x) if (tag == Tags._GRAIN_RATIONAL_BOXED_NUM_TAG) { // avoid extra alloc x } else if (tag == Tags._GRAIN_INT64_BOXED_NUM_TAG) { - newRational( + DS.Rational.make( BI.makeWrappedInt32(coerceNumberToWasmI32(WasmRef.toGrain(x): Number)), BI.makeWrappedInt32(1n) ) @@ -2271,7 +2248,7 @@ provide let coerceNumberToRational = (number: Number) => { */ @unsafe provide let coerceNumberToFloat32 = (number: Number) => { - let result = newFloat32(coerceNumberToWasmF32(number)) + let result = DS.Float32.make(coerceNumberToWasmF32(number)) WasmRef.toGrain(result): Float32 } @@ -2293,7 +2270,7 @@ provide let coerceNumberToFloat64 = (number: Number) => { // avoid extra alloc x } else { - newFloat64(coerceNumberToWasmF64(WasmRef.toGrain(x): Number)) + DS.Float64.make(coerceNumberToWasmF64(WasmRef.toGrain(x): Number)) } WasmRef.toGrain(result): Float64 } @@ -2308,7 +2285,7 @@ provide let coerceNumberToFloat64 = (number: Number) => { */ @unsafe provide let coerceInt8ToNumber = (value: Int8) => { - let num = untagInt8(value) + let num = DS.untagInt8(value) WasmRef.toGrain(tagSimple(num)): Number } @@ -2322,7 +2299,7 @@ provide let coerceInt8ToNumber = (value: Int8) => { */ @unsafe provide let coerceInt16ToNumber = (value: Int16) => { - let num = untagInt16(value) + let num = DS.untagInt16(value) WasmRef.toGrain(tagSimple(num)): Number } @@ -2336,7 +2313,7 @@ provide let coerceInt16ToNumber = (value: Int16) => { */ @unsafe provide let coerceUint8ToNumber = (value: Uint8) => { - let num = untagUint8(value) + let num = DS.untagUint8(value) WasmRef.toGrain(tagSimple(num)): Number } @@ -2350,7 +2327,7 @@ provide let coerceUint8ToNumber = (value: Uint8) => { */ @unsafe provide let coerceUint16ToNumber = (value: Uint16) => { - let num = untagUint16(value) + let num = DS.untagUint16(value) WasmRef.toGrain(tagSimple(num)): Number } @@ -2364,7 +2341,7 @@ provide let coerceUint16ToNumber = (value: Uint16) => { */ @unsafe provide let coerceInt32ToNumber = (value: Int32) => { - let x = getInt32Value(WasmRef.fromGrain(value)) + let x = DS.Int32.getValue(WasmRef.fromGrain(value)) let result = reducedInteger(WasmI64.extendI32S(x)) WasmRef.toGrain(result): Number } @@ -2427,9 +2404,9 @@ provide let coerceRationalToNumber = (rational: Rational) => { */ @unsafe provide let coerceFloat32ToNumber = (float: Float32) => { - let x = getFloat32Value(WasmRef.fromGrain(float)) + let x = DS.Float32.getValue(WasmRef.fromGrain(float)) let x64 = WasmF64.promoteF32(x) - WasmRef.toGrain(newFloat64(x64)): Number + WasmRef.toGrain(DS.Float64.make(x64)): Number } /** @@ -3040,6 +3017,6 @@ provide let (**) = (base, power) => { } else { let x = coerceNumberToWasmF64(base) let y = coerceNumberToWasmF64(power) - WasmRef.toGrain(newFloat64(powf(x, y))): Number + WasmRef.toGrain(DS.Float64.make(powf(x, y))): Number } } diff --git a/stdlib/runtime/string.gr b/stdlib/runtime/string.gr index 94d12d52ad..87c64d1f01 100644 --- a/stdlib/runtime/string.gr +++ b/stdlib/runtime/string.gr @@ -32,18 +32,7 @@ from "runtime/unsafe/tags" include Tags from "runtime/numberUtils" include NumberUtils from "runtime/utf8" include Utf8 use Utf8.{ usvEncodeLength, writeUtf8CodePoint } -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - allocateString, - allocateArray, - tagSimpleNumber, - untagSimpleNumber, - getCompoundValueArrayRef, - getStringArrayRef, - loadCycleMarker, - storeCycleMarker, - loadAdtVariant, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS foreign wasm fd_write: (WasmI32, WasmI32, WasmI32, WasmI32) => WasmI32 from "wasi_snapshot_preview1" @@ -116,26 +105,26 @@ let makeVec = () => { @unsafe let vecPush = (vec, val) => { if (vec.size is vec.capacity) { - let newCapacity = untagSimpleNumber(vec.capacity) * 2n + let newCapacity = DS.untagSimpleNumber(vec.capacity) * 2n let newArray = WasmArrayRef.makeAny(newCapacity, WasmRef.fromGrain(void)) WasmArrayRef.copyAny( newArray, 0n, vec.data, 0n, - untagSimpleNumber(vec.capacity) + DS.untagSimpleNumber(vec.capacity) ) vec.data = newArray - vec.capacity = tagSimpleNumber(newCapacity) + vec.capacity = DS.tagSimpleNumber(newCapacity) } - let size = untagSimpleNumber(vec.size) + let size = DS.untagSimpleNumber(vec.size) WasmArrayRef.setAny(vec.data, size, val) - vec.size = tagSimpleNumber(size + 1n) + vec.size = DS.tagSimpleNumber(size + 1n) } @unsafe let vecLen = vec => { - untagSimpleNumber(vec.size) + DS.untagSimpleNumber(vec.size) } @unsafe @@ -151,20 +140,20 @@ let vecFindIndex = (vec, val) => { @unsafe let isListVariant = variant => { - let typeId = DataStructures.loadVariantTypeId(variant) + let typeId = DS.ADT.getTypeId(variant) typeId is _LIST_ID } @unsafe let isRangeRecord = record_ => { - let typeId = DataStructures.loadRecordTypeId(record_) + let typeId = DS.Record.getTypeId(record_) typeId is _RANGE_ID } @unsafe let getBuiltinVariantName = variant => { - let typeId = DataStructures.loadVariantTypeId(variant) - let variantId = loadAdtVariant(variant) + let typeId = DS.ADT.getTypeId(variant) + let variantId = DS.ADT.getVariantId(variant) match (typeId) { id when id is _OPTION_ID => { @@ -187,20 +176,20 @@ let getBuiltinVariantName = variant => { @unsafe let getFieldArray = (fields, arity) => { - let fieldArray = getCompoundValueArrayRef( - allocateArray(arity, WasmRef.fromGrain(void)) + let fieldArray = DS.Array.getData( + DS.Array.make(arity, WasmRef.fromGrain(void)) ) let mut fieldOffset = 0n for (let mut i = 0n; i < arity; i += 1n) { let fieldLength = WasmI32.load(fields + fieldOffset, 4n) - let fieldName = allocateString(fieldLength) + let fieldName = DS.String.make(fieldLength) Memory.copyLinearMemoryToRefArray( - getStringArrayRef(fieldName), + DS.String.getData(fieldName), fields + fieldOffset + 8n, fieldLength ) - WasmArrayRef.setAny(fieldArray, i, fieldName) + WasmArrayRef.setAny(fieldArray, i, WasmRef.fromGrain(fieldName)) fieldOffset += WasmI32.load(fields + fieldOffset, 0n) } @@ -210,8 +199,8 @@ let getFieldArray = (fields, arity) => { @unsafe let getVariantMetadata = variant => { - let typeHash = untagSimpleNumber(DataStructures.loadVariantTypeHash(variant)) - let variantId = untagSimpleNumber(loadAdtVariant(variant)) + let typeHash = DS.untagSimpleNumber(DS.ADT.getTypeHash(variant)) + let variantId = DS.untagSimpleNumber(DS.ADT.getVariantId(variant)) let mut block = findTypeMetadata(typeHash) @@ -233,11 +222,11 @@ let getVariantMetadata = variant => { @unsafe let getRecordFieldNames = record_ => { - let typeHash = untagSimpleNumber(DataStructures.loadRecordTypeHash(record_)) + let typeHash = DS.untagSimpleNumber(DS.Record.getTypeHash(record_)) if (isRangeRecord(record_)) { - return Some(getCompoundValueArrayRef(WasmRef.fromGrain(_RANGE_FIELDS))) + return Some(DS.Array.getData(DS.Array.fromGrain(_RANGE_FIELDS))) } else { - let arity = WasmArrayRef.length(getCompoundValueArrayRef(record_)) + let arity = WasmArrayRef.length(DS.Record.getData(record_)) let mut fields = findTypeMetadata(typeHash) @@ -252,7 +241,7 @@ let getRecordFieldNames = record_ => { let rec totalBytes = (acc, list) => { match (list) { [hd, ...tl] => - totalBytes(acc + DataStructures.getStringSize(WasmRef.fromGrain(hd)), tl), + totalBytes(acc + DS.String.getSize(DS.String.fromGrain(hd)), tl), [] => acc, } } @@ -261,7 +250,7 @@ let rec totalBytes = (acc, list) => { let rec writeStrings = (buf, offset, list) => { match (list) { [hd, ...tl] => { - let hd = getStringArrayRef(WasmRef.fromGrain(hd)) + let hd = DS.String.getData(DS.String.fromGrain(hd)) let hdSize = WasmArrayRef.length(hd) WasmArrayRef.copyI8(buf, offset, hd, 0n, hdSize) writeStrings(buf, offset + hdSize, tl) @@ -273,9 +262,9 @@ let rec writeStrings = (buf, offset, list) => { @unsafe let join = list => { let len = totalBytes(0n, list) - let str = allocateString(len) - writeStrings(getStringArrayRef(str), 0n, list) - WasmRef.toGrain(str): String + let str = DS.String.make(len) + writeStrings(DS.String.getData(str), 0n, list) + DS.String.toGrain(str) } @unsafe @@ -303,30 +292,30 @@ let reverse = list => { */ @unsafe provide let concat = (str1: String, str2: String) => { - let ref1 = WasmRef.fromGrain(str1) - let ref2 = WasmRef.fromGrain(str2) + let ref1 = DS.String.fromGrain(str1) + let ref2 = DS.String.fromGrain(str2) - let size1 = DataStructures.getStringSize(ref1) - let size2 = DataStructures.getStringSize(ref2) + let size1 = DS.String.getSize(ref1) + let size2 = DS.String.getSize(ref2) - let newString = allocateString(size1 + size2) + let newString = DS.String.make(size1 + size2) WasmArrayRef.copyI8( - getStringArrayRef(newString), + DS.String.getData(newString), 0n, - getStringArrayRef(ref1), + DS.String.getData(ref1), 0n, size1 ) WasmArrayRef.copyI8( - getStringArrayRef(newString), + DS.String.getData(newString), size1, - getStringArrayRef(ref2), + DS.String.getData(ref2), 0n, size2 ) - WasmRef.toGrain(newString): String + DS.String.toGrain(newString) } @unsafe @@ -343,7 +332,7 @@ let escape = (ref, isString) => { let _SEQ_QUOTE = if (isString) _SEQ_DQUOTE else _SEQ_SQUOTE - let data = getStringArrayRef(ref) + let data = DS.String.getData(ref) let size = WasmArrayRef.length(data) let mut newSize = 2n // extra space for quote characters @@ -361,8 +350,8 @@ let escape = (ref, isString) => { } } - let escapedString = allocateString(newSize) - let escapedStringArray = getStringArrayRef(escapedString) + let escapedString = DS.String.make(newSize) + let escapedStringArray = DS.String.getData(escapedString) // one extra byte for leading quote character let mut j = 1n @@ -397,17 +386,17 @@ let escape = (ref, isString) => { WasmArrayRef.setI8(escapedStringArray, 0n, _SEQ_QUOTE) WasmArrayRef.setI8(escapedStringArray, j, _SEQ_QUOTE) - WasmRef.toGrain(escapedString): String + DS.String.toGrain(escapedString) } @unsafe let escapeString = (s: String) => { - escape(WasmRef.fromGrain(s), true) + escape(DS.String.fromGrain(s), true) } @unsafe let escapeChar = (s: String) => { - escape(WasmRef.fromGrain(s), false) + escape(DS.String.fromGrain(s), false) } @unsafe @@ -433,7 +422,7 @@ let cyclePrefix = (ref, cycles) => { @unsafe let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { - let tag = DataStructures.loadValueTag(ref) + let tag = DS.getHeapTag(ref) match (tag) { t when t == Tags._GRAIN_STRING_HEAP_TAG => { if (toplevel) { @@ -443,8 +432,9 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { } }, t when t == Tags._GRAIN_BYTES_HEAP_TAG => { - let data = DataStructures.getBytesArrayRef(ref) - let mut numBytes = DataStructures.getBytesSize(ref) + let ref = WasmRef.toGrain(ref): DS.Bytes.BytesRef + let data = DS.Bytes.getData(ref) + let mut numBytes = DS.Bytes.getSize(ref) let mut needsEllipsis = false if (numBytes > 32n) { numBytes = 32n @@ -459,8 +449,8 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { 1n // > } let strLen = headBytes + hexBytes + tailBytes - let str = allocateString(strLen) - let strArray = getStringArrayRef(str) + let str = DS.String.make(strLen) + let strArray = DS.String.getData(str) // { WasmArrayRef.setI8(strArray, hexBytes + strOffset, 0x3en) } - WasmRef.toGrain(str): String + DS.String.toGrain(str) }, t when t == Tags._GRAIN_ADT_HEAP_TAG => { // [ , , , , , elts ... ] @@ -518,23 +508,21 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { "" } else { let length = WasmI32.load(variantPtr, 12n) - let variantName = allocateString(length) + let variantName = DS.String.make(length) Memory.copyLinearMemoryToRefArray( - getStringArrayRef(variantName), + DS.String.getData(variantName), variantPtr + 16n, length ) - let variantName = WasmRef.toGrain(variantName): String + let variantName = DS.String.toGrain(variantName) let distToRecordFields = WasmI32.load(variantPtr, 4n) let isRecordVariant = distToRecordFields != 0n if (isRecordVariant) { let fields = variantPtr + distToRecordFields - let recordArity = WasmArrayRef.length( - getCompoundValueArrayRef(ref) - ) + let recordArity = WasmArrayRef.length(DS.ADT.getData(ref)) let recordVariantFields = getFieldArray(fields, recordArity) let recordString = recordToString( - ref, + DS.ADT.getData(ref), recordArity, recordVariantFields, extraIndents, @@ -552,22 +540,22 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { } }, t when t == Tags._GRAIN_RECORD_HEAP_TAG => { - let recordArity = WasmArrayRef.length(getCompoundValueArrayRef(ref)) + let recordArity = WasmArrayRef.length(DS.Record.getData(ref)) let fields = getRecordFieldNames(ref) match (fields) { Some(fields) => { - if (loadCycleMarker(ref) != 0n) { + if (DS.loadCycleMarker(ref) != 0n) { reportCycle(ref, cycles) } else { - storeCycleMarker(ref, _VISITED_BIT) + DS.storeCycleMarker(ref, _VISITED_BIT) let result = recordToString( - ref, + DS.Record.getData(ref), recordArity, fields, extraIndents, cycles ) - storeCycleMarker(ref, 0n) + DS.storeCycleMarker(ref, 0n) join([cyclePrefix(ref, cycles), result]) } }, @@ -575,15 +563,16 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { } }, t when t == Tags._GRAIN_ARRAY_HEAP_TAG => { - let arity = WasmArrayRef.length(getCompoundValueArrayRef(ref)) - if (loadCycleMarker(ref) != 0n) { + let arrRef = WasmRef.toGrain(ref): DS.Array.ArrayRef + let arity = DS.Array.getSize(arrRef) + if (DS.loadCycleMarker(ref) != 0n) { reportCycle(ref, cycles) } else { - storeCycleMarker(ref, _VISITED_BIT) + DS.storeCycleMarker(ref, _VISITED_BIT) let rbrack = "]" let mut strings = [rbrack] let comspace = ", " - let data = getCompoundValueArrayRef(ref) + let data = DS.Array.getData(arrRef) for (let mut i = arity - 1n; i >= 0n; i -= 1n) { let item = toStringHelp( WasmArrayRef.getAny(data, i), @@ -596,7 +585,7 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { strings = [comspace, ...strings] } } - storeCycleMarker(ref, 0n) + DS.storeCycleMarker(ref, 0n) let lbrack = "[> " strings = [lbrack, ...strings] @@ -604,27 +593,23 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { } }, t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => { - let numberTag = DataStructures.getNumberTag(ref) + let numberTag = DS.getBoxedNumberTag(ref) match (numberTag) { t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG => { - NumberUtils.itoa64(DataStructures.getInt64Value(ref), 10n) + NumberUtils.itoa64(DS.Int64.getValue(ref), 10n) }, t when t == Tags._GRAIN_BIGINT_BOXED_NUM_TAG => { BI.bigIntToString10(ref) }, t when t == Tags._GRAIN_RATIONAL_BOXED_NUM_TAG => { - let numerator = BI.bigIntToString10( - DataStructures.getRationalNumerator(ref) - ) - let denominator = BI.bigIntToString10( - DataStructures.getRationalDenominator(ref) - ) + let numerator = BI.bigIntToString10(DS.Rational.getNumerator(ref)) + let denominator = BI.bigIntToString10(DS.Rational.getDenominator(ref)) let slash = "/" let strings = [numerator, slash, denominator] join(strings) }, t when t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => { - NumberUtils.dtoa(DataStructures.getFloat64Value(ref)) + NumberUtils.dtoa(DS.Float64.getValue(ref)) }, _ => { "" @@ -632,28 +617,29 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { } }, t when t == Tags._GRAIN_INT32_HEAP_TAG => { - NumberUtils.itoa32(DataStructures.getInt32Value(ref), 10n) + NumberUtils.itoa32(DS.Int32.getValue(ref), 10n) }, t when t == Tags._GRAIN_FLOAT32_HEAP_TAG => { - NumberUtils.dtoa(WasmF64.promoteF32(DataStructures.getFloat32Value(ref))) + NumberUtils.dtoa(WasmF64.promoteF32(DS.Float32.getValue(ref))) }, t when t == Tags._GRAIN_UINT32_HEAP_TAG => { - NumberUtils.utoa32(DataStructures.getUint32Value(ref), 10n) + NumberUtils.utoa32(DS.Uint32.getValue(ref), 10n) }, t when t == Tags._GRAIN_UINT64_HEAP_TAG => { - NumberUtils.utoa64(DataStructures.getUint64Value(ref), 10n) + NumberUtils.utoa64(DS.Uint64.getValue(ref), 10n) }, t when t == Tags._GRAIN_TUPLE_HEAP_TAG => { - let tupleLength = WasmArrayRef.length(getCompoundValueArrayRef(ref)) - if (loadCycleMarker(ref) != 0n) { + let tupleRef = WasmRef.toGrain(ref): DS.Tuple.TupleRef + let tupleLength = DS.Tuple.getSize(tupleRef) + if (DS.loadCycleMarker(ref) != 0n) { reportCycle(ref, cycles) } else { - storeCycleMarker(ref, _VISITED_BIT) + DS.storeCycleMarker(ref, _VISITED_BIT) let comspace = ", " let rparen = ")" let mut lparen = "(" let mut strings = [rparen] - let data = getCompoundValueArrayRef(ref) + let data = DS.Tuple.getData(tupleRef) for (let mut i = tupleLength - 1n; i >= 0n; i -= 1n) { let item = toStringHelp( WasmArrayRef.getAny(data, i), @@ -666,7 +652,7 @@ let rec heapValueToString = (ref, extraIndents, toplevel, cycles) => { strings = [comspace, ...strings] } } - storeCycleMarker(ref, 0n) + DS.storeCycleMarker(ref, 0n) strings = [lparen, ...strings] if (tupleLength <= 1n) { @@ -711,9 +697,9 @@ and toStringHelp = (grainValue, extraIndents, toplevel, cycles) => { let shortValTag = (grainValue & 0xF8n) >> 3n if (shortValTag == Tags._GRAIN_CHAR_SHORTVAL_TAG) { let byteCount = usvEncodeLength(shortVal) - let string = allocateString(byteCount) - writeUtf8CodePoint(getStringArrayRef(string), 0n, shortVal) - let string = WasmRef.toGrain(string): String + let string = DS.String.make(byteCount) + writeUtf8CodePoint(DS.String.getData(string), 0n, shortVal) + let string = DS.String.toGrain(string) if (toplevel) { return string } else { @@ -749,7 +735,7 @@ and listToString = (ref, extraIndents, cycles) => { let mut strings = [lbrack] while (true) { - let variantId = untagSimpleNumber(loadAdtVariant(cur)) + let variantId = DS.untagSimpleNumber(DS.ADT.getVariantId(cur)) if (variantId == 1n) { break } else { @@ -757,7 +743,7 @@ and listToString = (ref, extraIndents, cycles) => { strings = [commaspace, ...strings] } isFirst = false - let data = getCompoundValueArrayRef(cur) + let data = DS.ADT.getData(cur) let item = toStringHelp( WasmArrayRef.getAny(data, 0n), extraIndents, @@ -774,7 +760,7 @@ and listToString = (ref, extraIndents, cycles) => { join(reversed) } and tupleVariantToString = (ref, variantName, extraIndents, cycles) => { - let data = getCompoundValueArrayRef(ref) + let data = DS.ADT.getData(ref) let variantArity = WasmArrayRef.length(data) if (variantArity == 0n) { variantName @@ -800,25 +786,24 @@ and tupleVariantToString = (ref, variantName, extraIndents, cycles) => { join(strings) } } -and recordToString = (ref, recordArity, fields, extraIndents, cycles) => { +and recordToString = (recordValues, recordArity, fields, extraIndents, cycles) => { let prevPadAmount = extraIndents * 2n let prevSpacePadding = if (prevPadAmount == 0n) { "" } else { - let v = allocateString(prevPadAmount) - WasmArrayRef.fillI8(getStringArrayRef(v), 0n, 0x20n, prevPadAmount) // create indentation for closing brace - WasmRef.toGrain(v): String + let v = DS.String.make(prevPadAmount) + WasmArrayRef.fillI8(DS.String.getData(v), 0n, 0x20n, prevPadAmount) // create indentation for closing brace + DS.String.toGrain(v) } let padAmount = (extraIndents + 1n) * 2n - let spacePadding = allocateString(padAmount) - WasmArrayRef.fillI8(getStringArrayRef(spacePadding), 0n, 0x20n, padAmount) // create indentation - let spacePadding = WasmRef.toGrain(spacePadding): String + let spacePadding = DS.String.make(padAmount) + WasmArrayRef.fillI8(DS.String.getData(spacePadding), 0n, 0x20n, padAmount) // create indentation + let spacePadding = DS.String.toGrain(spacePadding) let newline = "\n" let rbrace = "}" let mut strings = [newline, prevSpacePadding, rbrace] let colspace = ": " let comlf = ",\n" - let recordValues = getCompoundValueArrayRef(ref) for (let mut i = recordArity - 1n; i >= 0n; i -= 1n) { let fieldName = WasmRef.toGrain(WasmArrayRef.getAny(fields, i)): String let fieldValue = toStringHelp( @@ -868,8 +853,8 @@ provide let toString = value => { @unsafe provide let print = (value, suffix="\n") => { // First convert the value to string, if it isn't one already. - let valueRef = getStringArrayRef(WasmRef.fromGrain(toString(value))) - let suffixRef = getStringArrayRef(WasmRef.fromGrain(suffix)) + let valueRef = DS.String.getData(DS.String.fromGrain(toString(value))) + let suffixRef = DS.String.getData(DS.String.fromGrain(suffix)) let valueLength = WasmArrayRef.length(valueRef) let suffixLength = WasmArrayRef.length(suffixRef) let length = valueLength + suffixLength diff --git a/stdlib/runtime/unsafe/conv.gr b/stdlib/runtime/unsafe/conv.gr deleted file mode 100644 index a2ca4a6a41..0000000000 --- a/stdlib/runtime/unsafe/conv.gr +++ /dev/null @@ -1,124 +0,0 @@ -@runtimeMode -module Conv - -from "runtime/unsafe/wasmi32" include WasmI32 -from "runtime/unsafe/wasmi64" include WasmI64 -from "runtime/unsafe/wasmf32" include WasmF32 -from "runtime/unsafe/wasmf64" include WasmF64 -from "runtime/unsafe/wasmref" include WasmRef -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - newInt32, - newUint32, - newInt64, - newUint64, - newFloat32, - newFloat64, - getInt32Value, - getInt64Value, - getUint32Value, - getUint64Value, - getFloat32Value, - getFloat64Value, - tagSimpleNumber, - untagSimpleNumber, -} - -@unsafe -provide let toInt32 = n => { - WasmRef.toGrain(newInt32(n)): Int32 -} - -@unsafe -provide let toUint32 = n => { - WasmRef.toGrain(newUint32(n)): Uint32 -} - -@unsafe -provide let fromInt32 = (n: Int32) => { - let ref = WasmRef.fromGrain(n) - DataStructures.getInt32Value(ref) -} - -@unsafe -provide let fromUint32 = (n: Uint32) => { - let ref = WasmRef.fromGrain(n) - DataStructures.getUint32Value(ref) -} - -@unsafe -provide let toInt64 = n => { - WasmRef.toGrain(newInt64(n)): Int64 -} - -@unsafe -provide let toUint64 = n => { - WasmRef.toGrain(newUint64(n)): Uint64 -} - -@unsafe -provide let fromInt64 = (n: Int64) => { - let ref = WasmRef.fromGrain(n) - getInt64Value(ref) -} - -@unsafe -provide let fromUint64 = (n: Uint64) => { - let ref = WasmRef.fromGrain(n) - getUint64Value(ref) -} - -@unsafe -provide let toFloat32 = n => { - WasmRef.toGrain(newFloat32(n)): Float32 -} - -@unsafe -provide let fromFloat32 = (n: Float32) => { - let ref = WasmRef.fromGrain(n) - getFloat32Value(ref) -} - -@unsafe -provide let toFloat64 = n => { - WasmRef.toGrain(newFloat64(n)): Float64 -} - -@unsafe -provide let fromFloat64 = (n: Float64) => { - let ref = WasmRef.fromGrain(n) - getFloat64Value(ref) -} - -/** - * Converts a WasmI32 value to Number. - * - * @param n: The WasmI32 to convert - * @returns The value converted to either a simple or a 32 bit heap allocated number. - */ -@unsafe -provide let wasmI32ToNumber = (n: WasmI32) => { - use WasmI32.{ (==) } - // Follows a little optimization. Instead of testing if n is range of allowed - // non heap allocated simple numbers (-1073741824n..1073741823n), actually - // make a simple number, convert it back to WasmI32, and test if it stayed - // the same. This may sound counter intuitive, but for numbers that fit into - // the range it is faster than performing the check with WasmI32.geS and - // WasmI32.leS by about 20%. For the case of numbers not fitting into the - // range, the cost of these simple ALU instructions are negligible compared - // to heap allocations. - - let simple = tagSimpleNumber(n) - - if (untagSimpleNumber(simple) == n) { - // Just test if the untagged number is the same. If it didn't overflow, then - // we're good. We just need to cast the raw value into a Number at the type - // system level. - return simple - } - - // If it did overflow, then the value differs and we need to discard it and - // allocate the number on the heap. We can do this by extending the 32 bit - // number to an Int64. - return WasmRef.toGrain(newInt64(WasmI64.extendI32S(n))): Number -} diff --git a/stdlib/runtime/unsafe/conv.md b/stdlib/runtime/unsafe/conv.md deleted file mode 100644 index d3215c8b75..0000000000 --- a/stdlib/runtime/unsafe/conv.md +++ /dev/null @@ -1,100 +0,0 @@ ---- -title: Conv ---- - -## Values - -Functions and constants included in the Conv module. - -### Conv.**toInt32** - -```grain -toInt32: (n: WasmI32) => Int32 -``` - -### Conv.**toUint32** - -```grain -toUint32: (n: WasmI32) => Uint32 -``` - -### Conv.**fromInt32** - -```grain -fromInt32: (n: Int32) => WasmI32 -``` - -### Conv.**fromUint32** - -```grain -fromUint32: (n: Uint32) => WasmI32 -``` - -### Conv.**toInt64** - -```grain -toInt64: (n: WasmI64) => Int64 -``` - -### Conv.**toUint64** - -```grain -toUint64: (n: WasmI64) => Uint64 -``` - -### Conv.**fromInt64** - -```grain -fromInt64: (n: Int64) => WasmI64 -``` - -### Conv.**fromUint64** - -```grain -fromUint64: (n: Uint64) => WasmI64 -``` - -### Conv.**toFloat32** - -```grain -toFloat32: (n: WasmF32) => Float32 -``` - -### Conv.**fromFloat32** - -```grain -fromFloat32: (n: Float32) => WasmF32 -``` - -### Conv.**toFloat64** - -```grain -toFloat64: (n: WasmF64) => Float64 -``` - -### Conv.**fromFloat64** - -```grain -fromFloat64: (n: Float64) => WasmF64 -``` - -### Conv.**wasmI32ToNumber** - -```grain -wasmI32ToNumber: (n: WasmI32) => Number -``` - -Converts a WasmI32 value to Number. - -Parameters: - -| param | type | description | -| ----- | --------- | ---------------------- | -| `n` | `WasmI32` | The WasmI32 to convert | - -Returns: - -| type | description | -| -------- | ------------------------------------------------------------------------- | -| `Number` | The value converted to either a simple or a 32 bit heap allocated number. | - diff --git a/stdlib/runtime/unsafe/dataStructures.gr b/stdlib/runtime/unsafe/dataStructures.gr new file mode 100644 index 0000000000..8f2be2b8c8 --- /dev/null +++ b/stdlib/runtime/unsafe/dataStructures.gr @@ -0,0 +1,848 @@ +/** + * Low-level utilties for working with Grain's built-in data structures, such as, + * tuples, arrays, records, ADTs, strings, bytes, numbers, etc. + * + * This modules provides unsafe functions and should be used with extreme caution; + * incorrect usage will lead to runtime errors or undefined behavior, the core team + * does not guarantee the stability of these APIs, and they may change without a + * major version bump, use at your own risk. + */ +@runtimeMode +module DataStructures + +from "runtime/unsafe/wasmi32" include WasmI32 +from "runtime/unsafe/wasmi64" include WasmI64 +from "runtime/unsafe/wasmref" include WasmRef +use WasmRef.{ module WasmArrayRef } + +primitive magic = "@magic" +primitive getCompoundValueArrayRef = "@compound_value.refarray" +let getCompoundValueArrayRef = ref => + WasmArrayRef.fromWasmRef(getCompoundValueArrayRef(ref)) + +/** + * Gets the tag of a grain heap value. + * + * @param ref: The reference to the value + * @returns The tag of the value, which can be used to determine its type and how to work with it + */ +provide primitive getHeapTag = "@grain_value.load_tag" + +/** + * Gets the tag of a boxed number. + * + * NOTE: If the provided value is not a boxed number, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the boxed number value + * @returns The boxed Number tag + */ +provide primitive getBoxedNumberTag = "@boxed_number.get_tag" + +/** + * Gets a compound Grain value's cycle marker. + * + * NOTE: If the provided value is not a compound value, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The value ref + * @returns The cycle marker + */ +provide primitive loadCycleMarker = "@grain_value.load_cycle_marker" + +/** + * Sets a compound Grain value's cycle marker. + * + * NOTE: If the provided value is not a compound value, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The value ref + * @param value: The cycle marker to set + */ +provide primitive storeCycleMarker = "@grain_value.store_cycle_marker" + +/** Utilities for working with Grain's tuple data structure. */ +provide module Tuple { + /** The reference type for a Grain tuple. */ + abstract type TupleRef = WasmRef + + primitive make = "@allocate.tuple" + /** + * Creates a new tuple of the specified size, with all elements initialized to `(ref i31 0)`. + * + * @param size: The number of elements to be contained in this tuple + * @returns The reference to the tuple + */ + provide let make = size => make(size): TupleRef + + /** + * Converts a grain tuple to a reference that can be used with the rest of the APIs in this module. + * + * NOTE: If the provided value is not a tuple, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param tuple: The Grain tuple to convert + * @returns The reference to the tuple + */ + provide let fromGrain = (tuple: a) => WasmRef.fromGrain(tuple): TupleRef + + /** + * Converts a tuple reference to a Grain tuple. + * + * NOTE: + * The type information of the tuple is not preserved in the reference form, + * the caller must ensure that the value is of the expected type and arity, + * + * @param ref: The reference to convert + * @returns The Grain tuple + */ + provide let toGrain = (ref: TupleRef) => WasmRef.toGrain(ref): a + + /** + * Gets the size (number of elements) of a Grain tuple. + * + * @param ref: The reference to the Grain tuple value + * @returns The size of the tuple + */ + provide let getSize = (ref: TupleRef) => + WasmArrayRef.length(getCompoundValueArrayRef(ref)) + + /** + * Gets the backing array containing the content of a Grain tuple. + * + * @param ref: The reference to the Grain tuple value + * @returns The reference to the underlying array + */ + provide let getData = (ref: TupleRef) => getCompoundValueArrayRef(ref) +} +/** Utilities for working with Grain's array data structure. */ +provide module Array { + /** The reference type for a Grain array. */ + abstract type ArrayRef = WasmRef + + primitive make = "@allocate.array" + /** + * Creates a new array of the specified size, with all elements initialized to the initial value. + * + * @param size: The number of elements to be contained in this array + * @param initial: The initial value to fill the array with + * @returns The reference to the array + */ + provide let make = (size, initial: WasmRef) => make(size, initial): ArrayRef + + /** + * Converts a grain array to a reference that can be used with the rest of the APIs in this module. + * + * @param arr: The Grain array to convert + * @returns The reference to the array + */ + provide let fromGrain = (arr: Array) => WasmRef.fromGrain(arr): ArrayRef + + /** + * Converts an array reference to a Grain array. + * + * NOTE: + * The type information of the array items are not preserved in the reference form, + * the caller must ensure that the items are of the expected type, otherwise using the + * resulting Grain array may lead to runtime errors or undefined behavior. + * + * @param ref: The reference to convert + * @returns The Grain array + */ + provide let toGrain = (ref: ArrayRef) => WasmRef.toGrain(ref): Array + + /** + * Gets the size (number of elements) of a Grain array. + * + * @param ref: The reference to the Grain array value + * @returns The size of the array + */ + provide let getSize = (ref: ArrayRef) => + WasmArrayRef.length(getCompoundValueArrayRef(ref)) + + /** + * Gets the backing array containing the content of a Grain array. + * + * @param ref: The reference to the Grain array value + * @returns The reference to the underlying array + */ + provide let getData = (ref: ArrayRef) => getCompoundValueArrayRef(ref) +} +/** Utilities for working with Grain's record closure structure. */ +provide module Closure { + /** + * Gets the underlying array containing the content of a Grain lambda closure. + * + * NOTE: If the provided value is not a lambda closure, the behavior is undefined + * and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the Grain lambda closure value + * + * @returns The reference to the underlying array + */ + provide let getData = ref => getCompoundValueArrayRef(ref) +} +/** Utilities for working with Grain's record data structure. */ +provide module Record { + primitive make = "@allocate.record" + /** + * Creates a new record of the specified size, with all fields initialized to `(ref i31 0)`. + * + * @param valuesArray: The WasmArrayRef backing the record's fields + * @param typeHash: The records type hash + * @param typeId: The records type id + * @returns The reference to the record + */ + provide let make = (valuesArray: WasmArrayRef.WasmArrayRef, typeHash, typeId) => + make(magic(valuesArray): WasmRef, typeHash, typeId): WasmRef + + /** + * Gets a record's type hash. + * + * NOTE: If the provided value is not a record, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the record + * @returns: The type hash + */ + provide primitive getTypeHash = "@record.load_typehash" + + /** + * Gets a record's type id. + * + * NOTE: If the provided value is not a record, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the record + * @returns: The type id + */ + provide primitive getTypeId = "@record.load_typeid" + + /** + * Get the size (arity) of a Grain record value. + * + * NOTE: If the provided value is not an record variant, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the Grain record value + * @returns The size of the record value + */ + provide let getSize = ref => + WasmArrayRef.length(getCompoundValueArrayRef(ref)) + + /** + * Gets the backing array containing the content of a Grain record value. + * + * NOTE: If the provided value is not an record variant, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the Grain record value + * @returns The reference to the underlying array + */ + provide let getData = ref => getCompoundValueArrayRef(ref) +} +/** Utilities for working with Grain's ADT data structure. */ +provide module ADT { + primitive make = "@allocate.adt" + /** + * Creates a new ADT value (enum variant) with the specified type and variant information, + * and the provided values array as its content. + * + * @param valuesArray: The `WasmArrayRef` backing the ADT's contents + * @param typeHash: The ADT's type hash + * @param typeId: The ADT's type id + * @param variantId: The ADT variant id + * @returns The reference to the ADT value + */ + provide let make = ( + valuesArray: WasmArrayRef.WasmArrayRef, + typeHash, + typeId, + variantId, + ) => make(magic(valuesArray): WasmRef, typeHash, typeId, variantId): WasmRef + + /** + * Gets a variant's type hash. + * + * NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the variant + * @returns: The type hash + */ + provide primitive getTypeHash = "@adt.load_typehash" + + /** + * Gets a variant's type id. + * + * NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the variant + * @returns: The type id + */ + provide primitive getTypeId = "@adt.load_typeid" + + /** + * Gets the variant id of an ADT value. + * + * @param ref: The reference to the variant + * @returns The variant id of the ADT value + */ + provide primitive getVariantId = "@adt.load_variant" + + /** + * Get the size (arity) of a Grain ADT value. + * + * NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the Grain ADT value + * @returns The size of the ADT value + */ + provide let getSize = ref => + WasmArrayRef.length(getCompoundValueArrayRef(ref)) + + /** + * Gets the backing array containing the content of a Grain ADT value. + * + * NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + * + * @param ref: The reference to the Grain ADT value + * @returns The reference to the underlying array + */ + provide let getData = ref => getCompoundValueArrayRef(ref) +} +/** Utilities for working with Grain's string data structure. */ +provide module String { + /** The reference type for a Grain string. */ + abstract type StringRef = WasmRef + + primitive make = "@allocate.string" + /** + * Creates a new string of the specified size, with all characters initialized to `U+0000`. + * + * @param size: The size (in bytes) of the string to allocate + * @returns The reference to the string + */ + provide let make = size => make(size): StringRef + + /** + * Converts a grain string to a reference that can be used with the rest of the APIs in this module. + * + * @param str: The Grain string to convert + * @returns The reference to the string + */ + provide let fromGrain = (str: String) => WasmRef.fromGrain(str): StringRef + + /** + * Converts a string reference to a Grain string. + * + * @param ref: The reference to convert + * @returns The Grain string + */ + provide let toGrain = (ref: StringRef) => WasmRef.toGrain(ref): String + + primitive getSize = "@string.size" + /** + * Gets the size of a Grain string in bytes. + * + * @param ref: The reference to the Grain String value + * @returns The size of the string + */ + provide let getSize = (ref: StringRef) => getSize(ref) + + primitive getStringArrayRef = "@string.refarray" + /** + * Gets the backing array containing the content of a Grain string. + * + * Grain strings are backed by a byte array of UTF-8 encoded characters, + * more information about the string layout can be found in the contributor docs. + * + * @param ref: The reference to the Grain String value + * @returns The reference to the underlying array + */ + provide let getData = (ref: StringRef) => + WasmArrayRef.fromWasmRef(getStringArrayRef(ref)) +} +/** Utilities for working with Grain's bytes data structure. */ +provide module Bytes { + /** The reference type for Grain bytes. */ + abstract type BytesRef = WasmRef + + primitive make = "@allocate.bytes" + /** + * Creates a new bytes buffer of the specified size, with all bytes initialized to 0. + * + * @param size: The number of bytes to be contained in this buffer + * @returns The reference to the bytes + */ + provide let make = size => make(size): BytesRef + + /** + * Converts a grain bytes value to a reference that can be used with the rest of the APIs in this module. + * + * @param bytes: The Grain bytes value to convert + * @returns The reference to the bytes value + */ + provide let fromGrain = (bytes: Bytes) => WasmRef.fromGrain(bytes): BytesRef + + /** + * Converts a bytes reference to a Grain bytes value. + * + * @param ref: The reference to convert + * @returns The Grain bytes value + */ + provide let toGrain = (ref: BytesRef) => WasmRef.toGrain(ref): Bytes + + primitive getSize = "@bytes.size" + /** + * Gets the size of a Grain Bytes value. + * + * @param ref: The reference to the Grain Bytes value + * @returns The size of the bytes + */ + provide let getSize = (ref: BytesRef) => getSize(ref) + + primitive getBytesArrayRef = "@bytes.refarray" + /** + * Gets the backing array containing the bytes of a Grain Bytes value. + * + * @param ref: The reference to the Grain Bytes value + * @returns The reference to the underlying array + */ + provide let getData = (ref: BytesRef) => + WasmArrayRef.fromWasmRef(getBytesArrayRef(ref)) +} + +// --- Numbers --- + +/** Utilities for working with Grain's int32 data structure. */ +provide module Int32 { + /** + * Creates a new Int32 with the specified value. + * + * @param int: The value to store + * @returns The reference to the Int32 + */ + provide primitive make = "@new.int32" + + /** + * Gets the value of an Int32 + * + * @param ref: The reference to the Int32 value + * @returns The inner i32 value + */ + provide primitive getValue = "@boxed_number.get_int32" + + /** + * Converts a WasmI32 value to a Grain Int32. + * + * @param n: The number to convert + * @returns The value converted to a Grain Int32 + */ + @unsafe + provide let toInt32 = n => { + WasmRef.toGrain(make(n)): Int32 + } + + /** + * Converts a Grain Int32 to a WasmI32 value. + * + * @param n: The value to convert + * @returns The value converted to a WasmI32 + */ + @unsafe + provide let fromInt32 = (n: Int32) => { + getValue(WasmRef.fromGrain(n)) + } +} +/** Utilities for working with Grain's uint32 data structure. */ +provide module Uint32 { + /** + * Creates a new Uint32 with the specified value. + * + * @param int: The value to store + * @returns The reference to the Uint32 + */ + provide primitive make = "@new.uint32" + + /** + * Gets the value of a Uint32 + * + * @param ref: The reference to the Uint32 value + * @returns The inner i32 value + */ + provide primitive getValue = "@boxed_number.get_uint32" + + /** + * Converts a WasmI32 value to a Grain Uint32. + * + * @param n: The number to convert + * @returns The value converted to a Grain Uint32 + */ + @unsafe + provide let toUint32 = n => { + WasmRef.toGrain(make(n)): Uint32 + } + + /** + * Converts a Grain Uint32 to a WasmI32 value. + * + * @param n: The value to convert + * @returns The value converted to a WasmI32 + */ + @unsafe + provide let fromUint32 = (n: Uint32) => { + getValue(WasmRef.fromGrain(n)) + } +} +/** Utilities for working with Grain's int64 data structure. */ +provide module Int64 { + /** + * Creates a new Int64 with the specified value. + * + * @param int: The value to store + * @returns The reference to the Int64 + */ + provide primitive make = "@new.int64" + + /** + * Gets the value of an Int64 + * + * @param ref: The reference to the Int64 value + * @returns The inner i64 value + */ + provide primitive getValue = "@boxed_number.get_int64" + + /** + * Converts a WasmI64 value to a Grain Int64. + * + * @param n: The number to convert + * @returns The value converted to a Grain Int64 + */ + @unsafe + provide let toInt64 = n => { + WasmRef.toGrain(make(n)): Int64 + } + + /** + * Converts a Grain Int64 to a WasmI64 value. + * + * @param n: The value to convert + * @returns The value converted to a WasmI64 + */ + @unsafe + provide let fromInt64 = (n: Int64) => { + getValue(WasmRef.fromGrain(n)) + } +} +/** Utilities for working with Grain's uint64 data structure. */ +provide module Uint64 { + /** + * Creates a new Uint64 with the specified value. + * + * @param int: The value to store + * @returns The reference to the Uint64 + */ + provide primitive make = "@new.uint64" + + /** + * Gets the value of a Uint64 + * + * @param ref: The reference to the Uint64 value + * @returns The inner i64 value + */ + provide primitive getValue = "@boxed_number.get_uint64" + + /** + * Converts a WasmI64 value to a Grain Uint64. + * + * @param n: The number to convert + * @returns The value converted to a Grain Uint64 + */ + @unsafe + provide let toUint64 = n => { + WasmRef.toGrain(make(n)): Uint64 + } + + /** + * Converts a Grain Uint64 to a WasmI64 value. + * + * @param n: The value to convert + * @returns The value converted to a WasmI64 + */ + @unsafe + provide let fromUint64 = (n: Uint64) => { + getValue(WasmRef.fromGrain(n)) + } +} +/** Utilities for working with Grain's float32 data structure. */ +provide module Float32 { + /** + * Creates a new Float32 with the specified value. + * + * @param float: The value to store + * @returns the reference to the Float32 + */ + provide primitive make = "@new.float32" + + /** + * Gets the value of a Float32 + * + * @param ref: The reference to the Float32 value + * @returns The inner f32 value + */ + provide primitive getValue = "@boxed_number.get_float32" + + /** + * Converts a WasmF32 value to a Grain Float32. + * + * @param n: The number to convert + * @returns The value converted to a Grain Float32 + */ + @unsafe + provide let toFloat32 = n => { + WasmRef.toGrain(make(n)): Float32 + } + + /** + * Converts a Grain Float32 to a WasmF32 value. + * + * @param n: The value to convert + * @returns The value converted to a WasmF32 + */ + @unsafe + provide let fromFloat32 = (n: Float32) => { + getValue(WasmRef.fromGrain(n)) + } +} +/** Utilities for working with Grain's float64 data structure. */ +provide module Float64 { + /** + * Creates a new Float64 with the specified value. + * + * @param float: The value to store + * @returns The reference to the Float64 + */ + provide primitive make = "@new.float64" + + /** + * Gets the value of a Float64 + * + * @param ref: The reference to the Float64 value + * @returns The inner f64 value + */ + provide primitive getValue = "@boxed_number.get_float64" + + /** + * Converts a WasmF64 value to a Grain Float64. + * + * @param n: The number to convert + * @returns The value converted to a Grain Float64 + */ + @unsafe + provide let toFloat64 = n => { + WasmRef.toGrain(make(n)): Float64 + } + + /** + * Converts a Grain Float64 to a WasmF64 value. + * + * @param n: The value to convert + * @returns The value converted to a WasmF64 + */ + @unsafe + provide let fromFloat64 = (n: Float64) => { + getValue(WasmRef.fromGrain(n)) + } +} +/** Utilities for working with Grain's rational data structure. */ +provide module Rational { + /** + * Creates a new Rational with the specified numerator and denominator values. + * + * @param numerator: The numerator value to store + * @param denominator: The denominator value to store + * @returns The reference to the Rational + */ + provide primitive make = "@new.rational" + + /** + * Gets the numerator of a Rational + * + * @param ref: The reference to the Rational value + * @returns The rational numerator + */ + provide primitive getNumerator = "@boxed_number.get_rational_numerator" + + /** + * Gets the denominator of a Rational + * + * @param ref: The reference to the Rational value + * @returns The rational denominator + */ + provide primitive getDenominator = "@boxed_number.get_rational_denominator" +} +/** Utilities for working with Grain's bigint data structure. */ +provide module BigInt { + /** + * Creates a new BigInt with the specified number of limbs, with all limbs initialized to 0, and returns a reference to it. + * + * @param size: The number of limbs + * @returns The reference to the BigInt + */ + provide primitive make = "@allocate.bigint" + + /** + * Gets number of limbs of a BigInt. + * + * @param ref: The reference to the BigInt value + * @returns The number of BigInt limbs + */ + provide primitive getSize = "@bigint.size" + + /** + * Gets the flags of a BigInt. + * + * @param ref: The reference to the BigInt value + * @returns The BigInt flags + */ + provide primitive getFlags = "@bigint.flags" + + /** + * Sets the flags of a BigInt. + * + * @param ref: The reference to the BigInt value + * @param flags: The BigInt flags to set + */ + provide primitive setFlags = "@bigint.set_flags" + + primitive getBigIntArrayRef = "@bigint.refarray" + /** + * Gets the reference to the underlying array containing the BigInt limbs, + * the layout of which can be found in the contributor docs. + * + * @param ref: The reference to the BigInt value + * @returns The reference to the BigInt limbs + */ + provide let getLimbData = ref => + WasmArrayRef.fromWasmRef(getBigIntArrayRef(ref)) +} +/** + * Tag a simple number. + * + * @param num: The number to tag + * @returns The tagged number + */ +provide primitive tagSimpleNumber = "@tag.simple_number" + +/** + * Untag a simple number. + * + * @param num: The number to untag + * @returns The untagged number + */ +provide primitive untagSimpleNumber = "@untag.simple_number" + +/** + * Converts a WasmI32 value to Number. + * + * @param n: The WasmI32 to convert + * @returns The value converted to either a simple or a 32 bit heap allocated number. + */ +@unsafe +provide let wasmI32ToNumber = (n: WasmI32) => { + use WasmI32.{ (==) } + // Follows a little optimization. Instead of testing if n is range of allowed + // non heap allocated simple numbers (-1073741824n..1073741823n), actually + // make a simple number, convert it back to WasmI32, and test if it stayed + // the same. This may sound counter intuitive, but for numbers that fit into + // the range it is faster than performing the check with WasmI32.geS and + // WasmI32.leS by about 20%. For the case of numbers not fitting into the + // range, the cost of these simple ALU instructions are negligible compared + // to heap allocations. + + let simple = tagSimpleNumber(n) + + if (untagSimpleNumber(simple) == n) { + // Just test if the untagged number is the same. If it didn't overflow, then + // we're good. We just need to cast the raw value into a Number at the type + // system level. + return simple + } + + // If it did overflow, then the value differs and we need to discard it and + // allocate the number on the heap. We can do this by extending the 32 bit + // number to an Int64. + return WasmRef.toGrain(Int64.make(WasmI64.extendI32S(n))): Number +} + +/** + * Tag a char. + * + * @param char: The usv to tag + * @returns The tagged char + */ +provide primitive tagChar = "@tag.char" + +/** + * Untag a char. + * + * @param char: The char to untag + * @returns The untagged usv + */ +provide primitive untagChar = "@untag.char" + +/** + * Tag an int8. + * + * @param int: The int8 to tag + * @returns The tagged int8 + */ +provide primitive tagInt8 = "@tag.int8" + +/** + * Untag an int8. + * + * @param int: The int8 to untag + * @returns The untagged int8 + */ +provide primitive untagInt8 = "@untag.int8" + +/** + * Tag an int16. + * + * @param int: The int16 to tag + * @returns The tagged int16 + */ +provide primitive tagInt16 = "@tag.int16" + +/** + * Untag an int16. + * + * @param int: The int16 to untag + * @returns The untagged int16 + */ +provide primitive untagInt16 = "@untag.int16" + +/** + * Tag a uint8. + * + * @param int: The uint8 to tag + * @returns The tagged uint8 + */ +provide primitive tagUint8 = "@tag.uint8" + +/** + * Untag a uint8. + * + * @param int: The uint8 to untag + * @returns The untagged uint8 + */ +provide primitive untagUint8 = "@untag.uint8" + +/** + * Tag a uint16. + * + * @param int: The uint16 to tag + * @returns The tagged uint16 + */ +provide primitive tagUint16 = "@tag.uint16" + +/** + * Untag a uint16. + * + * @param int: The uint16 to untag + * @returns The untagged uint16 + */ +provide primitive untagUint16 = "@untag.uint16" diff --git a/stdlib/runtime/unsafe/dataStructures.md b/stdlib/runtime/unsafe/dataStructures.md new file mode 100644 index 0000000000..fdb672ca11 --- /dev/null +++ b/stdlib/runtime/unsafe/dataStructures.md @@ -0,0 +1,1846 @@ +--- +title: DataStructures +--- + +Low-level utilties for working with Grain's built-in data structures, such as, +tuples, arrays, records, ADTs, strings, bytes, numbers, etc. + +This modules provides unsafe functions and should be used with extreme caution; +incorrect usage will lead to runtime errors or undefined behavior, the core team +does not guarantee the stability of these APIs, and they may change without a +major version bump, use at your own risk. + +## Values + +Functions and constants included in the DataStructures module. + +### DataStructures.**getHeapTag** + +```grain +getHeapTag: (ref: WasmRef) => WasmI32 +``` + +Gets the tag of a grain heap value. + +Parameters: + +| param | type | description | +| ----- | --------- | -------------------------- | +| `ref` | `WasmRef` | The reference to the value | + +Returns: + +| type | description | +| --------- | ------------------------------------------------------------------------------------- | +| `WasmI32` | The tag of the value, which can be used to determine its type and how to work with it | + +### DataStructures.**getBoxedNumberTag** + +```grain +getBoxedNumberTag: (ref: WasmRef) => WasmI32 +``` + +Gets the tag of a boxed number. + +NOTE: If the provided value is not a boxed number, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------------- | +| `ref` | `WasmRef` | The reference to the boxed number value | + +Returns: + +| type | description | +| --------- | -------------------- | +| `WasmI32` | The boxed Number tag | + +### DataStructures.**loadCycleMarker** + +```grain +loadCycleMarker: (ref: WasmRef) => WasmI32 +``` + +Gets a compound Grain value's cycle marker. + +NOTE: If the provided value is not a compound value, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | ------------- | +| `ref` | `WasmRef` | The value ref | + +Returns: + +| type | description | +| --------- | ---------------- | +| `WasmI32` | The cycle marker | + +### DataStructures.**storeCycleMarker** + +```grain +storeCycleMarker: (ref: WasmRef, value: WasmI32) => Void +``` + +Sets a compound Grain value's cycle marker. + +NOTE: If the provided value is not a compound value, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ------- | --------- | ----------------------- | +| `ref` | `WasmRef` | The value ref | +| `value` | `WasmI32` | The cycle marker to set | + +### DataStructures.**tagSimpleNumber** + +```grain +tagSimpleNumber: (num: WasmI32) => Number +``` + +Tag a simple number. + +Parameters: + +| param | type | description | +| ----- | --------- | ----------------- | +| `num` | `WasmI32` | The number to tag | + +Returns: + +| type | description | +| -------- | ----------------- | +| `Number` | The tagged number | + +### DataStructures.**untagSimpleNumber** + +```grain +untagSimpleNumber: (num: Number) => WasmI32 +``` + +Untag a simple number. + +Parameters: + +| param | type | description | +| ----- | -------- | ------------------- | +| `num` | `Number` | The number to untag | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmI32` | The untagged number | + +### DataStructures.**wasmI32ToNumber** + +```grain +wasmI32ToNumber: (n: WasmI32) => Number +``` + +Converts a WasmI32 value to Number. + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------------- | +| `n` | `WasmI32` | The WasmI32 to convert | + +Returns: + +| type | description | +| -------- | ------------------------------------------------------------------------- | +| `Number` | The value converted to either a simple or a 32 bit heap allocated number. | + +### DataStructures.**tagChar** + +```grain +tagChar: (char: WasmI32) => Char +``` + +Tag a char. + +Parameters: + +| param | type | description | +| ------ | --------- | -------------- | +| `char` | `WasmI32` | The usv to tag | + +Returns: + +| type | description | +| ------ | --------------- | +| `Char` | The tagged char | + +### DataStructures.**untagChar** + +```grain +untagChar: (char: Char) => WasmI32 +``` + +Untag a char. + +Parameters: + +| param | type | description | +| ------ | ------ | ----------------- | +| `char` | `Char` | The char to untag | + +Returns: + +| type | description | +| --------- | ---------------- | +| `WasmI32` | The untagged usv | + +### DataStructures.**tagInt8** + +```grain +tagInt8: (int: WasmI32) => Int8 +``` + +Tag an int8. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------- | +| `int` | `WasmI32` | The int8 to tag | + +Returns: + +| type | description | +| ------ | --------------- | +| `Int8` | The tagged int8 | + +### DataStructures.**untagInt8** + +```grain +untagInt8: (int: Int8) => WasmI32 +``` + +Untag an int8. + +Parameters: + +| param | type | description | +| ----- | ------ | ----------------- | +| `int` | `Int8` | The int8 to untag | + +Returns: + +| type | description | +| --------- | ----------------- | +| `WasmI32` | The untagged int8 | + +### DataStructures.**tagInt16** + +```grain +tagInt16: (int: WasmI32) => Int16 +``` + +Tag an int16. + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------- | +| `int` | `WasmI32` | The int16 to tag | + +Returns: + +| type | description | +| ------- | ---------------- | +| `Int16` | The tagged int16 | + +### DataStructures.**untagInt16** + +```grain +untagInt16: (int: Int16) => WasmI32 +``` + +Untag an int16. + +Parameters: + +| param | type | description | +| ----- | ------- | ------------------ | +| `int` | `Int16` | The int16 to untag | + +Returns: + +| type | description | +| --------- | ------------------ | +| `WasmI32` | The untagged int16 | + +### DataStructures.**tagUint8** + +```grain +tagUint8: (int: WasmI32) => Uint8 +``` + +Tag a uint8. + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------- | +| `int` | `WasmI32` | The uint8 to tag | + +Returns: + +| type | description | +| ------- | ---------------- | +| `Uint8` | The tagged uint8 | + +### DataStructures.**untagUint8** + +```grain +untagUint8: (int: Uint8) => WasmI32 +``` + +Untag a uint8. + +Parameters: + +| param | type | description | +| ----- | ------- | ------------------ | +| `int` | `Uint8` | The uint8 to untag | + +Returns: + +| type | description | +| --------- | ------------------ | +| `WasmI32` | The untagged uint8 | + +### DataStructures.**tagUint16** + +```grain +tagUint16: (int: WasmI32) => Uint16 +``` + +Tag a uint16. + +Parameters: + +| param | type | description | +| ----- | --------- | ----------------- | +| `int` | `WasmI32` | The uint16 to tag | + +Returns: + +| type | description | +| -------- | ----------------- | +| `Uint16` | The tagged uint16 | + +### DataStructures.**untagUint16** + +```grain +untagUint16: (int: Uint16) => WasmI32 +``` + +Untag a uint16. + +Parameters: + +| param | type | description | +| ----- | -------- | ------------------- | +| `int` | `Uint16` | The uint16 to untag | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmI32` | The untagged uint16 | + +## DataStructures.Tuple + +Utilities for working with Grain's tuple data structure. + +### Types + +Type declarations included in the DataStructures.Tuple module. + +#### DataStructures.Tuple.**TupleRef** + +```grain +type TupleRef +``` + +The reference type for a Grain tuple. + +### Values + +Functions and constants included in the DataStructures.Tuple module. + +#### DataStructures.Tuple.**make** + +```grain +make: (size: WasmI32) => TupleRef +``` + +Creates a new tuple of the specified size, with all elements initialized to `(ref i31 0)`. + +Parameters: + +| param | type | description | +| ------ | --------- | ---------------------------------------------------- | +| `size` | `WasmI32` | The number of elements to be contained in this tuple | + +Returns: + +| type | description | +| ---------- | -------------------------- | +| `TupleRef` | The reference to the tuple | + +#### DataStructures.Tuple.**fromGrain** + +```grain +fromGrain: (tuple: a) => TupleRef +``` + +Converts a grain tuple to a reference that can be used with the rest of the APIs in this module. + +NOTE: If the provided value is not a tuple, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ------- | ---- | -------------------------- | +| `tuple` | `a` | The Grain tuple to convert | + +Returns: + +| type | description | +| ---------- | -------------------------- | +| `TupleRef` | The reference to the tuple | + +#### DataStructures.Tuple.**toGrain** + +```grain +toGrain: (ref: TupleRef) => a +``` + +Converts a tuple reference to a Grain tuple. + +NOTE: +The type information of the tuple is not preserved in the reference form, +the caller must ensure that the value is of the expected type and arity, + +Parameters: + +| param | type | description | +| ----- | ---------- | ------------------------ | +| `ref` | `TupleRef` | The reference to convert | + +Returns: + +| type | description | +| ---- | --------------- | +| `a` | The Grain tuple | + +#### DataStructures.Tuple.**getSize** + +```grain +getSize: (ref: TupleRef) => WasmI32 +``` + +Gets the size (number of elements) of a Grain tuple. + +Parameters: + +| param | type | description | +| ----- | ---------- | -------------------------------------- | +| `ref` | `TupleRef` | The reference to the Grain tuple value | + +Returns: + +| type | description | +| --------- | --------------------- | +| `WasmI32` | The size of the tuple | + +#### DataStructures.Tuple.**getData** + +```grain +getData: (ref: TupleRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the backing array containing the content of a Grain tuple. + +Parameters: + +| param | type | description | +| ----- | ---------- | -------------------------------------- | +| `ref` | `TupleRef` | The reference to the Grain tuple value | + +Returns: + +| type | description | +| ----------------------------------- | ------------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the underlying array | + +## DataStructures.Array + +Utilities for working with Grain's array data structure. + +### Types + +Type declarations included in the DataStructures.Array module. + +#### DataStructures.Array.**ArrayRef** + +```grain +type ArrayRef +``` + +The reference type for a Grain array. + +### Values + +Functions and constants included in the DataStructures.Array module. + +#### DataStructures.Array.**make** + +```grain +make: (size: WasmI32, initial: WasmRef) => ArrayRef +``` + +Creates a new array of the specified size, with all elements initialized to the initial value. + +Parameters: + +| param | type | description | +| --------- | --------- | ---------------------------------------------------- | +| `size` | `WasmI32` | The number of elements to be contained in this array | +| `initial` | `WasmRef` | The initial value to fill the array with | + +Returns: + +| type | description | +| ---------- | -------------------------- | +| `ArrayRef` | The reference to the array | + +#### DataStructures.Array.**fromGrain** + +```grain +fromGrain: (arr: Array) => ArrayRef +``` + +Converts a grain array to a reference that can be used with the rest of the APIs in this module. + +Parameters: + +| param | type | description | +| ----- | ---------- | -------------------------- | +| `arr` | `Array` | The Grain array to convert | + +Returns: + +| type | description | +| ---------- | -------------------------- | +| `ArrayRef` | The reference to the array | + +#### DataStructures.Array.**toGrain** + +```grain +toGrain: (ref: ArrayRef) => Array +``` + +Converts an array reference to a Grain array. + +NOTE: +The type information of the array items are not preserved in the reference form, +the caller must ensure that the items are of the expected type, otherwise using the +resulting Grain array may lead to runtime errors or undefined behavior. + +Parameters: + +| param | type | description | +| ----- | ---------- | ------------------------ | +| `ref` | `ArrayRef` | The reference to convert | + +Returns: + +| type | description | +| ---------- | --------------- | +| `Array` | The Grain array | + +#### DataStructures.Array.**getSize** + +```grain +getSize: (ref: ArrayRef) => WasmI32 +``` + +Gets the size (number of elements) of a Grain array. + +Parameters: + +| param | type | description | +| ----- | ---------- | -------------------------------------- | +| `ref` | `ArrayRef` | The reference to the Grain array value | + +Returns: + +| type | description | +| --------- | --------------------- | +| `WasmI32` | The size of the array | + +#### DataStructures.Array.**getData** + +```grain +getData: (ref: ArrayRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the backing array containing the content of a Grain array. + +Parameters: + +| param | type | description | +| ----- | ---------- | -------------------------------------- | +| `ref` | `ArrayRef` | The reference to the Grain array value | + +Returns: + +| type | description | +| ----------------------------------- | ------------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the underlying array | + +## DataStructures.Closure + +Utilities for working with Grain's record closure structure. + +### Values + +Functions and constants included in the DataStructures.Closure module. + +#### DataStructures.Closure.**getData** + +```grain +getData: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the underlying array containing the content of a Grain lambda closure. + +NOTE: If the provided value is not a lambda closure, the behavior is undefined + and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | ----------------------------------------------- | +| `ref` | `WasmRef` | The reference to the Grain lambda closure value | + +Returns: + +| type | description | +| ----------------------------------- | ------------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the underlying array | + +## DataStructures.Record + +Utilities for working with Grain's record data structure. + +### Values + +Functions and constants included in the DataStructures.Record module. + +#### DataStructures.Record.**make** + +```grain +make: + (valuesArray: WasmRef.WasmArrayRef.WasmArrayRef, typeHash: WasmRef, + typeId: WasmRef) => WasmRef +``` + +Creates a new record of the specified size, with all fields initialized to `(ref i31 0)`. + +Parameters: + +| param | type | description | +| ------------- | ----------------------------------- | -------------------------------------------- | +| `valuesArray` | `WasmRef.WasmArrayRef.WasmArrayRef` | The WasmArrayRef backing the record's fields | +| `typeHash` | `WasmRef` | The records type hash | +| `typeId` | `WasmRef` | The records type id | + +Returns: + +| type | description | +| --------- | --------------------------- | +| `WasmRef` | The reference to the record | + +#### DataStructures.Record.**getTypeHash** + +```grain +getTypeHash: (ref: WasmRef) => Number +``` + +Gets a record's type hash. + +NOTE: If the provided value is not a record, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------- | +| `ref` | `WasmRef` | The reference to the record | + +Returns: + +| type | description | +| -------- | --------------- | +| `Number` | : The type hash | + +#### DataStructures.Record.**getTypeId** + +```grain +getTypeId: (ref: WasmRef) => Number +``` + +Gets a record's type id. + +NOTE: If the provided value is not a record, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------- | +| `ref` | `WasmRef` | The reference to the record | + +Returns: + +| type | description | +| -------- | ------------- | +| `Number` | : The type id | + +#### DataStructures.Record.**getSize** + +```grain +getSize: (ref: WasmRef) => WasmI32 +``` + +Get the size (arity) of a Grain record value. + +NOTE: If the provided value is not an record variant, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------------- | +| `ref` | `WasmRef` | The reference to the Grain record value | + +Returns: + +| type | description | +| --------- | ---------------------------- | +| `WasmI32` | The size of the record value | + +#### DataStructures.Record.**getData** + +```grain +getData: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the backing array containing the content of a Grain record value. + +NOTE: If the provided value is not an record variant, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------------- | +| `ref` | `WasmRef` | The reference to the Grain record value | + +Returns: + +| type | description | +| ----------------------------------- | ------------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the underlying array | + +## DataStructures.ADT + +Utilities for working with Grain's ADT data structure. + +### Values + +Functions and constants included in the DataStructures.ADT module. + +#### DataStructures.ADT.**make** + +```grain +make: + (valuesArray: WasmRef.WasmArrayRef.WasmArrayRef, typeHash: WasmRef, + typeId: WasmRef, variantId: WasmRef) => WasmRef +``` + +Creates a new ADT value (enum variant) with the specified type and variant information, +and the provided values array as its content. + +Parameters: + +| param | type | description | +| ------------- | ----------------------------------- | --------------------------------------------- | +| `valuesArray` | `WasmRef.WasmArrayRef.WasmArrayRef` | The `WasmArrayRef` backing the ADT's contents | +| `typeHash` | `WasmRef` | The ADT's type hash | +| `typeId` | `WasmRef` | The ADT's type id | +| `variantId` | `WasmRef` | The ADT variant id | + +Returns: + +| type | description | +| --------- | ------------------------------ | +| `WasmRef` | The reference to the ADT value | + +#### DataStructures.ADT.**getTypeHash** + +```grain +getTypeHash: (ref: WasmRef) => Number +``` + +Gets a variant's type hash. + +NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------------------- | +| `ref` | `WasmRef` | The reference to the variant | + +Returns: + +| type | description | +| -------- | --------------- | +| `Number` | : The type hash | + +#### DataStructures.ADT.**getTypeId** + +```grain +getTypeId: (ref: WasmRef) => Number +``` + +Gets a variant's type id. + +NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------------------- | +| `ref` | `WasmRef` | The reference to the variant | + +Returns: + +| type | description | +| -------- | ------------- | +| `Number` | : The type id | + +#### DataStructures.ADT.**getVariantId** + +```grain +getVariantId: (ref: WasmRef) => Number +``` + +Gets the variant id of an ADT value. + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------------------- | +| `ref` | `WasmRef` | The reference to the variant | + +Returns: + +| type | description | +| -------- | ------------------------------- | +| `Number` | The variant id of the ADT value | + +#### DataStructures.ADT.**getSize** + +```grain +getSize: (ref: WasmRef) => WasmI32 +``` + +Get the size (arity) of a Grain ADT value. + +NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | ------------------------------------ | +| `ref` | `WasmRef` | The reference to the Grain ADT value | + +Returns: + +| type | description | +| --------- | ------------------------- | +| `WasmI32` | The size of the ADT value | + +#### DataStructures.ADT.**getData** + +```grain +getData: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the backing array containing the content of a Grain ADT value. + +NOTE: If the provided value is not an ADT variant, the behavior is undefined and will lead to runtime errors, use with caution. + +Parameters: + +| param | type | description | +| ----- | --------- | ------------------------------------ | +| `ref` | `WasmRef` | The reference to the Grain ADT value | + +Returns: + +| type | description | +| ----------------------------------- | ------------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the underlying array | + +## DataStructures.String + +Utilities for working with Grain's string data structure. + +### Types + +Type declarations included in the DataStructures.String module. + +#### DataStructures.String.**StringRef** + +```grain +type StringRef +``` + +The reference type for a Grain string. + +### Values + +Functions and constants included in the DataStructures.String module. + +#### DataStructures.String.**make** + +```grain +make: (size: WasmI32) => StringRef +``` + +Creates a new string of the specified size, with all characters initialized to `U+0000`. + +Parameters: + +| param | type | description | +| ------ | --------- | --------------------------------------------- | +| `size` | `WasmI32` | The size (in bytes) of the string to allocate | + +Returns: + +| type | description | +| ----------- | --------------------------- | +| `StringRef` | The reference to the string | + +#### DataStructures.String.**fromGrain** + +```grain +fromGrain: (str: String) => StringRef +``` + +Converts a grain string to a reference that can be used with the rest of the APIs in this module. + +Parameters: + +| param | type | description | +| ----- | -------- | --------------------------- | +| `str` | `String` | The Grain string to convert | + +Returns: + +| type | description | +| ----------- | --------------------------- | +| `StringRef` | The reference to the string | + +#### DataStructures.String.**toGrain** + +```grain +toGrain: (ref: StringRef) => String +``` + +Converts a string reference to a Grain string. + +Parameters: + +| param | type | description | +| ----- | ----------- | ------------------------ | +| `ref` | `StringRef` | The reference to convert | + +Returns: + +| type | description | +| -------- | ---------------- | +| `String` | The Grain string | + +#### DataStructures.String.**getSize** + +```grain +getSize: (ref: StringRef) => WasmI32 +``` + +Gets the size of a Grain string in bytes. + +Parameters: + +| param | type | description | +| ----- | ----------- | --------------------------------------- | +| `ref` | `StringRef` | The reference to the Grain String value | + +Returns: + +| type | description | +| --------- | ---------------------- | +| `WasmI32` | The size of the string | + +#### DataStructures.String.**getData** + +```grain +getData: (ref: StringRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the backing array containing the content of a Grain string. + +Grain strings are backed by a byte array of UTF-8 encoded characters, +more information about the string layout can be found in the contributor docs. + +Parameters: + +| param | type | description | +| ----- | ----------- | --------------------------------------- | +| `ref` | `StringRef` | The reference to the Grain String value | + +Returns: + +| type | description | +| ----------------------------------- | ------------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the underlying array | + +## DataStructures.Bytes + +Utilities for working with Grain's bytes data structure. + +### Types + +Type declarations included in the DataStructures.Bytes module. + +#### DataStructures.Bytes.**BytesRef** + +```grain +type BytesRef +``` + +The reference type for Grain bytes. + +### Values + +Functions and constants included in the DataStructures.Bytes module. + +#### DataStructures.Bytes.**make** + +```grain +make: (size: WasmI32) => BytesRef +``` + +Creates a new bytes buffer of the specified size, with all bytes initialized to 0. + +Parameters: + +| param | type | description | +| ------ | --------- | -------------------------------------------------- | +| `size` | `WasmI32` | The number of bytes to be contained in this buffer | + +Returns: + +| type | description | +| ---------- | -------------------------- | +| `BytesRef` | The reference to the bytes | + +#### DataStructures.Bytes.**fromGrain** + +```grain +fromGrain: (bytes: Bytes) => BytesRef +``` + +Converts a grain bytes value to a reference that can be used with the rest of the APIs in this module. + +Parameters: + +| param | type | description | +| ------- | ------- | -------------------------------- | +| `bytes` | `Bytes` | The Grain bytes value to convert | + +Returns: + +| type | description | +| ---------- | -------------------------------- | +| `BytesRef` | The reference to the bytes value | + +#### DataStructures.Bytes.**toGrain** + +```grain +toGrain: (ref: BytesRef) => Bytes +``` + +Converts a bytes reference to a Grain bytes value. + +Parameters: + +| param | type | description | +| ----- | ---------- | ------------------------ | +| `ref` | `BytesRef` | The reference to convert | + +Returns: + +| type | description | +| ------- | --------------------- | +| `Bytes` | The Grain bytes value | + +#### DataStructures.Bytes.**getSize** + +```grain +getSize: (ref: BytesRef) => WasmI32 +``` + +Gets the size of a Grain Bytes value. + +Parameters: + +| param | type | description | +| ----- | ---------- | -------------------------------------- | +| `ref` | `BytesRef` | The reference to the Grain Bytes value | + +Returns: + +| type | description | +| --------- | --------------------- | +| `WasmI32` | The size of the bytes | + +#### DataStructures.Bytes.**getData** + +```grain +getData: (ref: BytesRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the backing array containing the bytes of a Grain Bytes value. + +Parameters: + +| param | type | description | +| ----- | ---------- | -------------------------------------- | +| `ref` | `BytesRef` | The reference to the Grain Bytes value | + +Returns: + +| type | description | +| ----------------------------------- | ------------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the underlying array | + +## DataStructures.Int32 + +Utilities for working with Grain's int32 data structure. + +### Values + +Functions and constants included in the DataStructures.Int32 module. + +#### DataStructures.Int32.**make** + +```grain +make: (int: WasmI32) => WasmRef +``` + +Creates a new Int32 with the specified value. + +Parameters: + +| param | type | description | +| ----- | --------- | ------------------ | +| `int` | `WasmI32` | The value to store | + +Returns: + +| type | description | +| --------- | -------------------------- | +| `WasmRef` | The reference to the Int32 | + +#### DataStructures.Int32.**getValue** + +```grain +getValue: (ref: WasmRef) => WasmI32 +``` + +Gets the value of an Int32 + +Parameters: + +| param | type | description | +| ----- | --------- | -------------------------------- | +| `ref` | `WasmRef` | The reference to the Int32 value | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmI32` | The inner i32 value | + +#### DataStructures.Int32.**toInt32** + +```grain +toInt32: (n: WasmI32) => Int32 +``` + +Converts a WasmI32 value to a Grain Int32. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------- | +| `n` | `WasmI32` | The number to convert | + +Returns: + +| type | description | +| ------- | ------------------------------------ | +| `Int32` | The value converted to a Grain Int32 | + +#### DataStructures.Int32.**fromInt32** + +```grain +fromInt32: (n: Int32) => WasmI32 +``` + +Converts a Grain Int32 to a WasmI32 value. + +Parameters: + +| param | type | description | +| ----- | ------- | -------------------- | +| `n` | `Int32` | The value to convert | + +Returns: + +| type | description | +| --------- | -------------------------------- | +| `WasmI32` | The value converted to a WasmI32 | + +## DataStructures.Uint32 + +Utilities for working with Grain's uint32 data structure. + +### Values + +Functions and constants included in the DataStructures.Uint32 module. + +#### DataStructures.Uint32.**make** + +```grain +make: (int: WasmI32) => WasmRef +``` + +Creates a new Uint32 with the specified value. + +Parameters: + +| param | type | description | +| ----- | --------- | ------------------ | +| `int` | `WasmI32` | The value to store | + +Returns: + +| type | description | +| --------- | --------------------------- | +| `WasmRef` | The reference to the Uint32 | + +#### DataStructures.Uint32.**getValue** + +```grain +getValue: (ref: WasmRef) => WasmI32 +``` + +Gets the value of a Uint32 + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------- | +| `ref` | `WasmRef` | The reference to the Uint32 value | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmI32` | The inner i32 value | + +#### DataStructures.Uint32.**toUint32** + +```grain +toUint32: (n: WasmI32) => Uint32 +``` + +Converts a WasmI32 value to a Grain Uint32. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------- | +| `n` | `WasmI32` | The number to convert | + +Returns: + +| type | description | +| -------- | ------------------------------------- | +| `Uint32` | The value converted to a Grain Uint32 | + +#### DataStructures.Uint32.**fromUint32** + +```grain +fromUint32: (n: Uint32) => WasmI32 +``` + +Converts a Grain Uint32 to a WasmI32 value. + +Parameters: + +| param | type | description | +| ----- | -------- | -------------------- | +| `n` | `Uint32` | The value to convert | + +Returns: + +| type | description | +| --------- | -------------------------------- | +| `WasmI32` | The value converted to a WasmI32 | + +## DataStructures.Int64 + +Utilities for working with Grain's int64 data structure. + +### Values + +Functions and constants included in the DataStructures.Int64 module. + +#### DataStructures.Int64.**make** + +```grain +make: (int: WasmI64) => WasmRef +``` + +Creates a new Int64 with the specified value. + +Parameters: + +| param | type | description | +| ----- | --------- | ------------------ | +| `int` | `WasmI64` | The value to store | + +Returns: + +| type | description | +| --------- | -------------------------- | +| `WasmRef` | The reference to the Int64 | + +#### DataStructures.Int64.**getValue** + +```grain +getValue: (ref: WasmRef) => WasmI64 +``` + +Gets the value of an Int64 + +Parameters: + +| param | type | description | +| ----- | --------- | -------------------------------- | +| `ref` | `WasmRef` | The reference to the Int64 value | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmI64` | The inner i64 value | + +#### DataStructures.Int64.**toInt64** + +```grain +toInt64: (n: WasmI64) => Int64 +``` + +Converts a WasmI64 value to a Grain Int64. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------- | +| `n` | `WasmI64` | The number to convert | + +Returns: + +| type | description | +| ------- | ------------------------------------ | +| `Int64` | The value converted to a Grain Int64 | + +#### DataStructures.Int64.**fromInt64** + +```grain +fromInt64: (n: Int64) => WasmI64 +``` + +Converts a Grain Int64 to a WasmI64 value. + +Parameters: + +| param | type | description | +| ----- | ------- | -------------------- | +| `n` | `Int64` | The value to convert | + +Returns: + +| type | description | +| --------- | -------------------------------- | +| `WasmI64` | The value converted to a WasmI64 | + +## DataStructures.Uint64 + +Utilities for working with Grain's uint64 data structure. + +### Values + +Functions and constants included in the DataStructures.Uint64 module. + +#### DataStructures.Uint64.**make** + +```grain +make: (int: WasmI64) => WasmRef +``` + +Creates a new Uint64 with the specified value. + +Parameters: + +| param | type | description | +| ----- | --------- | ------------------ | +| `int` | `WasmI64` | The value to store | + +Returns: + +| type | description | +| --------- | --------------------------- | +| `WasmRef` | The reference to the Uint64 | + +#### DataStructures.Uint64.**getValue** + +```grain +getValue: (ref: WasmRef) => WasmI64 +``` + +Gets the value of a Uint64 + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------- | +| `ref` | `WasmRef` | The reference to the Uint64 value | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmI64` | The inner i64 value | + +#### DataStructures.Uint64.**toUint64** + +```grain +toUint64: (n: WasmI64) => Uint64 +``` + +Converts a WasmI64 value to a Grain Uint64. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------- | +| `n` | `WasmI64` | The number to convert | + +Returns: + +| type | description | +| -------- | ------------------------------------- | +| `Uint64` | The value converted to a Grain Uint64 | + +#### DataStructures.Uint64.**fromUint64** + +```grain +fromUint64: (n: Uint64) => WasmI64 +``` + +Converts a Grain Uint64 to a WasmI64 value. + +Parameters: + +| param | type | description | +| ----- | -------- | -------------------- | +| `n` | `Uint64` | The value to convert | + +Returns: + +| type | description | +| --------- | -------------------------------- | +| `WasmI64` | The value converted to a WasmI64 | + +## DataStructures.Float32 + +Utilities for working with Grain's float32 data structure. + +### Values + +Functions and constants included in the DataStructures.Float32 module. + +#### DataStructures.Float32.**make** + +```grain +make: (float: WasmF32) => WasmRef +``` + +Creates a new Float32 with the specified value. + +Parameters: + +| param | type | description | +| ------- | --------- | ------------------ | +| `float` | `WasmF32` | The value to store | + +Returns: + +| type | description | +| --------- | ---------------------------- | +| `WasmRef` | the reference to the Float32 | + +#### DataStructures.Float32.**getValue** + +```grain +getValue: (ref: WasmRef) => WasmF32 +``` + +Gets the value of a Float32 + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------------------------- | +| `ref` | `WasmRef` | The reference to the Float32 value | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmF32` | The inner f32 value | + +#### DataStructures.Float32.**toFloat32** + +```grain +toFloat32: (n: WasmF32) => Float32 +``` + +Converts a WasmF32 value to a Grain Float32. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------- | +| `n` | `WasmF32` | The number to convert | + +Returns: + +| type | description | +| --------- | -------------------------------------- | +| `Float32` | The value converted to a Grain Float32 | + +#### DataStructures.Float32.**fromFloat32** + +```grain +fromFloat32: (n: Float32) => WasmF32 +``` + +Converts a Grain Float32 to a WasmF32 value. + +Parameters: + +| param | type | description | +| ----- | --------- | -------------------- | +| `n` | `Float32` | The value to convert | + +Returns: + +| type | description | +| --------- | -------------------------------- | +| `WasmF32` | The value converted to a WasmF32 | + +## DataStructures.Float64 + +Utilities for working with Grain's float64 data structure. + +### Values + +Functions and constants included in the DataStructures.Float64 module. + +#### DataStructures.Float64.**make** + +```grain +make: (float: WasmF64) => WasmRef +``` + +Creates a new Float64 with the specified value. + +Parameters: + +| param | type | description | +| ------- | --------- | ------------------ | +| `float` | `WasmF64` | The value to store | + +Returns: + +| type | description | +| --------- | ---------------------------- | +| `WasmRef` | The reference to the Float64 | + +#### DataStructures.Float64.**getValue** + +```grain +getValue: (ref: WasmRef) => WasmF64 +``` + +Gets the value of a Float64 + +Parameters: + +| param | type | description | +| ----- | --------- | ---------------------------------- | +| `ref` | `WasmRef` | The reference to the Float64 value | + +Returns: + +| type | description | +| --------- | ------------------- | +| `WasmF64` | The inner f64 value | + +#### DataStructures.Float64.**toFloat64** + +```grain +toFloat64: (n: WasmF64) => Float64 +``` + +Converts a WasmF64 value to a Grain Float64. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------- | +| `n` | `WasmF64` | The number to convert | + +Returns: + +| type | description | +| --------- | -------------------------------------- | +| `Float64` | The value converted to a Grain Float64 | + +#### DataStructures.Float64.**fromFloat64** + +```grain +fromFloat64: (n: Float64) => WasmF64 +``` + +Converts a Grain Float64 to a WasmF64 value. + +Parameters: + +| param | type | description | +| ----- | --------- | -------------------- | +| `n` | `Float64` | The value to convert | + +Returns: + +| type | description | +| --------- | -------------------------------- | +| `WasmF64` | The value converted to a WasmF64 | + +## DataStructures.Rational + +Utilities for working with Grain's rational data structure. + +### Values + +Functions and constants included in the DataStructures.Rational module. + +#### DataStructures.Rational.**make** + +```grain +make: (numerator: WasmRef, denominator: WasmRef) => WasmRef +``` + +Creates a new Rational with the specified numerator and denominator values. + +Parameters: + +| param | type | description | +| ------------- | --------- | ------------------------------ | +| `numerator` | `WasmRef` | The numerator value to store | +| `denominator` | `WasmRef` | The denominator value to store | + +Returns: + +| type | description | +| --------- | ----------------------------- | +| `WasmRef` | The reference to the Rational | + +#### DataStructures.Rational.**getNumerator** + +```grain +getNumerator: (ref: WasmRef) => WasmRef +``` + +Gets the numerator of a Rational + +Parameters: + +| param | type | description | +| ----- | --------- | ----------------------------------- | +| `ref` | `WasmRef` | The reference to the Rational value | + +Returns: + +| type | description | +| --------- | ---------------------- | +| `WasmRef` | The rational numerator | + +#### DataStructures.Rational.**getDenominator** + +```grain +getDenominator: (ref: WasmRef) => WasmRef +``` + +Gets the denominator of a Rational + +Parameters: + +| param | type | description | +| ----- | --------- | ----------------------------------- | +| `ref` | `WasmRef` | The reference to the Rational value | + +Returns: + +| type | description | +| --------- | ------------------------ | +| `WasmRef` | The rational denominator | + +## DataStructures.BigInt + +Utilities for working with Grain's bigint data structure. + +### Values + +Functions and constants included in the DataStructures.BigInt module. + +#### DataStructures.BigInt.**make** + +```grain +make: (size: WasmI32) => WasmRef +``` + +Creates a new BigInt with the specified number of limbs, with all limbs initialized to 0, and returns a reference to it. + +Parameters: + +| param | type | description | +| ------ | --------- | ------------------- | +| `size` | `WasmI32` | The number of limbs | + +Returns: + +| type | description | +| --------- | --------------------------- | +| `WasmRef` | The reference to the BigInt | + +#### DataStructures.BigInt.**getSize** + +```grain +getSize: (ref: WasmRef) => WasmI32 +``` + +Gets number of limbs of a BigInt. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------- | +| `ref` | `WasmRef` | The reference to the BigInt value | + +Returns: + +| type | description | +| --------- | -------------------------- | +| `WasmI32` | The number of BigInt limbs | + +#### DataStructures.BigInt.**getFlags** + +```grain +getFlags: (ref: WasmRef) => WasmI32 +``` + +Gets the flags of a BigInt. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------- | +| `ref` | `WasmRef` | The reference to the BigInt value | + +Returns: + +| type | description | +| --------- | ---------------- | +| `WasmI32` | The BigInt flags | + +#### DataStructures.BigInt.**setFlags** + +```grain +setFlags: (ref: WasmRef, flags: WasmI32) => Void +``` + +Sets the flags of a BigInt. + +Parameters: + +| param | type | description | +| ------- | --------- | --------------------------------- | +| `ref` | `WasmRef` | The reference to the BigInt value | +| `flags` | `WasmI32` | The BigInt flags to set | + +#### DataStructures.BigInt.**getLimbData** + +```grain +getLimbData: (ref: WasmRef) => WasmRef.WasmArrayRef.WasmArrayRef +``` + +Gets the reference to the underlying array containing the BigInt limbs, +the layout of which can be found in the contributor docs. + +Parameters: + +| param | type | description | +| ----- | --------- | --------------------------------- | +| `ref` | `WasmRef` | The reference to the BigInt value | + +Returns: + +| type | description | +| ----------------------------------- | --------------------------------- | +| `WasmRef.WasmArrayRef.WasmArrayRef` | The reference to the BigInt limbs | + diff --git a/stdlib/runtime/debugPrint.gr b/stdlib/runtime/unsafe/debugPrint.gr similarity index 85% rename from stdlib/runtime/debugPrint.gr rename to stdlib/runtime/unsafe/debugPrint.gr index eace72f378..eb007a25fc 100644 --- a/stdlib/runtime/debugPrint.gr +++ b/stdlib/runtime/unsafe/debugPrint.gr @@ -9,15 +9,16 @@ from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/memory" include Memory from "runtime/malloc" include Malloc -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures as DS foreign wasm fd_write: (WasmI32, WasmI32, WasmI32, WasmI32) => WasmI32 from "wasi_snapshot_preview1" @unsafe provide let print = (msg: String) => { - let msg = DataStructures.getStringArrayRef(WasmRef.fromGrain(msg)) - let msgLen = WasmArrayRef.length(msg) + let msgRef = DS.String.fromGrain(msg) + let msgData = DS.String.getData(msgRef) + let msgLen = DS.String.getSize(msgRef) // iov layout: [ <4 byte padding> ] let iov = Malloc.malloc(17n + msgLen) @@ -27,7 +28,7 @@ provide let print = (msg: String) => { WasmI32.store(iov, bufPtr, 0n) WasmI32.store(iov, msgLen + 1n, 4n) WasmI32.store(iov, writtenPtr, 8n) - Memory.copyRefArrayToLinearMemory(bufPtr, msg, msgLen) + Memory.copyRefArrayToLinearMemory(bufPtr, msgData, msgLen) WasmI32.store8(bufPtr, 10n, msgLen) fd_write(1n, iov, 1n, writtenPtr) diff --git a/stdlib/runtime/debugPrint.md b/stdlib/runtime/unsafe/debugPrint.md similarity index 100% rename from stdlib/runtime/debugPrint.md rename to stdlib/runtime/unsafe/debugPrint.md diff --git a/stdlib/runtime/unsafe/panic.gr b/stdlib/runtime/unsafe/panic.gr index 349aa24bd3..9ebee09884 100644 --- a/stdlib/runtime/unsafe/panic.gr +++ b/stdlib/runtime/unsafe/panic.gr @@ -7,8 +7,7 @@ from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/unsafe/memory" include Memory from "runtime/malloc" include Malloc -from "runtime/dataStructures" include DataStructures -use DataStructures.{ getStringArrayRef } +from "runtime/unsafe/dataStructures" include DataStructures as DS foreign wasm fd_write: (WasmI32, WasmI32, WasmI32, WasmI32) => WasmI32 from "wasi_snapshot_preview1" @@ -16,8 +15,9 @@ foreign wasm fd_write: primitive unreachable = "@unreachable" provide let panic = (msg: String) => { - let msg = getStringArrayRef(WasmRef.fromGrain(msg)) - let msgLen = WasmArrayRef.length(msg) + let msgRef = DS.String.fromGrain(msg) + let msgData = DS.String.getData(msgRef) + let msgLen = DS.String.getSize(msgRef) // iov layout: [ <4 byte padding> ] let iov = Malloc.malloc(17n + msgLen) @@ -27,7 +27,7 @@ provide let panic = (msg: String) => { WasmI32.store(iov, bufPtr, 0n) WasmI32.store(iov, msgLen + 1n, 4n) WasmI32.store(iov, writtenPtr, 8n) - Memory.copyRefArrayToLinearMemory(bufPtr, msg, msgLen) + Memory.copyRefArrayToLinearMemory(bufPtr, msgData, msgLen) WasmI32.store8(bufPtr, 10n, msgLen) fd_write(2n, iov, 1n, writtenPtr) diff --git a/stdlib/runtime/wasi.gr b/stdlib/runtime/wasi.gr index 8b73ffda13..39ccdc03d9 100644 --- a/stdlib/runtime/wasi.gr +++ b/stdlib/runtime/wasi.gr @@ -4,7 +4,7 @@ from "runtime/unsafe/wasmi32" include WasmI32 from "exception" include Exception from "runtime/string" include String use String.{ concat as (++), toString } -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures use DataStructures.{ untagSimpleNumber } // env diff --git a/stdlib/string.gr b/stdlib/string.gr index c42b0fe9cd..2c99f2297b 100644 --- a/stdlib/string.gr +++ b/stdlib/string.gr @@ -14,22 +14,7 @@ from "runtime/unsafe/wasmi32" include WasmI32 from "runtime/unsafe/wasmref" include WasmRef use WasmRef.{ module WasmArrayRef } from "runtime/exception" include Exception -from "runtime/unsafe/conv" include Conv -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - untagSimpleNumber, - tagSimpleNumber, - tagChar, - untagChar, - allocateArray, - allocateString, - allocateBytes, - getStringSize, - getBytesSize, - getStringArrayRef, - getBytesArrayRef, - getCompoundValueArrayRef, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/utf8" include Utf8 use Utf8.{ isLeadingByte, @@ -43,7 +28,7 @@ use Utf8.{ @unsafe let convSimpleNumber = (x: Number, err: String) => { if (WasmRef.isRefI31(WasmRef.fromGrain(x))) { - untagSimpleNumber(x) + DS.untagSimpleNumber(x) } else { throw InvalidArgument(err) } @@ -88,10 +73,10 @@ provide let concat = (++) @unsafe provide let length = (string: String) => { use WasmI32.{ (+), ltU as (<) } - let stringRef = WasmRef.fromGrain(string) - let size = getStringSize(stringRef) + let stringRef = DS.String.fromGrain(string) + let size = DS.String.getSize(stringRef) - let stringData = getStringArrayRef(stringRef) + let stringData = DS.String.getData(stringRef) let mut len = 0n @@ -100,7 +85,7 @@ provide let length = (string: String) => { if (isLeadingByte(byte)) len += 1n } - Conv.wasmI32ToNumber(len) + DS.wasmI32ToNumber(len) } /** @@ -115,8 +100,8 @@ provide let length = (string: String) => { */ @unsafe provide let byteLength = (string: String) => { - let stringRef = WasmRef.fromGrain(string) - Conv.wasmI32ToNumber(getStringSize(stringRef)) + let stringRef = DS.String.fromGrain(string) + DS.wasmI32ToNumber(DS.String.getSize(stringRef)) } /** @@ -130,8 +115,8 @@ provide let byteLength = (string: String) => { @unsafe provide let isEmpty = (string: String) => { use WasmI32.{ (==) } - let strRef = WasmRef.fromGrain(string) - getStringSize(strRef) == 0n + let strRef = DS.String.fromGrain(string) + DS.String.getSize(strRef) == 0n } /** @@ -148,23 +133,23 @@ provide let isEmpty = (string: String) => { @unsafe provide let indexOf = (search: String, string: String) => { use WasmI32.{ (+), (-), ltU as (<), gtU as (>), (==) } - let searchRef = WasmRef.fromGrain(search) - let stringRef = WasmRef.fromGrain(string) + let searchRef = DS.String.fromGrain(search) + let stringRef = DS.String.fromGrain(string) - let size = getStringSize(stringRef) - let psize = getStringSize(searchRef) + let size = DS.String.getSize(stringRef) + let psize = DS.String.getSize(searchRef) if (psize > size) { return None } - let strData = getStringArrayRef(stringRef) - let searchData = getStringArrayRef(searchRef) + let strData = DS.String.getData(stringRef) + let searchData = DS.String.getData(searchRef) let end = size - psize + 1n let mut index = 0n for (let mut i = 0n; i < end;) { if (WasmArrayRef.compareI8(strData, i, searchData, 0n, psize) == 0n) { - return Some(tagSimpleNumber(index)) + return Some(DS.tagSimpleNumber(index)) } let byte = WasmArrayRef.getI8U(strData, i) i += utf8ByteCount(byte) @@ -187,16 +172,16 @@ provide let indexOf = (search: String, string: String) => { */ @unsafe provide let lastIndexOf = (search: String, string: String) => { - let searchRef = WasmRef.fromGrain(search) - let searchData = getStringArrayRef(searchRef) - let searchSize = getStringSize(searchRef) - let stringRef = WasmRef.fromGrain(string) - let stringData = getStringArrayRef(stringRef) - let stringSize = getStringSize(stringRef) + let searchRef = DS.String.fromGrain(search) + let searchData = DS.String.getData(searchRef) + let searchSize = DS.String.getSize(searchRef) + let stringRef = DS.String.fromGrain(string) + let stringData = DS.String.getData(stringRef) + let stringSize = DS.String.getSize(stringRef) // The last possible index in the string given the length of the search - let mut lastIndex = untagSimpleNumber(length(string) - length(search)) + let mut lastIndex = DS.untagSimpleNumber(length(string) - length(search)) use WasmI32.{ (-), gtU as (>), (>=) } - if (WasmI32.eqz(searchSize)) return Some(tagSimpleNumber(lastIndex)) + if (WasmI32.eqz(searchSize)) return Some(DS.tagSimpleNumber(lastIndex)) if (searchSize > stringSize) { return None } @@ -219,7 +204,7 @@ provide let lastIndexOf = (search: String, string: String) => { ) ) ) { - return Some(tagSimpleNumber(lastIndex)) + return Some(DS.tagSimpleNumber(lastIndex)) } lastIndex -= 1n } @@ -233,10 +218,10 @@ let charAtHelp = (position, string: String) => { } // Implementation is similar to explodeHelp, but doesn't perform unneeded memory allocations use WasmI32.{ (+), ltU as (<), (==) } - let stringRef = WasmRef.fromGrain(string) - let size = getStringSize(stringRef) - let position = untagSimpleNumber(position) - let data = getStringArrayRef(stringRef) + let stringRef = DS.String.fromGrain(string) + let size = DS.String.getSize(stringRef) + let position = DS.untagSimpleNumber(position) + let data = DS.String.getData(stringRef) let mut counter = 0n for (let mut i = 0n; i < size;) { if (counter == position) { @@ -268,7 +253,7 @@ let charAtHelp = (position, string: String) => { */ @unsafe provide let charCodeAt = (position, string: String) => { - tagSimpleNumber(charAtHelp(position, string)) + DS.tagSimpleNumber(charAtHelp(position, string)) } /** @@ -287,19 +272,19 @@ provide let charCodeAt = (position, string: String) => { */ @unsafe provide let charAt = (position, string: String) => { - tagChar(charAtHelp(position, string)) + DS.tagChar(charAtHelp(position, string)) } @unsafe let explodeHelp = (str: String, chars) => { use WasmI32.{ (+), ltU as (<) } - let strRef = WasmRef.fromGrain(str) - let strSize = getStringSize(strRef) - let strData = getStringArrayRef(strRef) + let strRef = DS.String.fromGrain(str) + let strSize = DS.String.getSize(strRef) + let strData = DS.String.getData(strRef) - let len = untagSimpleNumber(length(str)) + let len = DS.untagSimpleNumber(length(str)) - let arr = allocateArray(len, WasmRef.fromGrain(void)) + let arr = DS.Array.make(len, WasmRef.fromGrain(void)) let mut offset = 0n let mut arrIdx = 0n while (offset < strSize) { @@ -307,15 +292,15 @@ let explodeHelp = (str: String, chars) => { let byteCount = utf8ByteCount(byte) let character = if (chars) { - WasmRef.fromGrain(tagChar(getCodePoint(strData, offset))) + WasmRef.fromGrain(DS.tagChar(getCodePoint(strData, offset))) } else { - let newStrRef = allocateString(byteCount) - let newStrData = getStringArrayRef(newStrRef) + let newStrRef = DS.String.make(byteCount) + let newStrData = DS.String.getData(newStrRef) WasmArrayRef.copyI8(newStrData, 0n, strData, offset, byteCount) - newStrRef + WasmRef.fromGrain(newStrRef) } - WasmArrayRef.setAny(getCompoundValueArrayRef(arr), arrIdx, character) + WasmArrayRef.setAny(DS.Array.getData(arr), arrIdx, character) arrIdx += 1n offset += byteCount } @@ -337,7 +322,7 @@ let explodeHelp = (str: String, chars) => { */ @unsafe provide let explode = string => { - WasmRef.toGrain(explodeHelp(string, true)): Array + DS.Array.toGrain(explodeHelp(string, true)): Array } /** @@ -365,27 +350,25 @@ provide let implode = (arr: Array) => { leU as (<=), } - let arrLength = WasmArrayRef.length( - getCompoundValueArrayRef(WasmRef.fromGrain(arr)) - ) + let arrLength = WasmArrayRef.length(DS.Array.getData(DS.Array.fromGrain(arr))) let mut stringByteLength = 0n for (let mut i = 0n; i < arrLength; i += 1n) { - let usv = untagChar(arr[tagSimpleNumber(i)]) + let usv = DS.untagChar(arr[DS.tagSimpleNumber(i)]) stringByteLength += usvEncodeLength(usv) } - let str = allocateString(stringByteLength) - let strArray = getStringArrayRef(str) + let str = DS.String.make(stringByteLength) + let strArray = DS.String.getData(str) let mut offset = 0n for (let mut i = 0n; i < arrLength; i += 1n) { - let usv = untagChar(arr[tagSimpleNumber(i)]) + let usv = DS.untagChar(arr[DS.tagSimpleNumber(i)]) offset += writeUtf8CodePoint(strArray, offset, usv) } - WasmRef.toGrain(str): String + DS.String.toGrain(str) } // Helper to get the length in constant time without depending on Array @@ -430,18 +413,18 @@ provide let reverse = string => { provide let split = (separator: String, string: String) => { use WasmI32.{ (+), (-), (&), (<<), ltU as (<), gtU as (>), (==) } - let strRef = WasmRef.fromGrain(string) - let strSize = getStringSize(strRef) - let sepRef = WasmRef.fromGrain(separator) - let sepSize = getStringSize(sepRef) + let strRef = DS.String.fromGrain(string) + let strSize = DS.String.getSize(strRef) + let sepRef = DS.String.fromGrain(separator) + let sepSize = DS.String.getSize(sepRef) if (sepSize == 0n) { - WasmRef.toGrain(explodeHelp(string, false)): Array + DS.Array.toGrain(explodeHelp(string, false)): Array } else if (sepSize > strSize) { [> string] } else { - let strData = getStringArrayRef(strRef) - let sepData = getStringArrayRef(sepRef) + let strData = DS.String.getData(strRef) + let sepData = DS.String.getData(sepRef) let strEnd = strSize - sepSize + 1n let mut numStrings = 1n @@ -457,16 +440,16 @@ provide let split = (separator: String, string: String) => { let mut offset = 0n let mut last = 0n - let arr = allocateArray(numStrings, WasmRef.fromGrain(void)) - let wasmArray = getCompoundValueArrayRef(arr) + let arr = DS.Array.make(numStrings, WasmRef.fromGrain(void)) + let wasmArray = DS.Array.getData(arr) let mut arrIdx = 0n while (offset < strEnd) { if (WasmArrayRef.compareI8(strData, offset, sepData, 0n, sepSize) == 0n) { let strSize = offset - last - let str = allocateString(strSize) - WasmArrayRef.copyI8(getStringArrayRef(str), 0n, strData, last, strSize) - WasmArrayRef.setAny(wasmArray, arrIdx, str) + let str = DS.String.make(strSize) + WasmArrayRef.copyI8(DS.String.getData(str), 0n, strData, last, strSize) + WasmArrayRef.setAny(wasmArray, arrIdx, WasmRef.fromGrain(str)) arrIdx += 1n offset += sepSize last = offset @@ -478,11 +461,11 @@ provide let split = (separator: String, string: String) => { // Grab last string let strSize = strSize - last - let lastStr = allocateString(strSize) - WasmArrayRef.copyI8(getStringArrayRef(lastStr), 0n, strData, last, strSize) - WasmArrayRef.setAny(wasmArray, arrIdx, lastStr) + let lastStr = DS.String.make(strSize) + WasmArrayRef.copyI8(DS.String.getData(lastStr), 0n, strData, last, strSize) + WasmArrayRef.setAny(wasmArray, arrIdx, WasmRef.fromGrain(lastStr)) - WasmRef.toGrain(arr): Array + DS.Array.toGrain(arr): Array } } @@ -510,9 +493,9 @@ provide let split = (separator: String, string: String) => { provide let slice = (start: Number, end=length(string), string: String) => { use WasmI32.{ (+), (-), (<), (>), (==) } - let stringRef = WasmRef.fromGrain(string) - let len = untagSimpleNumber(length(string)) - let size = getStringSize(stringRef) + let stringRef = DS.String.fromGrain(string) + let len = DS.untagSimpleNumber(length(string)) + let size = DS.String.getSize(stringRef) let mut start = convSimpleNumber(start, "Invalid start index") let mut to = convSimpleNumber(end, "Invalid end index") @@ -532,7 +515,7 @@ provide let slice = (start: Number, end=length(string), string: String) => { throw InvalidArgument("Start index exceeds end index") } - let data = getStringArrayRef(stringRef) + let data = DS.String.getData(stringRef) let mut begin = 0n let mut end = 0n @@ -558,11 +541,11 @@ provide let slice = (start: Number, end=length(string), string: String) => { } let newSize = end - begin - let newStringRef = allocateString(newSize) + let newStringRef = DS.String.make(newSize) - WasmArrayRef.copyI8(getStringArrayRef(newStringRef), 0n, data, begin, newSize) + WasmArrayRef.copyI8(DS.String.getData(newStringRef), 0n, data, begin, newSize) - WasmRef.toGrain(newStringRef): String + DS.String.toGrain(newStringRef) } /** @@ -586,14 +569,14 @@ provide let contains = (search: String, string: String) => { let pOrig = search let sOrig = string - let stringRef = WasmRef.fromGrain(string) - let searchRef = WasmRef.fromGrain(search) + let stringRef = DS.String.fromGrain(string) + let searchRef = DS.String.fromGrain(search) - let n = getStringSize(stringRef) - let m = getStringSize(searchRef) + let n = DS.String.getSize(stringRef) + let m = DS.String.getSize(searchRef) - let stringData = getStringArrayRef(stringRef) - let searchData = getStringArrayRef(searchRef) + let stringData = DS.String.getData(stringRef) + let searchData = DS.String.getData(searchRef) let mut j = 0n and k = 0n @@ -673,14 +656,14 @@ provide let startsWith = (search: String, string: String) => { let pOrig = search let sOrig = string - let searchRef = WasmRef.fromGrain(search) - let stringRef = WasmRef.fromGrain(string) + let searchRef = DS.String.fromGrain(search) + let stringRef = DS.String.fromGrain(string) - let n = getStringSize(stringRef) - let m = getStringSize(searchRef) + let n = DS.String.getSize(stringRef) + let m = DS.String.getSize(searchRef) - let stringData = getStringArrayRef(stringRef) - let searchData = getStringArrayRef(searchRef) + let stringData = DS.String.getData(stringRef) + let searchData = DS.String.getData(searchRef) // Bail if pattern length is longer than input length if (m > n) { @@ -705,14 +688,14 @@ provide let startsWith = (search: String, string: String) => { provide let endsWith = (search: String, string: String) => { use WasmI32.{ (+), (-), gtU as (>), (==) } - let searchRef = WasmRef.fromGrain(search) - let stringRef = WasmRef.fromGrain(string) + let searchRef = DS.String.fromGrain(search) + let stringRef = DS.String.fromGrain(string) - let n = getStringSize(stringRef) - let m = getStringSize(searchRef) + let n = DS.String.getSize(stringRef) + let m = DS.String.getSize(searchRef) - let stringData = getStringArrayRef(stringRef) - let searchData = getStringArrayRef(searchRef) + let stringData = DS.String.getData(stringRef) + let searchData = DS.String.getData(searchRef) // Bail if pattern length is longer than input length if (m > n) { @@ -742,20 +725,20 @@ provide let replaceFirst = ( ) => { use WasmI32.{ (+), (-), gtU as (>), ltU as (<), (==) } - let patternRef = WasmRef.fromGrain(searchPattern) - let stringRef = WasmRef.fromGrain(string) - let replacementRef = WasmRef.fromGrain(replacement) + let patternRef = DS.String.fromGrain(searchPattern) + let stringRef = DS.String.fromGrain(string) + let replacementRef = DS.String.fromGrain(replacement) - let patternLen = getStringSize(patternRef) - let stringLen = getStringSize(stringRef) - let replacementLen = getStringSize(replacementRef) + let patternLen = DS.String.getSize(patternRef) + let stringLen = DS.String.getSize(stringRef) + let replacementLen = DS.String.getSize(replacementRef) // Bail if search str is longer than the string if (stringLen < patternLen) { return string } - let patternData = getStringArrayRef(patternRef) - let stringData = getStringArrayRef(stringRef) - let replacementData = getStringArrayRef(replacementRef) + let patternData = DS.String.getData(patternRef) + let stringData = DS.String.getData(stringRef) + let replacementData = DS.String.getData(replacementRef) // Search for an instance of the string let mut foundIndex = -1n @@ -768,8 +751,8 @@ provide let replaceFirst = ( == 0n ) { // Create the new string - let newStrRef = allocateString(stringLen - patternLen + replacementLen) - let newStrData = getStringArrayRef(newStrRef) + let newStrRef = DS.String.make(stringLen - patternLen + replacementLen) + let newStrData = DS.String.getData(newStrRef) WasmArrayRef.copyI8(newStrData, 0n, stringData, 0n, foundIndex) WasmArrayRef.copyI8( newStrData, @@ -786,7 +769,7 @@ provide let replaceFirst = ( stringLen - (foundIndex + patternLen) ) // Copy over the str - return WasmRef.toGrain(newStrRef): String + return DS.String.toGrain(newStrRef) } } @@ -813,21 +796,21 @@ provide let replaceLast = ( ) => { use WasmI32.{ (+), (-), (>=), ltU as (<), (==) } - let patternRef = WasmRef.fromGrain(searchPattern) - let stringRef = WasmRef.fromGrain(string) - let replacementRef = WasmRef.fromGrain(replacement) + let patternRef = DS.String.fromGrain(searchPattern) + let stringRef = DS.String.fromGrain(string) + let replacementRef = DS.String.fromGrain(replacement) - let patternLen = getStringSize(patternRef) - let stringLen = getStringSize(stringRef) - let replacementLen = getStringSize(replacementRef) + let patternLen = DS.String.getSize(patternRef) + let stringLen = DS.String.getSize(stringRef) + let replacementLen = DS.String.getSize(replacementRef) // Bail if search str is longer than the string if (stringLen < patternLen) { return string } - let patternData = getStringArrayRef(patternRef) - let stringData = getStringArrayRef(stringRef) - let replacementData = getStringArrayRef(replacementRef) + let patternData = DS.String.getData(patternRef) + let stringData = DS.String.getData(stringRef) + let replacementData = DS.String.getData(replacementRef) let mut found = false // Search for an instance of the string @@ -841,8 +824,8 @@ provide let replaceLast = ( == 0n ) { // Create the new string - let newStrRef = allocateString(stringLen - patternLen + replacementLen) - let newStrData = getStringArrayRef(newStrRef) + let newStrRef = DS.String.make(stringLen - patternLen + replacementLen) + let newStrData = DS.String.getData(newStrRef) WasmArrayRef.copyI8(newStrData, 0n, stringData, 0n, foundIndex) WasmArrayRef.copyI8( newStrData, @@ -859,7 +842,7 @@ provide let replaceLast = ( stringLen - (foundIndex + patternLen) ) // Copy over the str - return WasmRef.toGrain(newStrRef): String + return DS.String.toGrain(newStrRef) } } @@ -886,21 +869,21 @@ provide let replaceAll = ( ) => { use WasmI32.{ (+), (-), (*), (>=), ltU as (<), (==) } - let patternRef = WasmRef.fromGrain(searchPattern) - let stringRef = WasmRef.fromGrain(string) - let replacementRef = WasmRef.fromGrain(replacement) + let patternRef = DS.String.fromGrain(searchPattern) + let stringRef = DS.String.fromGrain(string) + let replacementRef = DS.String.fromGrain(replacement) - let patternLen = getStringSize(patternRef) - let stringLen = getStringSize(stringRef) - let replacementLen = getStringSize(replacementRef) + let patternLen = DS.String.getSize(patternRef) + let stringLen = DS.String.getSize(stringRef) + let replacementLen = DS.String.getSize(replacementRef) // Bail if search str is longer than the string if (stringLen < patternLen) { string } else { - let patternData = getStringArrayRef(patternRef) - let stringData = getStringArrayRef(stringRef) - let replacementData = getStringArrayRef(replacementRef) + let patternData = DS.String.getData(patternRef) + let stringData = DS.String.getData(stringRef) + let replacementData = DS.String.getData(replacementRef) let mut found = false // Search for an instance of the string @@ -917,23 +900,23 @@ provide let replaceAll = ( ) { found = true foundCount += 1n - foundIndexes = [tagSimpleNumber(foundIndex), ...foundIndexes] + foundIndexes = [DS.tagSimpleNumber(foundIndex), ...foundIndexes] foundIndex -= patternLen - 1n i -= patternLen - 1n } } if (found) { // Create the new string - let newStrRef = allocateString( + let newStrRef = DS.String.make( stringLen - (patternLen - replacementLen) * foundCount ) - let newStrData = getStringArrayRef(newStrRef) + let newStrData = DS.String.getData(newStrRef) let mut strOffset = 0n let mut lastIndex = 0n while (true) { match (foundIndexes) { [idx, ...rest] => { - let index = untagSimpleNumber(idx) + let index = DS.untagSimpleNumber(idx) // Copy the part before WasmArrayRef.copyI8( newStrData, @@ -966,7 +949,7 @@ provide let replaceAll = ( lastIndex, stringLen - lastIndex ) - WasmRef.toGrain(newStrRef): String + DS.String.toGrain(newStrRef) } else { string } @@ -993,11 +976,11 @@ let grainToWasmNumber = (num, name) => { let utf16Length = (s: String) => { use WasmI32.{ (+), (<<), ltU as (<), (==) } - let sRef = WasmRef.fromGrain(s) - let size = getStringSize(sRef) - let len = untagSimpleNumber(length(s)) + let sRef = DS.String.fromGrain(s) + let size = DS.String.getSize(sRef) + let len = DS.untagSimpleNumber(length(s)) - let data = getStringArrayRef(sRef) + let data = DS.String.getData(sRef) let mut count = 0n // <- number of UTF-16 code words for (let mut i = 0n; i < size;) { @@ -1012,7 +995,7 @@ let utf16Length = (s: String) => { } // multiply by two to get number of bytes - tagSimpleNumber(count << 1n) + DS.tagSimpleNumber(count << 1n) } let encodedLength = (s: String, encoding) => { @@ -1034,15 +1017,15 @@ let encodeAtHelp = ( destPos: Number, ) => { use WasmI32.{ (+), (-), (&), (>>>), ltU as (<), (>), leU as (<=), (==) } - let stringRef = WasmRef.fromGrain(string) - let byteSize = getStringSize(stringRef) - let len = untagSimpleNumber(length(string)) + let stringRef = DS.String.fromGrain(string) + let byteSize = DS.String.getSize(stringRef) + let len = DS.untagSimpleNumber(length(string)) - let data = getStringArrayRef(stringRef) + let data = DS.String.getData(stringRef) - let bytes = WasmRef.fromGrain(dest) - let destSize = getBytesSize(bytes) - let bytes = getBytesArrayRef(bytes) + let bytes = DS.Bytes.fromGrain(dest) + let destSize = DS.Bytes.getSize(bytes) + let bytes = DS.Bytes.getData(bytes) let mut bytesIdx = grainToWasmNumber(destPos, _OFFSET_NAME) if (includeBom) { @@ -1262,7 +1245,7 @@ let encodeHelp = (string: String, encoding: Encoding, includeBom: Bool) => { } else { 0 }) - let bytes = WasmRef.toGrain(allocateBytes(untagSimpleNumber(size))) + let bytes = DS.Bytes.toGrain(DS.Bytes.make(DS.untagSimpleNumber(size))) encodeAtHelp(string, encoding, includeBom, bytes, 0) } @@ -1286,9 +1269,9 @@ provide let encode = (string: String, encoding: Encoding, includeBom=false) => { @unsafe let bytesHaveBom = (bytes: Bytes, encoding: Encoding, start: WasmI32) => { use WasmI32.{ (+), geU as (>=), (==) } - let ref = WasmRef.fromGrain(bytes) - let bytesSize = getBytesSize(ref) - let data = getBytesArrayRef(ref) + let ref = DS.Bytes.fromGrain(bytes) + let bytesSize = DS.Bytes.getSize(ref) + let data = DS.Bytes.getData(ref) match (encoding) { UTF8 => { bytesSize >= 3n @@ -1344,16 +1327,16 @@ let decodedLength = ( (==), (!=), } - let ref = WasmRef.fromGrain(bytes) + let ref = DS.Bytes.fromGrain(bytes) let bytesSize = { - let tmp = getBytesSize(ref) - start + let tmp = DS.Bytes.getSize(ref) - start if (size < tmp) { size } else { tmp } } - let data = getBytesArrayRef(ref) + let data = DS.Bytes.getData(ref) match (encoding) { UTF8 => bytesSize, UTF16_BE => { @@ -1485,12 +1468,12 @@ let decodeRangeHelp = ( let hasBom = bytesHaveBom(bytes, encoding, start) let stringSize = decodedLength(bytes, encoding, start, size) let stringSize = if (skipBom && hasBom) stringSize - 3n else stringSize - let str = allocateString(stringSize) - let strData = getStringArrayRef(str) - let bytesRef = WasmRef.fromGrain(bytes) - let bytesData = getBytesArrayRef(bytesRef) + let str = DS.String.make(stringSize) + let strData = DS.String.getData(str) + let bytesRef = DS.Bytes.fromGrain(bytes) + let bytesData = DS.Bytes.getData(bytesRef) let bytesSize = { - let tmp = getBytesSize(bytesRef) - start + let tmp = DS.Bytes.getSize(bytesRef) - start if (size < tmp) { size } else { @@ -1510,7 +1493,7 @@ let decodeRangeHelp = ( } } if (stringSize == 0n) { - WasmRef.toGrain(str): String + DS.String.toGrain(str) } else { match (encoding) { UTF8 => { @@ -1588,7 +1571,7 @@ let decodeRangeHelp = ( } }, } - WasmRef.toGrain(str): String + DS.String.toGrain(str) } } @@ -1622,9 +1605,9 @@ provide let decodeRange = ( @unsafe let decodeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool) => { - let bytesRef = WasmRef.fromGrain(bytes) - let bytesSize = getBytesSize(bytesRef) - decodeRangeHelp(bytes, encoding, skipBom, 0, tagSimpleNumber(bytesSize)) + let bytesRef = DS.Bytes.fromGrain(bytes) + let bytesSize = DS.Bytes.getSize(bytesRef) + decodeRangeHelp(bytes, encoding, skipBom, 0, DS.tagSimpleNumber(bytesSize)) } /** @@ -1656,9 +1639,9 @@ provide let decode = (bytes: Bytes, encoding: Encoding, keepBom=false) => { provide let forEachCodePoint = (fn: Number => Void, str: String) => { use WasmI32.{ (+), ltU as (<) } - let strRef = WasmRef.fromGrain(str) - let strData = getStringArrayRef(strRef) - let strSize = getStringSize(strRef) + let strRef = DS.String.fromGrain(str) + let strData = DS.String.getData(strRef) + let strSize = DS.String.getSize(strRef) let mut byteIndex = 0n while (byteIndex < strSize) { @@ -1673,7 +1656,7 @@ provide let forEachCodePoint = (fn: Number => Void, str: String) => { // avoid heap allocations. `getCodePoint` will throw // MalformedUnicode exception for values exceeding this limit. let codePoint = getCodePoint(strData, byteIndex) - fn(tagSimpleNumber(codePoint)) + fn(DS.tagSimpleNumber(codePoint)) byteIndex += codePointByteCount } @@ -1696,9 +1679,9 @@ provide let forEachCodePoint = (fn: Number => Void, str: String) => { provide let forEachCodePointi = (fn: (Number, Number) => Void, str: String) => { use WasmI32.{ (+), ltU as (<) } - let strRef = WasmRef.fromGrain(str) - let strData = getStringArrayRef(strRef) - let strSize = getStringSize(strRef) + let strRef = DS.String.fromGrain(str) + let strData = DS.String.getData(strRef) + let strSize = DS.String.getSize(strRef) let mut byteIndex = 0n let mut index = 0n @@ -1714,7 +1697,7 @@ provide let forEachCodePointi = (fn: (Number, Number) => Void, str: String) => { // avoid heap allocations. `getCodePoint` will throw // MalformedUnicode exception for values exceeding this limit. let codePoint = getCodePoint(strData, byteIndex) - fn(tagSimpleNumber(codePoint), tagSimpleNumber(index)) + fn(DS.tagSimpleNumber(codePoint), DS.tagSimpleNumber(index)) byteIndex += codePointByteCount index += 1n @@ -1736,9 +1719,9 @@ provide let forEachCodePointi = (fn: (Number, Number) => Void, str: String) => { provide let forEachChar = (fn: Char => Void, str: String) => { use WasmI32.{ (+), ltU as (<), (==) } - let strRef = WasmRef.fromGrain(str) - let strData = getStringArrayRef(strRef) - let strSize = getStringSize(strRef) + let strRef = DS.String.fromGrain(str) + let strData = DS.String.getData(strRef) + let strSize = DS.String.getSize(strRef) let mut byteIndex = 0n while (byteIndex < strSize) { @@ -1753,7 +1736,7 @@ provide let forEachChar = (fn: Char => Void, str: String) => { // avoid heap allocations. `getCodePoint` will throw // MalformedUnicode exception for values exceeding this limit. let codePoint = getCodePoint(strData, byteIndex) - fn(tagChar(codePoint)) + fn(DS.tagChar(codePoint)) byteIndex += codePointByteCount } @@ -1776,9 +1759,9 @@ provide let forEachChar = (fn: Char => Void, str: String) => { provide let forEachChari = (fn: (Char, Number) => Void, str: String) => { use WasmI32.{ (+), ltU as (<), (==) } - let strRef = WasmRef.fromGrain(str) - let strData = getStringArrayRef(strRef) - let strSize = getStringSize(strRef) + let strRef = DS.String.fromGrain(str) + let strData = DS.String.getData(strRef) + let strSize = DS.String.getSize(strRef) let mut byteIndex = 0n let mut index = 0n @@ -1794,7 +1777,7 @@ provide let forEachChari = (fn: (Char, Number) => Void, str: String) => { // avoid heap allocations. `getCodePoint` will throw // MalformedUnicode exception for values exceeding this limit. let codePoint = getCodePoint(strData, byteIndex) - fn(tagChar(codePoint), tagSimpleNumber(index)) + fn(DS.tagChar(codePoint), DS.tagSimpleNumber(index)) byteIndex += codePointByteCount index += 1n @@ -1921,19 +1904,19 @@ let trimString = ( @unsafe provide let trimStart = (string: String) => { use WasmI32.{ (-), (+) } - let srcRef = WasmRef.fromGrain(string) - let srcData = getStringArrayRef(srcRef) - let srcSize = getStringSize(srcRef) + let srcRef = DS.String.fromGrain(string) + let srcData = DS.String.getData(srcRef) + let srcSize = DS.String.getSize(srcRef) let count = trimString(srcData, srcSize, false) - let str = allocateString(srcSize - count) + let str = DS.String.make(srcSize - count) WasmArrayRef.copyI8( - getStringArrayRef(str), + DS.String.getData(str), 0n, srcData, count, srcSize - count ) - WasmRef.toGrain(str): String + DS.String.toGrain(str) } /** * Trims the end of a string—removing any trailing whitespace characters. @@ -1948,13 +1931,13 @@ provide let trimStart = (string: String) => { @unsafe provide let trimEnd = (string: String) => { use WasmI32.{ (-), (+) } - let srcRef = WasmRef.fromGrain(string) - let srcData = getStringArrayRef(srcRef) - let srcSize = getStringSize(srcRef) + let srcRef = DS.String.fromGrain(string) + let srcData = DS.String.getData(srcRef) + let srcSize = DS.String.getSize(srcRef) let count = trimString(srcData, srcSize, true) - let str = allocateString(srcSize - count) - WasmArrayRef.copyI8(getStringArrayRef(str), 0n, srcData, 0n, srcSize - count) - WasmRef.toGrain(str): String + let str = DS.String.make(srcSize - count) + WasmArrayRef.copyI8(DS.String.getData(str), 0n, srcData, 0n, srcSize - count) + DS.String.toGrain(str) } /** * Trims a string—removing all leading and trailing whitespace characters. @@ -1969,21 +1952,21 @@ provide let trimEnd = (string: String) => { @unsafe provide let trim = (string: String) => { use WasmI32.{ (-), (+), (==) } - let srcRef = WasmRef.fromGrain(string) - let srcData = getStringArrayRef(srcRef) - let srcSize = getStringSize(srcRef) + let srcRef = DS.String.fromGrain(string) + let srcData = DS.String.getData(srcRef) + let srcSize = DS.String.getSize(srcRef) let startCount = trimString(srcData, srcSize, false) if (startCount == srcSize) return "" let endCount = trimString(srcData, srcSize, true) - let str = allocateString(srcSize - startCount - endCount) + let str = DS.String.make(srcSize - startCount - endCount) WasmArrayRef.copyI8( - getStringArrayRef(str), + DS.String.getData(str), 0n, srcData, startCount, srcSize - startCount - endCount ) - return WasmRef.toGrain(str): String + return DS.String.toGrain(str) } /** @@ -2046,13 +2029,13 @@ provide let repeat = (count, string: String) => { if (rawCount < 0n) { throw InvalidArgument("Invalid count must be a positive integer or zero") } - let stringRef = WasmRef.fromGrain(string) - let stringData = getStringArrayRef(stringRef) - let stringSize = getStringSize(stringRef) - let newStrRef = allocateString(stringSize * rawCount) - let newStrData = getStringArrayRef(newStrRef) + let stringRef = DS.String.fromGrain(string) + let stringData = DS.String.getData(stringRef) + let stringSize = DS.String.getSize(stringRef) + let newStrRef = DS.String.make(stringSize * rawCount) + let newStrData = DS.String.getData(newStrRef) for (let mut i = 0n; i < rawCount; i += 1n) { WasmArrayRef.copyI8(newStrData, stringSize * i, stringData, 0n, stringSize) } - return WasmRef.toGrain(newStrRef): String + return DS.String.toGrain(newStrRef) } diff --git a/stdlib/uint16.gr b/stdlib/uint16.gr index f1b9fcd8a5..973f71f1bf 100644 --- a/stdlib/uint16.gr +++ b/stdlib/uint16.gr @@ -27,7 +27,7 @@ use WasmI32.{ gtU as (>), geU as (>=), } -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures use DataStructures.{ untagInt16, tagUint16, untagUint16 } from "runtime/numbers" include Numbers use Numbers.{ diff --git a/stdlib/uint32.gr b/stdlib/uint32.gr index 726611d018..8d1f370092 100644 --- a/stdlib/uint32.gr +++ b/stdlib/uint32.gr @@ -26,10 +26,10 @@ use WasmI32.{ geU as (>=), } from "runtime/unsafe/wasmi64" include WasmI64 +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/exception" include Exception from "runtime/numbers" include Numbers use Numbers.{ reducedUnsignedInteger, coerceNumberToUnsignedWasmI32 } -from "runtime/unsafe/conv" include Conv /** * Converts a Number to a Uint32. @@ -42,7 +42,7 @@ from "runtime/unsafe/conv" include Conv @unsafe provide let fromNumber = (number: Number) => { let result = coerceNumberToUnsignedWasmI32(number) - Conv.toUint32(result) + DS.Uint32.toUint32(result) } /** @@ -55,7 +55,7 @@ provide let fromNumber = (number: Number) => { */ @unsafe provide let toNumber = (value: Uint32) => { - let value = Conv.fromUint32(value) + let value = DS.Uint32.fromUint32(value) let result = reducedUnsignedInteger(WasmI64.extendI32U(value)) WasmRef.toGrain(result): Number } @@ -70,8 +70,8 @@ provide let toNumber = (value: Uint32) => { */ @unsafe provide let fromInt32 = (number: Int32) => { - let value = Conv.fromInt32(number) - Conv.toUint32(value) + let value = DS.Int32.fromInt32(number) + DS.Uint32.toUint32(value) } /** @@ -87,9 +87,9 @@ provide let fromInt32 = (number: Int32) => { */ @unsafe provide let reinterpretFloat32 = (value: Float32) => { - let value = Conv.fromFloat32(value) + let value = DS.Float32.fromFloat32(value) let value = WasmI32.reinterpretF32(value) - Conv.toUint32(value) + DS.Uint32.toUint32(value) } /** @@ -102,8 +102,8 @@ provide let reinterpretFloat32 = (value: Float32) => { */ @unsafe provide let incr = (value: Uint32) => { - let value = Conv.fromUint32(value) - Conv.toUint32(value + 1n) + let value = DS.Uint32.fromUint32(value) + DS.Uint32.toUint32(value + 1n) } /** @@ -116,8 +116,8 @@ provide let incr = (value: Uint32) => { */ @unsafe provide let decr = (value: Uint32) => { - let value = Conv.fromUint32(value) - Conv.toUint32(value - 1n) + let value = DS.Uint32.fromUint32(value) + DS.Uint32.toUint32(value - 1n) } /** @@ -131,9 +131,9 @@ provide let decr = (value: Uint32) => { */ @unsafe provide let (+) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(xv + yv) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(xv + yv) } /** @@ -147,9 +147,9 @@ provide let (+) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (-) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(xv - yv) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(xv - yv) } /** @@ -163,9 +163,9 @@ provide let (-) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (*) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(xv * yv) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(xv * yv) } /** @@ -179,9 +179,9 @@ provide let (*) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (/) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(xv / yv) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(xv / yv) } /** @@ -195,9 +195,9 @@ provide let (/) = (x: Uint32, y: Uint32) => { */ @unsafe provide let rem = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(WasmI32.remU(xv, yv)) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(WasmI32.remU(xv, yv)) } /** @@ -211,9 +211,9 @@ provide let rem = (x: Uint32, y: Uint32) => { */ @unsafe provide let rotl = (value: Uint32, amount: Uint32) => { - let value = Conv.fromUint32(value) - let amount = Conv.fromUint32(amount) - Conv.toUint32(WasmI32.rotl(value, amount)) + let value = DS.Uint32.fromUint32(value) + let amount = DS.Uint32.fromUint32(amount) + DS.Uint32.toUint32(WasmI32.rotl(value, amount)) } /** @@ -227,9 +227,9 @@ provide let rotl = (value: Uint32, amount: Uint32) => { */ @unsafe provide let rotr = (value: Uint32, amount: Uint32) => { - let value = Conv.fromUint32(value) - let amount = Conv.fromUint32(amount) - Conv.toUint32(WasmI32.rotr(value, amount)) + let value = DS.Uint32.fromUint32(value) + let amount = DS.Uint32.fromUint32(amount) + DS.Uint32.toUint32(WasmI32.rotr(value, amount)) } /** @@ -243,9 +243,9 @@ provide let rotr = (value: Uint32, amount: Uint32) => { */ @unsafe provide let (<<) = (value: Uint32, amount: Uint32) => { - let value = Conv.fromUint32(value) - let amount = Conv.fromUint32(amount) - Conv.toUint32(value << amount) + let value = DS.Uint32.fromUint32(value) + let amount = DS.Uint32.fromUint32(amount) + DS.Uint32.toUint32(value << amount) } /** @@ -259,9 +259,9 @@ provide let (<<) = (value: Uint32, amount: Uint32) => { */ @unsafe provide let (>>>) = (value: Uint32, amount: Uint32) => { - let value = Conv.fromUint32(value) - let amount = Conv.fromUint32(amount) - Conv.toUint32(value >>> amount) + let value = DS.Uint32.fromUint32(value) + let amount = DS.Uint32.fromUint32(amount) + DS.Uint32.toUint32(value >>> amount) } /** @@ -275,8 +275,8 @@ provide let (>>>) = (value: Uint32, amount: Uint32) => { */ @unsafe provide let (==) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) xv == yv } @@ -291,8 +291,8 @@ provide let (==) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (!=) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) xv != yv } @@ -306,7 +306,7 @@ provide let (!=) = (x: Uint32, y: Uint32) => { */ @unsafe provide let eqz = (value: Uint32) => { - let value = Conv.fromUint32(value) + let value = DS.Uint32.fromUint32(value) WasmI32.eqz(value) } @@ -321,8 +321,8 @@ provide let eqz = (value: Uint32) => { */ @unsafe provide let (<) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) xv < yv } @@ -337,8 +337,8 @@ provide let (<) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (>) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) xv > yv } @@ -353,8 +353,8 @@ provide let (>) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (<=) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) xv <= yv } @@ -369,8 +369,8 @@ provide let (<=) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (>=) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) xv >= yv } @@ -384,8 +384,8 @@ provide let (>=) = (x: Uint32, y: Uint32) => { */ @unsafe provide let lnot = (value: Uint32) => { - let value = Conv.fromUint32(value) - Conv.toUint32(value ^ 0xffffffffn) + let value = DS.Uint32.fromUint32(value) + DS.Uint32.toUint32(value ^ 0xffffffffn) } /** @@ -399,9 +399,9 @@ provide let lnot = (value: Uint32) => { */ @unsafe provide let (&) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(xv & yv) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(xv & yv) } /** @@ -415,9 +415,9 @@ provide let (&) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (|) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(xv | yv) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(xv | yv) } /** @@ -431,9 +431,9 @@ provide let (|) = (x: Uint32, y: Uint32) => { */ @unsafe provide let (^) = (x: Uint32, y: Uint32) => { - let xv = Conv.fromUint32(x) - let yv = Conv.fromUint32(y) - Conv.toUint32(xv ^ yv) + let xv = DS.Uint32.fromUint32(x) + let yv = DS.Uint32.fromUint32(y) + DS.Uint32.toUint32(xv ^ yv) } /** @@ -446,8 +446,8 @@ provide let (^) = (x: Uint32, y: Uint32) => { */ @unsafe provide let clz = (value: Uint32) => { - let value = Conv.fromUint32(value) - Conv.toUint32(WasmI32.clz(value)) + let value = DS.Uint32.fromUint32(value) + DS.Uint32.toUint32(WasmI32.clz(value)) } /** @@ -460,8 +460,8 @@ provide let clz = (value: Uint32) => { */ @unsafe provide let ctz = (value: Uint32) => { - let value = Conv.fromUint32(value) - Conv.toUint32(WasmI32.ctz(value)) + let value = DS.Uint32.fromUint32(value) + DS.Uint32.toUint32(WasmI32.ctz(value)) } /** @@ -474,6 +474,6 @@ provide let ctz = (value: Uint32) => { */ @unsafe provide let popcnt = (value: Uint32) => { - let value = Conv.fromUint32(value) - Conv.toUint32(WasmI32.popcnt(value)) + let value = DS.Uint32.fromUint32(value) + DS.Uint32.toUint32(WasmI32.popcnt(value)) } diff --git a/stdlib/uint64.gr b/stdlib/uint64.gr index f1c0831693..a776205c8f 100644 --- a/stdlib/uint64.gr +++ b/stdlib/uint64.gr @@ -25,10 +25,10 @@ use WasmI64.{ gtU as (>), geU as (>=), } +from "runtime/unsafe/dataStructures" include DataStructures as DS from "runtime/exception" include Exception from "runtime/numbers" include Numbers use Numbers.{ reducedUnsignedInteger, coerceNumberToUnsignedWasmI64 } -from "runtime/unsafe/conv" include Conv /** * Converts a Number to a Uint64. @@ -41,7 +41,7 @@ from "runtime/unsafe/conv" include Conv @unsafe provide let fromNumber = (number: Number) => { let result = coerceNumberToUnsignedWasmI64(number) - Conv.toUint64(result) + DS.Uint64.toUint64(result) } /** @@ -54,7 +54,7 @@ provide let fromNumber = (number: Number) => { */ @unsafe provide let toNumber = (value: Uint64) => { - let value = Conv.fromUint64(value) + let value = DS.Uint64.fromUint64(value) let result = reducedUnsignedInteger(value) WasmRef.toGrain(result): Number } @@ -69,8 +69,8 @@ provide let toNumber = (value: Uint64) => { */ @unsafe provide let fromInt64 = (number: Int64) => { - let value = Conv.fromInt64(number) - Conv.toUint64(value) + let value = DS.Int64.fromInt64(number) + DS.Uint64.toUint64(value) } /** @@ -86,9 +86,9 @@ provide let fromInt64 = (number: Int64) => { */ @unsafe provide let reinterpretFloat64 = (value: Float64) => { - let value = Conv.fromFloat64(value) + let value = DS.Float64.fromFloat64(value) let value = WasmI64.reinterpretF64(value) - Conv.toUint64(value) + DS.Uint64.toUint64(value) } /** @@ -101,8 +101,8 @@ provide let reinterpretFloat64 = (value: Float64) => { */ @unsafe provide let incr = (value: Uint64) => { - let value = Conv.fromUint64(value) - Conv.toUint64(value + 1N) + let value = DS.Uint64.fromUint64(value) + DS.Uint64.toUint64(value + 1N) } /** @@ -115,8 +115,8 @@ provide let incr = (value: Uint64) => { */ @unsafe provide let decr = (value: Uint64) => { - let value = Conv.fromUint64(value) - Conv.toUint64(value - 1N) + let value = DS.Uint64.fromUint64(value) + DS.Uint64.toUint64(value - 1N) } /** @@ -130,9 +130,9 @@ provide let decr = (value: Uint64) => { */ @unsafe provide let (+) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(xv + yv) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(xv + yv) } /** @@ -146,9 +146,9 @@ provide let (+) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (-) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(xv - yv) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(xv - yv) } /** @@ -162,9 +162,9 @@ provide let (-) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (*) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(xv * yv) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(xv * yv) } /** @@ -178,9 +178,9 @@ provide let (*) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (/) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(xv / yv) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(xv / yv) } /** @@ -194,9 +194,9 @@ provide let (/) = (x: Uint64, y: Uint64) => { */ @unsafe provide let rem = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(WasmI64.remU(xv, yv)) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(WasmI64.remU(xv, yv)) } /** @@ -210,9 +210,9 @@ provide let rem = (x: Uint64, y: Uint64) => { */ @unsafe provide let rotl = (value: Uint64, amount: Uint64) => { - let value = Conv.fromUint64(value) - let amount = Conv.fromUint64(amount) - Conv.toUint64(WasmI64.rotl(value, amount)) + let value = DS.Uint64.fromUint64(value) + let amount = DS.Uint64.fromUint64(amount) + DS.Uint64.toUint64(WasmI64.rotl(value, amount)) } /** @@ -226,9 +226,9 @@ provide let rotl = (value: Uint64, amount: Uint64) => { */ @unsafe provide let rotr = (value: Uint64, amount: Uint64) => { - let value = Conv.fromUint64(value) - let amount = Conv.fromUint64(amount) - Conv.toUint64(WasmI64.rotr(value, amount)) + let value = DS.Uint64.fromUint64(value) + let amount = DS.Uint64.fromUint64(amount) + DS.Uint64.toUint64(WasmI64.rotr(value, amount)) } /** @@ -242,9 +242,9 @@ provide let rotr = (value: Uint64, amount: Uint64) => { */ @unsafe provide let (<<) = (value: Uint64, amount: Uint64) => { - let value = Conv.fromUint64(value) - let amount = Conv.fromUint64(amount) - Conv.toUint64(value << amount) + let value = DS.Uint64.fromUint64(value) + let amount = DS.Uint64.fromUint64(amount) + DS.Uint64.toUint64(value << amount) } /** @@ -258,9 +258,9 @@ provide let (<<) = (value: Uint64, amount: Uint64) => { */ @unsafe provide let (>>>) = (value: Uint64, amount: Uint64) => { - let value = Conv.fromUint64(value) - let amount = Conv.fromUint64(amount) - Conv.toUint64(value >>> amount) + let value = DS.Uint64.fromUint64(value) + let amount = DS.Uint64.fromUint64(amount) + DS.Uint64.toUint64(value >>> amount) } /** @@ -274,8 +274,8 @@ provide let (>>>) = (value: Uint64, amount: Uint64) => { */ @unsafe provide let (==) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) xv == yv } @@ -290,8 +290,8 @@ provide let (==) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (!=) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) xv != yv } @@ -305,7 +305,7 @@ provide let (!=) = (x: Uint64, y: Uint64) => { */ @unsafe provide let eqz = (value: Uint64) => { - let value = Conv.fromUint64(value) + let value = DS.Uint64.fromUint64(value) WasmI64.eqz(value) } @@ -320,8 +320,8 @@ provide let eqz = (value: Uint64) => { */ @unsafe provide let (<) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) xv < yv } @@ -336,8 +336,8 @@ provide let (<) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (>) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) xv > yv } @@ -352,8 +352,8 @@ provide let (>) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (<=) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) xv <= yv } @@ -368,8 +368,8 @@ provide let (<=) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (>=) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) xv >= yv } @@ -383,8 +383,8 @@ provide let (>=) = (x: Uint64, y: Uint64) => { */ @unsafe provide let lnot = (value: Uint64) => { - let value = Conv.fromUint64(value) - Conv.toUint64(value ^ 0xffffffffffffffffN) + let value = DS.Uint64.fromUint64(value) + DS.Uint64.toUint64(value ^ 0xffffffffffffffffN) } /** @@ -398,9 +398,9 @@ provide let lnot = (value: Uint64) => { */ @unsafe provide let (&) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(xv & yv) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(xv & yv) } /** @@ -414,9 +414,9 @@ provide let (&) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (|) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(xv | yv) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(xv | yv) } /** @@ -430,9 +430,9 @@ provide let (|) = (x: Uint64, y: Uint64) => { */ @unsafe provide let (^) = (x: Uint64, y: Uint64) => { - let xv = Conv.fromUint64(x) - let yv = Conv.fromUint64(y) - Conv.toUint64(xv ^ yv) + let xv = DS.Uint64.fromUint64(x) + let yv = DS.Uint64.fromUint64(y) + DS.Uint64.toUint64(xv ^ yv) } /** @@ -445,8 +445,8 @@ provide let (^) = (x: Uint64, y: Uint64) => { */ @unsafe provide let clz = (value: Uint64) => { - let value = Conv.fromUint64(value) - Conv.toUint64(WasmI64.clz(value)) + let value = DS.Uint64.fromUint64(value) + DS.Uint64.toUint64(WasmI64.clz(value)) } /** @@ -459,8 +459,8 @@ provide let clz = (value: Uint64) => { */ @unsafe provide let ctz = (value: Uint64) => { - let value = Conv.fromUint64(value) - Conv.toUint64(WasmI64.ctz(value)) + let value = DS.Uint64.fromUint64(value) + DS.Uint64.toUint64(WasmI64.ctz(value)) } /** @@ -473,6 +473,6 @@ provide let ctz = (value: Uint64) => { */ @unsafe provide let popcnt = (value: Uint64) => { - let value = Conv.fromUint64(value) - Conv.toUint64(WasmI64.popcnt(value)) + let value = DS.Uint64.fromUint64(value) + DS.Uint64.toUint64(WasmI64.popcnt(value)) } diff --git a/stdlib/uint8.gr b/stdlib/uint8.gr index 41d3b3f289..dfd11cdd10 100644 --- a/stdlib/uint8.gr +++ b/stdlib/uint8.gr @@ -27,7 +27,7 @@ use WasmI32.{ gtU as (>), geU as (>=), } -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures use DataStructures.{ untagInt8, tagUint8, untagUint8 } from "runtime/numbers" include Numbers use Numbers.{ diff --git a/stdlib/wasi/file.gr b/stdlib/wasi/file.gr index eb7e78a914..805225880a 100644 --- a/stdlib/wasi/file.gr +++ b/stdlib/wasi/file.gr @@ -30,21 +30,7 @@ from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/wasi" include Wasi from "runtime/malloc" include Malloc from "runtime/unsafe/memory" include Memory -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - tagSimpleNumber, - untagSimpleNumber, - allocateArray, - allocateBytes, - getBytesSize, - getBytesArrayRef, - allocateString, - getStringSize, - getStringArrayRef, - newInt32, - newInt64, -} -from "runtime/unsafe/conv" include Conv +from "runtime/unsafe/dataStructures" include DataStructures as DS from "list" include List @@ -56,7 +42,7 @@ provide enum FileDescriptor { } @unsafe -let getRawFileDescriptor = (FileDescriptor(fd)) => untagSimpleNumber(fd) +let getRawFileDescriptor = (FileDescriptor(fd)) => DS.untagSimpleNumber(fd) /** * Flags that determine how paths should be resolved when looking up a file or directory. @@ -524,14 +510,14 @@ provide record Filestats { @unsafe let buildFileStats = (structPtr: WasmI32) => { - let device = Conv.toInt64(WasmI64.load(structPtr, 0n)) - let inode = Conv.toInt64(WasmI64.load(structPtr, 8n)) + let device = DS.Int64.toInt64(WasmI64.load(structPtr, 0n)) + let inode = DS.Int64.toInt64(WasmI64.load(structPtr, 8n)) let filetype = filetypeFromNumber(WasmI32.load8U(structPtr, 16n)) - let linkcount = Conv.toInt64(WasmI64.load(structPtr, 24n)) - let size = Conv.toInt64(WasmI64.load(structPtr, 32n)) - let accessed = Conv.toInt64(WasmI64.load(structPtr, 40n)) - let modified = Conv.toInt64(WasmI64.load(structPtr, 48n)) - let changed = Conv.toInt64(WasmI64.load(structPtr, 56n)) + let linkcount = DS.Int64.toInt64(WasmI64.load(structPtr, 24n)) + let size = DS.Int64.toInt64(WasmI64.load(structPtr, 32n)) + let accessed = DS.Int64.toInt64(WasmI64.load(structPtr, 40n)) + let modified = DS.Int64.toInt64(WasmI64.load(structPtr, 48n)) + let changed = DS.Int64.toInt64(WasmI64.load(structPtr, 56n)) { device, inode, filetype, linkcount, size, accessed, modified, changed } } @@ -590,9 +576,9 @@ provide let pathOpen = ( let combinedDirFlags = combineLookupFlags(dirFlags) - let path = WasmRef.fromGrain(path) - let pathLength = getStringSize(path) - let pathData = getStringArrayRef(path) + let path = DS.String.fromGrain(path) + let pathLength = DS.String.getSize(path) + let pathData = DS.String.getData(path) let combinedOFlags = combineOpenFlags(openFlags) @@ -621,10 +607,10 @@ provide let pathOpen = ( if (err != Wasi._ESUCCESS) { Malloc.free(newFd) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } - let fd = FileDescriptor(tagSimpleNumber(WasmI32.load(newFd, 0n))) + let fd = FileDescriptor(DS.tagSimpleNumber(WasmI32.load(newFd, 0n))) Malloc.free(newFd) return Ok(fd) } @@ -641,7 +627,7 @@ provide let fdRead = (fd: FileDescriptor, size: Number) => { let fdArg = fd let fd = getRawFileDescriptor(fd) - let n = untagSimpleNumber(size) + let n = DS.untagSimpleNumber(size) let dataPtr = Malloc.malloc(n) // ptr to buffer (4 bytes) | length of buffer (4 bytes) | bytes written output (4 bytes) @@ -654,15 +640,15 @@ provide let fdRead = (fd: FileDescriptor, size: Number) => { if (err != Wasi._ESUCCESS) { Malloc.free(iovs) Malloc.free(dataPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let nread = WasmI32.load(nreadPtr, 0n) - let bytesRef = allocateBytes(nread) - Memory.copyLinearMemoryToRefArray(getBytesArrayRef(bytesRef), dataPtr, nread) + let bytesRef = DS.Bytes.make(nread) + Memory.copyLinearMemoryToRefArray(DS.Bytes.getData(bytesRef), dataPtr, nread) Malloc.free(iovs) Malloc.free(dataPtr) - return Ok((WasmRef.toGrain(bytesRef): Bytes, tagSimpleNumber(nread))) + return Ok((DS.Bytes.toGrain(bytesRef), DS.tagSimpleNumber(nread))) } /** @@ -677,9 +663,9 @@ provide let fdRead = (fd: FileDescriptor, size: Number) => { provide let fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => { let fd = getRawFileDescriptor(fd) - let offset = Conv.fromInt64(offset) + let offset = DS.Int64.fromInt64(offset) - let n = untagSimpleNumber(size) + let n = DS.untagSimpleNumber(size) let dataPtr = Malloc.malloc(n) // ptr to buffer (4 bytes) | length of buffer (4 bytes) | bytes written output (4 bytes) @@ -692,15 +678,15 @@ provide let fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => { if (err != Wasi._ESUCCESS) { Malloc.free(iovs) Malloc.free(dataPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let nread = WasmI32.load(nreadPtr, 0n) - let bytesRef = allocateBytes(nread) - Memory.copyLinearMemoryToRefArray(getBytesArrayRef(bytesRef), dataPtr, nread) + let bytesRef = DS.Bytes.make(nread) + Memory.copyLinearMemoryToRefArray(DS.Bytes.getData(bytesRef), dataPtr, nread) Malloc.free(iovs) Malloc.free(dataPtr) - return Ok((WasmRef.toGrain(bytesRef): Bytes, tagSimpleNumber(nread))) + return Ok((DS.Bytes.toGrain(bytesRef), DS.tagSimpleNumber(nread))) } /** @@ -720,7 +706,7 @@ provide let fdPrestatGet = (fd: FileDescriptor) => { let err = Wasi.fd_prestat_get(fd, dir) if (err != Wasi._ESUCCESS) { Malloc.free(dir) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let prefixLen = WasmI32.load(dir, 4n) @@ -730,7 +716,7 @@ provide let fdPrestatGet = (fd: FileDescriptor) => { let err = Wasi.fd_prestat_dir_name(fd, prefix, prefixLen) if (err != Wasi._ESUCCESS) { Malloc.free(prefix) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let prefixLen = if ( @@ -744,15 +730,15 @@ provide let fdPrestatGet = (fd: FileDescriptor) => { prefixLen } - let prefixRef = allocateString(prefixLen) + let prefixRef = DS.String.make(prefixLen) Memory.copyLinearMemoryToRefArray( - getStringArrayRef(prefixRef), + DS.String.getData(prefixRef), prefix, prefixLen ) Malloc.free(prefix) - return Ok(Dir{ prefix: WasmRef.toGrain(prefixRef): String, fd: fdArg }) + return Ok(Dir{ prefix: DS.String.toGrain(prefixRef), fd: fdArg }) } /** @@ -766,11 +752,11 @@ provide let fdPrestatGet = (fd: FileDescriptor) => { provide let fdWrite = (fd: FileDescriptor, data: Bytes) => { let fd = getRawFileDescriptor(fd) - let dstRef = WasmRef.fromGrain(data) - let dstSize = getBytesSize(dstRef) + let dstRef = DS.Bytes.fromGrain(data) + let dstSize = DS.Bytes.getSize(dstRef) let dataPtr = Malloc.malloc(dstSize) - Memory.copyRefArrayToLinearMemory(dataPtr, getBytesArrayRef(dstRef), dstSize) + Memory.copyRefArrayToLinearMemory(dataPtr, DS.Bytes.getData(dstRef), dstSize) // ptr to buffer (4 bytes) | length of buffer (4 bytes) | bytes written output (4 bytes) let iovs = Malloc.malloc(3n * 4n) WasmI32.store(iovs, dataPtr, 0n) @@ -782,12 +768,12 @@ provide let fdWrite = (fd: FileDescriptor, data: Bytes) => { Malloc.free(dataPtr) if (err != Wasi._ESUCCESS) { Malloc.free(iovs) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let nwritten = WasmI32.load(nWrittenPtr, 0n) Malloc.free(iovs) - return Ok(tagSimpleNumber(nwritten)) + return Ok(DS.tagSimpleNumber(nwritten)) } /** @@ -802,9 +788,9 @@ provide let fdWrite = (fd: FileDescriptor, data: Bytes) => { provide let fdPwrite = (fd: FileDescriptor, data: Bytes, offset: Int64) => { let fd = getRawFileDescriptor(fd) - let dstRef = WasmRef.fromGrain(data) - let dstSize = getBytesSize(dstRef) - let dstData = getBytesArrayRef(dstRef) + let dstRef = DS.Bytes.fromGrain(data) + let dstSize = DS.Bytes.getSize(dstRef) + let dstData = DS.Bytes.getData(dstRef) let dataPtr = Malloc.malloc(dstSize) Memory.copyRefArrayToLinearMemory(dataPtr, dstData, dstSize) @@ -813,7 +799,7 @@ provide let fdPwrite = (fd: FileDescriptor, data: Bytes, offset: Int64) => { WasmI32.store(iovs, dataPtr, 0n) WasmI32.store(iovs, dstSize, 4n) - let offset = Conv.fromInt64(offset) + let offset = DS.Int64.fromInt64(offset) let nWrittenPtr = iovs + 3n * 4n @@ -821,12 +807,12 @@ provide let fdPwrite = (fd: FileDescriptor, data: Bytes, offset: Int64) => { Malloc.free(dataPtr) if (err != Wasi._ESUCCESS) { Malloc.free(iovs) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let nwritten = WasmI32.load(nWrittenPtr, 0n) Malloc.free(iovs) - return Ok(tagSimpleNumber(nwritten)) + return Ok(DS.tagSimpleNumber(nwritten)) } /** @@ -841,12 +827,12 @@ provide let fdPwrite = (fd: FileDescriptor, data: Bytes, offset: Int64) => { provide let fdAllocate = (fd: FileDescriptor, offset: Int64, size: Int64) => { let fd = getRawFileDescriptor(fd) - let offset = Conv.fromInt64(offset) - let size = Conv.fromInt64(size) + let offset = DS.Int64.fromInt64(offset) + let size = DS.Int64.fromInt64(size) let err = Wasi.fd_allocate(fd, offset, size) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -864,7 +850,7 @@ provide let fdClose = (fd: FileDescriptor) => { let err = Wasi.fd_close(fd) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -882,7 +868,7 @@ provide let fdDatasync = (fd: FileDescriptor) => { let err = Wasi.fd_datasync(fd) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -900,7 +886,7 @@ provide let fdSync = (fd: FileDescriptor) => { let err = Wasi.fd_sync(fd) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -954,7 +940,7 @@ provide let fdStats = (fd: FileDescriptor) => { let err = Wasi.fd_fdstat_get(fd, structPtr) if (err != Wasi._ESUCCESS) { Malloc.free(structPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let filetype = filetypeFromNumber(WasmI32.load8U(structPtr, 0n)) let fdflags = WasmI32.load16U(structPtr, 4n) @@ -1037,7 +1023,7 @@ provide let fdSetFlags = (fd: FileDescriptor, flags: List) => { let err = Wasi.fd_fdstat_set_flags(fd, flags) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1064,7 +1050,7 @@ provide let fdSetRights = ( let err = Wasi.fd_fdstat_set_rights(fd, rights, rightsInheriting) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1085,7 +1071,7 @@ provide let fdFilestats = (fd: FileDescriptor) => { let err = Wasi.fd_filestat_get(fd, fileStatsStruct) if (err != Wasi._ESUCCESS) { Malloc.free(fileStatsStruct) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let fileStats = buildFileStats(fileStatsStruct) @@ -1104,11 +1090,11 @@ provide let fdFilestats = (fd: FileDescriptor) => { provide let fdSetSize = (fd: FileDescriptor, size: Int64) => { let fd = getRawFileDescriptor(fd) - let size = Conv.fromInt64(size) + let size = DS.Int64.fromInt64(size) let err = Wasi.fd_filestat_set_size(fd, size) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1125,11 +1111,11 @@ provide let fdSetSize = (fd: FileDescriptor, size: Int64) => { provide let fdSetAccessTime = (fd: FileDescriptor, timestamp: Int64) => { let fd = getRawFileDescriptor(fd) - let time = Conv.fromInt64(timestamp) + let time = DS.Int64.fromInt64(timestamp) let err = Wasi.fd_filestat_set_times(fd, time, 0N, Wasi._TIME_SET_ATIM) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1147,7 +1133,7 @@ provide let fdSetAccessTimeNow = (fd: FileDescriptor) => { let err = Wasi.fd_filestat_set_times(fd, 0N, 0N, Wasi._TIME_SET_ATIM_NOW) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1164,11 +1150,11 @@ provide let fdSetAccessTimeNow = (fd: FileDescriptor) => { provide let fdSetModifiedTime = (fd: FileDescriptor, timestamp: Int64) => { let fd = getRawFileDescriptor(fd) - let time = Conv.fromInt64(timestamp) + let time = DS.Int64.fromInt64(timestamp) let err = Wasi.fd_filestat_set_times(fd, 0N, time, Wasi._TIME_SET_MTIM) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1186,7 +1172,7 @@ provide let fdSetModifiedTimeNow = (fd: FileDescriptor) => { let err = Wasi.fd_filestat_set_times(fd, 0N, 0N, Wasi._TIME_SET_MTIM_NOW) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1214,7 +1200,7 @@ provide let fdReaddir = (fd: FileDescriptor) => { if (err != Wasi._ESUCCESS) { Malloc.free(buf) Malloc.free(bufUsed) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let used = WasmI32.load(bufUsed, 0n) @@ -1254,7 +1240,7 @@ provide let fdReaddir = (fd: FileDescriptor) => { bufs = next } Malloc.free(bufUsed) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } if (WasmI32.load(bufUsed, 0n) != bufLen) { @@ -1269,23 +1255,23 @@ provide let fdReaddir = (fd: FileDescriptor) => { Malloc.free(bufUsed) - let arrRef = allocateArray(numEntries, WasmRef.fromGrain(void)) - let arrData = DataStructures.getCompoundValueArrayRef(arrRef) + let arrRef = DS.Array.make(numEntries, WasmRef.fromGrain(void)) + let arrData = DS.Array.getData(arrRef) for (let mut i = numEntries - 1n; i >= 0n; i -= 1n) { let ent = WasmI32.load(bufs, 0n) - let inode = Conv.toInt64(WasmI64.load(ent, 8n)) + let inode = DS.Int64.toInt64(WasmI64.load(ent, 8n)) let dirnameLen = WasmI32.load(ent, 16n) - let dirname = allocateString(dirnameLen) - let dirnameData = getStringArrayRef(dirname) + let dirname = DS.String.make(dirnameLen) + let dirnameData = DS.String.getData(dirname) Memory.copyLinearMemoryToRefArray( dirnameData, ent + structWidth, dirnameLen ) - let path = WasmRef.toGrain(dirname): String + let path = DS.String.toGrain(dirname) let filetype = filetypeFromNumber(WasmI32.load8U(ent, 20n)) @@ -1300,7 +1286,7 @@ provide let fdReaddir = (fd: FileDescriptor) => { bufs = next } - return Ok(WasmRef.toGrain(arrRef): Array) + return Ok(DS.Array.toGrain(arrRef): Array) } /** @@ -1317,7 +1303,7 @@ provide let fdRenumber = (fromFd: FileDescriptor, toFd: FileDescriptor) => { let err = Wasi.fd_renumber(fromFd, toFd) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1335,7 +1321,7 @@ provide let fdRenumber = (fromFd: FileDescriptor, toFd: FileDescriptor) => { provide let fdSeek = (fd: FileDescriptor, offset: Int64, whence: Whence) => { let fd = getRawFileDescriptor(fd) - let offset = Conv.fromInt64(offset) + let offset = DS.Int64.fromInt64(offset) let whence = match (whence) { Set => Wasi._WHENCE_SET, @@ -1348,9 +1334,9 @@ provide let fdSeek = (fd: FileDescriptor, offset: Int64, whence: Whence) => { let err = Wasi.fd_seek(fd, offset, whence, newoffsetPtr) if (err != Wasi._ESUCCESS) { Malloc.free(newoffsetPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } - let newoffset = Conv.toInt64(WasmI64.load(newoffsetPtr, 0n)) + let newoffset = DS.Int64.toInt64(WasmI64.load(newoffsetPtr, 0n)) Malloc.free(newoffsetPtr) return Ok(newoffset) @@ -1371,9 +1357,9 @@ provide let fdTell = (fd: FileDescriptor) => { let err = Wasi.fd_tell(fd, offsetPtr) if (err != Wasi._ESUCCESS) { Malloc.free(offsetPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } - let offset = Conv.toInt64(WasmI64.load(offsetPtr, 0n)) + let offset = DS.Int64.toInt64(WasmI64.load(offsetPtr, 0n)) Malloc.free(offsetPtr) return Ok(offset) @@ -1390,19 +1376,19 @@ provide let fdTell = (fd: FileDescriptor) => { provide let pathCreateDirectory = (fd: FileDescriptor, path: String) => { let fd = getRawFileDescriptor(fd) - let stringRef = WasmRef.fromGrain(path) - let stringSize = getStringSize(stringRef) + let stringRef = DS.String.fromGrain(path) + let stringSize = DS.String.getSize(stringRef) let stringPtr = Malloc.malloc(stringSize) Memory.copyRefArrayToLinearMemory( stringPtr, - getStringArrayRef(stringRef), + DS.String.getData(stringRef), stringSize ) let err = Wasi.path_create_directory(fd, stringPtr + 8n, stringSize) Malloc.free(stringPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1426,9 +1412,9 @@ provide let pathFilestats = ( let combinedDirFlags = combineLookupFlags(dirFlags) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) @@ -1445,7 +1431,7 @@ provide let pathFilestats = ( Malloc.free(pathPtr) if (err != Wasi._ESUCCESS) { Malloc.free(filestatsStruct) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let filestats = buildFileStats(filestatsStruct) @@ -1473,14 +1459,14 @@ provide let pathSetAccessTime = ( let combinedDirFlags = combineLookupFlags(dirFlags) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) - let time = Conv.fromInt64(timestamp) + let time = DS.Int64.fromInt64(timestamp) let err = Wasi.path_filestat_set_times( fd, @@ -1493,7 +1479,7 @@ provide let pathSetAccessTime = ( ) Malloc.free(pathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1517,9 +1503,9 @@ provide let pathSetAccessTimeNow = ( let combinedDirFlags = combineLookupFlags(dirFlags) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) @@ -1535,7 +1521,7 @@ provide let pathSetAccessTimeNow = ( ) Malloc.free(pathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1561,14 +1547,14 @@ provide let pathSetModifiedTime = ( let combinedDirFlags = combineLookupFlags(dirFlags) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) - let time = Conv.fromInt64(timestamp) + let time = DS.Int64.fromInt64(timestamp) let err = Wasi.path_filestat_set_times( fd, @@ -1581,7 +1567,7 @@ provide let pathSetModifiedTime = ( ) Malloc.free(pathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1605,9 +1591,9 @@ provide let pathSetModifiedTimeNow = ( let combinedDirFlags = combineLookupFlags(dirFlags) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) @@ -1623,7 +1609,7 @@ provide let pathSetModifiedTimeNow = ( ) Malloc.free(pathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1652,9 +1638,9 @@ provide let pathLink = ( let combinedDirFlags = combineLookupFlags(dirFlags) - let sourcePathRef = WasmRef.fromGrain(sourcePath) - let sourcePathSize = getStringSize(sourcePathRef) - let sourcePathData = getStringArrayRef(sourcePathRef) + let sourcePathRef = DS.String.fromGrain(sourcePath) + let sourcePathSize = DS.String.getSize(sourcePathRef) + let sourcePathData = DS.String.getData(sourcePathRef) let sourcePathPtr = Malloc.malloc(sourcePathSize) Memory.copyRefArrayToLinearMemory( @@ -1663,9 +1649,9 @@ provide let pathLink = ( sourcePathSize ) - let targetPathRef = WasmRef.fromGrain(targetPath) - let targetPathSize = getStringSize(targetPathRef) - let targetPathData = getStringArrayRef(targetPathRef) + let targetPathRef = DS.String.fromGrain(targetPath) + let targetPathSize = DS.String.getSize(targetPathRef) + let targetPathData = DS.String.getData(targetPathRef) let targetPathPtr = Malloc.malloc(targetPathSize) Memory.copyRefArrayToLinearMemory( @@ -1686,7 +1672,7 @@ provide let pathLink = ( Malloc.free(sourcePathPtr) Malloc.free(targetPathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1708,18 +1694,18 @@ provide let pathSymlink = ( ) => { let fd = getRawFileDescriptor(fd) - let sourcePathRef = WasmRef.fromGrain(sourcePath) - let sourcePathSize = getStringSize(sourcePathRef) - let sourcePathData = getStringArrayRef(sourcePathRef) + let sourcePathRef = DS.String.fromGrain(sourcePath) + let sourcePathSize = DS.String.getSize(sourcePathRef) + let sourcePathData = DS.String.getData(sourcePathRef) // TODO this workaround can be deleted once a uvwasi release with PR https://github.com/nodejs/uvwasi/pull/234 is released let sourceBuf = Malloc.malloc(sourcePathSize + 1n) Memory.copyRefArrayToLinearMemory(sourceBuf, sourcePathData, sourcePathSize) WasmI32.store8(sourceBuf + sourcePathSize, 0n, 0n) - let targetPathRef = WasmRef.fromGrain(targetPath) - let targetPathSize = getStringSize(targetPathRef) - let targetPathData = getStringArrayRef(targetPathRef) + let targetPathRef = DS.String.fromGrain(targetPath) + let targetPathSize = DS.String.getSize(targetPathRef) + let targetPathData = DS.String.getData(targetPathRef) let targetPtr = Malloc.malloc(targetPathSize) Memory.copyRefArrayToLinearMemory(targetPtr, targetPathData, targetPathSize) @@ -1734,7 +1720,7 @@ provide let pathSymlink = ( Malloc.free(sourceBuf) Malloc.free(targetPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1751,9 +1737,9 @@ provide let pathSymlink = ( provide let pathUnlink = (fd: FileDescriptor, path: String) => { let fd = getRawFileDescriptor(fd) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) @@ -1761,7 +1747,7 @@ provide let pathUnlink = (fd: FileDescriptor, path: String) => { let err = Wasi.path_unlink_file(fd, pathPtr + 8n, pathSize) Malloc.free(pathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1779,14 +1765,14 @@ provide let pathUnlink = (fd: FileDescriptor, path: String) => { provide let pathReadlink = (fd: FileDescriptor, path: String, size: Number) => { let fd = getRawFileDescriptor(fd) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) - let size = untagSimpleNumber(size) + let size = DS.untagSimpleNumber(size) let strPtr = Malloc.malloc(size) let nread = Malloc.malloc(4n) @@ -1796,18 +1782,18 @@ provide let pathReadlink = (fd: FileDescriptor, path: String, size: Number) => { if (err != Wasi._ESUCCESS) { Malloc.free(strPtr) Malloc.free(nread) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } - let strRef = allocateString(size) - let strData = getStringArrayRef(strRef) + let strRef = DS.String.make(size) + let strData = DS.String.getData(strRef) Memory.copyLinearMemoryToRefArray(strData, strPtr, size) - let read = tagSimpleNumber(WasmI32.load(nread, 0n)) + let read = DS.tagSimpleNumber(WasmI32.load(nread, 0n)) Malloc.free(strPtr) Malloc.free(nread) - return Ok((WasmRef.toGrain(strRef): String, read)) + return Ok((DS.String.toGrain(strRef), read)) } /** @@ -1821,9 +1807,9 @@ provide let pathReadlink = (fd: FileDescriptor, path: String, size: Number) => { provide let pathRemoveDirectory = (fd: FileDescriptor, path: String) => { let fd = getRawFileDescriptor(fd) - let pathRef = WasmRef.fromGrain(path) - let pathSize = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathSize = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let pathPtr = Malloc.malloc(pathSize) Memory.copyRefArrayToLinearMemory(pathPtr, pathData, pathSize) @@ -1831,7 +1817,7 @@ provide let pathRemoveDirectory = (fd: FileDescriptor, path: String) => { let err = Wasi.path_remove_directory(fd, pathPtr, pathSize) Malloc.free(pathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1856,9 +1842,9 @@ provide let pathRename = ( let sourceFd = getRawFileDescriptor(sourceFd) let targetFd = getRawFileDescriptor(targetFd) - let sourcePathRef = WasmRef.fromGrain(sourcePath) - let sourcePathSize = getStringSize(sourcePathRef) - let sourcePathData = getStringArrayRef(sourcePathRef) + let sourcePathRef = DS.String.fromGrain(sourcePath) + let sourcePathSize = DS.String.getSize(sourcePathRef) + let sourcePathData = DS.String.getData(sourcePathRef) let sourcePathPtr = Malloc.malloc(sourcePathSize) Memory.copyRefArrayToLinearMemory( @@ -1867,9 +1853,9 @@ provide let pathRename = ( sourcePathSize ) - let targetPathRef = WasmRef.fromGrain(targetPath) - let targetPathSize = getStringSize(targetPathRef) - let targetPathData = getStringArrayRef(targetPathRef) + let targetPathRef = DS.String.fromGrain(targetPath) + let targetPathSize = DS.String.getSize(targetPathRef) + let targetPathData = DS.String.getData(targetPathRef) let targetPathPtr = Malloc.malloc(targetPathSize) Memory.copyRefArrayToLinearMemory( @@ -1889,7 +1875,7 @@ provide let pathRename = ( Malloc.free(sourcePathPtr) Malloc.free(targetPathPtr) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) @@ -1902,9 +1888,9 @@ let _CHAR_DOT = 0x2en @unsafe let stripPrefixes = (path: String) => { - let origPathRef = WasmRef.fromGrain(path) - let origPathSize = getStringSize(origPathRef) - let origPathData = getStringArrayRef(origPathRef) + let origPathRef = DS.String.fromGrain(path) + let origPathSize = DS.String.getSize(origPathRef) + let origPathData = DS.String.getData(origPathRef) let mut ptr = 0n let mut len = origPathSize @@ -1931,15 +1917,15 @@ let stripPrefixes = (path: String) => { if (ptr == 0n) { path } else { - let strippedPath = allocateString(len) + let strippedPath = DS.String.make(len) WasmArrayRef.copyI8( - getStringArrayRef(strippedPath), + DS.String.getData(strippedPath), 0n, origPathData, ptr, len ) - WasmRef.toGrain(strippedPath): String + DS.String.toGrain(strippedPath) } } @@ -1949,12 +1935,12 @@ let populatePreopens = () => { let mut preopenedDirs = [] // Skip stdin, stdout, and stderr. for (let mut fd = 3n; fd != 0n; fd += 1n) { - let fd = FileDescriptor(tagSimpleNumber(fd)) + let fd = FileDescriptor(DS.tagSimpleNumber(fd)) match (fdPrestatGet(fd)) { Ok(Dir{ prefix, fd }) => preopenedDirs = [(fd, stripPrefixes(prefix)), ...preopenedDirs], // We've reached a bad file descriptor; we're done. - Err(Wasi.SystemError(e)) when untagSimpleNumber(e) == Wasi._EBADF => + Err(Wasi.SystemError(e)) when DS.untagSimpleNumber(e) == Wasi._EBADF => break, Err(_) => fail "Failed to populate preopens", } @@ -1965,13 +1951,13 @@ let populatePreopens = () => { @unsafe let prefixMatches = (prefix: String, path: String, pathStart: WasmI32) => { - let prefixRef = WasmRef.fromGrain(prefix) - let prefixLen = getStringSize(prefixRef) - let prefixData = getStringArrayRef(prefixRef) + let prefixRef = DS.String.fromGrain(prefix) + let prefixLen = DS.String.getSize(prefixRef) + let prefixData = DS.String.getData(prefixRef) - let pathRef = WasmRef.fromGrain(path) - let pathLen = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathLen = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) // Allow an empty string as a prefix of any relative path. if ( WasmArrayRef.getI8U(pathData, pathStart) != _CHAR_SLASH @@ -2012,13 +1998,18 @@ let rec findPath = ( let prefixRef = WasmRef.fromGrain(prefix) let (hasBestMatch, matchLen) = match (bestMatch) { - Some((matchPrefix, _)) => - (true, tagSimpleNumber(getStringSize(WasmRef.fromGrain(matchPrefix)))), + Some((_, matchPrefix)) => + ( + true, + DS.tagSimpleNumber( + DS.String.getSize(DS.String.fromGrain(matchPrefix)) + ), + ), None => (false, 0), } let bestMatch = if ( - (!hasBestMatch || pathLen > untagSimpleNumber(matchLen)) + (!hasBestMatch || pathLen > DS.untagSimpleNumber(matchLen)) && prefixMatches(prefix, path, pathStart) ) { Some(preopen) @@ -2031,10 +2022,10 @@ let rec findPath = ( [] => { match (bestMatch) { Some((fd, prefix)) => { - let matchLen = getStringSize(WasmRef.fromGrain(prefix)) + let matchLen = DS.String.getSize(DS.String.fromGrain(prefix)) let mut computed = matchLen - let pathRef = WasmRef.fromGrain(path) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathData = DS.String.getData(pathRef) while ( WasmArrayRef.getI8U(pathData, pathStart + computed) == _CHAR_SLASH @@ -2045,19 +2036,19 @@ let rec findPath = ( "." } else { let computedLen = pathLen - computed - let computedPath = allocateString(computedLen) + let computedPath = DS.String.make(computedLen) WasmArrayRef.copyI8( - getStringArrayRef(computedPath), + DS.String.getData(computedPath), 0n, pathData, pathStart + computed, computedLen ) - WasmRef.toGrain(computedPath): String + DS.String.toGrain(computedPath) } Ok((fd, computedPath)) }, - None => Err(Wasi.SystemError(tagSimpleNumber(Wasi._ENOENT))), + None => Err(Wasi.SystemError(DS.tagSimpleNumber(Wasi._ENOENT))), } }, } @@ -2065,9 +2056,9 @@ let rec findPath = ( @unsafe let findPath = (path: String) => { - let pathRef = WasmRef.fromGrain(path) - let pathLen = getStringSize(pathRef) - let pathData = getStringArrayRef(pathRef) + let pathRef = DS.String.fromGrain(path) + let pathLen = DS.String.getSize(pathRef) + let pathData = DS.String.getData(pathRef) let mut ptr = 0n let mut len = pathLen @@ -2091,9 +2082,9 @@ let cwd = "/" @unsafe let makeAbsolute = (path: String) => { - let origPathRef = WasmRef.fromGrain(path) - let origPathSize = getStringSize(origPathRef) - let origPathData = getStringArrayRef(origPathRef) + let origPathRef = DS.String.fromGrain(path) + let origPathSize = DS.String.getSize(origPathRef) + let origPathData = DS.String.getData(origPathRef) // already absolute if (WasmArrayRef.getI8U(origPathData, 0n) == _CHAR_SLASH) { @@ -2124,25 +2115,25 @@ let makeAbsolute = (path: String) => { } // TODO: Does this litterally just check if the cwd ends with a slash?? - let cwdRef = WasmRef.fromGrain(cwd) - let cwdLen = getStringSize(cwdRef) - let cwdData = getStringArrayRef(cwdRef) + let cwdRef = DS.String.fromGrain(cwd) + let cwdLen = DS.String.getSize(cwdRef) + let cwdData = DS.String.getData(cwdRef) let needSlash = WasmArrayRef.getI8U(cwdData, cwdLen - 1n) != _CHAR_SLASH if (needSlash) { - let fullPath = allocateString(cwdLen + 1n + len) - let fullPathData = getStringArrayRef(fullPath) + let fullPath = DS.String.make(cwdLen + 1n + len) + let fullPathData = DS.String.getData(fullPath) WasmArrayRef.copyI8(fullPathData, 0n, cwdData, 0n, cwdLen) WasmArrayRef.setI8(fullPathData, cwdLen, _CHAR_SLASH) WasmArrayRef.copyI8(fullPathData, cwdLen + 1n, origPathData, ptr, len) - return WasmRef.toGrain(fullPath): String + return DS.String.toGrain(fullPath): String } else { - let fullPath = allocateString(cwdLen + len) - let fullPathData = getStringArrayRef(fullPath) + let fullPath = DS.String.make(cwdLen + len) + let fullPathData = DS.String.getData(fullPath) WasmArrayRef.copyI8(fullPathData, 0n, cwdData, 0n, cwdLen) WasmArrayRef.copyI8(fullPathData, cwdLen, origPathData, ptr, len) - return WasmRef.toGrain(fullPath): String + return DS.String.toGrain(fullPath): String } } diff --git a/stdlib/wasi/process.gr b/stdlib/wasi/process.gr index a989ec45b6..8373d5cb71 100644 --- a/stdlib/wasi/process.gr +++ b/stdlib/wasi/process.gr @@ -14,14 +14,7 @@ use WasmI32.{ (+), (*), (>>), (>>>), (&), (==), (!=), (<) } from "runtime/unsafe/memory" include Memory from "runtime/malloc" include Malloc from "runtime/wasi" include Wasi -from "runtime/dataStructures" include DataStructures -use DataStructures.{ - tagSimpleNumber, - untagSimpleNumber, - allocateArray, - allocateString, - loadAdtVariant, -} +from "runtime/unsafe/dataStructures" include DataStructures as DS /** * Signals that can be sent to the host system. @@ -150,7 +143,7 @@ provide let argv = () => { let err = Wasi.args_sizes_get(argcPtr, argvBufSizePtr) if (err != Wasi._ESUCCESS) { Malloc.free(argcPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let argc = WasmI32.load(argcPtr, 0n) @@ -172,11 +165,11 @@ provide let argv = () => { if (err != Wasi._ESUCCESS) { Malloc.free(argvPtr) Malloc.free(argvBufPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } - let arrRef = allocateArray(argc, WasmRef.fromGrain(void)) - let arrData = DataStructures.getCompoundValueArrayRef(arrRef) + let arrRef = DS.Array.make(argc, WasmRef.fromGrain(void)) + let arrData = DS.Array.getData(arrRef) let mut i = 0n for (let mut ptr = 0n; ptr < argsLength; ptr += 4n) { @@ -186,18 +179,18 @@ provide let argv = () => { strLength += 1n } - let grainStrRef = allocateString(strLength) - let grainStrArr = DataStructures.getStringArrayRef(grainStrRef) + let grainStrRef = DS.String.make(strLength) + let grainStrArr = DS.String.getData(grainStrRef) Memory.copyLinearMemoryToRefArray(grainStrArr, strPtr, strLength) - WasmArrayRef.setAny(arrData, i, WasmRef.toGrain(grainStrRef)) + WasmArrayRef.setAny(arrData, i, WasmRef.fromGrain(grainStrRef)) i += 1n } Malloc.free(argvPtr) Malloc.free(argvBufPtr) - return Ok(WasmRef.toGrain(arrRef): Array) + return Ok(DS.Array.toGrain(arrRef): Array) } /** @@ -213,7 +206,7 @@ provide let env = () => { let err = Wasi.environ_sizes_get(envcPtr, envvBufSizePtr) if (err != Wasi._ESUCCESS) { Malloc.free(envcPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let envc = WasmI32.load(envcPtr, 0n) let envvBufSize = WasmI32.load(envvBufSizePtr, 0n) @@ -231,10 +224,10 @@ provide let env = () => { Malloc.free(envcPtr) Malloc.free(envvPtr) Malloc.free(envvBufPtr) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } - let arrRef = allocateArray(envc, WasmRef.fromGrain(void)) - let arrData = DataStructures.getCompoundValueArrayRef(arrRef) + let arrRef = DS.Array.make(envc, WasmRef.fromGrain(void)) + let arrData = DS.Array.getData(arrRef) let envsLength = envc * 4n let mut i = 0n @@ -245,11 +238,11 @@ provide let env = () => { strLength += 1n } - let grainStrRef = allocateString(strLength) - let grainStrArr = DataStructures.getStringArrayRef(grainStrRef) + let grainStrRef = DS.String.make(strLength) + let grainStrArr = DS.String.getData(grainStrRef) Memory.copyLinearMemoryToRefArray(grainStrArr, strPtr, strLength) - WasmArrayRef.setAny(arrData, i, WasmRef.toGrain(grainStrRef)) + WasmArrayRef.setAny(arrData, i, WasmRef.fromGrain(grainStrRef)) i += 1n } @@ -257,7 +250,7 @@ provide let env = () => { Malloc.free(envvPtr) Malloc.free(envvBufPtr) - return Ok(WasmRef.toGrain(arrRef): Array) + return Ok(DS.Array.toGrain(arrRef): Array) } /** @@ -288,10 +281,10 @@ provide let exit = (code: Number) => { @unsafe provide let sigRaise = (signal: Signal) => { let signal = WasmRef.fromGrain(signal) - let signal = untagSimpleNumber(loadAdtVariant(signal)) + let signal = DS.untagSimpleNumber(DS.ADT.getVariantId(signal)) let err = Wasi.proc_raise(signal) if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) } @@ -305,7 +298,7 @@ provide let sigRaise = (signal: Signal) => { provide let schedYield = () => { let err = Wasi.sched_yield() if (err != Wasi._ESUCCESS) { - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } return Ok(void) } diff --git a/stdlib/wasi/random.gr b/stdlib/wasi/random.gr index 1abca24e33..7d19a6e89c 100644 --- a/stdlib/wasi/random.gr +++ b/stdlib/wasi/random.gr @@ -11,8 +11,7 @@ use WasmI32.{ (==), (!=) } from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/malloc" include Malloc from "runtime/wasi" include Wasi -from "runtime/dataStructures" include DataStructures -use DataStructures.{ tagSimpleNumber, newUint32, newUint64 } +from "runtime/unsafe/dataStructures" include DataStructures as DS /** * Produce a random 32-bit integer. This function can be slow, so it's best to seed a generator if lots of random data is needed. @@ -29,11 +28,11 @@ provide let randomUint32 = () => { let err = Wasi.random_get(buf, 4n) if (err != Wasi._ESUCCESS) { Malloc.free(buf) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let rand = WasmI32.load(buf, 0n) Malloc.free(buf) - return Ok(WasmRef.toGrain(newUint32(rand)): Uint32) + return Ok(DS.Uint32.toUint32(rand)) } /** @@ -51,11 +50,11 @@ provide let randomUint64 = () => { let err = Wasi.random_get(buf, 8n) if (err != Wasi._ESUCCESS) { Malloc.free(buf) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let rand = WasmI64.load(buf, 0n) Malloc.free(buf) - return Ok(WasmRef.toGrain(newUint64(rand)): Uint64) + return Ok(DS.Uint64.toUint64(rand)) } /** @@ -70,9 +69,9 @@ provide let random = () => { let err = Wasi.random_get(buf, 4n) if (err != Wasi._ESUCCESS) { Malloc.free(buf) - return Err(Wasi.SystemError(tagSimpleNumber(err))) + return Err(Wasi.SystemError(DS.tagSimpleNumber(err))) } let rand = WasmI32.load(buf, 0n) Malloc.free(buf) - return Ok(tagSimpleNumber(rand)) + return Ok(DS.tagSimpleNumber(rand)) } diff --git a/stdlib/wasi/time.gr b/stdlib/wasi/time.gr index edb67006f4..416971e0b5 100644 --- a/stdlib/wasi/time.gr +++ b/stdlib/wasi/time.gr @@ -10,9 +10,8 @@ use WasmI32.{ (+), (==), (!=) } from "runtime/unsafe/wasmi64" include WasmI64 from "runtime/malloc" include Malloc from "runtime/wasi" include Wasi -from "runtime/dataStructures" include DataStructures +from "runtime/unsafe/dataStructures" include DataStructures use DataStructures.{ tagSimpleNumber } -from "runtime/unsafe/conv" include Conv @unsafe let getClockTime = (clockid, precision) => { @@ -23,7 +22,7 @@ let getClockTime = (clockid, precision) => { if (err != Wasi._ESUCCESS) { return Err(Wasi.SystemError(tagSimpleNumber(err))) } - return Ok(Conv.toInt64(time)) + return Ok(DataStructures.Int64.toInt64(time)) } /**