Skip to main content

Module unstable

Module unstable 

Source
Expand description

§Unstable helpers

Functions that shorten generated code. They are not part of the crate’s API: nothing here is stable, and any of them may change, move, or disappear in any release.

fjs compile <input> <output>.rs prints a FunctionalScript module as Rust that depends on this crate and nothing else, and the operator corpus in tests/test/gen.corpus/ is printed by the same printer, so what both would otherwise repeat is defined here, once, rather than copied into every generated file. The module is also how the crate learns what writing code against the VM API actually needs: a helper that keeps earning its calls is a candidate for the vm API proper; one that does not is dropped.

Anyone — a person or an agent — adding to the printers is encouraged to add a helper here and print a call to it, rather than printing the same expression again. Each is a plain function on purpose: Any stays simple, and a generated module needs no sugar.

Today’s helpers are the values a literal becomes, each pinning the A a bare into() cannot infer at the point of use, and the two equality operators, === and !==: their nanvm-lib form is PartialEq, which answers a bool, and generated code wants the Result<Any<A>, Any<A>> every other operator returns. An array spread has four: spread_array builds the array an item list holds, [a, ...b], as spread_array([value_item(a), spread_item(b)?]), and spread_call calls with such a list. spread_item establishes its iterator immediately and returns a Result, so generated code propagates a non-iterable operand’s failure before evaluating the next item. spread_array and spread_call also return a Result. Arrays and strings are the only iterables, and stepping their established iterators cannot fail or run represented code. Array construction retains its length and allocation checks after item evaluation. An object’s, {k: v, ...o}, is spread_object([property_item(k, v), spread_entries(o)]), a value, since an object spread never throws (Any::object_spread).

A computed property uses property_item((key).to_string()?, value). Any::to_string converts the key before Rust starts the value argument and propagates conversion failure at that position. property_item takes the established VM string directly, for both literal and computed keys.

Enums§

ArrayItem
One item of an array literal or a call’s argument list, as spread_array and spread_call read it: a value, or an established spread iterator (Any::get_iterator).
ObjectItem
One entry of an object literal, as spread_object reads it: a property, a key and its value, or a spread whose entries the value gives (Any::object_spread).

Functions§

bigint_any
An Any holding the bigint v.
bigint_any_words
An Any holding the bigint of any size whose magnitude is words, least significant word first, negated if negative: the spelling of a literal outside i64, which bigint_any cannot take. A zero magnitude is 0n whatever the sign, and leading zero words are dropped.
f64_any
An Any holding the number whose IEEE 754 bits are v — the one spelling fjs/media/rust gives every number, so a generated module holds the double a JavaScript engine held, bit for bit, -0 included, except a NaN: the language has one, so every NaN arrives as the quiet, empty-payload one, whatever sign or payload the engine held it with.
number_text
v spelled as ECMAScript’s Number::toString does, the text a host puts in a message when it names a number a program passed.
property_item
A property entry, k: v in {k: v, ...o}. A computed key uses property_item(key.to_string()?, value), establishing its string conversion before the value argument is evaluated. The VM has no symbols, so Any::to_string supplies ToPropertyKey for its values. A plain object becomes "[object Object]"; an own callable toString supplies the primitive, with valueOf as the fallback if needed. A throw or failure to produce a primitive propagates before the value starts.
spread_array
The array an item list holds, a spread among them: each value an element, and each spread every value its operand iterates, in order. A spread of a value that is not iterable throws, so the array is an operation’s Result where an array without a spread is a value.
spread_call
f(...) over an argument list holding a spread: the arguments spread_array builds, then the call, so a spread’s throw comes first, as JavaScript evaluates the arguments before it calls.
spread_entries
A spread entry, ...o in {k: v, ...o}.
spread_item
A spread item, ...b in [a, ...b]. Take its iterator before the next operand is evaluated: generated item lists propagate this Result with ? at the item’s position. Iteration itself cannot throw or run represented code for the VM’s immutable arrays and strings (IteratorRecord).
spread_object
The object an entry list holds, a spread among them: each property and each spread’s entries appended in order to the raw property list, so a later key overwrites an earlier one’s value and keeps its position, as JavaScript’s does, through the view every reader takes (Object::own_entries). An object spread never throws, so this is a value, not a Result.
static_read
A static function read from the runtime value by its id, Object.is: what a compiled module’s read of a static spells, static_read(&runtime, "Object.is")? — the value’s static function, called with the id — the one closure the runtime holds for it, so two reads are one function, as Object.is === Object.is is in JavaScript. An id the runtime answers no function for is refused ([error::no_static_function]): the parser admitted the id, so the runtime’s table is incomplete, and undefined would be a plausible wrong answer. A throw of static itself is the read’s.
strict_eq
=== as an operator result. == on Any is exactly JavaScript’s ===, but it yields a bool and so pins neither operand’s A; this gives both the same one and lifts the answer into the Result every other operator returns, so a printed === is a statement like any other operator’s.
strict_ne
!==, the negation of strict_eq, for the same reason.
string_any
An Any holding the string v.
string_any_utf16
An Any holding the string of UTF-16 code units v: the spelling of a string no &str can hold, one with a lone surrogate.
string_key
An object property key.
string_key_utf16
An object property key of UTF-16 code units, for the same reason.
value_item
An item that is its own value, a in [a, ...b].