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§
- Array
Item - One item of an array literal or a call’s argument list, as
spread_arrayandspread_callread it: a value, or an established spread iterator (Any::get_iterator). - Object
Item - One entry of an object literal, as
spread_objectreads it: a property, a key and its value, or a spread whose entries the value gives (Any::object_spread).
Functions§
- bigint_
any - An
Anyholding the bigintv. - bigint_
any_ words - An
Anyholding the bigint of any size whose magnitude iswords, least significant word first, negated ifnegative: the spelling of a literal outsidei64, whichbigint_anycannot take. A zero magnitude is0nwhatever the sign, and leading zero words are dropped. - f64_any
- An
Anyholding the number whose IEEE 754 bits arev— the one spellingfjs/media/rustgives every number, so a generated module holds the double a JavaScript engine held, bit for bit,-0included, except aNaN: the language has one, so everyNaNarrives as the quiet, empty-payload one, whatever sign or payload the engine held it with. - number_
text vspelled as ECMAScript’sNumber::toStringdoes, the text a host puts in a message when it names a number a program passed.- property_
item - A property entry,
k: vin{k: v, ...o}. A computed key usesproperty_item(key.to_string()?, value), establishing its string conversion before the value argument is evaluated. The VM has no symbols, soAny::to_stringsuppliesToPropertyKeyfor its values. A plain object becomes"[object Object]"; an own callabletoStringsupplies the primitive, withvalueOfas 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
Resultwhere an array without a spread is a value. - spread_
call f(...)over an argument list holding a spread: the argumentsspread_arraybuilds, then the call, so a spread’s throw comes first, as JavaScript evaluates the arguments before it calls.- spread_
entries - A spread entry,
...oin{k: v, ...o}. - spread_
item - A spread item,
...bin[a, ...b]. Take its iterator before the next operand is evaluated: generated item lists propagate thisResultwith?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 aResult. - 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’sstaticfunction, called with the id — the one closure the runtime holds for it, so two reads are one function, asObject.is === Object.isis 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, andundefinedwould be a plausible wrong answer. A throw ofstaticitself is the read’s. - strict_
eq ===as an operator result.==onAnyis exactly JavaScript’s===, but it yields abooland so pins neither operand’sA; this gives both the same one and lifts the answer into theResultevery other operator returns, so a printed===is a statement like any other operator’s.- strict_
ne !==, the negation ofstrict_eq, for the same reason.- string_
any - An
Anyholding the stringv. - string_
any_ utf16 - An
Anyholding the string of UTF-16 code unitsv: the spelling of a string no&strcan 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,
ain[a, ...b].