quickjs-pure 0.17.1

Embeddable JavaScript engine written in pure Rust (QuickJS-compatible)
Documentation
//! The global functions and `console`.
//!
//! `print` and `console` are host additions (the reference's CLI provides them), so
//! they live here with the language-defined globals rather than in a separate host
//! module.

use crate::builtins::number;
use crate::builtins::{arg, define_method};
use crate::interp::{Interp, JsResult};
use crate::object::{JsObject, NativeFn, Property};
use crate::value::Value;
use js_gc::Gc;

/// Install the global functions, constants and `console`.
pub fn install(interp: &mut Interp<'_>, boolean_proto: &Gc<JsObject>) {
    number::install_boolean(interp, boolean_proto);
    let global = interp.realm.global.clone();

    // §21.1.2.13/§21.1.2.12: the global `parseInt`/`parseFloat` are the *same function
    // objects* as `Number.parseInt`/`Number.parseFloat`, so they are taken from the Number
    // constructor rather than defined twice. `Number` is installed before this runs
    // (`builtins::mod`), and both are fallible only if `Number` were missing.
    for (name, length, func) in [
        ("isNaN", 1, number::global_is_nan as NativeFn),
        ("isFinite", 1, number::global_is_finite),
        ("eval", 1, eval),
        // The host-facing collector hook. Not a language global: it is installed under a
        // double-underscore name and the `$262` harness object exposes it as `gc`, which is
        // how a test asks for a collection. A script has no other way to request one, and
        // the reference's `$262.gc` is the same arrangement.
        ("__gc", 0, gc),
        ("encodeURIComponent", 1, encode_uri_component),
        ("decodeURIComponent", 1, decode_uri_component),
        ("encodeURI", 1, encode_uri),
        ("decodeURI", 1, decode_uri),
        // `escape`/`unescape` are Annex B, not removed: they are still in every engine and
        // test262 checks them (`test/built-ins/escape`, `…/unescape`).
        ("escape", 1, escape),
        ("unescape", 1, unescape),
    ] {
        define_method(&global, name, length, func, interp);
    }
    for name in ["parseInt", "parseFloat"] {
        let value = interp
            .realm
            .global
            .own_property("Number")
            .and_then(|property| property.value)
            .and_then(|value| value.as_object().cloned())
            .and_then(|number| number.own_property(name))
            .and_then(|property| property.value);
        if let Some(value) = value {
            global.define(name, Property::with_attributes(value, true, false));
        }
    }
    // `eval` is special-cased in the reference: a *direct* `eval(...)` compiles to an
    // opcode rather than a call, so it never appears as a frame in a stack trace.
    // Ours reaches the same native through an ordinary call, so the marker tells the
    // interpreter to leave that frame out and keep the trace identical.
    if let Some(property) = global.own_property("eval")
        && let Some(Value::Object(eval_fn)) = property.value
    {
        eval_fn.define(
            "_frame_less",
            Property::with_attributes(Value::Bool(true), false, false),
        );
    }

    // `print` and `console` are what the CLI adds; nothing in the language needs
    // them, but every script in the reference's test suite uses them.
    let print = interp.new_native("print", 0, js_print);
    global.define_data("print", Value::Object(print.clone()));
    let console = interp.new_object(Some(interp.realm.object_proto.clone()));
    for name in ["log", "info", "debug", "warn", "error"] {
        console.define_data(name, Value::Object(print.clone()));
    }
    global.define_data("console", Value::Object(console));
    global.define_data("globalThis", Value::Object(global.clone()));

    for (name, value) in [
        ("undefined", Value::Undefined),
        ("NaN", Value::Float(f64::NAN)),
        ("Infinity", Value::Float(f64::INFINITY)),
    ] {
        global.define(name, Property::with_attributes(value, false, false));
    }
}

/// `print(...)`: join the arguments with a space and write a line.
///
/// `undefined` is the result, so `print(print(1))` prints two lines.
fn js_print(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let mut line = String::new();
    for (index, argument) in args.iter().enumerate() {
        if index > 0 {
            line.push(' ');
        }
        line.push_str(&crate::interp::debug_display(interp, argument));
    }
    line.push('\n');
    interp.output.push_str(&line);
    // Straight through to the host, flushed per line, so output is visible as it happens
    // rather than when the script ends. Dropping the error is deliberate: `print` cannot
    // report a broken pipe to the script, and the reference ignores write failures too.
    if let Some(sink) = interp.output_sink.as_mut() {
        let _ = sink.write_all(line.as_bytes());
        let _ = sink.flush();
    }
    Ok(Value::Undefined)
}

