Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

5.3 KB
Raw Blame
mod attrs;
mod converter;
mod expand;
mod snapshot;

use proc_macro::TokenStream;
use syn::{DeriveInput, parse_macro_input};

/// Derives positional WebIDL argument parsing for V8 native binding structs.
///
/// The generated implementation reads fields from `v8::FunctionCallbackArguments`
/// and returns a Rust struct. Field attributes control required arguments,
/// explicit indexes, defaults, nullable values, variadic tails, custom
/// converters, and hand-written parser hooks.
/// `#[webidl(interface = Type)]` validates a V8 object using `Type::NAME` and
/// `Type::is_instance`, with an optional `brand_check = path` override. It
/// composes with optional/nullable fields and variadic object arguments.
/// Non-optional interface fields without a default convert at their field
/// position; `required` additionally enables the initial arity check.
/// `#[webidl(dictionary)]` delegates to a dictionary derive. Missing/nullish
/// values parse an empty dictionary; optional fields skip missing/undefined.
/// `#[webidl(sequence)]` converts one iterable to `Vec<T>`, using inferred or
/// explicit item conversion, including `interface` and `brand_check` metadata.
/// `sequence, converter = "dictionary"` converts each item as a dictionary.
#[proc_macro_derive(WebIdlArgs, attributes(webidl))]
pub fn derive_webidl_args(input: TokenStream) -> TokenStream {
    match expand::expand_webidl_args(parse_macro_input!(input as DeriveInput)) {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

/// Derives named WebIDL dictionary member parsing for Rust structs.
///
/// The generated implementation reads object properties with ordinary V8
/// property access so getter side effects and getter exceptions are preserved.
/// Field attributes control member names, required/default handling,
/// `nullable`, legacy nullish handling, explicit converters, and hand-written
/// member parser hooks. Unnamed fields use `camelCase` member names by default,
/// matching common WebIDL dictionary spelling.
/// Members are read and converted in lexicographical order of their final
/// JavaScript names, including `name` and `rename_all` overrides. Rust raw
/// identifiers such as `r#type` refer to the member `type`.
/// One `#[webidl(inherit)]` field delegates to its `WebIdlDictionary` type on
/// the same object before all own members, without reading a property for the
/// field. Inheritance cannot be combined with member/conversion attributes.
/// Object members support `interface = Type` and an optional `brand_check = path`
/// using the same native identity conversion as positional arguments.
/// Nested members support `dictionary`: null parses an empty dictionary while
/// `Option<T>` skips missing/undefined and `nullable` also skips null.
/// Iterable members support `sequence` with `Vec<T>` or `Option<Vec<T>>`.
/// `#[webidl(snapshot)]` also derives `WebIdlDictionarySnapshot` and
/// `WebIdlDictionarySerialize`. `to_js_snapshot` serializes converted Rust
/// fields; `to_snapshot` creates a typed, V8-traced private carrier for later
/// copies. Input getters and iterators are never consulted again.
/// Inheritance, nullable/optional values, sequences, records, buffers and
/// callback identity/context anchors are supported. Raw/custom converters use
/// `WebIdlSnapshotValue` to describe their canonical JS shape.
///
/// Field policies: `snapshot_with = path` overrides output conversion;
/// `snapshot_kind = expression` describes its copy policy; `snapshot_frozen`
/// freezes a sequence; `snapshot_default = expression` handles an absent Option;
/// `snapshot_order = integer` sets output order independently of input order.
/// `initialize_snapshot` writes existing backing state: `snapshot_slot = key`
/// maps a member to a private slot, and `snapshot_attributes = expression`
/// preserves state data-property attributes. Public dictionary output always
/// uses writable, enumerable, configurable WebIDL member properties.
/// `snapshot_state_with = path` overrides only native-state conversion, e.g.
/// storing an invocation carrier while snapshots expose the original callback.
/// Interface allocation, callback invocation and API validation remain with
/// their native owners; hooks implement special policies such as base64url
/// decoding or producing a branded platform object from a nested dictionary.
#[proc_macro_derive(WebIdlDictionary, attributes(webidl))]
pub fn derive_webidl_dictionary(input: TokenStream) -> TokenStream {
    match expand::expand_webidl_dictionary(parse_macro_input!(input as DeriveInput)) {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

/// Derives WebIDL enum token parsing.
///
/// Unit enum variants are converted to lowercase tokens by default. Container
/// `#[webidl(rename_all = "...")]` supports `lowercase`, `kebab-case`,
/// `camelCase`, and `none`, while variant-level `#[webidl(token = "...")]`
/// overrides the generated token. Types with custom parsing can use
/// `#[webidl(parse_with = path)]`, where the path returns `Option<Self>`.
#[proc_macro_derive(WebIdlEnum, attributes(webidl))]
pub fn derive_webidl_enum(input: TokenStream) -> TokenStream {
    match expand::expand_webidl_enum(parse_macro_input!(input as DeriveInput)) {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}