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
30 changes: 23 additions & 7 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,9 @@ caller imports it with no ignores.
place, and others, which Python may change while Rust reads, are copied. Bytes Rust
returns, or a record holds, are a read-only `memoryview` lending Rust's buffer until it
and its views are released. Those Rust lends a closure or trait method are copied into
`bytes`. Strings are `str`, `u128` an `int`, `Option<T>` `T | None`, a tuple a `tuple`, `Duration`
`bytes`. Strings are `str`, `u128` an `int`, a float a `float`, whose 64 bits hold an
`f32`'s NaN as `struct` widens one, its payload shifted up and its quiet bit kept, so a
signaling one returns exactly where the hardware's conversion would quiet it, `Option<T>` `T | None`, a tuple a `tuple`, `Duration`
the runtime's `Duration`, a `NamedTuple` of `secs` and `nanos` as Rust holds them, which
raises `OverflowError` or `ValueError` passed in out of their range; its `from_timedelta`
and `to_timedelta` raise rather than lose anything, as a `timedelta` holds only whole
Expand Down Expand Up @@ -1336,6 +1338,12 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass
`isize` past 32 bits on a 32-bit target, and a zero passed for a `NonZero` throws an
`ArgumentError`. A `char` is a `String` of one Unicode scalar, and any other throws an
`ArgumentError`.
- **Floats** are `double`. An `f32` crosses `dart:ffi` as its bits, a NaN widened into the
`double`'s by them, its payload shifted up and its quiet bit kept, since the hardware's
conversion `dart:ffi` makes of a `Float` would quiet a signaling one: the glue converts
its bits at each call, and a struct holding one crosses as the glue's twin of it, whose
`f32`s are integers as Dart's class declares them. Any other `double` passed for one
rounds to the nearest `f32`.
- **Bytes** a call takes are a `Uint8List`, and text a `String`, which throws an
`ArgumentError` passed in holding a lone surrogate, since UTF-8 has no form of one and
Dart's encoder would replace it. Rust keeps Dart's own
Expand Down Expand Up @@ -1522,7 +1530,14 @@ packs the api fixture's with npm, installs it into a project of its own, type-ch
by `tag`. A tuple is an array, `Option<T>` is `T | undefined`, or `T | rt.None` where `T`
can already be `undefined`, `Result<T, E>` an `rt.Result`, `{ ok: true, value }` or `{ ok:
false, error }`, and an error is that value, never thrown. `u8` through `u32`, `usize`,
`isize`, `f32`, `f64`, and `char` are numbers, each checked as it crosses in; `u64`, `i64`,
`isize`, and `char` are numbers, each checked as it crosses in. `f32` and `f64` are
`rt.f32` and `rt.f64`, a number but for a NaN other than Rust's `NAN`, which `NaN`
stands for: that one is its bits, `{ bits }` or `{ hi, lo }`, since an engine quiets or,
once it optimizes, canonicalizes a NaN in any number it converts. A NaN's form follows
its bits, which wasm leaves arithmetic to choose by host: Rust's `0.0 / 0.0` is `NaN` on
ARM, but on x86 it has its sign set, so it is `{ bits: 0xffc00000 }`. A number passed for an
`f32` must be one exactly, and `rt.f32Number` or `rt.f64Number` give a plain number,
dropping a NaN's bits. `u64`, `i64`,
and `u128` are rust2-ts's `rt.u64` and so on, a number while a safe integer and 32-bit
limbs past it, converted from the `bigint` a wasm `i64` is at the boundary, which costs a
getter 9% over returning that `bigint` as it is. `String` and `&str` are strings, bytes a
Expand All @@ -1534,6 +1549,9 @@ packs the api fixture's with npm, installs it into a project of its own, type-ch
memory, which it gives back once the call returns, and reads what Rust returns at the
layouts C's wasm32 ABI gives the header's structs, passing a struct of one scalar as it,
any other through a pointer to a copy, and one returned through a pointer passed first.
A float, alone or a struct's one scalar, crosses as a struct does, read and written as
its bits: the glue wraps each function passing one by value, and shims each JavaScript
implements, to take it through memory.
Text Rust returns is copied out, and its buffer freed at once, so no view outlives a
growth of memory, which detaches it; what Rust lends a closure or trait is copied before
any call it makes can grow memory.
Expand Down Expand Up @@ -1606,7 +1624,7 @@ just ffi moq-cpp # the same export, running only moq/moq.cpp
just ffi moq-swift # the same export, running only moq/moq.swift
just ffi moq-api <target> # stage moq's API on another target into target/moq-api, which the exports above merge
just ffi census # census each into target/census
just ffi shapes # round-trip every sampled type shape in every language but KNOWN's, as the nightly does
just ffi shapes # round-trip every sampled type shape in every language, as the nightly does
just ffi corpus # census, export, and bind each crate of corpus/crates.txt through a facade, as the nightly does; it runs their build scripts
just ffi corpus-update # take corpus/crates.txt afresh from crates.io's top crates, and resolve corpus/Cargo.lock
just ffi protocol # run longer protocol scripts from more seeds through the C ABI and every binding, as the nightly does
Expand Down Expand Up @@ -1683,10 +1701,8 @@ language's driver, the web's on Node included, is a generated list of these call
printed digests must match the crate's own, run natively, or as wasm under Node's WASI
for the web, where `usize` is 32 bits. The gate runs a subset; `just ffi shapes` runs
every sampled shape and, as the nightly does, more drawn from the day's seed, which it
prints and `RUST2_SHAPES_SEED` replays. `KNOWN` lists what a binding still gets wrong,
each with the quest that fixes it; the driver skips those calls, so the fix deletes its
entry, and the nightly fails on an entry that matches no call of the fixed set. A new IR
`Type` variant fails `ir_covered` until the sampler covers it.
prints and `RUST2_SHAPES_SEED` replays. A new IR `Type` variant fails `ir_covered` until
the sampler covers it.
The corpus (`tests/fixtures/corpus.rs`) exports stock crates as a consumer would: each
crate of `corpus/crates.txt`, the top crates on crates.io by downloads that are libraries
building as they are, pinned by `corpus/Cargo.lock`, through a facade of its own, `pub use
Expand Down
28 changes: 28 additions & 0 deletions ffi/runtime/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,34 @@ pub fn float_out(value: f64) -> R<Obj> {
unsafe { Obj::new(PyFloat_FromDouble(value)) }
}

/// An `f32`, from a `float` that [`f32_out`] widened: a NaN narrowed by its bits, keeping
/// its sign, its quiet bit, and its payload's top 23 bits, as CPython's `struct` packs one,
/// where the hardware's narrowing would set the quiet bit.
pub fn f32_in(obj: *mut PyObject) -> R<f32> {
let value = float_in(obj)?;
if !value.is_nan() {
return Ok(value as f32);
}
let bits = value.to_bits();
let mut payload = (bits >> 29) as u32 & 0x7f_ffff;
// A payload only in the low bits would narrow to infinity: quieted instead, as the
// hardware does.
if payload == 0 {
payload = 1 << 22;
}
Ok(f32::from_bits((bits >> 32) as u32 & 1 << 31 | 0x7f80_0000 | payload))
}

/// A `float` of an `f32`: a NaN widened by its bits, its payload shifted up and its quiet
/// bit kept, as CPython's `struct` unpacks one, so [`f32_in`] narrows it back exactly.
pub fn f32_out(value: f32) -> R<Obj> {
if !value.is_nan() {
return float_out(value as f64);
}
let bits = u64::from(value.to_bits());
float_out(f64::from_bits((bits & 1 << 31) << 32 | 0x7ff0_0000_0000_0000 | (bits & 0x7f_ffff) << 29))
}

/// A `char`, from a `str` of one code point that is a Unicode scalar value.
pub fn char_in(obj: *mut PyObject) -> R<u32> {
// SAFETY: the GIL is held, and `obj` is live.
Expand Down
33 changes: 33 additions & 0 deletions ffi/runtime/rt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,39 @@ final class _Native {
int i32(int v) =>
v >= -0x80000000 && v <= 0x7fffffff ? v : throw RangeError.range(v, -0x80000000, 0x7fffffff);

/// Where [f32Bits] and [f32Of] convert, by bits for a NaN.
final _float = typed.ByteData(8);

/// The bits of the `f32` nearest `v`, as a cast rounds: a NaN narrowed by its bits, as
/// [f32Of] widened it, keeping its sign, its quiet bit, and its payload's top 23 bits,
/// where the hardware's narrowing would set the quiet bit.
int f32Bits(double v) {
final b = _float;
if (!v.isNaN) {
b.setFloat32(0, v, typed.Endian.host);
return b.getUint32(0, typed.Endian.host);
}
b.setFloat64(0, v, typed.Endian.host);
final bits = b.getUint64(0, typed.Endian.host);
var payload = (bits >> 29) & 0x7fffff;
// A payload only in the low bits would narrow to infinity: quieted instead, as the
// hardware does.
if (payload == 0) payload = 1 << 22;
return ((bits >> 32) & 0x80000000) | 0x7f800000 | payload;
}

/// The `double` of an `f32`'s bits: a NaN widened by its bits, its payload shifted up and
/// its quiet bit kept, so [f32Bits] narrows it back exactly.
double f32Of(int bits) {
final b = _float;
if ((bits & 0x7fffffff) <= 0x7f800000) {
b.setUint32(0, bits, typed.Endian.host);
return b.getFloat32(0, typed.Endian.host);
}
b.setUint64(0, ((bits & 0x80000000) << 32) | 0x7ff0000000000000 | ((bits & 0x7fffff) << 29), typed.Endian.host);
return b.getFloat64(0, typed.Endian.host);
}

/// A `NonZero`'s integer, which Rust's type holds only if it is not zero.
int nonZero(int v) => v != 0 ? v : throw ArgumentError.value(v, 'NonZero', 'a NonZero is zero');

Expand Down
95 changes: 90 additions & 5 deletions ffi/runtime/rt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ export type i64 = Num<"i64"> | I64Limbs;
/** Rust's `u128`: a number while it is a safe integer, and its limbs past that. */
export type u128 = Num<"u128"> | U128Limbs;

/** An `f32` NaN other than `NaN`, which stands for Rust's `f32::NAN`: its bits, a uint32. */
export type F32Nan = { readonly bits: number; readonly [brand]: "f32" };
/** An `f64` NaN other than `NaN`, which stands for Rust's `f64::NAN`: its high and low 32
* bits, each a uint32. */
export type F64Nan = { readonly hi: number; readonly lo: number; readonly [brand]: "f64" };

/**
* Rust's `f32`: a number, `NaN` among them as Rust's `f32::NAN`, and the bits of any other
* NaN, which no number keeps: an engine may quiet or canonicalize a NaN it converts. Each
* value has one form.
*/
export type f32 = Num<"f32"> | F32Nan;
/** Rust's `f64`: a number, `NaN` as `f64::NAN`, and the bits of any other NaN. */
export type f64 = Num<"f64"> | F64Nan;

/** Rust's `Result`: `{ ok: true, value }` or `{ ok: false, error }`. */
export type Result<T, E> = { readonly ok: true; readonly value: T } | { readonly ok: false; readonly error: E };

Expand Down Expand Up @@ -191,7 +206,9 @@ let bytes: Uint8Array<ArrayBuffer> = new Uint8Array(0);

/** A view of the module's memory, made again once memory grows, which detaches the last. */
export function dv(): DataView {
if (view.buffer !== memory.buffer) {
// A detached view's length reads 0, a third the cost of reading `memory.buffer`, which
// every value a call reads or writes through memory pays.
if (bytes.byteLength === 0) {
// Never shared: the module is built without threads. Deno's types say it may be.
const buffer = memory.buffer as ArrayBuffer;
view = new DataView(buffer);
Expand Down Expand Up @@ -511,6 +528,9 @@ export function lentHandle<T extends string>(p: number, k: Kind): Handle<T> {
// Integers.

const TWO32 = 4294967296;
/** The bits of Rust's `f32::NAN`, and the high bits of its `f64::NAN`, whose low are 0. */
const F32_NAN = 0x7fc00000;
const F64_NAN_HI = 0x7ff80000;

/** `v`, unless it is no integer from `min` through `max`, which throws naming `what`. */
export function int(v: number, min: number, max: number, what: string): number {
Expand All @@ -524,10 +544,72 @@ export function nonZero<T extends number | u64 | i64>(v: T): T {
return v;
}

/** An `f32`: a number a single holds exactly, or NaN. */
export function f32(v: number): number {
if (Math.fround(v) === v || v !== v) return v;
throw new RangeError(`${v} is no f32`);
/** The `f32` at `p`: a NaN by its bits, which no number read through a float keeps. */
export function f32Get(p: number): f32 {
const d = dv();
const bits = d.getUint32(p, true);
if ((bits & 0x7fffffff) <= 0x7f800000) return d.getFloat32(p, true);
return bits === F32_NAN ? NaN : ({ bits } as F32Nan);
}

/** Write the `f32` `v` at `p`: a number a single holds exactly, `NaN` as `f32::NAN`, or a
* NaN's bits. Any other throws. */
export function f32Put(p: number, v: f32): void {
if (typeof v === "number") {
const d = dv();
if (v !== v) d.setUint32(p, F32_NAN, true);
else if (Math.fround(v) === v) d.setFloat32(p, v, true);
else throw new RangeError(`${v} is no f32`);
return;
}
const bits = v.bits;
if (bits >>> 0 !== bits || (bits & 0x7fffffff) <= 0x7f800000 || bits === F32_NAN) {
throw new RangeError(`${JSON.stringify(v)} is no f32 NaN in its one form`);
}
// Taken after the payload's getters, which may run code that grows memory.
dv().setUint32(p, bits, true);
}

/** The `f64` at `p`: a NaN by its bits. */
export function f64Get(p: number): f64 {
const d = dv();
const hi = d.getUint32(p + 4, true);
const lo = d.getUint32(p, true);
const high = hi & 0x7fffffff;
if (high < 0x7ff00000 || (high === 0x7ff00000 && lo === 0)) return d.getFloat64(p, true);
return hi === F64_NAN_HI && lo === 0 ? NaN : ({ hi, lo } as F64Nan);
}

/** Write the `f64` `v` at `p`: a number, `NaN` as `f64::NAN`, or a NaN's bits. */
export function f64Put(p: number, v: f64): void {
if (typeof v === "number") {
const d = dv();
if (v === v) d.setFloat64(p, v, true);
else {
d.setUint32(p, 0, true);
d.setUint32(p + 4, F64_NAN_HI, true);
}
return;
}
const { hi, lo } = v;
const high = hi & 0x7fffffff;
const nan = high > 0x7ff00000 || (high === 0x7ff00000 && lo !== 0);
if (hi >>> 0 !== hi || lo >>> 0 !== lo || !nan || (hi === F64_NAN_HI && lo === 0)) {
throw new RangeError(`${JSON.stringify(v)} is no f64 NaN in its one form`);
}
const d = dv();
d.setUint32(p, lo, true);
d.setUint32(p + 4, hi, true);
}

/** An `f32` as a number: a NaN of its own bits as `NaN`, which loses them. */
export function f32Number(v: f32): number {
return typeof v === "number" ? v : NaN;
}

/** An `f64` as a number: a NaN of its own bits as `NaN`, which loses them. */
export function f64Number(v: f64): number {
return typeof v === "number" ? v : NaN;
}

/** A `char`: a Unicode scalar value. */
Expand Down Expand Up @@ -1696,6 +1778,9 @@ async function instantiate(source: Source, glue: Glue): Promise<void> {
});
const exports = instance.exports as Exports;
memory = exports.memory as WebAssembly.Memory;
// A refused load's views are of its own memory, which this one never detaches.
view = new DataView(new ArrayBuffer(0));
bytes = new Uint8Array(0);
prefix = glue.prefix;
space = glue.space;
exports._initialize?.();
Expand Down
Loading