diff --git a/ffi/README.md b/ffi/README.md index a8f4dcf8f..876e5dc1d 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -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 | 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 | 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 @@ -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 @@ -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` is `T | undefined`, or `T | rt.None` where `T` can already be `undefined`, `Result` 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 @@ -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. @@ -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 # 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 @@ -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 diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 3ee2199ab..61ec1b812 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -791,6 +791,34 @@ pub fn float_out(value: f64) -> R { 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 { + 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 { + 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 { // SAFETY: the GIL is held, and `obj` is live. diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index fc6ac9902..91b6a27f7 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -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'); diff --git a/ffi/runtime/rt.ts b/ffi/runtime/rt.ts index 2c22d66ee..15c00c655 100644 --- a/ffi/runtime/rt.ts +++ b/ffi/runtime/rt.ts @@ -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 = { readonly ok: true; readonly value: T } | { readonly ok: false; readonly error: E }; @@ -191,7 +206,9 @@ let bytes: Uint8Array = 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); @@ -511,6 +528,9 @@ export function lentHandle(p: number, k: Kind): Handle { // 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 { @@ -524,10 +544,72 @@ export function nonZero(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. */ @@ -1696,6 +1778,9 @@ async function instantiate(source: Source, glue: Glue): Promise { }); 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?.(); diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 40809eb66..34192cece 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -73,6 +73,8 @@ pub(crate) fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result { variants: HashMap<(String, String), String>, /// Each C struct's `dart:ffi` class, by C name. structs: BTreeMap, + /// What each C struct holds by value, by C name: what decides whether it holds an `f32`, + /// and its twin's fields if so. + layouts: HashMap, + /// Each C struct holding an `f32` whose twin the glue defines. + twins: BTreeSet, /// Each C function's `@Native` declaration, by C name. natives: BTreeMap, /// The conversions, polls, and trampolines, by name. @@ -689,9 +696,9 @@ impl<'a> Gen<'a> { Ty::Void => "ffi.Void".to_string(), Ty::Bool => "ffi.Bool".to_string(), Ty::Int(int) => native_int(*int).to_string(), - Ty::F32 => "ffi.Float".to_string(), + // Its bits, which no float `dart:ffi` converts keeps for a signaling NaN. + Ty::F32 | Ty::Char => "ffi.Uint32".to_string(), Ty::F64 => "ffi.Double".to_string(), - Ty::Char => "ffi.Uint32".to_string(), Ty::Enum(_) | Ty::Poll => "ffi.Int".to_string(), Ty::Status => "ffi.Int32".to_string(), Ty::Handle { .. } | Ty::Future(_) | Ty::Data => "ffi.Pointer".to_string(), @@ -715,8 +722,8 @@ impl<'a> Gen<'a> { match ty { Ty::Void => "void".to_string(), Ty::Bool => "bool".to_string(), - Ty::Int(_) | Ty::Char | Ty::Enum(_) | Ty::Poll | Ty::Status => "int".to_string(), - Ty::F32 | Ty::F64 => "double".to_string(), + Ty::Int(_) | Ty::F32 | Ty::Char | Ty::Enum(_) | Ty::Poll | Ty::Status => "int".to_string(), + Ty::F64 => "double".to_string(), ty => self.nt(ty), } } @@ -760,6 +767,7 @@ impl<'a> Gen<'a> { return class; } self.structs.insert(name.clone(), String::new()); + self.layouts.insert(name.clone(), Layout::Opaque); if let Some(n) = abi.arrays.get(&name) { let code = format!( "final class {class} extends ffi.Struct {{\n\t@ffi.Array({n})\n\texternal ffi.Array items;\n}}\n\n" @@ -815,13 +823,19 @@ impl<'a> Gen<'a> { let mut extra = String::new(); let union = private_type(&format!("{name}_u")); let mut members = Vec::new(); + let mut payloads = Vec::new(); for variant in e.variants.iter().filter(|v| !v.fields.is_empty()) { - let payload_class = private_type(&abi::payload(&name, &variant.name)); + let payload = abi::payload(&name, &variant.name); + let payload_class = private_type(&payload); let mut decls = Vec::new(); + let mut tys = Vec::new(); for field in &variant.fields { let ty = abi.ty(&field.ty); decls.push(self.field(&ty, &abi::escape(&field.name))); + tys.push(ty); } + self.layouts.insert(payload.clone(), Layout::Fields(tys)); + payloads.push(payload.clone()); let _ = write!( extra, "final class {payload_class} extends ffi.Struct {{\n{}}}\n\n", @@ -841,6 +855,7 @@ impl<'a> Gen<'a> { "{}\texternal {union} $u;\n", self.field(&Ty::Enum(format!("{name}_tag")), "tag") ); + self.layouts.insert(name.clone(), Layout::Enum(payloads)); self.structs.insert( name, format!("final class {class} extends ffi.Struct {{\n{decls}}}\n\n{extra}"), @@ -850,6 +865,10 @@ impl<'a> Gen<'a> { unreachable!("`{name}` is no struct of the API"); } let decls: String = fields.iter().map(|(n, ty)| self.field(ty, n)).collect(); + self.layouts.insert( + name.clone(), + Layout::Fields(fields.into_iter().map(|(_, ty)| ty).collect()), + ); self.structs.insert( name, format!("final class {class} extends ffi.Struct {{\n{decls}}}\n\n"), @@ -857,14 +876,112 @@ impl<'a> Gen<'a> { class } + /// Whether `ty` holds an `f32` by value, which Dart passes as its bits: in a struct, which + /// `dart:ffi` then passes by an ABI of its own, as one of integers where C's has a float. + fn holds_f32(&mut self, ty: &Ty) -> bool { + match ty { + Ty::F32 => true, + Ty::Struct(name) => { + // A data enum's payload is laid out with its enum, which has no class of its own. + if !self.layouts.contains_key(name) { + self.struct_class(ty); + } + match self.layouts.get(name).cloned() { + Some(Layout::Fields(tys)) => tys.iter().any(|ty| self.holds_f32(ty)), + Some(Layout::Enum(payloads)) => payloads.iter().any(|p| self.holds_f32(&Ty::Struct(p.clone()))), + Some(Layout::Opaque) | None => false, + } + } + _ => false, + } + } + + /// The Rust type a C type crosses between Dart and the glue as: an `f32` as its bits, and + /// a struct holding one as its twin, which Dart's class mirrors. + fn crossing(&mut self, ty: &Ty) -> String { + match ty { + Ty::F32 => "u32".to_string(), + Ty::Struct(name) if self.holds_f32(ty) => self.twin(name), + ty => rs(ty), + } + } + + /// The twin of the C struct `name`, which holds an `f32`: of its layout, each `f32` in it + /// as its bits, so it crosses by the ABI `dart:ffi` gives its class. Named in the glue's + /// namespace, which no C name takes. + fn twin(&mut self, name: &str) -> String { + let twin = format!("{}_twin_{}", self.space, self.bare(name)); + let union = format!("{}_union_{}", self.space, self.bare(name)); + if !self.twins.insert(name.to_string()) { + return twin; + } + let code = match self.layouts.get(name).cloned() { + Some(Layout::Fields(tys)) => { + let fields: Vec = tys.iter().map(|ty| format!("pub {}", self.crossing(ty))).collect(); + format!( + "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {twin}({});\n\n", + fields.join(", ") + ) + } + Some(Layout::Enum(payloads)) => { + let members: Vec = (payloads.iter().enumerate()) + .map(|(n, p)| format!("\tpub v{n}: {},\n", self.crossing(&Ty::Struct(p.clone())))) + .collect(); + format!( + "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {twin}(pub i32, pub {union});\n\n#[repr(C)]\n#[derive(Clone, Copy)]\npub union {union} {{\n{}}}\n\n", + members.concat() + ) + } + Some(Layout::Opaque) | None => unreachable!("`{name}` holds no f32"), + }; + self.glue.push_str(&code); + twin + } + + /// The C value of `e`, which crossed from Dart as [`Self::crossing`] gives `ty`. + fn dart_in(&mut self, ty: &Ty, e: &str) -> String { + match ty { + Ty::F32 => format!("f32::from_bits({e})"), + Ty::Struct(_) if self.holds_f32(ty) => { + let twin = self.crossing(ty); + format!("unsafe {{ std::mem::transmute::<{twin}, {}>({e}) }}", rs(ty)) + } + _ => e.to_string(), + } + } + + /// The C value `e` of type `ty` as it crosses to Dart, as [`Self::crossing`] gives it. + fn dart_out(&mut self, ty: &Ty, e: &str) -> String { + match ty { + Ty::F32 => format!("{e}.to_bits()"), + Ty::Struct(_) if self.holds_f32(ty) => { + let twin = self.crossing(ty); + format!("unsafe {{ std::mem::transmute::<{}, {twin}>({e}) }}", rs(ty)) + } + _ => e.to_string(), + } + } + + /// The Rust type of a Dart trampoline taking its data, then `params`, and returning `ret`, + /// each as it crosses. + fn crossing_fn(&mut self, params: &[Ty], ret: &Ty) -> String { + let params: String = params.iter().map(|ty| format!(", {}", self.crossing(ty))).collect(); + format!("unsafe extern \"C\" fn(*mut c_void{params}) -> {}", self.crossing(ret)) + } + + /// Whether a C function of `params` and `ret` passes an `f32` by value, as its bits to + /// Dart, so Dart calls it, or it calls Dart, through the glue. + fn passes_f32(&mut self, params: &[Ty], ret: &Ty) -> bool { + self.holds_f32(ret) || params.iter().any(|ty| self.holds_f32(ty)) + } + /// A struct's field of C type `ty`, named `$name`. fn field(&mut self, ty: &Ty, name: &str) -> String { let annotation = match ty { Ty::Bool => Some("ffi.Bool"), Ty::Int(int) => Some(native_int(*int)), - Ty::F32 => Some("ffi.Float"), + Ty::F32 | Ty::Char => Some("ffi.Uint32"), Ty::F64 => Some("ffi.Double"), - Ty::Char => Some("ffi.Uint32"), Ty::Enum(_) | Ty::Poll => Some("ffi.Int"), _ => None, }; @@ -913,9 +1030,11 @@ impl<'a> Gen<'a> { let mut params = vec![Ty::Data]; params.extend(call.params.iter().map(|(_, ty)| ty.clone())); let decls: Vec = (call.params.iter().enumerate()) - .map(|(n, (_, ty))| format!(", p{n}: {}", rs(ty))) + .map(|(n, (_, ty))| format!(", p{n}: {}", self.crossing(ty))) + .collect(); + let mut args: Vec = (call.params.iter().enumerate()) + .map(|(n, (_, ty))| self.dart_in(ty, &format!("p{n}"))) .collect(); - let mut args: Vec = (0..call.params.len()).map(|n| format!("p{n}")).collect(); // A status the call writes reaches the isolate's one cell only once `serve` stopped // serving: a call it served meanwhile, on the isolate's thread, read its own first. A // null one, which aborts on a failed check, passes through. @@ -934,15 +1053,16 @@ impl<'a> Gen<'a> { }; let body = match cell { Some(cell) => format!( - "let mut status = 0;\n\tlet s_ = match {cell}.is_null() {{\n\t\ttrue => {cell},\n\t\tfalse => &raw mut status,\n\t}};\n\tlet r = unsafe {{ home.serve(move || {call_}) }};\n\tif !{cell}.is_null() {{\n\t\tunsafe {{ *{cell} = status }};\n\t}}\n\tr" + "let mut status = 0;\n\tlet s_ = match {cell}.is_null() {{\n\t\ttrue => {cell},\n\t\tfalse => &raw mut status,\n\t}};\n\tlet r = unsafe {{ home.serve(move || {call_}) }};\n\tif !{cell}.is_null() {{\n\t\tunsafe {{ *{cell} = status }};\n\t}}\n\t{}", + self.dart_out(&call.ret, "r") ), - None => format!("unsafe {{ home.serve(move || {call_}) }}"), + None => self.dart_out(&call.ret, &format!("unsafe {{ home.serve(move || {call_}) }}")), }; + let ret = self.crossing(&call.ret); let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}(home: *const c_void{}) -> {} {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\t{body}\n}}\n\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}(home: *const c_void{}) -> {ret} {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\t{body}\n}}\n\n", decls.concat(), - rs(&call.ret), ); let native = self.native_fn(&name, params, call.ret.clone()); self.checked_on(&name, native, call.params.len() + 1, &call.name) @@ -952,7 +1072,34 @@ impl<'a> Gen<'a> { /// the C function itself, on the isolate's thread, which no call it makes back waits on. fn leaf(&mut self, call: &Sig) -> String { let name = format!("{}_leaf_{}", self.space, self.bare(&call.name)); - let native = self.declare(&name, call, true); + let params: Vec = call.params.iter().map(|(_, ty)| ty.clone()).collect(); + // One passing an `f32` calls the glue's twin of it, which converts its bits. + let native = match self.passes_f32(¶ms, &call.ret) { + true => { + let decls: Vec = (params.iter().enumerate()) + .map(|(n, ty)| format!("p{n}: {}", self.crossing(ty))) + .collect(); + let args: Vec = (params.iter().enumerate()) + .map(|(n, ty)| self.dart_in(ty, &format!("p{n}"))) + .collect(); + let body = self.dart_out( + &call.ret, + &format!("unsafe {{ crate::{}({}) }}", call.name, args.join(", ")), + ); + let ret = self.crossing(&call.ret); + let _ = write!( + self.glue, + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\t{body}\n}}\n\n", + decls.join(", "), + ); + let twin = Sig { + name: name.clone(), + ..call.clone() + }; + self.declare(&name, &twin, true) + } + false => self.declare(&name, call, true), + }; self.checked_on(&name, native, call.params.len(), &call.name) } @@ -1074,7 +1221,8 @@ impl<'a> Gen<'a> { /// a call's arguments: an object held by the arena `a_`. fn lower(&mut self, ty: &Type, e: &str) -> String { match ty { - Type::Bool | Type::F32 | Type::F64 => e.to_string(), + Type::Bool | Type::F64 => e.to_string(), + Type::F32 => format!("_n.f32Bits({e})"), Type::NonZero(int) => format!("_n.nonZero({})", self.lower(&Type::Int(*int), e)), Type::Int(int) => match int { Int::U8 => format!("_n.u8({e})"), @@ -1220,11 +1368,13 @@ impl<'a> Gen<'a> { let some = self.write(inner, "v", "c.$value", "t"); format!("if (v == null) {{\n\t\tc.$some = false;\n\t}} else {{\n\t\tc.$some = true;\n\t\t{some}\n\t}}") } - // A block is copied whole into the arena: `U64`s as the `int`s they are. + // A block is copied whole into the arena: `U64`s as the `int`s they are, and `f32`s + // as their bits, which the C pointer holds. Type::List(_, elem) if ty.block().is_some() => { let nt = self.nt(&abi.ty(elem)); let v = match **elem { Type::Int(Int::U64) => "v as List", + Type::F32 => "v.buffer.asUint32List(v.offsetInBytes, v.length)", _ => "v", }; format!( @@ -1376,10 +1526,15 @@ impl<'a> Gen<'a> { Type::Int(Int::U64) => " as List", _ => "", }; + // A copy of typed data, which keeps each float's bits. + let ptr = match block { + Type::F32 => "c.$ptr.cast()", + _ => "c.$ptr", + }; return self.finish_reader( name, format!( - "{dt} {{NAME}}({class} c) {{\n\tfinal out = typed.{list}.fromList(c.$ptr.asTypedList(c.$len));\n\t{free}(c);\n\treturn out{cast};\n}}\n\n" + "{dt} {{NAME}}({class} c) {{\n\tfinal out = typed.{list}.fromList({ptr}.asTypedList(c.$len));\n\t{free}(c);\n\treturn out{cast};\n}}\n\n" ), ); } @@ -1448,7 +1603,8 @@ impl<'a> Gen<'a> { Type::Path | Type::PathBuf => format!("PathBuf._(_n.bytes({e}))"), Type::Slice | Type::Vec | Type::Bytes => format!("_n.bytes({e})"), Type::Int(Int::U64 | Int::Usize) | Type::NonZero(Int::U64 | Int::Usize) => format!("U64.fromBits({e})"), - Type::Bool | Type::F32 | Type::F64 | Type::Int(_) | Type::NonZero(_) => e.to_string(), + Type::F32 => format!("_n.f32Of({e})"), + Type::Bool | Type::F64 | Type::Int(_) | Type::NonZero(_) => e.to_string(), Type::Char => format!("_n.scalar({e})"), Type::U128 => format!("_n.u128Of({e})"), Type::Duration => format!("RustDuration._of({e})"), @@ -2196,18 +2352,34 @@ impl<'a> Gen<'a> { let nt = self.nt(&Ty::Fn(c_params.clone(), Box::new(ret.clone()))); let inner = nt["ffi.Pointer<".len()..nt.len() - 1].to_string(); relays.push(format!("ffi.Native.addressOf<{inner}>({native}).cast()")); - let args: Vec = (0..c_params.len()).map(|k| format!("p{k}")).collect(); + let args: Vec = (c_params.iter().enumerate()) + .map(|(k, ty)| self.dart_out(ty, &format!("p{k}"))) + .collect(); let decls: Vec = (c_params.iter().enumerate()) .map(|(k, ty)| format!(", p{k}: {}", rs(ty))) .collect(); let mut call_args = vec!["relay.token".to_string()]; call_args.extend(args); + // The trampoline takes and returns an `f32` as its bits, whatever C's table says. + let cast = match self.passes_f32(&c_params, &ret) { + true => format!( + "\n\tlet f = unsafe {{ std::mem::transmute::<_, {}>(f) }};", + self.crossing_fn(&c_params, &ret) + ), + false => String::new(), + }; + let called = self.dart_in( + &ret, + &format!( + "unsafe {{ relay.home.call(\"{what}\", move || f({})) }}", + call_args.join(", ") + ), + ); let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {relay}(data: *mut c_void{}) -> {} {{\n\tlet relay = unsafe {{ &*data.cast::() }};\n\tlet table = unsafe {{ &*relay.f.cast::() }};\n\tlet f = table.{m}.unwrap_or_else(|| rt::fail(\"{c}_vtable.{m}\", \"the function is null\"));\n\tunsafe {{ relay.home.call(\"{what}\", move || f({})) }}\n}}\n\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {relay}(data: *mut c_void{}) -> {} {{\n\tlet relay = unsafe {{ &*data.cast::() }};\n\tlet table = unsafe {{ &*relay.f.cast::() }};\n\tlet f = table.{m}.unwrap_or_else(|| rt::fail(\"{c}_vtable.{m}\", \"the function is null\"));{cast}\n\t{called}\n}}\n\n", decls.concat(), rs(&ret), - call_args.join(", "), m = method.name(), ); } @@ -2258,8 +2430,8 @@ impl<'a> Gen<'a> { // takes none. let exceptional = match ret { Ty::Bool => ", false".to_string(), - Ty::F32 | Ty::F64 => ", 0.0".to_string(), - Ty::Int(_) | Ty::Char | Ty::Enum(_) => ", 0".to_string(), + Ty::F64 => ", 0.0".to_string(), + Ty::Int(_) | Ty::F32 | Ty::Char | Ty::Enum(_) => ", 0".to_string(), _ => String::new(), }; let mut native_params = vec!["ffi.Pointer".to_string()]; @@ -2293,15 +2465,18 @@ impl<'a> Gen<'a> { let decls: Vec = (c_params.iter().enumerate()) .map(|(n, ty)| format!(", p{n}: {}", rs(ty))) .collect(); - let args: String = (0..c_params.len()).map(|n| format!(", p{n}")).collect(); - let fn_ty = format!( - "unsafe extern \"C\" fn(*mut c_void{}) -> {}", - c_params.iter().map(|ty| format!(", {}", rs(ty))).collect::(), - rs(&c_ret) + let args: String = (c_params.iter().enumerate()) + .map(|(n, ty)| format!(", {}", self.dart_out(ty, &format!("p{n}")))) + .collect(); + // The trampoline takes and returns an `f32` as its bits. + let fn_ty = self.crossing_fn(&c_params, &c_ret); + let called = self.dart_in( + &c_ret, + &format!("unsafe {{ relay.home.call(\"a closure\", move || f(relay.token{args})) }}"), ); let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {space}_fn_{k}(data: *mut c_void{}) -> {} {{\n\tlet relay = unsafe {{ &*data.cast::() }};\n\tlet f = unsafe {{ std::mem::transmute::<*const c_void, {fn_ty}>(relay.f) }};\n\tunsafe {{ relay.home.call(\"a closure\", move || f(relay.token{args})) }}\n}}\n\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {space}_fn_{k}(data: *mut c_void{}) -> {} {{\n\tlet relay = unsafe {{ &*data.cast::() }};\n\tlet f = unsafe {{ std::mem::transmute::<*const c_void, {fn_ty}>(relay.f) }};\n\t{called}\n}}\n\n", decls.concat(), rs(&c_ret), ); @@ -3068,6 +3243,17 @@ struct Owned { close: Option, } +/// What a C struct holds by value, as Dart declares it. +#[derive(Clone)] +enum Layout { + /// Fields of these types. + Fields(Vec), + /// A data enum's tag, then a union of these payload structs. + Enum(Vec), + /// Only bytes or integers: an array, or an opaque record. + Opaque, +} + /// How a function throws its error. enum Thrown { Itself, diff --git a/ffi/src/python.rs b/ffi/src/python.rs index ec8c5b753..2bec3f43c 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -640,7 +640,7 @@ impl<'a> Gen<'a> { Type::Bool => format!("rt::bool_in({o})"), Type::Int(int) => format!("rt::int_in::<{}>({o})", int.rust()), Type::NonZero(int) => format!("rt::nonzero_in::<{}>({o})", int.rust()), - Type::F32 => format!("rt::float_in({o}).map(|f| f as f32)"), + Type::F32 => format!("rt::f32_in({o})"), Type::F64 => format!("rt::float_in({o})"), Type::Char => format!("rt::char_in({o})"), Type::Str | Type::String => format!("rt::str_in({o})"), @@ -708,7 +708,8 @@ impl<'a> Gen<'a> { Type::Bool => format!("rt::bool_out({v})"), Type::Int(int) | Type::NonZero(int) if signed(*int) => format!("rt::int_out({v} as i64)"), Type::Int(_) | Type::NonZero(_) => format!("rt::uint_out({v} as u64)"), - Type::F32 | Type::F64 => format!("rt::float_out({v} as f64)"), + Type::F32 => format!("rt::f32_out({v})"), + Type::F64 => format!("rt::float_out({v})"), Type::Char => format!("rt::char_out({v})"), Type::Str | Type::String => format!("rt::str_out({v})"), Type::Slice | Type::Vec | Type::Bytes => format!("rt::bytes_out({v})"), diff --git a/ffi/src/web.rs b/ffi/src/web.rs index 0e7442d32..f5efd5d22 100644 --- a/ffi/src/web.rs +++ b/ffi/src/web.rs @@ -30,7 +30,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::fmt::Write; -use crate::abi::{self, Abi, Ty, pascal}; +use crate::abi::{self, Abi, Sig, Ty, pascal}; use crate::config::Package; use crate::doc; use crate::glue::rs; @@ -214,6 +214,7 @@ pub(crate) fn generate(abi: &Abi<'_>, facade: &Package, contract: u64) -> Result names: BTreeMap::new(), locked: BTreeMap::new(), errors: Vec::new(), + wrapped: BTreeSet::new(), }; g.locked = crate::lock::types(abi, &g.classes); g.items(); @@ -472,6 +473,8 @@ struct Gen<'a> { names: BTreeMap, locked: BTreeMap, errors: Vec, + /// Each wrapper the glue defines, which passes a C function's floats through memory. + wrapped: BTreeSet, } impl<'a> Gen<'a> { @@ -604,7 +607,9 @@ impl<'a> Gen<'a> { Type::Bool => "boolean".to_string(), Type::Int(Int::U64) | Type::NonZero(Int::U64) => "rt.u64".to_string(), Type::Int(Int::I64) | Type::NonZero(Int::I64) => "rt.i64".to_string(), - Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::F64 | Type::Char => "number".to_string(), + Type::F32 => "rt.f32".to_string(), + Type::F64 => "rt.f64".to_string(), + Type::Int(_) | Type::NonZero(_) | Type::Char => "number".to_string(), Type::U128 => "rt.u128".to_string(), Type::Str | Type::String => "string".to_string(), Type::Slice | Type::Vec | Type::Bytes | Type::Array(_) => "Uint8Array".to_string(), @@ -819,8 +824,21 @@ impl<'a> Gen<'a> { (l, round(4, align)) } - /// The scalar a C type crosses a wasm call as, if it is one, or a struct of exactly one. + /// The scalar a C type crosses a wasm call as, if it is one, or a struct of exactly one, + /// to the binding: a float crosses through memory instead, as [`Self::floats`] says. fn scalar(&self, ty: &Ty) -> Option { + self.wasm(ty).filter(|w| !matches!(w, Wasm::F32 | Wasm::F64)) + } + + /// Whether C's wasm32 ABI passes `ty` as a float, which the binding passes through memory + /// instead, by a wrapper in the glue: a NaN's bits are lost in any float an engine + /// converts to a number or back, as V8 quiets and, once it optimizes, canonicalizes one. + fn floats(&self, ty: &Ty) -> bool { + matches!(self.wasm(ty), Some(Wasm::F32 | Wasm::F64)) + } + + /// The scalar C's wasm32 ABI passes a C type as, if it is one, or a struct of exactly one. + fn wasm(&self, ty: &Ty) -> Option { match ty { Ty::Void => None, Ty::Int(Int::U64 | Int::I64) => Some(Wasm::I64), @@ -845,7 +863,7 @@ impl<'a> Gen<'a> { Ty::Struct(name) => match self.shape(name) { Shape::Bytes(1) => Some(Wasm::I32), Shape::Units(unit, 1) => Some(if unit == 8 { Wasm::I64 } else { Wasm::I32 }), - Shape::Fields(fields) if fields.len() == 1 => self.scalar(&fields[0]), + Shape::Fields(fields) if fields.len() == 1 => self.wasm(&fields[0]), _ => None, }, } @@ -855,7 +873,9 @@ impl<'a> Gen<'a> { fn plain(ty: &Ty) -> bool { !matches!( ty, - Ty::Buf + Ty::F32 + | Ty::F64 + | Ty::Buf | Ty::U128 | Ty::Duration | Ty::SocketAddr(_) @@ -872,8 +892,7 @@ impl<'a> Gen<'a> { match w { Wasm::I32 => ("Int32", ""), Wasm::I64 => ("BigInt64", ""), - Wasm::F32 => ("Float32", ""), - Wasm::F64 => ("Float64", ""), + Wasm::F32 | Wasm::F64 => unreachable!("a float crosses through memory"), } } @@ -897,8 +916,8 @@ impl<'a> Gen<'a> { Int::U64 => format!("rt.u64Get({p})"), Int::I64 => format!("rt.i64Get({p})"), }, - Type::F32 => format!("rt.dv().getFloat32({p}, true)"), - Type::F64 => format!("rt.dv().getFloat64({p}, true)"), + Type::F32 => format!("rt.f32Get({p})"), + Type::F64 => format!("rt.f64Get({p})"), Type::Char => format!("rt.dv().getUint32({p}, true)"), Type::U128 => format!("rt.u128Get({p})"), Type::Str | Type::String => match own { @@ -957,7 +976,9 @@ impl<'a> Gen<'a> { match ty { Type::Unit => String::new(), Type::Bool => format!("rt.dv().setUint8({p}, {e} ? 1 : 0);"), - Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::F64 | Type::Char => { + Type::F32 => format!("rt.f32Put({p}, {e});"), + Type::F64 => format!("rt.f64Put({p}, {e});"), + Type::Int(_) | Type::NonZero(_) | Type::Char => { let (set, lowered) = match ty { Type::Int(Int::U64) | Type::NonZero(Int::U64) => { return format!("rt.u64Put({p}, {});", self.checked(ty, e)); @@ -966,8 +987,6 @@ impl<'a> Gen<'a> { return format!("rt.i64Put({p}, {});", self.checked(ty, e)); } Type::Int(int) | Type::NonZero(int) => (int_view(*int), self.checked(ty, e)), - Type::F32 => ("Float32", self.checked(ty, e)), - Type::F64 => ("Float64", e.to_string()), _ => ("Uint32", self.checked(ty, e)), }; match set { @@ -1052,7 +1071,6 @@ impl<'a> Gen<'a> { }; format!("rt.int({e}, {min}, {max}, \"{}\")", int.rust()) } - Type::F32 => format!("rt.f32({e})"), Type::Char => format!("rt.char({e})"), _ => e.to_string(), } @@ -1064,8 +1082,7 @@ impl<'a> Gen<'a> { Type::Bool => format!("({e} ? 1 : 0)"), Type::Int(Int::U64) | Type::NonZero(Int::U64) => format!("rt.u64Big({})", self.checked(ty, e)), Type::Int(Int::I64) | Type::NonZero(Int::I64) => format!("rt.i64Big({})", self.checked(ty, e)), - Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::Char => self.checked(ty, e), - Type::F64 => e.to_string(), + Type::Int(_) | Type::NonZero(_) | Type::Char => self.checked(ty, e), Type::Enum(i) => { let lower = self.enum_lower(*i); format!("{lower}({e})") @@ -1093,7 +1110,6 @@ impl<'a> Gen<'a> { Int::U64 => format!("rt.u64OfBig({e})"), Int::I64 => format!("rt.i64OfBig({e})"), }, - Type::F32 | Type::F64 => e.to_string(), Type::Char => format!("{e} >>> 0"), Type::Enum(i) => { let lift = self.enum_lift(*i); @@ -1675,21 +1691,24 @@ impl Gen<'_> { } } - /// Statements calling the C function `name` with `args`, returning `ret`, into `lines`: a - /// struct it returns is written through a pointer passed first, and with `status`, a check - /// it fails throws once it returns. - fn invoke(&mut self, name: &str, mut args: Vec, ret: &Ty, status: bool, lines: &mut Vec) -> Ret { + /// Statements calling `sig`'s C function with `args`, into `lines`: a struct it returns is + /// written through a pointer passed first, and with a status, a check it fails throws once + /// it returns. + fn invoke(&mut self, sig: &Sig, mut args: Vec, lines: &mut Vec) -> Ret { + let (name, ret, status) = (&sig.name, &sig.ret, sig.status().is_some()); if status { lines.push("const s$ = rt.alloc(4, 4);\nrt.dv().setInt32(s$, 0, true);".to_string()); args.push("s$".to_string()); } + let params: Vec = sig.params.iter().map(|(_, ty)| ty.clone()).collect(); + let callee = self.callee(name, ¶ms, ret); let sret = !Self::plain(ret) && self.scalar(ret).is_none(); if sret { let l = self.layout(ret); lines.push(format!("const rp$ = rt.alloc({}, {});", l.size, l.align)); args.insert(0, "rp$".to_string()); } - let call = format!("rt.w.{name}({})", args.join(", ")); + let call = format!("rt.w.{callee}({})", args.join(", ")); let ret = match ret { Ty::Void => { lines.push(format!("rt.s.d++;\n{call};\nrt.s.d--;")); @@ -1832,16 +1851,105 @@ impl Gen<'_> { } /// The Rust glue's declaration of `name`, a C function the module imports from `rust2` - /// taking `params` after its data and returning `ret`. - fn import_decl(&mut self, name: &str, params: &[Ty], ret: &Ty) { - let decls: String = (params.iter().enumerate()) + /// taking `params` after its data and returning `ret`, and the function Rust calls it by: + /// the import, or for a signature with a float, a shim passing each through memory, as + /// the import takes it. + fn import_decl(&mut self, name: &str, params: &[Ty], ret: &Ty) -> String { + let floats = self.floats(ret); + let mut decls = Vec::new(); + let mut args = Vec::new(); + if floats { + decls.push(format!("r: *mut {}", rs(ret))); + args.push("r.as_mut_ptr()".to_string()); + } + decls.push("data: *mut c_void".to_string()); + args.push("data".to_string()); + for (n, ty) in params.iter().enumerate() { + match self.floats(ty) { + true => { + decls.push(format!("p{n}: *const {}", rs(ty))); + args.push(format!("&raw const p{n}")); + } + false => { + decls.push(format!("p{n}: {}", rs(ty))); + args.push(format!("p{n}")); + } + } + } + let shimmed = floats || params.iter().any(|ty| self.floats(ty)); + let returns = match floats { + true => "()".to_string(), + false => rs(ret), + }; + let _ = write!( + self.rust, + "#[link(wasm_import_module = \"rust2\")]\nunsafe extern \"C\" {{\n\tfn {name}({}) -> {returns};\n}}\n\n", + decls.join(", ") + ); + if !shimmed { + return name.to_string(); + } + // In a prefix of its own, which no import's name takes. + let shim = format!("{}_shim_{}", self.space, &name[self.space.len() + 1..]); + let params: String = (params.iter().enumerate()) .map(|(n, ty)| format!(", p{n}: {}", rs(ty))) .collect(); + let call = format!("unsafe {{ {name}({}) }}", args.join(", ")); + let body = match floats { + true => format!( + "let mut r = std::mem::MaybeUninit::<{}>::uninit();\n\t{call};\n\tunsafe {{ r.assume_init() }}", + rs(ret) + ), + false => call, + }; let _ = write!( self.rust, - "#[link(wasm_import_module = \"rust2\")]\nunsafe extern \"C\" {{\n\tfn {name}(data: *mut c_void{decls}) -> {};\n}}\n\n", + "unsafe extern \"C\" fn {shim}(data: *mut c_void{params}) -> {} {{\n\t{body}\n}}\n\n", rs(ret) ); + shim + } + + /// The C function JavaScript calls for `name`, which takes `params` and returns `ret`: + /// itself, or for a signature with a float, a wrapper in the glue taking each through + /// memory, as an out pointer passed first for its return. + fn callee(&mut self, name: &str, params: &[Ty], ret: &Ty) -> String { + let floats = self.floats(ret); + if !floats && !params.iter().any(|ty| self.floats(ty)) { + return name.to_string(); + } + let wrapper = format!("{}_x_{}", self.space, self.bare(name)); + if !self.wrapped.insert(wrapper.clone()) { + return wrapper; + } + let mut decls = Vec::new(); + if floats { + decls.push(format!("r: *mut {}", rs(ret))); + } + let mut args = Vec::new(); + for (n, ty) in params.iter().enumerate() { + match self.floats(ty) { + true => { + decls.push(format!("p{n}: *const {}", rs(ty))); + args.push(format!("p{n}.read()")); + } + false => { + decls.push(format!("p{n}: {}", rs(ty))); + args.push(format!("p{n}")); + } + } + } + let call = format!("{name}({})", args.join(", ")); + let (body, returns) = match floats { + true => (format!("r.write({call})"), "()".to_string()), + false => (call, rs(ret)), + }; + let _ = write!( + self.rust, + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {wrapper}({}) -> {returns} {{\n\tunsafe {{ {body} }}\n}}\n\n", + decls.join(", ") + ); + wrapper } /// The glue's function giving the table index of the C function the Rust glue's `export` @@ -1878,8 +1986,8 @@ impl Gen<'_> { let name = format!("{space}_cb_{bare}_{m}"); let params: Vec = method.params.iter().map(|p| abi.ty(&p.ty)).collect(); let ret = abi.ty(&method.ok); - self.import_decl(&name, ¶ms, &ret); - fields.push(format!("{m}: Some({name})")); + let called = self.import_decl(&name, ¶ms, &ret); + fields.push(format!("{m}: Some({called})")); // The import, which calls the implementation the token names. let module = std::mem::replace(&mut self.module, "glue".to_string()); let t = self.named(class.clone()); @@ -1956,7 +2064,7 @@ impl Gen<'_> { self.relays.push(sig.clone()); let k = self.relays.len() - 1; let name = format!("{space}_i_fn_{k}"); - self.import_decl(&name, &c_params, &c_ret); + let called = self.import_decl(&name, &c_params, &c_ret); let fn_ty = format!( "unsafe extern \"C\" fn(*mut c_void{}) -> {}", c_params.iter().map(|ty| format!(", {}", rs(ty))).collect::(), @@ -1964,7 +2072,7 @@ impl Gen<'_> { ); let _ = write!( self.rust, - "#[unsafe(no_mangle)]\npub extern \"C\" fn {space}_fn_{k}() -> usize {{\n\t{name} as {fn_ty} as usize\n}}\n\n" + "#[unsafe(no_mangle)]\npub extern \"C\" fn {space}_fn_{k}() -> usize {{\n\t{called} as {fn_ty} as usize\n}}\n\n" ); // The import, which calls the trampoline the token names with the closure. let typed: Vec = (wasm.iter()) @@ -2450,6 +2558,15 @@ impl Gen<'_> { false => Ty::In(Box::new(c_ty.clone())), }; let status = abi.traits_status_of(ty); + // The signature of the trait's C function `what`, taking `params`, then a status if + // its receiver's handles are checked. + let sig = |what: &str, params: &[&Ty], ret: Ty, status: bool| Sig { + name: format!("{c}_{what}"), + params: (params.iter().map(|ty| (String::new(), (*ty).clone()))) + .chain(status.then(|| ("status".to_string(), Ty::Out(Box::new(Ty::Status))))) + .collect(), + ret, + }; // The statements passing `e` as the receiver, making `local`. let this = |g: &mut Self, e: &str, local: &str| -> String { match object { @@ -2480,10 +2597,8 @@ impl Gen<'_> { let text = |g: &mut Self, what: &str| -> Vec { let mut lines = vec![this(g, "self", "a$")]; let ret = g.invoke( - &format!("{c}_{what}"), + &sig(what, &[&recv], Ty::Buf, status), vec!["a$".to_string()], - &Ty::Buf, - status, &mut lines, ); let value = g.result(&Type::String, &ret); @@ -2514,7 +2629,11 @@ impl Gen<'_> { } if object && t.clone { let mut lines = vec![this(self, "self", "a$")]; - let ret = self.invoke(&format!("{c}_clone"), vec!["a$".to_string()], &recv, status, &mut lines); + let ret = self.invoke( + &sig("clone", &[&recv], recv.clone(), status), + vec!["a$".to_string()], + &mut lines, + ); let value = self.result(ty, &ret); lines.push(format!("return {value};")); emit( @@ -2530,7 +2649,7 @@ impl Gen<'_> { if t.eq && !plain { let mut lines = vec![this(self, "a", "a$"), this(self, "b", "b$")]; let args = vec!["a$".to_string(), "b$".to_string()]; - let ret = self.invoke(&format!("{c}_eq"), args, &Ty::Bool, status, &mut lines); + let ret = self.invoke(&sig("eq", &[&recv, &recv], Ty::Bool, status), args, &mut lines); let value = self.result(&Type::Bool, &ret); lines.push(format!("return {value};")); emit( @@ -2544,7 +2663,7 @@ impl Gen<'_> { if t.hash { let mut lines = vec![this(self, "self", "a$")]; let u64 = Ty::Int(Int::U64); - let ret = self.invoke(&format!("{c}_hash"), vec!["a$".to_string()], &u64, status, &mut lines); + let ret = self.invoke(&sig("hash", &[&recv], u64, status), vec!["a$".to_string()], &mut lines); let value = self.result(&Type::Int(Int::U64), &ret); lines.push(format!("return {value};")); emit( @@ -2564,7 +2683,7 @@ impl Gen<'_> { let mut lines = vec![this(self, "a", "a$"), this(self, "b", "b$")]; let args = vec!["a$".to_string(), "b$".to_string()]; let i8 = Ty::Int(Int::I8); - let ret = self.invoke(&format!("{c}_{suffix}"), args, &i8, status, &mut lines); + let ret = self.invoke(&sig(suffix, &[&recv, &recv], i8, status), args, &mut lines); let value = self.result(&Type::Int(Int::I8), &ret); let (member, doc, ret_t) = match suffix { "cmp" => { @@ -2596,7 +2715,7 @@ impl Gen<'_> { false => c_ty.clone(), }; let mut lines = Vec::new(); - let ret = self.invoke(&format!("{c}_default"), Vec::new(), &ret_c, false, &mut lines); + let ret = self.invoke(&sig("default", &[], ret_c, false), Vec::new(), &mut lines); let value = self.result(ty, &ret); lines.push(format!("return {value};")); emit(self, "default", "Rust's `Default`.", "", &name, lines); @@ -2789,7 +2908,6 @@ impl Gen<'_> { } } let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); - let status = export.call.status().is_some(); lines.extend(gives); let ok = strip_refs(&function.ok); @@ -2830,13 +2948,7 @@ impl Gen<'_> { let failed = err .as_ref() .map(|err| format!("rt.Err({})", self.get(err, "o_err", true))); - let ret = self.invoke( - &poll.name, - poll_args, - &poll.ret, - poll.status().is_some(), - &mut poll_lines, - ); + let ret = self.invoke(poll, poll_args, &mut poll_lines); let Ret::Value(r) = ret else { unreachable!("a poll returns its state") }; @@ -2871,7 +2983,7 @@ impl Gen<'_> { } lines.push("const of$ = rt.alloc(4, 4);".to_string()); args.push("of$".to_string()); - let started = self.invoke(&start.name, args, &start.ret, status, &mut lines); + let started = self.invoke(start, args, &mut lines); let Ret::Value(r) = started else { unreachable!("a start returns its state") }; @@ -2890,7 +3002,11 @@ impl Gen<'_> { lines.push(out_decl(self, n, ty)); args.push(format!("o_{n}")); } - let ret = self.invoke(&call_name, args, &export.call.ret, status, &mut lines); + let call = Sig { + name: call_name.clone(), + ..export.call.clone() + }; + let ret = self.invoke(&call, args, &mut lines); lines.extend(writes.iter().cloned()); match &err { Some(err) => { diff --git a/ffi/surface/api/web.d.ts b/ffi/surface/api/web.d.ts index a181f0033..0e91a3568 100644 --- a/ffi/surface/api/web.d.ts +++ b/ffi/surface/api/web.d.ts @@ -109,18 +109,19 @@ export declare function debug(self: BoxedU8): string; export declare function eq(a: BoxedU8, b: BoxedU8): boolean; // Builtin.ts +import * as rt from "./rt.ts"; export type Builtin = { tag: "Any"; _0: Int32Array; } | { tag: "Boolean"; - _0: number; + _0: rt.f64; } | { tag: "Int"; - _0: number; + _0: rt.f64; } | { tag: "Native"; - _0: number; + _0: rt.f64; }; export declare function eq(a: Builtin, b: Builtin): boolean; @@ -195,10 +196,10 @@ export type Clash = { _0: Event; } | { tag: "Double"; - _0: number; + _0: rt.f64; } | { tag: "Float"; - _0: number; + _0: rt.f32; } | { tag: "String"; _0: string; @@ -236,11 +237,11 @@ export type Clash = { export declare function debug(self: Clash): string; export declare function eq(a: Clash, b: Clash): boolean; export declare function point(self: Clash): Point | undefined; -export declare function of(value: number): Clash; +export declare function of(value: rt.f64): Clash; export declare function bytes(self: Clash): rt.Bytes; export declare function reading(self: Clash, kotlin: number): Reading; export declare function listed(java: Uint32Array, kotlin: rt.Place): Uint32Array[]; -export declare function after(first: Promise, kotlin: number): rt.Call; +export declare function after(first: Promise, kotlin: rt.f64): rt.Call; export declare function joined(self: Clash, lent: Uint8Array | rt.Bytes, owned: Uint8Array | rt.Bytes, maybe: Uint8Array | rt.Bytes | undefined): rt.Bytes; export declare function tagged(self: Clash, tag: string): rt.Call; export declare function stamp(self: Clash, kotlin: rt.u64): Stamp; @@ -824,14 +825,15 @@ export declare function offset(self: Marker, dx: number, dy: number): Point; export { new$ as new }; // Measure.ts +import * as rt from "./rt.ts"; import type { Numbers } from "./Numbers.ts"; export type Measure = { - value: number; + value: rt.f64; numbers: Numbers; }; export declare function debug(self: Measure): string; export declare function eq(a: Measure, b: Measure): boolean; -export declare function new$(value: number, negative: boolean): Measure; +export declare function new$(value: rt.f64, negative: boolean): Measure; export { new$ as new }; // Message.ts @@ -1146,9 +1148,10 @@ export declare function debug(self: Polar_Turn): string; export declare function eq(a: Polar_Turn, b: Polar_Turn): boolean; // Qualifiers.ts +import * as rt from "./rt.ts"; export type Qualifiers = { - api: number; - kotlin: number; + api: rt.f64; + kotlin: rt.f64; }; export declare function eq(a: Qualifiers, b: Qualifiers): boolean; @@ -1191,8 +1194,8 @@ import type { Point } from "./Point.ts"; import type { Shape } from "./Shape.ts"; export type Reading = { ok: boolean; - level: number; - ratio: number; + level: rt.f32; + ratio: rt.f64; mark: number; shape: Shape; at: Point; @@ -2691,7 +2694,7 @@ export declare function wasmLog(log: Log): void; export declare function boothCount(booth: Booth): rt.u64; export declare function page(): Page; export declare function beats(beat: Beat): number; -export declare function same(a: number, b: number): number; +export declare function same(a: rt.f64, b: rt.f64): number; export declare function returnValues(point: rt.Place, fail: boolean): rt.Result<[string, [number, number, number, rt.u64] | undefined, Uint8Array[]], Error_>; export declare function returnMoved(counter: Counter, fail: boolean): rt.Result; export declare function nonZeros(): NonZeros; @@ -2757,6 +2760,17 @@ type Num = number & { export type u64 = Num<"u64"> | U64Limbs; export type i64 = Num<"i64"> | I64Limbs; export type u128 = Num<"u128"> | U128Limbs; +export type F32Nan = { + readonly bits: number; + readonly [brand]: "f32"; +}; +export type F64Nan = { + readonly hi: number; + readonly lo: number; + readonly [brand]: "f64"; +}; +export type f32 = Num<"f32"> | F32Nan; +export type f64 = Num<"f64"> | F64Nan; export type Result = { readonly ok: true; readonly value: T; @@ -2874,7 +2888,12 @@ export declare function holdOr(handle: Handle | undefined): number; export declare function lentHandle(p: number, k: Kind): Handle; export declare function int(v: number, min: number, max: number, what: string): number; export declare function nonZero(v: T): T; -export declare function f32(v: number): number; +export declare function f32Get(p: number): f32; +export declare function f32Put(p: number, v: f32): void; +export declare function f64Get(p: number): f64; +export declare function f64Put(p: number, v: f64): void; +export declare function f32Number(v: f32): number; +export declare function f64Number(v: f64): number; export declare function char(v: number): number; export declare function u64Of(hi: number, lo: number): u64; export declare function i64Of(hi: number, lo: number): i64; diff --git a/ffi/surface/moq-web/web.d.ts b/ffi/surface/moq-web/web.d.ts index 8f63f3f9b..4458b2aff 100644 --- a/ffi/surface/moq-web/web.d.ts +++ b/ffi/surface/moq-web/web.d.ts @@ -210,7 +210,7 @@ export declare function fromBps(bps: rt.u64): BandwidthRate; export declare function fromKbps(kbps: rt.u64): BandwidthRate; export declare function fromMbps(mbps: rt.u64): BandwidthRate; export declare function asBps(self: BandwidthRate): rt.u64; -export declare function scaled(self: BandwidthRate, factor: number): BandwidthRate; +export declare function scaled(self: BandwidthRate, factor: rt.f64): BandwidthRate; export declare function absDiff(self: BandwidthRate, other: BandwidthRate): BandwidthRate; export { default$ as default }; @@ -1148,9 +1148,10 @@ export declare function new$(mode: HangCatalogMode): HangCatalogBinaryConfig; export { new$ as new }; // HangCatalogChromaticity.ts +import * as rt from "./rt.ts"; export type HangCatalogChromaticity = { - x: number; - y: number; + x: rt.f64; + y: rt.f64; }; export declare function debug(self: HangCatalogChromaticity): string; export declare function eq(a: HangCatalogChromaticity, b: HangCatalogChromaticity): boolean; @@ -1434,14 +1435,15 @@ export declare function new$(mode: HangCatalogMode): HangCatalogJsonConfig; export { new$ as new }; // HangCatalogMastering.ts +import * as rt from "./rt.ts"; import type { HangCatalogChromaticity } from "./HangCatalogChromaticity.ts"; export type HangCatalogMastering = { red: HangCatalogChromaticity; green: HangCatalogChromaticity; blue: HangCatalogChromaticity; white: HangCatalogChromaticity; - maxLuminance: number; - minLuminance: number; + maxLuminance: rt.f64; + minLuminance: rt.f64; }; export declare function debug(self: HangCatalogMastering): string; export declare function eq(a: HangCatalogMastering, b: HangCatalogMastering): boolean; @@ -1643,8 +1645,8 @@ export declare function renditionsGet(self: HangCatalogVideo): ([string, HangCat export declare function renditionsSet(self: HangCatalogVideo, value: ([string, HangCatalogVideoConfig])[]): void; export declare function displayGet(self: HangCatalogVideo): HangCatalogDisplay | undefined; export declare function displaySet(self: HangCatalogVideo, value: HangCatalogDisplay | undefined): void; -export declare function rotationGet(self: HangCatalogVideo): number | undefined; -export declare function rotationSet(self: HangCatalogVideo, value: number | undefined): void; +export declare function rotationGet(self: HangCatalogVideo): rt.f64 | undefined; +export declare function rotationSet(self: HangCatalogVideo, value: rt.f64 | undefined): void; export declare function flipGet(self: HangCatalogVideo): boolean | undefined; export declare function flipSet(self: HangCatalogVideo, value: boolean | undefined): void; export declare function insert(self: HangCatalogVideo, name: string, config: HangCatalogVideoConfig): rt.Result; @@ -1735,8 +1737,8 @@ export declare function enabledGet(self: HangCatalogVideoConfig): boolean; export declare function enabledSet(self: HangCatalogVideoConfig, value: boolean): void; export declare function preferenceGet(self: HangCatalogVideoConfig): number; export declare function preferenceSet(self: HangCatalogVideoConfig, value: number): void; -export declare function framerateGet(self: HangCatalogVideoConfig): number | undefined; -export declare function framerateSet(self: HangCatalogVideoConfig, value: number | undefined): void; +export declare function framerateGet(self: HangCatalogVideoConfig): rt.f64 | undefined; +export declare function framerateSet(self: HangCatalogVideoConfig, value: rt.f64 | undefined): void; export declare function reorderGet(self: HangCatalogVideoConfig): number | undefined; export declare function reorderSet(self: HangCatalogVideoConfig, value: number | undefined): void; export declare function optimizeForLatencyGet(self: HangCatalogVideoConfig): boolean | undefined; @@ -1761,8 +1763,8 @@ export declare function eq(a: HangCatalogVideoProperties, b: HangCatalogVideoPro export declare function default$(): HangCatalogVideoProperties; export declare function displayGet(self: HangCatalogVideoProperties): HangCatalogDisplay | undefined; export declare function displaySet(self: HangCatalogVideoProperties, value: HangCatalogDisplay | undefined): void; -export declare function rotationGet(self: HangCatalogVideoProperties): number | undefined; -export declare function rotationSet(self: HangCatalogVideoProperties, value: number | undefined): void; +export declare function rotationGet(self: HangCatalogVideoProperties): rt.f64 | undefined; +export declare function rotationSet(self: HangCatalogVideoProperties, value: rt.f64 | undefined): void; export declare function flipGet(self: HangCatalogVideoProperties): boolean | undefined; export declare function flipSet(self: HangCatalogVideoProperties, value: boolean | undefined): void; export { default$ as default }; @@ -2110,12 +2112,12 @@ export declare function bitrateGet(self: MuxCatalogEstimate): rt.u64 | undefined export declare function bitrateSet(self: MuxCatalogEstimate, value: rt.u64 | undefined): void; export declare function delayGet(self: MuxCatalogEstimate): rt.Duration | undefined; export declare function delaySet(self: MuxCatalogEstimate, value: rt.Duration | undefined): void; -export declare function framerateGet(self: MuxCatalogEstimate): number | undefined; -export declare function framerateSet(self: MuxCatalogEstimate, value: number | undefined): void; +export declare function framerateGet(self: MuxCatalogEstimate): rt.f64 | undefined; +export declare function framerateSet(self: MuxCatalogEstimate, value: rt.f64 | undefined): void; export declare function withJitter(self: MuxCatalogEstimate, jitter: rt.Duration | undefined): MuxCatalogEstimate; export declare function withBitrate(self: MuxCatalogEstimate, bitrate: rt.u64 | undefined): MuxCatalogEstimate; export declare function withDelay(self: MuxCatalogEstimate, delay: rt.Duration | undefined): MuxCatalogEstimate; -export declare function withFramerate(self: MuxCatalogEstimate, framerate: number | undefined): MuxCatalogEstimate; +export declare function withFramerate(self: MuxCatalogEstimate, framerate: rt.f64 | undefined): MuxCatalogEstimate; export { default$ as default }; // MuxCatalogGuard.ts @@ -3017,8 +3019,8 @@ export declare function colorGet(self: MuxCatalogVideoHint): HangCatalogColor | export declare function colorSet(self: MuxCatalogVideoHint, value: HangCatalogColor | undefined): void; export declare function bitrateGet(self: MuxCatalogVideoHint): rt.u64 | undefined; export declare function bitrateSet(self: MuxCatalogVideoHint, value: rt.u64 | undefined): void; -export declare function framerateGet(self: MuxCatalogVideoHint): number | undefined; -export declare function framerateSet(self: MuxCatalogVideoHint, value: number | undefined): void; +export declare function framerateGet(self: MuxCatalogVideoHint): rt.f64 | undefined; +export declare function framerateSet(self: MuxCatalogVideoHint, value: rt.f64 | undefined): void; export declare function reorderGet(self: MuxCatalogVideoHint): number | undefined; export declare function reorderSet(self: MuxCatalogVideoHint, value: number | undefined): void; export declare function optimizeForLatencyGet(self: MuxCatalogVideoHint): boolean | undefined; @@ -4532,8 +4534,8 @@ export declare function enabledGet(self: MuxContainerGuardVideoConfig): boolean; export declare function enabledSet(self: MuxContainerGuardVideoConfig, value: boolean): void; export declare function preferenceGet(self: MuxContainerGuardVideoConfig): number; export declare function preferenceSet(self: MuxContainerGuardVideoConfig, value: number): void; -export declare function framerateGet(self: MuxContainerGuardVideoConfig): number | undefined; -export declare function framerateSet(self: MuxContainerGuardVideoConfig, value: number | undefined): void; +export declare function framerateGet(self: MuxContainerGuardVideoConfig): rt.f64 | undefined; +export declare function framerateSet(self: MuxContainerGuardVideoConfig, value: rt.f64 | undefined): void; export declare function reorderGet(self: MuxContainerGuardVideoConfig): number | undefined; export declare function reorderSet(self: MuxContainerGuardVideoConfig, value: number | undefined): void; export declare function optimizeForLatencyGet(self: MuxContainerGuardVideoConfig): boolean | undefined; @@ -6734,7 +6736,7 @@ import type { MuxSource } from "./MuxSource.ts"; export type MuxContainerTsRelease = rt.Handle<"MuxContainerTsRelease">; export declare function new$(source: MuxSource, catalogFormat: MuxCatalogCatalogFormat, delay: rt.Duration): rt.Call>; export declare function dropped(self: MuxContainerTsRelease): rt.u64; -export declare function drift(self: MuxContainerTsRelease): number | undefined; +export declare function drift(self: MuxContainerTsRelease): rt.f64 | undefined; export declare function outOfTolerance(self: MuxContainerTsRelease): rt.u64; export declare function stats(self: MuxContainerTsRelease): MuxContainerTsStatsRelease; export declare function next(self: MuxContainerTsRelease): rt.Call>; @@ -6849,8 +6851,8 @@ export declare function marginGet(self: MuxContainerTsStatsRelease): rt.i64 | un export declare function marginSet(self: MuxContainerTsStatsRelease, value: rt.i64 | undefined): void; export declare function offsetGet(self: MuxContainerTsStatsRelease): rt.i64 | undefined; export declare function offsetSet(self: MuxContainerTsStatsRelease, value: rt.i64 | undefined): void; -export declare function driftGet(self: MuxContainerTsStatsRelease): number | undefined; -export declare function driftSet(self: MuxContainerTsStatsRelease, value: number | undefined): void; +export declare function driftGet(self: MuxContainerTsStatsRelease): rt.f64 | undefined; +export declare function driftSet(self: MuxContainerTsStatsRelease, value: rt.f64 | undefined): void; export declare function outOfToleranceGet(self: MuxContainerTsStatsRelease): rt.u64; export declare function outOfToleranceSet(self: MuxContainerTsStatsRelease, value: rt.u64): void; export { default$ as default }; @@ -10187,7 +10189,7 @@ export declare function r_option_clock_Instant(p: number, o: boolean): ClockInst export declare function r_option_clock_Wall(p: number, o: boolean): ClockWall | undefined; export declare function r_option_datagram_Datagram(p: number, o: boolean): DatagramDatagram | undefined; export declare function r_option_duration(p: number, o: boolean): rt.Duration | undefined; -export declare function r_option_f64(p: number, o: boolean): number | undefined; +export declare function r_option_f64(p: number, o: boolean): rt.f64 | undefined; export declare function r_option_hang_catalog_AudioLayout(p: number, o: boolean): HangCatalogAudioLayout | undefined; export declare function r_option_hang_catalog_Clock(p: number, o: boolean): HangCatalogClock | undefined; export declare function r_option_hang_catalog_ColorRange(p: number, o: boolean): HangCatalogColorRange | undefined; @@ -10395,7 +10397,7 @@ export declare function w_option_bandwidth_Rate(p: number, v: BandwidthRate | un export declare function w_option_bool(p: number, v: boolean | undefined): void; export declare function w_option_clock_Instant(p: number, v: ClockInstant | undefined): void; export declare function w_option_duration(p: number, v: rt.Duration | undefined): void; -export declare function w_option_f64(p: number, v: number | undefined): void; +export declare function w_option_f64(p: number, v: rt.f64 | undefined): void; export declare function w_option_hang_catalog_AudioLayout(p: number, v: HangCatalogAudioLayout | undefined): void; export declare function w_option_hang_catalog_Clock(p: number, v: HangCatalogClock | undefined): void; export declare function w_option_hang_catalog_ColorRange(p: number, v: HangCatalogColorRange | undefined): void; @@ -10594,6 +10596,17 @@ type Num = number & { export type u64 = Num<"u64"> | U64Limbs; export type i64 = Num<"i64"> | I64Limbs; export type u128 = Num<"u128"> | U128Limbs; +export type F32Nan = { + readonly bits: number; + readonly [brand]: "f32"; +}; +export type F64Nan = { + readonly hi: number; + readonly lo: number; + readonly [brand]: "f64"; +}; +export type f32 = Num<"f32"> | F32Nan; +export type f64 = Num<"f64"> | F64Nan; export type Result = { readonly ok: true; readonly value: T; @@ -10711,7 +10724,12 @@ export declare function holdOr(handle: Handle | undefined): number; export declare function lentHandle(p: number, k: Kind): Handle; export declare function int(v: number, min: number, max: number, what: string): number; export declare function nonZero(v: T): T; -export declare function f32(v: number): number; +export declare function f32Get(p: number): f32; +export declare function f32Put(p: number, v: f32): void; +export declare function f64Get(p: number): f64; +export declare function f64Put(p: number, v: f64): void; +export declare function f32Number(v: f32): number; +export declare function f64Number(v: f64): number; export declare function char(v: number): number; export declare function u64Of(hi: number, lo: number): u64; export declare function i64Of(hi: number, lo: number): i64; diff --git a/ffi/tests/fixtures/shapes.rs b/ffi/tests/fixtures/shapes.rs index 4587e6a04..dce64f22d 100644 --- a/ffi/tests/fixtures/shapes.rs +++ b/ffi/tests/fixtures/shapes.rs @@ -206,7 +206,7 @@ fn is_ord(shape: &Shape) -> bool { ) } -/// The fixed set [`KNOWN`] checks: each leaf alone, each leaf in each wrapper, then `extra` +/// The fixed set: each leaf alone, each leaf in each wrapper, then `extra` /// sampled to depth 3 from a fixed seed. fn shapes(extra: usize) -> Vec { let leaves = leaves(); @@ -634,13 +634,6 @@ impl Feed for f32 { fn feed(&self, h: &mut Fnv) { use std::fmt::Write; let _ = write!(h, "f32 {:#x};", self.to_bits()); - // The transcript marks a call that digests a NaN other than the one a float's - // arithmetic makes: signaling, its quiet bit clear, or quiet with a payload or sign. - #[cfg(feature = "transcript")] - if self.is_nan() && self.to_bits() != f32::NAN.to_bits() { - let class = if self.to_bits() & 1 << 22 == 0 { "signaling" } else { "quiet" }; - println!("nan {class} f32"); - } } } @@ -648,11 +641,6 @@ impl Feed for f64 { fn feed(&self, h: &mut Fnv) { use std::fmt::Write; let _ = write!(h, "f64 {:#x};", self.to_bits()); - #[cfg(feature = "transcript")] - if self.is_nan() && self.to_bits() != f64::NAN.to_bits() { - let class = if self.to_bits() & 1 << 51 == 0 { "signaling" } else { "quiet" }; - println!("nan {class} f64"); - } } } @@ -960,10 +948,8 @@ const SEEDS: [u64; 3] = [0, 1, 0xdead_beef]; /// The shapes' crate, as the census left it: which shapes export, and the script. struct Crate { dir: PathBuf, - shapes: Vec, - /// How many of `shapes` are the fixed set, which [`KNOWN`] checks, and the seed the - /// rest are drawn from, if any. - drawn: Option<(usize, u64)>, + /// The seed the shapes past the fixed set are drawn from, if any. + drawn: Option, /// Each shape's Rust type, by `N`. types: Vec, calls: Vec, @@ -973,7 +959,7 @@ struct Crate { /// Write the crate of `shapes` named `name` in the scratch directory, ask the exporter what /// exports, and run its script natively. -fn emit(name: &str, shapes: &[Shape], drawn: Option<(usize, u64)>) -> Crate { +fn emit(name: &str, shapes: &[Shape], drawn: Option) -> Crate { let dir = scratch().join(name); let mut emit = Emit { items: String::new(), @@ -998,8 +984,7 @@ fn emit(name: &str, shapes: &[Shape], drawn: Option<(usize, u64)>) -> Crate { ); let manifest = format!( "[package]\nname = \"{name}\"\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\ - [dependencies]\nbytes = \"1\"\nserde_json = \"1\"\nurl = \"2\"\n\n\ - # Marks each call of the native script that digests a NaN of its own bits.\n[features]\ntranscript = []\n\n[workspace]\n" + [dependencies]\nbytes = \"1\"\nserde_json = \"1\"\nurl = \"2\"\n\n[workspace]\n" ); super::write_all(&dir, &[("Cargo.toml", &manifest), ("src/lib.rs", &lib)]); // The api fixture's versions, resolved offline. @@ -1080,12 +1065,11 @@ fn emit(name: &str, shapes: &[Shape], drawn: Option<(usize, u64)>) -> Crate { super::write_all(&dir, &[("examples/transcript.rs", &script)]); // Apart from the exporter's build, which cleans the crate each time. let transcript = run(cargo() - .args(["run", "--quiet", "--example", "transcript", "--features", "transcript"]) + .args(["run", "--quiet", "--example", "transcript"]) .env("CARGO_TARGET_DIR", dir.join("native")) .current_dir(&dir)); Crate { dir, - shapes: shapes.to_vec(), drawn, types, calls, @@ -1222,9 +1206,9 @@ struct Built { web: std::sync::OnceLock<(PathBuf, String)>, } -/// Emit, census, export, and build the crate of `shapes` named `name`, of which `drawn` -/// says how many are the fixed set and the seed of the rest, if any. -fn build(name: &str, shapes: &[Shape], drawn: Option<(usize, u64)>) -> Built { +/// Emit, census, export, and build the crate of `shapes` named `name`, those past the fixed +/// set drawn from the seed `drawn`, if any. +fn build(name: &str, shapes: &[Shape], drawn: Option) -> Built { let krate = emit(name, shapes, drawn); let out = krate.dir.join("out"); run(Command::new(env!("CARGO_BIN_EXE_rust2-ffi")) @@ -1290,14 +1274,7 @@ impl Built { .env("RUSTFLAGS", "-D warnings")); let native = self.krate.dir.join("native-web"); run(cargo() - .args([ - "build", - "--quiet", - "--example", - "transcript", - "--features", - "transcript", - ]) + .args(["build", "--quiet", "--example", "transcript"]) .args(["--target", super::WEB]) .env("CARGO_TARGET_DIR", &native) .current_dir(&self.krate.dir)); @@ -1331,46 +1308,15 @@ impl Built { self.krate.dir.join("drivers").join(language) } - /// The calls `language`'s driver runs, each with the line native Rust printed for it in - /// `transcript`: every call but those [`KNOWN`] says it gets wrong today, each entry of - /// which must match one of the fixed set's. - fn expected<'a>(&self, language: &str, transcript: &'a str) -> Vec<(Call, &'a str)> { - // Each call's line, after the NaNs it digested, as `signaling f32` or `quiet f64`. - let mut lines: Vec<(&str, Vec<&str>)> = Vec::new(); - let mut nans = Vec::new(); - for line in transcript.lines() { - match line.strip_prefix("nan ") { - Some(nan) => nans.push(nan), - None => lines.push((line, std::mem::take(&mut nans))), - } - } + /// The calls a driver runs, each with the line native Rust printed for it in `transcript`. + fn expected<'a>(&self, transcript: &'a str) -> Vec<(Call, &'a str)> { + let lines: Vec<&str> = transcript.lines().collect(); assert_eq!( lines.len(), self.krate.calls.len(), "the native script printed a line per call" ); - let entries: Vec<&Known> = KNOWN.iter().filter(|k| k.language == language).collect(); - let calls: Vec<(Call, &str, &[&str])> = (self.krate.calls.iter().zip(&lines)) - .map(|(call, (line, nans))| (*call, *line, nans.as_slice())) - .collect(); - let skipped = - |(call, _, nans): &(Call, &str, &[&str]), k: &Known| (k.skips)(&self.krate.shapes[call.n], call.kind, nans); - if let Some((fixed, _)) = self.krate.drawn { - for k in &entries { - // The gate's subset may hold none of an entry's calls; the nightly's fixed set - // holds each. - assert!( - calls.iter().any(|c| c.0.n < fixed && skipped(c, k)), - "KNOWN's {language} entry for {} ({}) matches no call: remove it", - k.what, - k.quest - ); - } - } - (calls.iter()) - .filter(|c| !entries.iter().any(|k| skipped(c, k))) - .map(|&(call, line, _)| (call, line)) - .collect() + self.krate.calls.iter().copied().zip(lines).collect() } /// Asserts `language`'s driver printed what native Rust did for each of `expected`, @@ -1395,7 +1341,7 @@ impl Built { } } let drawn = match self.krate.drawn { - Some((_, seed)) => format!("; shapes past the fixed set drawn from RUST2_SHAPES_SEED={seed}"), + Some(seed) => format!("; shapes past the fixed set drawn from RUST2_SHAPES_SEED={seed}"), None => String::new(), }; assert!( @@ -1409,54 +1355,6 @@ impl Built { } } -/// A round trip a binding gets wrong today. -struct Known { - language: &'static str, - what: &'static str, - /// Whether a call of a shape, which digested these NaNs natively, as `signaling f32` or - /// `quiet f64`, does what it gets wrong, which the language's driver skips. - skips: fn(&Shape, Kind, &[&str]) -> bool, - /// The quest that fixes it. - quest: &'static str, -} - -/// The round trips a binding gets wrong today, each the quest's to fix: the language, what -/// it gets wrong, the calls it skips for it, and the quest. The nightly fails on an entry -/// matching no call of its fixed set, but its calls stay skipped after a fix, so the quest -/// that fixes one deletes its entry. -const KNOWN: &[Known] = &[ - Known { - language: "python", - what: "a signaling NaN of an f32, which a float holds quieted", - skips: |shape, _, nans| nans.contains(&"signaling f32") && crosses(shape, &|s| matches!(s, Shape::F32)), - quest: "quest/m1/ffi-export/signaling-nan.md", - }, - Known { - language: "dart", - what: "a signaling NaN of an f32, which a double holds quieted", - skips: |shape, _, nans| nans.contains(&"signaling f32") && crosses(shape, &|s| matches!(s, Shape::F32)), - quest: "quest/m1/ffi-export/signaling-nan.md", - }, - Known { - language: "web", - what: "a NaN's bits, which a number loses: quieted, and canonicalized once V8 optimizes", - skips: |shape, _, nans| !nans.is_empty() && crosses(shape, &|s| matches!(s, Shape::F32 | Shape::F64)), - quest: "quest/m1/ffi-export/signaling-nan.md", - }, -]; - -/// Whether a value of `shape` holds a `leaf` the binding converts, which one inside an -/// object does not: the object crosses as a handle, and only Rust reads its field. -fn crosses(shape: &Shape, leaf: &dyn Fn(&Shape) -> bool) -> bool { - match shape { - Shape::Object(_) => false, - Shape::Option(s) | Shape::Vec(s) | Shape::Set(s) | Shape::Box(s) | Shape::Enum(s) => crosses(s, leaf), - Shape::Map(k, v) => crosses(k, leaf) || crosses(v, leaf), - Shape::Tuple(s) | Shape::Record(s) => s.iter().any(|s| crosses(s, leaf)), - s => leaf(s), - } -} - /// A function the C header declares: each parameter's type and name. struct Proto { params: Vec<(String, String)>, @@ -2775,7 +2673,7 @@ function plain(v: T): Plain { fn round_trip(built: &Built, language: &str) { // The test that drew the shapes may have passed, its message captured: each failing one // names the seed again. - if let Some((_, seed)) = built.krate.drawn { + if let Some(seed) = built.krate.drawn { eprintln!("shapes past the fixed set drawn from RUST2_SHAPES_SEED={seed}"); } let name = built.name(); @@ -2785,7 +2683,7 @@ fn round_trip(built: &Built, language: &str) { "web" => built.wasm_transcript(), _ => &built.krate.transcript, }; - let expected = built.expected(language, transcript); + let expected = built.expected(transcript); let calls: Vec = expected.iter().map(|(call, _)| *call).collect(); let calls = &calls[..]; let output = match language { @@ -2971,11 +2869,10 @@ fn sampled() -> &'static Built { static BUILT: std::sync::OnceLock = std::sync::OnceLock::new(); BUILT.get_or_init(|| { let mut all = shapes(SAMPLES); - let fixed = all.len(); let seed = day_seed(); eprintln!("the nightly's new shapes are drawn from seed {seed}: RUST2_SHAPES_SEED={seed} reproduces them"); all.extend(drawn(seed, SAMPLES)); - build("sampled", &all, Some((fixed, seed))) + build("sampled", &all, Some(seed)) }) } diff --git a/quest/m1/ffi-export/signaling-nan.md b/quest/m1/ffi-export/signaling-nan.md deleted file mode 100644 index 80cca2842..000000000 --- a/quest/m1/ffi-export/signaling-nan.md +++ /dev/null @@ -1,40 +0,0 @@ -# [M] NaN bits cross Python, Dart, and the web exactly - -## Goal - -Every `f32` and `f64` crosses each binding and returns with its exact bits, a NaN's -payload, sign, and quiet bit included. Today Python and Dart quiet an `f32`'s signaling -NaN, and the web binding loses any NaN's bits: it quiets a signaling NaN, and once V8 -optimizes the code it canonicalizes a quiet NaN's payload and sign too. The shape tests' -`KNOWN` entries for them are deleted. - -## Plan - -Decided with the maintainer on 2026-11-19, in the pick of [#879](https://github.com/kixel-ai/rust2/pull/879): ✅ keep it, and (a) the web holds floats as runtime `rt.f32`/`rt.f64` types that keep NaN bits, with checked conversion to and from `number`, as the lossless-types rule prescribes (rejected: (b) documenting that the web keeps no NaN's bits; dropping the quest). - -Found by the shape tests (`ffi/tests/fixtures/shapes.rs`), whose floats draw signaling and -quiet NaNs, and whose native transcript marks each call that digests a NaN of its own bits -(`nan signaling f32`, `nan quiet f64`), which `KNOWN` skips. AGENTS.md's lossless rule -allows a native type only where it is exact. - -Python's `float` and Dart's `double` are 64-bit, and widening an `f32` to them with the -hardware's conversion sets the quiet bit (`cvtss2sd` does), as narrowing back does. -Recommended: widen and narrow by bits instead, keeping the native type: an `f32` NaN's -payload shifted into an `f64` NaN's, its quiet bit kept, so the round trip is exact. - -On the web the engine is where the bits go, so no glue fix keeps them in a number. Run in a -loop under Node 24, `Holder246.swap(h, v)` with `v[0]` the quiet NaN `0x7ffde633` hands Rust -those bits for the first 8 iterations, then `0x7fc00000` once V8 tiers the code up; the -nightly for `RUST2_SHAPES_SEED=20261119` failed the same way on `held 246` and `held 253` -(`f64` `0xfffff56f76bde633`). `tests/api.ts` already keeps NaN bits out of numbers for -Deno's V8. - -## Waiting - -- Maintainer: how the web binding holds a float, since a number cannot keep a NaN's bits. - Options: (a) a runtime float, `rt.f32` and `rt.f64`, a number but for a NaN of its own - bits, which crosses as its bits, as `rt.u64` is a number but past a safe integer, - breaking for every web caller of a float; (b) floats stay numbers, and the README says - the web keeps no NaN's bits, which breaks the lossless rule for one value class. An agent - cannot pick, since it is a public API break against a rule only the maintainer may relax. - Recommended: (a), since V8 is where the bits are lost.