A handle to a live JavaScript value.
A Value represents a live JavaScript value on a page. You use it to hold page objects and functions across calls, interacting with them directly without converting them to C++ types.
This example interacts with an object on the page:
double health = player[
"health"].
Or(0.0);
player["score"] = 1200;
player["respawn"](10, 20);
A handle to a live JavaScript value.
Definition Value.h:210
T Or(T fallback) const
Convert to a C++ type like To() but with a fallback.
Handle States
A handle is always in one of these states:
| State | When | operator bool | Calls |
| Valid | On a live page | true | Work normally |
| Empty | Default-constructed, moved from, or failed read | false | Do nothing |
| Gone | Page navigated, frame removed, or View destroyed | false | Do nothing |
An empty Value differs from JavaScript null and undefined. A missing property reads as a valid Value holding undefined, while an empty handle represents an uninitialized handle or a failed operation.
Every operation is safe in all three states, allowing property reads and method calls to chain without checking each step.
This call chains safely even if the HUD object doesn't exist:
ctx["hud"]["showToast"]("Saved");
When an operation returns a failed js::Result, inspect js::Error::is_empty() to check for an empty handle or js::Error::is_page_gone() to check whether the page navigated away.
A handle whose page is gone never recovers, even when that page is restored from the back-forward cache.
To interact with the new page, get a new Value from that page's js::Context. Every page-lifetime handle across the JavaScript API follows these rules.
Copies and Lifetimes
Copying a Value creates another reference to the same JavaScript value.
Holding a Value keeps the underlying JavaScript value from being garbage-collected, but it doesn't keep the page or its View alive.
For ownership cycles between bound instances and JavaScript objects, see js::ClassBuilder.
A const Value still lets you change the underlying JavaScript value, matching the behavior of a const RefPtr.
Type Conversions
The conversion methods To(), Maybe(), and Or() are strict about the JavaScript type (the string "7" doesn't convert to a number), differing only in how they report a failure.
To convert values using JavaScript's own coercion rules, call ToNumber(), ToString(), ToBoolean(), or ToJSON() directly on the Value. For the full list of supported types, see js::TypeTraits.
This example compares strict conversion against JavaScript coercion:
double strict = level.
Or(0.0);
double loose = level.
ToNumber().value_or(0.0);
Result< double > ToNumber() const
Convert to a number using JavaScript's rules.
Definition Value.h:354
Values Across Pages
A Value belongs to the page that created it. You can pass a Value directly only between frames of the same top-level page that share an exact origin, never across separate Views.
Using a Value on a page that can't take it fails with a TypeError whose error code is ULJS_CROSS_CONTEXT. To transfer data between separate pages or Views, copy the data using JSON or native C++ types.
This example transfers player data to a page in another View using JSON:
std::string json = player.
ToJSON().value_or(
"null");
bestiary[
"player"] =
js::OrEmpty(bestiary.MakeFromJSON(json));
JavaScript execution environment for a page.
Definition Context.h:151
Result< std::string > ToJSON(unsigned indent=0) const
Convert to JSON text like JSON.stringify() does.
Definition Value.h:413
T OrEmpty(Result< T > result)
Get a Result's value, or a default-constructed T if it holds an error.
Definition Error.h:539
- Note
- js::Value and dom::data::Value are unrelated types (dom::data::Value is the value type of the data-binding API).
- See also
- js::Context, js::Error, js::TypeTraits, js::WeakValue
|
| | Value () |
| | Create an empty Value.
|
| | Value (const Value &other) |
| | Copy constructor (adds a reference to the same JavaScript value).
|
| | Value (Value &&other) noexcept |
| | Move constructor (other becomes empty).
|
| Value & | operator= (Value other) noexcept |
| | Assignment (copies or moves other into this Value).
|
| | ~Value () |
| | Destructor (releases this handle).
|
| | operator bool () const |
| | Whether or not this Value is valid (it isn't empty and its page is still alive).
|
| bool | IsEmpty () const |
| | Whether or not this Value holds nothing (see "Handle States" above).
|
| bool | IsAlive () const |
| | Whether or not this Value's page is still alive (the same test as operator bool).
|
| Type | type () const |
| | Get the type of the value.
|
| bool | IsUndefined () const |
| | Whether or not the value is undefined.
|
| bool | IsNull () const |
| | Whether or not the value is null.
|
| bool | IsNullish () const |
| | Whether or not the value is null or undefined.
|
| bool | IsBoolean () const |
| | Whether or not the value is a boolean.
|
| bool | IsNumber () const |
| | Whether or not the value is a number.
|
| bool | IsBigInt () const |
| | Whether or not the value is a BigInt (eg, 10n).
|
| bool | IsString () const |
| | Whether or not the value is a string.
|
| bool | IsObject () const |
| | Whether or not the value is an object.
|
| bool | IsArray () const |
| | Whether or not the value is an Array.
|
| bool | IsCallable () const |
| | Whether or not the value can be called (see Call() and Invoke()).
|
| bool | IsFunction () const |
| | Whether or not the value is a function.
|
| bool | IsPromise () const |
| | Whether or not the value is a Promise.
|
| Result< bool > | ToBoolean () const |
| | Convert to a boolean using JavaScript's rules (this never runs script).
|
| Result< double > | ToNumber () const |
| | Convert to a number using JavaScript's rules.
|
| Result< std::string > | ToString () const |
| | Convert to a UTF-8 string using JavaScript's rules.
|
| Result< std::string > | ToJSON (unsigned indent=0) const |
| | Convert to JSON text like JSON.stringify() does.
|
| Result< Value > | GetProperty (const char *name) const |
| | Get a property of this object (own or inherited).
|
| bool | Has (const char *name) const |
| | Whether or not this object has a property (own or inherited).
|
| Result< bool > | HasProperty (const char *name) const |
| | Whether or not this object has a property (like Has(), but reports failures).
|
| Result< void > | SetProperty (const char *name, const Value &value, PropertyAttributes attributes=PropertyAttributes::None) const |
| | Set a property of this object.
|
| Result< Value > | Call (const Value &this_value, const Value *args, size_t argc) const |
| | Call this value as a function.
|
| Result< Value > | Call (const Value &this_value, std::span< const Value > args) const |
| | Call this value as a function with the arguments in a range (eg, a std::vector<js::Value>).
|
| template<typename T> |
| Result< T > | To () const |
| | Convert to a C++ type.
|
| template<typename T> |
| std::optional< T > | Maybe () const |
| | Convert to a C++ type like To() but without the failure reason.
|
| template<typename T> |
| T | Or (T fallback) const |
| | Convert to a C++ type like To() but with a fallback.
|
| std::string | Or (const char *fallback) const |
| | Convert to a std::string like To() but with a string-literal fallback.
|
| template<typename R = Value, typename... A> |
| Result< R > | Invoke (A &&... args) const |
| | Call this value as a function with typed arguments and result.
|
| template<typename R = Value, typename... A> |
| Result< R > | InvokeOn (const Value &this_value, A &&... args) const |
| | Call this value as a function with an explicit this (otherwise the same as Invoke()).
|
| template<typename... A> |
| Result< Value > | operator() (A &&... args) const |
| | Call this value as a function: on_save(path) is the same as on_save.Invoke(path).
|
| template<typename F> |
| bool | Then (F &&on_settled) const |
| | Wait for this promise to settle without a coroutine.
|
template<typename T, typename F>
requires (!std::is_same_v<T, Value>) |
| bool | Then (F &&on_settled) const |
| | Wait for this promise to settle and convert its value to a C++ type.
|
| Ref | operator[] (const char *name) const |
| | Access a property to read, assign, or call it.
|
template<typename I>
requires (std::is_integral_v<I> && !std::is_same_v<I, bool>) |
| Ref | operator[] (I index) const |
| ULJSValue | raw () const |
| | Get the C API handle without transferring ownership.
|
| ULJSValue | LeakRef () |
| | Give up ownership of the C API handle and return it (like RefPtr::LeakRef()).
|