Skip to content
Merged
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
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ deletes the quests that follow it.
- [Async in the TypeScript translator](/quest/m2/ts-async.md) - async code translates with Rust's lazy futures
- [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values
- [Collections](/quest/m2/ts-collections.md) - maps, sets, and queues keep Rust's key semantics
- [Source order](/quest/m2/ts-source-order.md) - each module's items come out in Rust source order
- [Translate moq-net's model to TypeScript](/quest/m2/ts-model.md) - the driver grows from moq-net's codec to its model, for moq to compare against WASM
- [Namespaced external types](/quest/m2/uniffi-namespaces.md) - two crates linked into one library can each export a same-named type in every generator
- [Zero-copy byte returns](/quest/m2/ffi-bytes-return.md) - returned payloads reach the host without a copy, if the payload numbers justify it
Expand Down
16 changes: 0 additions & 16 deletions quest/m2/ts-source-order.md

This file was deleted.

9 changes: 8 additions & 1 deletion ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ generic class holds a method per instance, each declaring the instance it takes
array, or an `Option` or `Result` holding one, is copied where Rust copies it, since JS
would share the object.

A file's items, and a class's or companion's methods, follow the Rust source, whatever
order translation reaches them in, so adding a root moves nothing it does not reach.
What a macro makes sits where the macro is called. The instances of one function sit
together, sorted by their suffix (`Pair` in `fmt$Pair`) and then by their full type
arguments.

Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` build sets
`rewriteRelativeImportExtensions`, or `allowImportingTsExtensions` with `noEmit`.

Expand Down Expand Up @@ -153,7 +159,8 @@ built natively for wasm32 and run beside its translation on the same inputs by
`tests/differential.ts`, which fails on any difference in output or panic messages.

- `codec` vendors moq-net's sync codec, which a harness outside the crate could not reach,
and writes its errors' `Display` and `Debug`.
and writes its errors' `Display` and `Debug`. It is also translated without
`bench::run` and then with it as the first root, which must keep every line in order.
- `lang` covers every integer width's arithmetic and casts, control flow, patterns,
`Copy` aggregates, formatting, methods on generic types, and iterators, with closures
that log what each adaptor reads. Its differential test runs through `caller.ts`, a
Expand Down
6 changes: 3 additions & 3 deletions ts/fixtures/codec/expected/bench.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { Decoder, type DecodeError } from "./coding/decode.ts";
import { Encoder, type EncodeError } from "./coding/encode.ts";
import { type DecodeError, Decoder } from "./coding/decode.ts";
import { type EncodeError, Encoder } from "./coding/encode.ts";
import { Form, zigzag, unzigzag } from "./coding/varint.ts";
import { form, liteVersion, putUsize, takeU64, putU64, encodeError, decodeError } from "./harness.ts";
import { form, liteVersion, putU64, putUsize, takeU64, decodeError, encodeError } from "./harness.ts";
import { Group } from "./lite/group.ts";
import { type Version } from "./lite/version.ts";

Expand Down
114 changes: 57 additions & 57 deletions ts/fixtures/codec/expected/coding/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as rt from "../rt.ts";
import { SessionError } from "../lib.ts";
import { type Form, type BoundsExceeded, read } from "../coding/varint.ts";
import { type BoundsExceeded, type Form, read } from "../coding/varint.ts";
import { type Control } from "../stats.ts";

/** A decode error. */
Expand All @@ -28,10 +28,6 @@ export type DecodeError =
| { tag: "MultipleAuthTokens" };

export const DecodeError = {
from$BoundsExceeded(_: BoundsExceeded): DecodeError {
return { tag: "BoundsExceeded" };
},

fmt$Display(self: DecodeError, __formatter: rt.Formatter): rt.Result<undefined, rt.FmtError> {
if (self.tag === "Short") {
return __formatter.writeStr("short buffer");
Expand Down Expand Up @@ -83,6 +79,10 @@ export const DecodeError = {
}
},

from$FromUtf8Error(source: rt.FromUtf8Error): DecodeError {
return { tag: "InvalidString", _0: source };
},

fmt$Debug(self: DecodeError, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
if (self.tag === "Short") {
return f.writeStr("Short");
Expand Down Expand Up @@ -130,10 +130,6 @@ export const DecodeError = {
}
},

from$FromUtf8Error(source: rt.FromUtf8Error): DecodeError {
return { tag: "InvalidString", _0: source };
},

/** A complete frame cannot be extended by reading more stream bytes. */
complete(self: DecodeError): DecodeError {
if (self.tag === "Short") {
Expand All @@ -143,6 +139,10 @@ export const DecodeError = {
return other;
}
},

from$BoundsExceeded(_: BoundsExceeded): DecodeError {
return { tag: "BoundsExceeded" };
},
};

/**
Expand Down Expand Up @@ -189,6 +189,13 @@ export class Decoder {
return this._need;
}

/** Record that a read wanted `more` bytes past the current position. */
short(more: number): DecodeError {
const need = rt.usize.saturatingAdd(rt.usize.sub(this.len, this.buf.length), more);
this._need = rt.max(this._need, need);
return { tag: "Short" };
}

/** The number of unread bytes. */
remaining(): number {
return this.buf.length;
Expand All @@ -199,20 +206,32 @@ export class Decoder {
return this.buf.length === 0;
}

/** Read a varint. */
varint(): rt.Result<bigint, DecodeError> {
const read_ = read(this.buf, this.form);
if (read_.ok) {
const value = read_.value[0];
const rest = read_.value[1];
this.buf = rest;
return rt.Ok(value);
} else if (!read_.ok && read_.error.tag === "Short") {
return rt.Err(this.short(rt.usize.add(this.buf.length, 1)));
} else {
const err = read_.error;
return rt.Err(err);
/** Read `len` raw bytes. */
slice(len: number): rt.Result<Uint8Array, DecodeError> {
const splitAtChecked = rt.slice.splitAtChecked(this.buf, len);
if (splitAtChecked === undefined) {
return rt.Err(this.short(len));
}
const head = splitAtChecked[0];
const rest = splitAtChecked[1];
this.buf = rest;
return rt.Ok(head);
}

/** Read every remaining byte. */
rest(): Uint8Array {
const taken = this.buf;
this.buf = new Uint8Array(0);
return taken;
}

/** Split off the next `len` bytes as their own decoder, e.g. a size-prefixed body. */
sub(len: number): rt.Result<Decoder, DecodeError> {
const slice = this.slice(len);
if (!slice.ok) {
return rt.Err(slice.error);
}
return rt.Ok(Decoder.new(slice.value, this.form));
}

/** Read a single byte. */
Expand Down Expand Up @@ -249,6 +268,22 @@ export class Decoder {
}
}

/** Read a varint. */
varint(): rt.Result<bigint, DecodeError> {
const read_ = read(this.buf, this.form);
if (read_.ok) {
const value = read_.value[0];
const rest = read_.value[1];
this.buf = rest;
return rt.Ok(value);
} else if (!read_.ok && read_.error.tag === "Short") {
return rt.Err(this.short(rt.usize.add(this.buf.length, 1)));
} else {
const err = read_.error;
return rt.Err(err);
}
}

/** Read an optional varint: 0 is `None`, and `n + 1` is `Some(n)`. */
varintOpt(): rt.Result<bigint | undefined, DecodeError> {
const varint = this.varint();
Expand Down Expand Up @@ -289,41 +324,6 @@ export class Decoder {
}
return rt.Ok(fromUtf8.value);
}

/** Read `len` raw bytes. */
slice(len: number): rt.Result<Uint8Array, DecodeError> {
const splitAtChecked = rt.slice.splitAtChecked(this.buf, len);
if (splitAtChecked === undefined) {
return rt.Err(this.short(len));
}
const head = splitAtChecked[0];
const rest = splitAtChecked[1];
this.buf = rest;
return rt.Ok(head);
}

/** Split off the next `len` bytes as their own decoder, e.g. a size-prefixed body. */
sub(len: number): rt.Result<Decoder, DecodeError> {
const slice = this.slice(len);
if (!slice.ok) {
return rt.Err(slice.error);
}
return rt.Ok(Decoder.new(slice.value, this.form));
}

/** Read every remaining byte. */
rest(): Uint8Array {
const taken = this.buf;
this.buf = new Uint8Array(0);
return taken;
}

/** Record that a read wanted `more` bytes past the current position. */
short(more: number): DecodeError {
const need = rt.usize.saturatingAdd(rt.usize.sub(this.len, this.buf.length), more);
this._need = rt.max(this._need, need);
return { tag: "Short" };
}
}

//# sourceMappingURL=decode.ts.map
32 changes: 16 additions & 16 deletions ts/fixtures/codec/expected/coding/encode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "../rt.ts";
import { type Form, type BoundsExceeded, write } from "../coding/varint.ts";
import { type BoundsExceeded, type Form, write } from "../coding/varint.ts";
import { type Control } from "../stats.ts";

/** An error that occurs during encoding. */
Expand All @@ -15,10 +15,6 @@ export type EncodeError =
| "Unsupported";

export const EncodeError = {
from(_: BoundsExceeded): EncodeError {
return "BoundsExceeded";
},

fmt$Display(self: EncodeError, __formatter: rt.Formatter): rt.Result<undefined, rt.FmtError> {
if (self === "BoundsExceeded") {
return __formatter.writeStr("bounds exceeded");
Expand All @@ -40,6 +36,10 @@ export const EncodeError = {
fmt$Debug(self: EncodeError, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.writeStr(self === "BoundsExceeded" ? "BoundsExceeded" : self === "TooLarge" ? "TooLarge" : self === "Short" ? "Short" : self === "InvalidState" ? "InvalidState" : self === "TooMany" ? "TooMany" : self === "Version" ? "Version" : "Unsupported");
},

from(_: BoundsExceeded): EncodeError {
return "BoundsExceeded";
},
};

/**
Expand All @@ -63,6 +63,11 @@ export class Encoder {
return new Encoder({ buf, form, control: undefined });
}

/** Write raw bytes. */
slice(v: Uint8Array): void {
this.buf.extendFromSlice(v);
}

/** Write a single byte. */
u8(v: number): void {
this.buf.push(v);
Expand Down Expand Up @@ -139,6 +144,12 @@ export class Encoder {
return rt.usize.sub(this.buf.length, prefix.body);
}

/** Drop a reserved prefix and everything written since, as if neither was written. */
discard(prefix: Prefix): void {
const start = prefix.kind === "Varint" ? rt.usize.sub(prefix.body, 1) : rt.usize.sub(prefix.body, 2);
this.buf.truncate(start);
}

/** Size a reserved prefix to everything written since. */
fill(prefix: Prefix): rt.Result<undefined, EncodeError> {
const body = prefix.body;
Expand Down Expand Up @@ -174,17 +185,6 @@ export class Encoder {
}
return rt.Ok(undefined);
}

/** Drop a reserved prefix and everything written since, as if neither was written. */
discard(prefix: Prefix): void {
const start = prefix.kind === "Varint" ? rt.usize.sub(prefix.body, 1) : rt.usize.sub(prefix.body, 2);
this.buf.truncate(start);
}

/** Write raw bytes. */
slice(v: Uint8Array): void {
this.buf.extendFromSlice(v);
}
}

/** A size prefix reserved ahead of a body, sized by [`Encoder::fill`] once it is written. */
Expand Down
8 changes: 4 additions & 4 deletions ts/fixtures/codec/expected/coding/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { type Version } from "../lite/version.ts";
/** The number does not fit the target: a varint wire form or a narrower integer. */
export class BoundsExceeded {

fmt$Display(__formatter: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return __formatter.writeStr("value out of range");
}

fmt$Debug(f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.writeStr("BoundsExceeded");
}

fmt$Display(__formatter: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return __formatter.writeStr("value out of range");
}
}

/** How a protocol version lays out a varint on the wire. */
Expand Down
Loading