/// `eval(source)`.
///
/// Direct `eval` is specified to see the calling scope, which needs the compiler to
/// emit a variable-environment hand-off. This implementation evaluates in global
/// scope only, which is correct for `eval("1+1")` — what the test suite uses — and
/// wrong for a direct eval that closes over local variables.
fn eval(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let source = match args.first() {
        Some(Value::String(text)) => text.to_string_lossy().into_owned(),
        // Anything that is not a string is returned unchanged, per the spec.
        Some(other) => return Ok(other.clone()),
        None => return Ok(Value::Undefined),
    };
    interp.eval_in_global(&source)
}

/// Collect unreachable cycles and drain the finalizer queue.
///
/// Reference counting already reclaimed everything with no live handle; this only matters
/// for a *cycle*, and for giving `FinalizationRegistry` a safe point to run its callbacks
/// from. It is what `$262.gc()` in the test harness ends up calling.
fn gc(interp: &mut Interp<'_>, _this: Value, _args: &[Value]) -> JsResult<Value> {
    interp.heap.collect();
    crate::builtins::weak::drain_finalizers(interp)?;
    Ok(Value::Undefined)
}

/// `encodeURIComponent`, percent-encoding everything outside the unreserved set.
fn encode_uri_component(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let text = crate::builtins::to_string_wtf8(interp, &arg(args, 0))?;
    let encoded = percent_encode(interp, &text, false)?;
    Ok(interp.string(encoded))
}

/// `encodeURI`, which additionally leaves the URI's own delimiters alone.
///
/// §19.2.6.3's `uriReserved` plus `#`: `;/?:@&=+$,#`. The difference from
/// `encodeURIComponent` is exactly this set, and it is why `encodeURI("a b/c")` is
/// `"a%20b/c"` while the component form gives `"a%20b%2Fc"`.
fn encode_uri(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let text = crate::builtins::to_string_wtf8(interp, &arg(args, 0))?;
    let encoded = percent_encode(interp, &text, true)?;
    Ok(interp.string(encoded))
}

/// The shared percent-encoder. `keep_reserved` is the only difference between the two.
///
/// It walks **code points**, not bytes, because §19.2.6.5's `Encode` step 3 asks whether the
/// code point is a *lone surrogate* and throws a `URIError` if it is: an unpaired surrogate
/// has no UTF-8 form, so `encodeURIComponent("\ud800")` is an error rather than
/// `%EF%BF%BD`. The input is `Wtf8` for exactly that reason — a `String` would have folded
/// the surrogate into U+FFFD before this could look.
fn percent_encode(
    interp: &mut Interp<'_>,
    text: &crate::value::Wtf8,
    keep_reserved: bool,
) -> JsResult<String> {
    let mut out = String::new();
    for point in text.code_points() {
        let units = point.to_units();
        if units.len() == 1 && (0xD800..0xE000).contains(&units[0]) {
            return Err(uri_error(interp));
        }
        for byte in point.as_bytes() {
            let unreserved = byte.is_ascii_alphanumeric()
                || matches!(
                    byte,
                    b'-' | b'_' | b'.' | b'!' | b'~' | b'*' | b'\'' | b'(' | b')'
                );
            let reserved = keep_reserved
                && matches!(
                    byte,
                    b';' | b'/' | b'?' | b':' | b'@' | b'&' | b'=' | b'+' | b'$' | b',' | b'#'
                );
            if unreserved || reserved {
                out.push(*byte as char);
            } else {
                out.push_str(&format!("%{byte:02X}"));
            }
        }
    }
    Ok(out)
}

/// `decodeURIComponent`.
fn decode_uri_component(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let text = crate::builtins::to_string_wtf8(interp, &arg(args, 0))?;
    decode_percent(interp, &text, false)
}

