Skip to content

Commit

Permalink
chore(stdlib): Cleanup usage of number tagging throughout the stdlib (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake authored Aug 16, 2024
1 parent ab3dde8 commit 18b9cd2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 76 deletions.
6 changes: 4 additions & 2 deletions stdlib/runtime/wasi.gr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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
use DataStructures.{ untagSimpleNumber }

// env
provide foreign wasm args_get:
Expand Down Expand Up @@ -322,9 +324,9 @@ provide let _ENOTCAPABLE = 76n
provide exception SystemError(Number)

@disableGC
provide let stringOfSystemError = code => {
provide let stringOfSystemError = (code: Number) => {
use WasmI32.{ (==), (>>) }
let n = WasmI32.fromGrain(code) >> 1n
let n = untagSimpleNumber(code)
match (n) {
n when n == _ESUCCESS =>
"SystemError: No error occurred. System call completed successfully.",
Expand Down
2 changes: 1 addition & 1 deletion stdlib/runtime/wasi.md
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,6 @@ _ENOTCAPABLE : WasmI32
### Wasi.**stringOfSystemError**

```grain
stringOfSystemError : (code: a) => String
stringOfSystemError : (code: Number) => String
```

48 changes: 19 additions & 29 deletions stdlib/string.gr
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ let charAtHelp = (position, string: String) => {
fail "Invalid offset: " ++ toString(position)
}
// Implementation is similar to explodeHelp, but doesn't perform unneeded memory allocations
use WasmI32.{ (+), (&), (>>>), ltU as (<), (==) }
let size = WasmI32.fromGrain(byteLength(string)) >>> 1n
let position = WasmI32.fromGrain(position) >>> 1n
use WasmI32.{ (+), (&), ltU as (<), (==) }
let size = untagSimpleNumber(byteLength(string))
let position = untagSimpleNumber(position)
let stringPre = WasmI32.fromGrain(string)
let mut ptr = stringPre + 8n
let end = ptr + size
Expand Down Expand Up @@ -349,8 +349,8 @@ provide let charAt = (position, string: String) => {
let explodeHelp = (s: String, chars) => {
use WasmI32.{ (+), (&), (>>>), ltU as (<), (==) }

let size = WasmI32.fromGrain(byteLength(s)) >>> 1n
let len = WasmI32.fromGrain(length(s)) >>> 1n
let size = untagSimpleNumber(byteLength(s))
let len = untagSimpleNumber(length(s))

let sPtr = WasmI32.fromGrain(s)

Expand Down Expand Up @@ -529,10 +529,10 @@ provide let reverse = string => {
*/
@unsafe
provide let split = (separator: String, string: String) => {
use WasmI32.{ (+), (-), (&), (<<), (>>), ltU as (<), gtU as (>), (==) }
use WasmI32.{ (+), (-), (&), (<<), ltU as (<), gtU as (>), (==) }

let size = WasmI32.fromGrain(byteLength(string)) >> 1n
let psize = WasmI32.fromGrain(byteLength(separator)) >> 1n
let size = untagSimpleNumber(byteLength(string))
let psize = untagSimpleNumber(byteLength(separator))

if (psize == 0n) {
WasmI32.toGrain(explodeHelp(string, false)): Array<String>
Expand Down Expand Up @@ -629,8 +629,8 @@ provide let split = (separator: String, string: String) => {
provide let slice = (start: Number, end=length(string), string: String) => {
use WasmI32.{ (+), (-), (&), (<<), (>>), (<), (>), (==), (!=) }

let len = WasmI32.fromGrain(length(string)) >> 1n
let size = WasmI32.fromGrain(byteLength(string)) >> 1n
let len = untagSimpleNumber(length(string))
let size = untagSimpleNumber(byteLength(string))

let stringPtr = WasmI32.fromGrain(string)

Expand Down Expand Up @@ -715,21 +715,12 @@ provide let contains = (search: String, string: String) => {
// searching phase in O(nm) time complexity
// slightly (by coefficient) sub-linear in the average case
// http://igm.univ-mlv.fr/~lecroq/string/node13.html#SECTION00130
use WasmI32.{
(+),
(-),
(>>),
ltU as (<),
gtU as (>),
leU as (<=),
(==),
(!=),
}
use WasmI32.{ (+), (-), ltU as (<), gtU as (>), leU as (<=), (==), (!=) }
let pOrig = search
let sOrig = string

let n = WasmI32.fromGrain(byteLength(string)) >> 1n
let m = WasmI32.fromGrain(byteLength(search)) >> 1n
let n = untagSimpleNumber(byteLength(string))
let m = untagSimpleNumber(byteLength(search))

let mut stringPtr = WasmI32.fromGrain(string)
let mut searchPtr = WasmI32.fromGrain(search)
Expand Down Expand Up @@ -1118,10 +1109,10 @@ let grainToWasmNumber = (num, name) => {

@unsafe
let utf16Length = (s: String) => {
use WasmI32.{ (+), (&), (>>>), (<<), ltU as (<), (==) }
use WasmI32.{ (+), (&), (<<), ltU as (<), (==) }

let size = WasmI32.fromGrain(byteLength(s)) >>> 1n
let len = WasmI32.fromGrain(length(s)) >>> 1n
let size = untagSimpleNumber(byteLength(s))
let len = untagSimpleNumber(length(s))

let sPtr = WasmI32.fromGrain(s)

Expand Down Expand Up @@ -1178,8 +1169,8 @@ let encodeAtHelp = (
destPos: Number,
) => {
use WasmI32.{ (+), (-), (&), (>>>), ltU as (<), (>), leU as (<=), (==) }
let byteSize = WasmI32.fromGrain(byteLength(string)) >>> 1n
let len = WasmI32.fromGrain(length(string)) >>> 1n
let byteSize = untagSimpleNumber(byteLength(string))
let len = untagSimpleNumber(length(string))

let stringPtr = WasmI32.fromGrain(string)

Expand Down Expand Up @@ -1446,8 +1437,7 @@ let encodeHelp = (string: String, encoding: Encoding, includeBom: Bool) => {
} else {
0
})
use WasmI32.{ (>>>) }
let bytes = WasmI32.toGrain(allocateBytes(WasmI32.fromGrain(size) >>> 1n))
let bytes = WasmI32.toGrain(allocateBytes(untagSimpleNumber(size)))
encodeAtHelp(string, encoding, includeBom, bytes, 0)
}

Expand Down
Loading

0 comments on commit 18b9cd2

Please sign in to comment.