Molybden
|
The base class for defining JavaScript accessible object classes that can be automatically converted to JavaScript objects and vice versa. More...
#include <js_accessible.hpp>
Additional Inherited Members | |
Public Member Functions inherited from molybden::JsProxyObject | |
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 () |
The base class for defining JavaScript accessible object classes that can be automatically converted to JavaScript objects and vice versa.
Derive your custom accessible class from JsAccessible
via CRTP pattern: class MyJsAccessibleClass : public JsAccessible<MyJsAccessibleClass> ...
To to make the object method callable from JavaScript, use JS_ACCESSIBLE_METHOD
macro in the private section in the end of the class definition: ... public: void Foo(int x); ... private: JS_ACCESSIBLE_METHOD(Foo); }
To make the object field accessible from JavaScript, use JS_ACCESSIBLE_FIELD
macro in the private section in the end of the class definition : ... private: int x; ... private: JS_ACCESSIBLE_FILED(x); }
The accessible objects as well as other custom 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 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.
If a class is derived from the JavaScript-accessible class via public or protected inheritance, it also becomes JavaScript-accessible. The custom downcast can be defined via defining the static method: class Base : public JsAccessible<Base> ... class Derived : public Base { ... static std::shared_ptr<Derived> downcast(std::shared_ptr<Base> 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 JavaScript-accessible class and the derived ones should be defined.
JsAccessible
provides default implementation of a JavaScript proxy object If you need custom implementation that cannot be achieved with JsAccessible
, derive C
from JsProxyObjectImpl<C>
and implement the interface of JsProxyObject
.
C | the class to make a proxy of. |
JsProxyObjectImpl
. JsProxyObject
.