/* * Copyright (c) 2026, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace JS::RustIntegration { // Result type for compile_script(). // NB: Uses GC::Root to prevent collection while the result is in transit // between compile_script() and the Script constructor. struct ScriptResult { GC::Root executable; bool is_strict_mode { false }; Vector lexical_names; Vector var_names; struct FunctionToInitialize { GC::Root shared_data; Utf16FlyString name; }; Vector functions_to_initialize; HashTable declared_function_names; Vector var_scoped_names; Vector annex_b_candidate_names; Vector lexical_bindings; }; // Result type for compile_eval() and compile_shadow_realm_eval(). // NB: Uses GC::Root to prevent collection while the result is in transit. struct EvalResult { GC::Root executable; bool is_strict_mode { false }; EvalDeclarationData declaration_data; }; // Result type for compile_module(). // NB: Uses GC::Root to prevent collection while the result is in transit. struct ModuleResult { bool has_top_level_await { false }; Vector requested_modules; Vector import_entries; Vector local_export_entries; Vector indirect_export_entries; Vector star_export_entries; Optional default_export_binding_name; Vector var_declared_names; Vector lexical_bindings; struct FunctionToInitialize { GC::Root shared_data; Utf16FlyString name; }; Vector functions_to_initialize; GC::Root executable; GC::Root tla_shared_data; }; // Compile a script. Returns nullopt if Rust is not available. Optional>> compile_script( StringView source_text, Realm& realm, StringView filename, size_t line_number_offset); // Compile eval code. Returns nullopt if Rust is not available. // On success, the executable's name is set to "eval". Optional> compile_eval( PrimitiveString& code_string, VM& vm, CallerMode strict_caller, bool in_function, bool in_method, bool in_derived_constructor, bool in_class_field_initializer); // Compile ShadowRealm eval code. Returns nullopt if Rust is not available. // On success, the executable's name is set to "ShadowRealmEval". Optional> compile_shadow_realm_eval( PrimitiveString& source_text, VM& vm); // Compile a module. Returns nullopt if Rust is not available. Optional>> compile_module( StringView source_text, Realm& realm, StringView filename); // Compile a dynamic function (new Function()). Returns nullopt if Rust is not available. // On success, returns a SharedFunctionInstanceData with source_text set. Optional, String>> compile_dynamic_function( VM& vm, StringView source_text, StringView parameters_string, StringView body_parse_string, FunctionKind kind); // Compile a builtin JS file. Returns nullopt if Rust is not available. Optional>> compile_builtin_file( unsigned char const* script_text, VM& vm); // Compile a function body for lazy compilation. // Returns nullptr if Rust is not available or the SFD doesn't use Rust compilation. GC::Ptr compile_function(VM& vm, SharedFunctionInstanceData& shared_data, bool builtin_abstract_operations_enabled); // Free a Rust function AST pointer. No-op if Rust is not available. void free_function_ast(void* ast); }