docs
Loading...
Searching...
No Matches
CallInfo.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
6#include <Ultralight/CAPI/CAPI_JSValue.h>
8
9namespace ultralight {
10namespace js {
11
12class Context;
13
14///
15/// Details of a call to a bound function: the context, `this`, and the raw arguments.
16///
17/// Add a CallInfo parameter to a bound function to receive it, either first or after the other
18/// parameters. It isn't a JavaScript argument, so the other parameters still convert from the
19/// page's arguments as usual:
20///
21/// ```
22/// api["inspect"] = [](std::string name, js::CallInfo info) {
23/// return (double)info.arg_count(); // `name` still converts from argument 1
24/// };
25/// ```
26///
27/// Declare only one. With a trailing js::Resolver the order is (arguments..., CallInfo,
28/// Resolver).
29///
30/// \parblock
31/// @note A CallInfo and the js::Arg values it gives you are valid only during the call (in a
32/// js::Task, until the Task finishes). Use Arg::ToValue() to keep an argument. You can
33/// keep the Context that context() returns.
34/// \endparblock
35///
36/// \parblock
37/// @note A function that takes a CallInfo receives every argument the page passes, so Strict
38/// diagnostics don't reject extra arguments for it (see js::Diagnostics).
39/// \endparblock
40///
41class CallInfo {
42 public:
43 /// @cond INTERNAL
44 // Constructed by the binding layer; a bound callable receives a CallInfo, never builds one.
45 CallInfo(ULJSContext ctx, ULJSValue this_value, const ULJSValue* args, size_t argc)
46 : ctx_(ctx), this_value_(this_value), args_(args), argc_(argc) {}
47 /// @endcond
48
49 ///
50 /// Get the calling context, to create values in it or to keep it after the call:
51 ///
52 /// ```
53 /// api["build"] = [](js::CallInfo info) {
54 /// return info.context().Make(std::vector<double> { 1, 2, 3 });
55 /// };
56 /// ```
57 ///
58 /// @return Returns a new reference to the context.
59 ///
60 /// @note Requires `<Ultralight/js/Context.h>` (or `<Ultralight/JS.h>`).
61 ///
62 Context context() const;
63
64 ///
65 /// Get the call's `this` value.
66 ///
67 Arg this_value() const { return Arg(this_value_); }
68
69 ///
70 /// Get the number of arguments the page passed.
71 ///
72 size_t arg_count() const { return argc_; }
73
74 ///
75 /// Get an argument.
76 ///
77 /// @param index The zero-based argument position.
78 ///
79 /// @return Returns the argument (an empty Arg if `index` is out of range).
80 ///
81 Arg arg(size_t index) const { return Arg(index < argc_ ? args_[index] : nullptr); }
82
83 private:
84 ULJSContext ctx_;
85 ULJSValue this_value_;
86 const ULJSValue* args_;
87 size_t argc_;
88};
89
90} // namespace js
91} // namespace ultralight
struct C_JSContext * ULJSContext
Opaque handle to a JavaScript execution context (one page's script world in one View).
Definition View.h:25
An argument passed to a bound function.
Definition Value.h:1118
Arg(ULJSValue borrowed)
Wrap a borrowed handle.
Definition Value.h:1127
Details of a call to a bound function: the context, this, and the raw arguments.
Definition CallInfo.h:41
Arg arg(size_t index) const
Get an argument.
Definition CallInfo.h:81
size_t arg_count() const
Get the number of arguments the page passed.
Definition CallInfo.h:72
Arg this_value() const
Get the call's this value.
Definition CallInfo.h:67
Context context() const
Get the calling context, to create values in it or to keep it after the call:
Definition Context.h:655
JavaScript execution environment for a page.
Definition Context.h:151
Type-checked JavaScript bridge between C++ and web pages.
Definition JSInterop.h:141
Root namespace for every public Ultralight type, function, and enumeration.