### Basic Runtime Lifecycle Source: https://hexdocs.pm/quickbeam Demonstrates starting a runtime, evaluating JavaScript code, and stopping the runtime. ```elixir iex> {:ok, rt} = QuickBEAM.start() iex> {:ok, 3} = QuickBEAM.eval(rt, "1 + 2") iex> QuickBEAM.stop(rt) :ok ``` -------------------------------- ### QuickBEAM.start/1 Source: https://hexdocs.pm/quickbeam Starts a new JavaScript runtime instance with configurable options for memory, stack size, and environment APIs. ```APIDOC ## QuickBEAM.start(opts \\ []) ### Description Starts a new JavaScript runtime. Returns `{:ok, pid}` on success. ### Parameters - **opts** (keyword) - Optional - Configuration options including :name, :handlers, :script, :apis, :define, :memory_limit, :max_stack_size, :wasm_stack_size, :wasm_heap_size, :max_convert_depth, and :max_convert_nodes. ``` -------------------------------- ### Start a new JavaScript runtime Source: https://hexdocs.pm/quickbeam Initializes a new JS runtime with configurable options for memory, stack size, and API surfaces. ```elixir @spec start(keyword()) :: GenServer.on_start() ``` ```elixir QuickBEAM.start(script: "build.ts", define: %{"outputDir" => "/tmp/site"}) ``` -------------------------------- ### Get memory usage statistics Source: https://hexdocs.pm/quickbeam Returns a map of current QuickJS memory usage metrics. ```elixir @spec memory_usage(runtime()) :: map() ``` -------------------------------- ### Supervising Runtimes Source: https://hexdocs.pm/quickbeam Configures a QuickBEAM runtime as a child process within an OTP supervision tree. ```elixir children = [ {QuickBEAM, name: :app, script: "priv/js/app.js", handlers: %{...}}, ] Supervisor.start_link(children, strategy: :one_for_one) ``` -------------------------------- ### load_addon(runtime, path, opts) Source: https://hexdocs.pm/quickbeam Loads a native .node addon via N-API and returns its exports. ```APIDOC ## load_addon(runtime, path, opts) ### Description Load a native addon (.node file) via N-API. The addon is loaded with dlopen and its entry point is called. ### Signature `load_addon(runtime(), String.t(), keyword()) :: {:ok, term()} | {:error, term()}` ### Options - **:as** (String.t) - Optional - Set the addon's exports as a global JS variable with this name. ``` -------------------------------- ### memory_usage(runtime) Source: https://hexdocs.pm/quickbeam Returns QuickJS memory usage statistics. ```APIDOC ## memory_usage(runtime) ### Description Return QuickJS memory usage statistics. ### Signature `memory_usage(runtime()) :: map()` ``` -------------------------------- ### info(runtime) Source: https://hexdocs.pm/quickbeam Returns runtime diagnostics including registered handlers, memory stats, and JS global count. ```APIDOC ## info(runtime) ### Description Return runtime diagnostics: registered handlers, memory stats, and JS global count. ### Signature `info(runtime()) :: map()` ``` -------------------------------- ### load_bytecode(runtime, bytecode) Source: https://hexdocs.pm/quickbeam Executes precompiled bytecode within the runtime context. ```APIDOC ## load_bytecode(runtime, bytecode) ### Description Execute precompiled bytecode from compile/2. The bytecode runs in the current runtime's context, with access to all globals, handlers, and builtins. ### Signature `load_bytecode(runtime(), binary()) :: js_result()` ``` -------------------------------- ### Load a native addon Source: https://hexdocs.pm/quickbeam Loads a .node file via N-API and returns its exports. Use the :as option to expose exports as a global JS variable. ```elixir @spec load_addon(runtime(), String.t(), keyword()) :: {:ok, term()} | {:error, term()} ``` ```elixir QuickBEAM.load_addon(rt, "/path/to/addon.node") QuickBEAM.load_addon(rt, "/path/to/crc32.node", as: "crc32") QuickBEAM.eval(rt, "crc32.crc32('hello')") ``` -------------------------------- ### load_module(runtime, name, code) Source: https://hexdocs.pm/quickbeam Loads an ES module into the runtime. ```APIDOC ## load_module(runtime, name, code) ### Description Load an ES module into the runtime. ### Signature `load_module(runtime(), String.t(), String.t()) :: :ok | {:error, String.t()}` ``` -------------------------------- ### Execute precompiled bytecode Source: https://hexdocs.pm/quickbeam Runs bytecode generated by compile/2 within the current runtime context. ```elixir @spec load_bytecode(runtime(), binary()) :: js_result() ``` -------------------------------- ### call(runtime, fn_name, args, opts) Source: https://hexdocs.pm/quickbeam Calls a global JavaScript function by name within the specified runtime. Arguments are converted to JS values, and the result is returned to Elixir. ```APIDOC ## call(runtime, fn_name, args, opts) ### Description Call a global JavaScript function by name. Arguments are converted to JS values; the return value is converted back. Promise-returning functions are automatically awaited. ### Parameters - **runtime** (runtime()) - Required - The server process running the JS context. - **fn_name** (String.t()) - Required - The name of the global JS function to call. - **args** (list()) - Optional - List of arguments to pass to the function. - **opts** (keyword()) - Optional - Options including :timeout (maximum execution time in milliseconds). ### Response - **js_result()** ({:ok, term()} | {:error, QuickBEAM.JSError.t()}) - The result of the function call or an error. ``` -------------------------------- ### globals(runtime, opts) Source: https://hexdocs.pm/quickbeam Lists global names defined in the JavaScript context. ```APIDOC ## globals(runtime, opts) ### Description List global names defined in the JS context. By default returns all globalThis property names. ### Parameters - **runtime** (runtime()) - Required - The QuickBEAM runtime instance. - **opts** (keyword()) - Optional - Options including user_only: true to exclude JS builtins and QuickBEAM internals. ### Returns - **{:ok, [String.t()]} | {:error, QuickBEAM.JSError.t()}** - A list of global names or an error. ``` -------------------------------- ### QuickBEAM Module Functions Source: https://hexdocs.pm/quickbeam Core functions for managing JavaScript runtimes, evaluating code, and interacting with the JS context. ```APIDOC ## QuickBEAM.start(opts \\ []) ### Description Starts a new JavaScript runtime as a GenServer. ### Parameters - **opts** (Keyword list) - Optional - Configuration options including :name, :id, :handlers, :script, :memory_limit, :max_stack_size, :wasm_stack_size, :wasm_heap_size, :max_convert_depth, and :max_convert_nodes. ## QuickBEAM.eval(runtime, code, opts \\ []) ### Description Evaluates JavaScript code within the specified runtime and returns the result. ## QuickBEAM.call(runtime, fn_name, args \\ [], opts \\ []) ### Description Calls a global JavaScript function by name within the runtime. ## QuickBEAM.stop(runtime) ### Description Stops a runtime and frees its resources. ## QuickBEAM.dom_find(runtime, selector) ### Description Finds the first element matching a CSS selector in the runtime's DOM, returning a Floki-compatible tuple. ## QuickBEAM.set_global(runtime, name, value) ### Description Sets a JavaScript global variable from Elixir. ``` -------------------------------- ### Retrieve runtime diagnostics Source: https://hexdocs.pm/quickbeam Returns a map containing registered handlers, memory statistics, and the count of JS globals. ```elixir @spec info(runtime()) :: map() ``` -------------------------------- ### Serialize DOM to HTML Source: https://hexdocs.pm/quickbeam Serializes the entire DOM tree to an HTML string. ```elixir @spec dom_html(runtime()) :: {:ok, String.t()} ``` ```elixir {:ok, rt} = QuickBEAM.start() QuickBEAM.eval(rt, "document.body.innerHTML = '
Hello
'") {:ok, html} = QuickBEAM.dom_html(rt) ``` -------------------------------- ### dom_html(runtime) Source: https://hexdocs.pm/quickbeam Serializes the entire DOM tree to an HTML string. ```APIDOC ## dom_html(runtime) ### Description Serializes the entire DOM tree to an HTML string. ### Signature `dom_html(runtime()) :: {:ok, String.t()}` ### Example ```elixir {:ok, rt} = QuickBEAM.start() QuickBEAM.eval(rt, "document.body.innerHTML = 'Hello
'") {:ok, html} = QuickBEAM.dom_html(rt) ``` ``` -------------------------------- ### Compile JavaScript to bytecode Source: https://hexdocs.pm/quickbeam Converts JavaScript source code into a binary format for precompilation or caching. ```elixir @spec compile(runtime(), String.t()) :: {:ok, binary()} | {:error, QuickBEAM.JSError.t()} ``` -------------------------------- ### Load an ES module Source: https://hexdocs.pm/quickbeam Loads and registers an ES module string into the runtime. ```elixir @spec load_module(runtime(), String.t(), String.t()) :: :ok | {:error, String.t()} ``` ```elixir iex> {:ok, rt} = QuickBEAM.start() iex> code = "export function add(a, b) { return a + b; }" iex> QuickBEAM.load_module(rt, "math", code) :ok iex> QuickBEAM.stop(rt) :ok ``` -------------------------------- ### QuickBEAM.send_message/2 Source: https://hexdocs.pm/quickbeam Sends a message to the JavaScript runtime's message handler. ```APIDOC ## QuickBEAM.send_message(runtime, message) ### Description Send a message to the runtime's JS handler. The message is delivered to the callback registered via `Beam.onMessage` in JS. ### Parameters - **runtime** (runtime()) - Required - The runtime process identifier. - **message** (term()) - Required - The message to send. ``` -------------------------------- ### dom_find_all(runtime, selector) Source: https://hexdocs.pm/quickbeam Finds all elements matching a CSS selector in the runtime's DOM. ```APIDOC ## dom_find_all(runtime, selector) ### Description Find all elements matching a CSS selector in the runtime's DOM. Returns a list of Floki-compatible `{tag, attrs, children}` tuples. ### Signature `dom_find_all(runtime(), String.t()) :: {:ok, list()}` ### Example ```elixir {:ok, rt} = QuickBEAM.start() QuickBEAM.eval(rt, ~s[document.body.innerHTML = 'Hello
'") {:ok, {"p", [{"class", "intro"}], ["Hello"]}} = QuickBEAM.dom_find(rt, "p.intro") ``` ``` -------------------------------- ### eval(runtime, code, opts) Source: https://hexdocs.pm/quickbeam Evaluates JavaScript code and returns the result, with support for top-level await. ```APIDOC ## eval(runtime, code, opts) ### Description Evaluate JavaScript code and return the result. Top-level `await` is supported. ### Signature `eval(runtime(), String.t(), keyword()) :: js_result()` ### Options - **timeout** (integer) - Maximum execution time in milliseconds. If exceeded, execution is interrupted. - **vars** (map) - A map of variable names to values, available in the code as globals. ### Example ```elixir {:ok, rt} = QuickBEAM.start() QuickBEAM.eval(rt, "40 + 2") # => {:ok, 42} QuickBEAM.eval(rt, "name.toUpperCase()", vars: %{"name" => "quickbeam"}) # => {:ok, "QUICKBEAM"} ``` ``` -------------------------------- ### Define js_result and runtime types Source: https://hexdocs.pm/quickbeam Type definitions for JavaScript execution results and the runtime server reference. ```elixir @type js_result() :: {:ok, term()} | {:error, QuickBEAM.JSError.t()} ``` ```elixir @type runtime() :: GenServer.server() ``` -------------------------------- ### disasm(runtime, code) Source: https://hexdocs.pm/quickbeam Compiles JavaScript source code and disassembles the resulting bytecode. ```APIDOC ## disasm(runtime, code) ### Description Compiles JavaScript source and disassembles the resulting bytecode. ### Signature `disasm(runtime(), String.t()) :: {:ok, QuickBEAM.Bytecode.t()} | {:error, term()}` ### Example ```elixir {:ok, %QuickBEAM.Bytecode{cpool: [%QuickBEAM.Bytecode{name: "add"}]}} = QuickBEAM.disasm(rt, "function add(a, b) { return a + b }") ``` ``` -------------------------------- ### compile(runtime, code) Source: https://hexdocs.pm/quickbeam Compiles JavaScript source code into bytecode without executing it. ```APIDOC ## compile(runtime, code) ### Description Compile JavaScript source to bytecode without executing it. Returns bytecode that can be loaded into any runtime. ### Parameters - **runtime** (runtime()) - Required - The server process. - **code** (String.t()) - Required - The JavaScript source code to compile. ### Response - **result** ({:ok, binary()} | {:error, QuickBEAM.JSError.t()}) - The compiled bytecode or an error. ``` -------------------------------- ### Disassemble bytecode or source code Source: https://hexdocs.pm/quickbeam Parses bytecode or source code into a structured representation without requiring a persistent runtime. ```elixir @spec disasm(binary()) :: {:ok, QuickBEAM.Bytecode.t()} | {:error, String.t()} ``` ```elixir {:ok, bytecode} = QuickBEAM.compile(rt, "function add(a, b) { return a + b }") {:ok, %QuickBEAM.Bytecode{}} = QuickBEAM.disasm(bytecode) ``` ```elixir {:ok, %QuickBEAM.Bytecode{}} = QuickBEAM.disasm(rt, "function add(a, b) { return a + b }") ``` -------------------------------- ### Find all matching DOM elements Source: https://hexdocs.pm/quickbeam Retrieves a list of all elements matching a CSS selector as Floki-compatible tuples. ```elixir @spec dom_find_all(runtime(), String.t()) :: {:ok, list()} ``` ```elixir {:ok, rt} = QuickBEAM.start() QuickBEAM.eval(rt, ~s[document.body.innerHTML = 'Hello
'") {:ok, {"p", [{"class", "intro"}], ["Hello"]}} = QuickBEAM.dom_find(rt, "p.intro") ``` -------------------------------- ### dom_attr(runtime, selector, attr_name) Source: https://hexdocs.pm/quickbeam Retrieves an attribute value from the first element matching a CSS selector. ```APIDOC ## dom_attr(runtime, selector, attr_name) ### Description Get an attribute value from the first element matching a CSS selector. Returns `nil` if the element or attribute is not found. ### Signature `dom_attr(runtime(), String.t(), String.t()) :: {:ok, String.t() | nil}` ### Example ```elixir {:ok, rt} = QuickBEAM.start() QuickBEAM.eval(rt, ~s[document.body.innerHTML = 'link']) {:ok, "/page"} = QuickBEAM.dom_attr(rt, "a", "href") ``` ``` -------------------------------- ### Evaluate JavaScript Code Source: https://hexdocs.pm/quickbeam Evaluates JavaScript code within the runtime, supporting top-level await. ```elixir @spec eval(runtime(), String.t(), keyword()) :: js_result() ``` ```elixir iex> {:ok, rt} = QuickBEAM.start() iex> QuickBEAM.eval(rt, "40 + 2") {:ok, 42} iex> QuickBEAM.eval(rt, "await Promise.all([1, 2].map(x => Promise.resolve(x)))") {:ok, [1, 2]} iex> QuickBEAM.stop(rt) :ok ``` -------------------------------- ### stop(runtime) Source: https://hexdocs.pm/quickbeam Stops a specified runtime and releases its associated resources. ```APIDOC ## stop(runtime) ### Description Stops a runtime and free its resources. ### Signature `stop(runtime()) :: :ok` ### Parameters - **runtime** (runtime()) - Required - The runtime instance to be stopped. ``` -------------------------------- ### Send a message to the JS runtime Source: https://hexdocs.pm/quickbeam Delivers a message to the handler registered via Beam.onMessage in JavaScript. ```elixir @spec send_message(runtime(), term()) :: :ok ``` -------------------------------- ### Reset the JavaScript runtime Source: https://hexdocs.pm/quickbeam Clears all state and functions within the JS context. ```elixir @spec reset(runtime()) :: :ok | {:error, String.t()} ``` ```elixir iex> {:ok, rt} = QuickBEAM.start() iex> QuickBEAM.eval(rt, "globalThis.x = 42") iex> QuickBEAM.reset(rt) :ok iex> QuickBEAM.eval(rt, "typeof x") {:ok, "undefined"} iex> QuickBEAM.stop(rt) :ok ``` -------------------------------- ### dom_text(runtime, selector) Source: https://hexdocs.pm/quickbeam Extracts text content from the first element matching a CSS selector. ```APIDOC ## dom_text(runtime, selector) ### Description Extracts text content from the first element matching a CSS selector. ### Signature `dom_text(runtime(), String.t()) :: {:ok, String.t()}` ### Example ```elixir {:ok, rt} = QuickBEAM.start() QuickBEAM.eval(rt, "document.body.innerHTML = '