/// `decodeURI`, which leaves a percent-encoded reserved character encoded.
///
/// §19.2.6.2: a decoded byte that is one of `;/?:@&=+$,#` is put back in its `%XX` form,
/// so `decodeURI("%2F")` is `"%2F"` while `decodeURIComponent("%2F")` is `"/"`.
fn decode_uri(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let text = crate::builtins::to_string_wtf8(interp, &arg(args, 0))?;
    decode_percent(interp, &text, true)
}

/// The shared percent-decoder: `keep_reserved` is `decodeURI`'s extra re-encoding step.
///
/// Two things make this awkward enough to spell out:
///
/// * **a run of escapes is decoded as one UTF-8 sequence.** `%E4%B8` is a truncated
///   sequence and `%ED%A0%80` decodes to a surrogate — both are `URIError`, and neither
///   can be judged one byte at a time. The run is therefore held in `pending` and only
///   flushed once a non-`%` byte (or the end) arrives;
/// * **everything that is not an escape is copied byte for byte.** Going through a Rust
///   `String` folded an unpaired surrogate into U+FFFD, so
///   `decodeURIComponent(String.fromCharCode(0xD800))` no longer answered its argument
///   (`S15.1.3.2_A2.1_T1` walks all 65536 code units). The input is `Wtf8`, and the copy
///   moves whole sequences so an unpaired surrogate's three bytes stay together.
fn decode_percent(
    interp: &mut Interp<'_>,
    text: &crate::value::Wtf8,
    keep_reserved: bool,
) -> JsResult<Value> {
    let bytes = text.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    // Each escape is held with the **span it came from**, because `decodeURI` re-emits a
    // reserved character as it was written: `decodeURI("%3b")` is `"%3b"`, keeping the
    // lower case (`S15.1.3.1_A3_T2`/`_T3`). Reformatting to upper case looked right and
    // failed four cases.
    let mut pending: Vec<(u8, usize, usize)> = Vec::new();
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] != b'%' {
            flush_escapes(interp, &mut pending, &mut out, keep_reserved, bytes)?;
            let width = sequence_width(bytes[index]);
            let end = (index + width).min(bytes.len());
            out.extend_from_slice(&bytes[index..end]);
            index = end;
            continue;
        }
        let (byte, next) = read_escape(bytes, index).ok_or_else(|| uri_error(interp))?;
        pending.push((byte, index, next));
        index = next;
    }
    flush_escapes(interp, &mut pending, &mut out, keep_reserved, bytes)?;
    // The whole thing is WTF-8 by construction: valid UTF-8 from the decoded runs, and
    // whole sequences from the verbatim copy.
    match crate::value::Wtf8::from_wtf8_bytes(&out) {
        Some(decoded) => Ok(interp.string_wtf8(decoded)),
        None => Err(uri_error(interp)),
    }
}

/// Decode one run of `%XX` bytes as a UTF-8 sequence and append it.
fn flush_escapes(
    interp: &mut Interp<'_>,
    pending: &mut Vec<(u8, usize, usize)>,
    out: &mut Vec<u8>,
    keep_reserved: bool,
    source: &[u8],
) -> JsResult<()> {
    if pending.is_empty() {
        return Ok(());
    }
    let decoded_bytes: Vec<u8> = pending.iter().map(|(byte, _, _)| *byte).collect();
    let decoded = std::str::from_utf8(&decoded_bytes).map_err(|_| uri_error(interp))?;
    let mut offset = 0;
    for character in decoded.chars() {
        if keep_reserved && character.is_ascii() && is_uri_reserved(character as u8) {
            let (_, start, end) = pending[offset];
            out.extend_from_slice(&source[start..end]);
        } else {
            out.extend_from_slice(character.to_string().as_bytes());
        }
        offset += character.len_utf8();
    }
    pending.clear();
    Ok(())
}

/// `uriReserved` plus `#`, which `decodeURI` leaves escaped.
fn is_uri_reserved(byte: u8) -> bool {
    matches!(
        byte,
        b';' | b'/' | b'?' | b':' | b'@' | b'&' | b'=' | b'+' | b'$' | b',' | b'#'
    )
}

