Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ and async in `rt/task.ts`.
| `async fn` | a function returning `rt.Future<T>`, around a generator |
| `Poll<T>`, `Waker`, `Context` | `T \| typeof rt.Pending`, refused if `T` can already be pending; `rt.Waker`, which is also the context |

`Vec<u8>` storage and `[x; N]` byte arrays up to 1 KiB share 8 KiB `ArrayBuffer`s, as Node's
`Buffer` pool does, so the `buffer` behind a view holds other bytes too: read it through
`byteOffset` and `byteLength`, and copy it before transferring it.

`usize` is 32 bits, as on wasm32, so it is exact in a `number`. Arithmetic is checked as
a debug build checks it, so translated code panics where native code would: it never
wraps silently. A panic throws `rt.Panic`, with the message native Rust panics with.
Expand Down
2 changes: 1 addition & 1 deletion ts/fixtures/codec/expected/coding/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class Encoder {
this.buf.truncate(end);
return rt.Ok(undefined);
}
let size2: Uint8Array = new Uint8Array(9);
let size2: Uint8Array = rt.bytes(9);
rt.slice.copyFromSlice(rt.slice.range(size2, 0, len), rt.slice.range(this.buf.view(), end));
this.buf.truncate(rt.usize.sub(rt.usize.add(end, len), 1));
rt.slice.copyWithin(this.buf.view(), body, end, rt.usize.sub(rt.usize.add(body, len), 1));
Expand Down
2 changes: 1 addition & 1 deletion ts/fixtures/codec/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function putUsize(out: rt.ByteVec, value: rt.usize): void {

/** Take up to 8 big-endian bytes as a `u64`, zero-padded on the right. */
export function takeU64(data: Uint8Array): [rt.u64, Uint8Array] {
let buf: Uint8Array = new Uint8Array(8);
let buf: Uint8Array = rt.bytes(8);
const n = rt.min(data.length, 8);
rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data, 0, n));
return [rt.u64.fromBeBytes(buf), rt.slice.range(data, n)];
Expand Down
26 changes: 13 additions & 13 deletions ts/fixtures/lang/expected/fmt.ts

Large diffs are not rendered by default.

88 changes: 44 additions & 44 deletions ts/fixtures/lang/expected/ints.ts

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions ts/runtime/rt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,35 @@ export const slice = {
},
};

const SLAB = 8192;
let slab = new ArrayBuffer(SLAB);
let slabUsed = 0;

/**
* `n` zeroed bytes. Up to 1 KiB, they share an `ArrayBuffer`, as Node's `Buffer` pool
* does: a lone array's own buffer costs V8 about a microsecond, made when the array is
* past 64 bytes or else the first time something views it. No byte is handed out twice.
*/
export function bytes(n: number): Uint8Array {
if (n > SLAB / 8) return new Uint8Array(n);
if (slabUsed + n > SLAB) {
slab = new ArrayBuffer(SLAB);
slabUsed = 0;
}
const out = new Uint8Array(slab, slabUsed, n);
// Aligned, as Node aligns its pool.
slabUsed += (n + 7) & ~7;
return out;
}

/** `Vec<u8>`: a growable byte buffer. */
export class ByteVec {
/** The storage; only the first `length` bytes are the vector. */
data: Uint8Array;
length = 0;

constructor(capacity = 0) {
this.data = new Uint8Array(capacity);
this.data = bytes(capacity);
}

static from(bytes: Uint8Array): ByteVec {
Expand All @@ -826,9 +847,8 @@ export class ByteVec {
reserve(additional: number): void {
const needed = this.length + additional;
if (needed <= this.data.length) return;
const grown = new Uint8Array(Math.max(needed, this.data.length * 2, 8));
// All of `data`, not a view of it: a view of a small array makes V8 move it off its
// heap, which costs more than copying the few bytes past `length`.
const grown = bytes(Math.max(needed, this.data.length * 2, 8));
// All of `data`, which costs less than making a view of its first `length` bytes.
grown.set(this.data);
this.data = grown;
}
Expand Down
2 changes: 1 addition & 1 deletion ts/src/lower/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> {
let v = self.operand(*value, out);
match elem_ty.kind() {
ty::Uint(ty::UintTy::U8) => {
let array = Expr::New(Box::new(Expr::ident("Uint8Array")), vec![Expr::lit(n.to_string())]);
let array = Expr::rt("bytes", vec![Expr::lit(n.to_string())]);
match &v {
Expr::Lit(text) if text == "0" => array,
_ => array.member("fill").call(vec![v]),
Expand Down