Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: Cleanup offset constants in String module #2237

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions stdlib/array.gr
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ use DataStructures.{ allocateArray, tagSimpleNumber }
from "runtime/exception" include Exception
from "runtime/numbers" include Numbers
use Numbers.{ coerceNumberToWasmI32 }

@unsafe
let _ARRAY_START_OFFSET = 8n
from "runtime/unsafe/offsets" include Offsets
use Offsets.{ _ARRAY_DATA_OFFSET }

@unsafe
let checkLength = length => {
Expand Down Expand Up @@ -88,7 +87,7 @@ provide let make = (length: Number, item: a) => {
WasmI32.store(
array + i,
Memory.incRef(WasmI32.fromGrain(item)),
_ARRAY_START_OFFSET
_ARRAY_DATA_OFFSET
)
}
ignore(item)
Expand Down Expand Up @@ -123,7 +122,7 @@ provide let init = (length: Number, fn: Number => a) => {
WasmI32.store(
array + i,
Memory.incRef(WasmI32.fromGrain(fn(tagSimpleNumber(index)))),
_ARRAY_START_OFFSET
_ARRAY_DATA_OFFSET
)
index += 1n
}
Expand Down
23 changes: 12 additions & 11 deletions stdlib/buffer.gr
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,15 @@ from "runtime/numbers" include Numbers
use Numbers.{ coerceNumberToWasmI32 }
from "runtime/utf8" include Utf8
use Utf8.{ usvEncodeLength }
from "runtime/unsafe/offsets" include Offsets
use Offsets.{ _BYTES_LEN_OFFSET, _BYTES_DATA_OFFSET }

abstract record Buffer {
mut len: Number,
initialSize: Number,
mut data: Bytes,
}

@unsafe
let _SIZE_OFFSET = 4n

@unsafe
let _VALUE_OFFSET = 8n

let _8BIT_LEN = 1

let _16BIT_LEN = 2
Expand All @@ -48,7 +44,7 @@ let _64BIT_LEN = 8

/* Gets the size of a Bytes via its ptr */
@unsafe
let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
let getSize = ptr => WasmI32.load(ptr, _BYTES_LEN_OFFSET)

/* Doubles the size of buffer's underlying byte sequence, if the given size is larger than the size of a buffer's underlying byte sequence */
let autogrow = (len, buf) => {
Expand Down Expand Up @@ -77,7 +73,11 @@ let autogrow = (len, buf) => {
@unsafe
let appendBytes = (srcOff, dstOff, len, src, dst) => {
use WasmI32.{ (+) }
Memory.copy(dst + _VALUE_OFFSET + dstOff, src + _VALUE_OFFSET + srcOff, len)
Memory.copy(
dst + _BYTES_DATA_OFFSET + dstOff,
src + _BYTES_DATA_OFFSET + srcOff,
len
)
}

/*
Expand Down Expand Up @@ -239,7 +239,7 @@ provide let truncate = (length, buffer) => {
let src = WasmI32.fromGrain(buffer.data)
let size = getSize(src)
let off = coerceNumberToWasmI32(length)
let ret = Memory.fill(src + _VALUE_OFFSET + off, 0n, size - off)
let ret = Memory.fill(src + _BYTES_DATA_OFFSET + off, 0n, size - off)
buffer.len = length
}

Expand Down Expand Up @@ -480,7 +480,8 @@ provide let addBytesSlice = (
use WasmI32.{ (-), (<), (>), (>=) }

// bounds check start
let bytelen = WasmI32.load(WasmI32.fromGrain(bytes), 4n)
let src = WasmI32.fromGrain(bytes)
let bytelen = getSize(src)
let srcOff = coerceNumberToWasmI32(start)
if (srcOff < 0n || srcOff >= bytelen) {
throw IndexOutOfBounds
Expand All @@ -495,9 +496,9 @@ provide let addBytesSlice = (
autogrow(length, buffer)

let dstOff = coerceNumberToWasmI32(buffer.len)
let src = WasmI32.fromGrain(bytes)
let dst = WasmI32.fromGrain(buffer.data)
appendBytes(srcOff, dstOff, len, src, dst)
ignore(src)

buffer.len += length
}
Expand Down
Loading
Loading