/// The byte length of the WTF-8 sequence a leading byte starts, so the verbatim copy can
/// move a whole code point (or a whole unpaired surrogate) at a time.
fn sequence_width(lead: u8) -> usize {
    match lead {
        0x00..=0x7F => 1,
        0xC0..=0xDF => 2,
        0xE0..=0xEF => 3,
        0xF0..=0xF7 => 4,
        // A continuation byte cannot start a sequence; one byte is the safe answer.
        _ => 1,
    }
}

/// One `%XX` escape at `index`, answering the byte and the index just past it.
///
/// The two digits are checked *as digits*. `u8::from_str_radix` accepts a leading `+`, so
/// `decodeURIComponent("%+1")` decoded to `0x01` instead of raising a `URIError` — the one
/// character `S15.1.3.2_A1.2_T1` catches out of its 65536-byte sweep.
fn read_escape(bytes: &[u8], index: usize) -> Option<(u8, usize)> {
    let high = hex_digit(*bytes.get(index + 1)?)?;
    let low = hex_digit(*bytes.get(index + 2)?)?;
    Some((high * 16 + low, index + 3))
}

/// One hexadecimal digit.
fn hex_digit(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'a'..=b'f' => Some(byte - b'a' + 10),
        b'A'..=b'F' => Some(byte - b'A' + 10),
        _ => None,
    }
}

/// `escape(string)` (Annex B §B.2.1.1).
///
/// Not a URI function at all, whatever the name suggests: it works on *code units*. A unit
/// that is ASCII alphanumeric or one of `@*_+-./` is kept, one below 256 becomes `%XX`, and
/// anything wider becomes `%uXXXX`. That is why `escape("\u00FF")` is `"%FF"` — a single
/// byte, not the UTF-8 pair `encodeURIComponent` would produce.
fn escape(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let text = arg(args, 0).to_js_string();
    let mut out = String::new();
    for unit in text.encode_utf16() {
        let kept = unit < 0x80
            && (char::from(unit as u8).is_ascii_alphanumeric()
                || matches!(unit as u8, b'@' | b'*' | b'_' | b'+' | b'-' | b'.' | b'/'));
        if kept {
            out.push(unit as u8 as char);
        } else if unit < 0x100 {
            out.push_str(&format!("%{unit:02X}"));
        } else {
            out.push_str(&format!("%u{unit:04X}"));
        }
    }
    Ok(interp.string(out))
}

/// `unescape(string)` (Annex B §B.2.1.2): the inverse of [`escape`].
///
/// Only `%uXXXX` and `%XX` are recognised; any other `%` is left as itself rather than
/// being a `URIError`, which is the one way this differs from `decodeURIComponent`.
fn unescape(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let text = arg(args, 0).to_js_string();
    let units: Vec<u16> = text.encode_utf16().collect();
    let mut out: Vec<u16> = Vec::with_capacity(units.len());
    let mut index = 0;
    while index < units.len() {
        if units[index] != b'%' as u16 {
            out.push(units[index]);
            index += 1;
            continue;
        }
        // `%uXXXX` first: its `u` would otherwise make it an invalid `%XX`.
        if units.get(index + 1) == Some(&(b'u' as u16)) {
            if let Some(hex) = units.get(index + 2..index + 6) {
                let digits: String = hex.iter().map(|unit| *unit as u8 as char).collect();
                if let Ok(unit) = u16::from_str_radix(&digits, 16) {
                    out.push(unit);
                    index += 6;
                    continue;
                }
            }
        } else if let Some(hex) = units.get(index + 1..index + 3) {
            let digits: String = hex.iter().map(|unit| *unit as u8 as char).collect();
            if let Ok(byte) = u8::from_str_radix(&digits, 16) {
                out.push(byte as u16);
                index += 3;
                continue;
            }
        }
        // Not an escape: the `%` stands for itself. `unescape("%")` is `"%"`.
        out.push(units[index]);
        index += 1;
    }
    Ok(Value::String(js_gc::Gc::new(
        interp.heap,
        crate::value::JsString::from_wtf8(crate::value::Wtf8::from_units(&out)),
    )))
}

/// The `URIError` a malformed percent-escape raises.
fn uri_error(interp: &Interp<'_>) -> crate::interp::Signal {
    crate::interp::Signal::Throw(interp.make_error("URIError", "malformed URI sequence"))
}