Molybden API
|
A JavaScript proxy object. More...
#include <js_proxy_object.hpp>
Public Member Functions | |
template<class T > | |
std::shared_ptr< T > | as () |
Downcasts the object to the appropriate proxy type. | |
virtual JsReturnValue | call (const std::string &method_name, const std::vector< JsValue > &args)=0 |
Called when the JavaScript proxy object method with method_name has been called in the corresponding JavaScript object with args passed. | |
virtual JsValue | getProperty (const std::string &property_name)=0 |
Called when the JavaScript proxy object property with property_name has been accessed to read. | |
virtual bool | setProperty (const std::string &property_name, const JsValue &value)=0 |
Called when the JavaScript proxy object property with property_name has been accessed to write. | |
virtual bool | hasMethod (const std::string &method_name)=0 |
Called when the JavaScript engine requests whether the proxy object has the method with method_name . | |
virtual bool | hasProperty (const std::string &property_name)=0 |
Called when the JavaScript engine requests whether the proxy object has the property with property_name . | |
virtual std::string | getType ()=0 |
Called when the JavaScript engine requests the proxy object type name. | |
virtual std::vector< std::string > | enumerateMembers ()=0 |
Called when the JavaScript engine requests all the members of the proxy object. | |
template<> | |
std::shared_ptr< JsProxyObject > | as () |
A JavaScript proxy object.
Proxy objects are C++ objects that can be converted to JavaScript to emulate the behavior of the JavaScript objects.
When the proxy object is passed to JavaScript, the corresponding "proxy" JavaScript object is created. The interface implementation defines how the method calling, property accessing and the other operations with the JavaScript proxy object should be handled.
IMPORTANT: Derive NOT from this class, but from JsProxyObjectImpl<C>
if you want to implement your own custom JavaScript proxy object.
If a class is derived from the JavaScript proxy class via public or protected inheritance its instances are also valid JavaScript proxy objects.
JavaScript proxy objects are distributed and stored via std::shared_ptr
, so one have to wrap the object into the shared pointer and only then pass it to the Molybden API. The ownership of the object will be shared between the client code and the library, and it can finally be deleted only if the all the pages where it was used are unloaded, and if at the same time it is not referenced by other objects and scopes anymore.
JsProxyObjectImpl
JsAccessible
std::shared_ptr< T > molybden::JsProxyObject::as | ( | ) |
Downcasts the object to the appropriate proxy type.
T
must be derived from JsProxyObjectImpl<T>
or JsAccessible<T>
, or equally, must be a custom JavaScript proxy object.
If RTTI is enabled, the proxy objects are downcasted via dynamic_cast
by default. Otherwise, no default downcast is provided and each downcast between the root proxy type and the derived ones should be defined.
The custom downcast can be defined via defining the static method: class Base : public JsProxyObjectImpl<Base> ... class Derived : public Base { ... static std::shared_ptr<Derived> downcast(std::shared_ptr<Base> object); ... };
T | a user-defined type to downcast this object to. |
|
pure virtual |
Called when the JavaScript proxy object method with method_name
has been called in the corresponding JavaScript object with args
passed.
The returned value will be passed to JavaScript and returned as the result of the method execution.
To throw a exception of returning a value on the JavaScript side, return JsError
.
method_name | the name of the JavaScript object method. |
args | the args that have been passed to the method. |
JsError
.
|
pure virtual |
Called when the JavaScript engine requests all the members of the proxy object.
|
pure virtual |
Called when the JavaScript proxy object property with property_name
has been accessed to read.
property_name | the name of the JavaScript object property. |
JsValue
that represents the JavaScript property value.
|
pure virtual |
Called when the JavaScript engine requests the proxy object type name.
|
pure virtual |
Called when the JavaScript engine requests whether the proxy object has the method with method_name
.
The behavior must be consistent with call(const std::string&, const std::vector<JsValue>&)
.
method_name | the name of the requested method. |
true
if such method exists.
|
pure virtual |
Called when the JavaScript engine requests whether the proxy object has the property with property_name
.
The behavior must be consistent with getProperty(const std::string&)
.
property_name | the name of the requested property. |
true
if such property exists.
|
pure virtual |
Called when the JavaScript proxy object property with property_name
has been accessed to write.
property_name | the name of the JavaScript object property. |
value | the value to set as the JavaScript object property with property_name . |
JsValue
that represents the JavaScript property value.