Skip to content
Closed
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ Unsupported syntax fails before the program runs, with a hint.
`Symbol.toStringTag` exist).
- **Binary and weak-reference built-ins:** typed arrays other than `Uint8Array`, resizable or shared
buffers, `WeakRef`, and `FinalizationRegistry`.
- **TypeScript syntax:** programs must be plain JavaScript.
- **Non-erasable TypeScript syntax:** pass `{ typescript: true }` to `execute`, `program`, `compile` or
`parseProgram` to run erasable TypeScript (type annotations, `interface`, `type`, `as`, `satisfies`, `!`,
generics, `import type`/`export type`, `declare`, type-only `namespace`, and class member modifiers are erased at
compile time). Syntax with runtime meaning (`enum`, `namespace` with values, parameter properties, decorators,
`import x = require`, `export =`, and `<T>expr` assertions) is refused with `UnsupportedSyntax` at its line.

## Determinism

Expand Down
8 changes: 7 additions & 1 deletion interpreter-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ the ultimate source of truth. Upstream test262 files run verbatim from `test/tes
## Source and execution model

- [x] JavaScript parsed with the latest syntax accepted by Acorn, then restricted by the interpreter allowlist.
TypeScript-only syntax is rejected rather than stripped before execution.
- [x] Optional erasable TypeScript (`{ typescript: true }` on `execute`, `program`, `compile`, and `parseProgram`,
or `CompileOptions::typescript(true)` / `ProgramOptions::typescript(true)` in Rust): type-only syntax
(annotations, `interface`, `type`, `as`, `satisfies`, `!`, generics, `import type`/`export type`, `declare`,
type-only `namespace`, overload signatures, and class member modifiers) is erased at compile time; non-erasable
syntax with runtime meaning (`enum`, `namespace` with values, constructor parameter properties, decorators,
`import =`, `export =`, and `<T>expr` assertions) fails at compile time with `UnsupportedSyntax` at its line.
Snapshots record `typescript: true` on saved sources so TypeScript runs suspend and resume cleanly.
- [x] Top-level `await` and `return` through the program's implicit async-function scope.
- [x] Explicit `return`, final top-level expression as a REPL-style result, and `null` when no value is produced.
- [x] The result boundary is `JSON.stringify` plus a short table. The program result crosses as
Expand Down
13 changes: 9 additions & 4 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ let mesh = HostFunction::new(|_, _| HostValue::TypedArray(TypedArray::from_f32(&
| `Host.fn`, `Host.durableFn`, `Host.namespace` | `HostFunction`, `DurableFunction`, `Namespace` |
| `Host.fail`, `Host.missing` | `Failure::new`, `Failure::missing` |
| `properties` of `Host.fn`, `Host.durableFn` | `HostFunction::properties`, `DurableFunction::properties` |
| `runtime.execute`, `runtime.program`, `runtime.compile`, `runtime.resume` | `execute`/`execute_async`, `program`, `compile`, `resume`/`resume_bytes` |
| `runtime.execute`, `runtime.program`, `runtime.compile`, `runtime.resume` | `execute`/`execute_with`/`execute_async`, `program`, `compile`/`compile_with`, `resume`/`resume_bytes` |
| `CompileOptions { typescript }`, `ProgramOptions { typescript }` | `CompileOptions::typescript`, `ProgramOptions::typescript` |
| `toBytes`, `fromBytes` | `to_bytes`, `from_bytes` |
| `limits.timeoutMs`, `limits.maxOutputBytes` | `Limits::timeout_ms`, `Limits::max_output_bytes` |
| a host tag's `strings.raw` | `Call::raw_strings` |
Expand Down Expand Up @@ -105,7 +106,7 @@ After a change to the TypeScript reference, regenerate the recordings and commit
them pass:

```sh
bun conformance/generate.ts && bun conformance/host.ts && bun conformance/boundary.ts && bun conformance/streaming.ts
bun conformance/generate.ts && bun conformance/host.ts && bun conformance/boundary.ts && bun conformance/streaming.ts && bun conformance/typescript.ts
bun conformance/date-locale.ts # the Date locale matrix, its expected output and its differences from ICU4X
bun conformance/number-locale.ts # the same for Number and BigInt toLocaleString
```
Expand All @@ -117,8 +118,8 @@ reference, never from hand:

- `conformance/cases/*.js` → `.json` (`generate.ts`), replayed by `bench/tests/conformance.rs`, also in round-trip mode,
which saves and restores every run at each host stop and loop slice.
- `conformance/host.json` (`host.ts`), `boundary.json` (`boundary.ts`) and `streaming.json` (`streaming.ts`), replayed
by `crates/psychoscript/tests/`.
- `conformance/host.json` (`host.ts`), `boundary.json` (`boundary.ts`), `streaming.json` (`streaming.ts`) and
`typescript.json` (`typescript.ts`), replayed by `crates/psychoscript/tests/`.
- Rust-only features have no reference: their expected results are written in Rust's own tests
(`crates/psychoscript/tests/heap.rs`, `clock.rs`, `typed.rs`, `host_recording.rs`, `value_depth.rs`; the programs
of `tests/cases/*.js` also run in round-trip mode).
Expand All @@ -132,6 +133,10 @@ workflow. The cases marked `@platform "linux"` depend on the system libm and are
Known differences:

- A saved state resumes only in the implementation and build that saved it: the snapshot formats differ.
- In TypeScript mode, Rust also refuses what oxc's own TypeScript checks refuse, as a `ParseError`: a class member
declared twice, a required parameter after an optional one, `abstract` in a class that isn't, a second declaration
of a `declare`d name or a type alias. The reference skips types without checking them and runs these; `tsc`
reports them all.
- Date's `toLocaleString`, `toLocaleDateString` and `toLocaleTimeString` format any locale and `Intl.DateTimeFormat`
options through ICU4X rather than the host ICU4C: the common cases — and every error kind and message — match, and
the rest render ICU4X's closest output. The differences are recorded by `conformance/date-locale.ts` in
Expand Down
4 changes: 3 additions & 1 deletion rust/bench/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ fn check(name: &str, code: &str, expected: &Json, future: bool) -> Result<(), St
},
};
let rt = runtime(limits, translate);
let options = ProgramOptions::new().bindings(bindings);
let options = ProgramOptions::new()
.bindings(bindings)
.typescript(expected["typescript"].as_bool().unwrap_or(false));
let run = || match future {
true => drive(rt.execute_async(code, &options)).unwrap_or_else(|i| psychoscript::Outcome {
result: Err(psychoscript::Diagnostic {
Expand Down
71 changes: 71 additions & 0 deletions rust/conformance/cases/typescript-erasure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// @typescript true
// Erasable TypeScript: annotations, interfaces, types, generics, as/satisfies/!, import/export type, declare,
// class modifiers, overloads, and async host calls (also exercised under round-trip snapshotting).
import type { Missing } from "does-not-exist"
import { type OnlyType } from "also-does-not-exist"
export type { Missing }
export interface ExportedShape { id: number }
export type ExportedAlias = string
export declare const ignoredAmbient: number

declare const tools: { echo: (x: unknown) => Promise<unknown> }
declare enum AmbientEnum { A, B }
declare namespace AmbientNs { const x: number }

interface Boxed<T> {
readonly value: T
}

type Pair<A, B = A> = readonly [A, B]

namespace TypeOnly {
export type Tag = "ok"
export interface Wrapped<T> { inner: T }
}

function add(a: number, b: number): number
function add(a: string, b: string): string
function add(a: any, b: any): any {
return a + b
}

abstract class Base<T> {
public readonly kind: TypeOnly.Tag = "ok"
declare ghost: number
[key: string]: unknown
protected v: T
protected constructor(v: T) {
this.v = v
}
public abstract get(): T
}

class Box<T> extends Base<T> implements Boxed<T> {
public readonly value: T
constructor(v: T)
constructor(v: T) {
super(v)
this.value = v
}
public override get(): T {
return this.v
}
}

const id = <T,>(x: T): T => x
const asyncId = async <T>(x: T): Promise<T> => (await tools.echo(x)) as T

let total: number = 0
for (let i: number = 1; i <= 3; i++) {
total += await asyncId<number>(i)
}

const b = new Box<number>(total)
const pair = [b.get(), add(10, 20)] as const satisfies Pair<number>
const maybe: { n?: number } = { n: pair[0]! + pair[1]! }
return {
kind: b.kind,
hasGhost: "ghost" in b,
sum: maybe.n!,
generic: id<string>("ts"),
}
13 changes: 13 additions & 0 deletions rust/conformance/cases/typescript-erasure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"bindings": {},
"typescript": true,
"ok": true,
"value": {
"kind": "ok",
"hasGhost": false,
"sum": 30,
"generic": "ts"
},
"logs": [],
"metadata": null
}
5 changes: 4 additions & 1 deletion rust/conformance/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// // @limits {"timeout_ms": 50, "max_output_bytes": 9} the runtime's `limits` (default none)
// // @translate {"functions": "drop", "promises": "await"} the runtime's `translate` (default the reference's:
// both "error")
// // @typescript true parse the source as erasable TypeScript (default false)
// // @platform "linux"
// A case with @platform depends on the system libm or compiler, so it is recorded and checked only on that platform
// (`process.platform`). Elsewhere this keeps its recorded outcome but rewrites it in the current format, and fails
Expand Down Expand Up @@ -88,6 +89,7 @@ for (const name of names) {
const bindings = header(code, "bindings") ?? {}
const limits = header(code, "limits")
const translate = header(code, "translate")
const typescript = header(code, "typescript") === true
if (platform !== undefined && platform !== process.platform) {
console.log(
`${name.padEnd(36)} ${refreshSkipped(name, { bindings, limits, translate }) ? "reformatted" : "skipped"} (${platform} only)`,
Expand All @@ -106,11 +108,12 @@ for (const name of names) {
}),
...(translate === undefined ? {} : { translate }),
})
const result = await Effect.runPromise(runtime.execute(code, { bindings }))
const result = await Effect.runPromise(runtime.execute(code, { bindings, ...(typescript ? { typescript: true } : {}) }))
const expected = {
bindings,
...(limits === undefined ? {} : { limits }),
...(translate === undefined ? {} : { translate }),
...(typescript ? { typescript: true } : {}),
...(result.ok ? { ok: true, value: result.value } : { ok: false, kind: result.error.kind }),
logs: result.logs ?? [],
metadata: result.metadata,
Expand Down
Loading