Molybden API
|
A JavaScript object. More...
#include <js_object.hpp>
Public Types | |
using | Private = JsObjectImpl |
Public Member Functions | |
JsObject (std::unique_ptr< Private > impl) | |
std::shared_ptr< molybden::Frame > | frame () |
Returns the shared pointer to the Frame instance of this JavaScript object. | |
template<class T > | |
std::shared_ptr< T > | as () |
Returns the shared pointer of type T to this object if it represents one, or nullptr otherwise. | |
template<class... T> | |
JsReturnValue | call (const std::string &method_name, T &&... args) |
Executes the method with the given method_name and the args in the JavaScript object. | |
template<class T > | |
bool | putProperty (const std::string &name, T &&value) |
Creates a new property with the given name or updates the existing one in the current JavaScript object and returns true if the property with the given name was created or updated successfully. | |
bool | removeProperty (const std::string &name) |
Removes a property with the given name in the JavaScript object and returns true if the property was successfully removed. | |
JsValue | getProperty (const std::string &name) |
Returns the value of the JavaScript object's property with the given name . | |
std::vector< std::string > | propertyNames () |
Returns an immutable list of the names of the properties of this JavaScript object, including properties from the prototype objects. | |
std::vector< std::string > | ownPropertyNames () |
Returns an immutable list of the names of the properties of this JavaScript object, excluding properties from the prototype objects. | |
bool | hasProperty (const std::string &name) |
Checks whether the JavaScript object has a property or function with the given name . | |
template<> | |
std::shared_ptr< JsArray > | as () |
template<> | |
std::shared_ptr< JsObject > | as () |
Protected Attributes | |
std::unique_ptr< Private > | impl_ |
A JavaScript object.
Allows accessing the object's properties and calling its functions. The JavaScript object is alive while the web page where the object was created is loaded. When the web page is unloaded, all the JavaScript objects will be automatically disposed. Access to the already disposed JavaScript object will have no effect.
std::shared_ptr< T > molybden::JsObject::as | ( | ) |
Returns the shared pointer of type T
to this object if it represents one, or nullptr otherwise.
T | the JsObject subclass type. |
JsReturnValue molybden::JsObject::call | ( | const std::string & | method_name, |
T &&... | args | ||
) |
Executes the method with the given method_name
and the args
in the JavaScript object.
This method blocks current thread execution until the function finishes its execution.
If the function raises an exception, then JsError
with an error message that describes the reason of the exception will be returned. Same error message will be printed in JavaScript Console.
The type mapping rules are the following:
C++ | JavaScript |
---|---|
double | Double |
std::string | String |
bool | Boolean |
std::shared_ptr<SomeJsObject> | Object |
std::shared_ptr<JsArray> | Array |
std::shared_ptr<SomeJsProxyObject> | Proxy Object |
where
SomeJsObject
is JsObject
or derived;SomeJsProxyObject
is JsProxyObject
or derived.Other types cannot be automatically converted to JavaScript values, and first must be converted to one of the types above.
T | the types of the arguments to pass, considered to be deduced. |
method_name | the name of the method. |
args | arguments to pass to the JavaScript object method. |
bool molybden::JsObject::putProperty | ( | const std::string & | name, |
T && | value | ||
) |
Creates a new property with the given name
or updates the existing one in the current JavaScript object and returns true
if the property with the given name
was created or updated successfully.
The type mapping rules are the following:
C++ | JavaScript |
---|---|
double | Double |
std::string | String |
bool | Boolean |
std::shared_ptr<SomeJsObject> | Object |
std::shared_ptr<JsArray> | Array |
std::shared_ptr<SomeJsProxyObject> | Proxy Object |
where
SomeJsObject
is JsObject
or derived;SomeJsProxyObject
is JsProxyObject
or derived.Other types cannot be automatically converted to JavaScript values, and first must be converted to one of the types above.
T | the type of the object to set as a property value. |
name | a string that represents the property name. |
value | a new value set as the property with the given name . |
bool molybden::JsObject::removeProperty | ( | const std::string & | name | ) |
Removes a property with the given name
in the JavaScript object and returns true
if the property was successfully removed.
Once you remove the property, it will not be available in the current JavaScript object anymore.
name | a string that represents the name of the property or function. |
true
if the property was successfully removed.