Molybden API
Loading...
Searching...
No Matches
molybden::JsProxyObject Class Referenceabstract

A JavaScript proxy object. More...

#include <js_proxy_object.hpp>

Inheritance diagram for molybden::JsProxyObject:
molybden::JsProxyObjectImpl< JsProxyFunctionObjectBase > molybden::JsProxyObjectImpl< C > molybden::JsAccessible< JsProxyFunctionObjectBase > molybden::JsAccessible< C >

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< JsProxyObjectas ()
 

Detailed Description

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.

See also
JsProxyObjectImpl
JsAccessible

Member Function Documentation

◆ as()

template<class T >
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); ... };

Template Parameters
Ta user-defined type to downcast this object to.
Returns
a shared pointer to T that holds this object if the object can be downcasted to the type T, or nullptr otherwise.

◆ call()

virtual JsReturnValue molybden::JsProxyObject::call ( const std::string &  method_name,
const std::vector< JsValue > &  args 
)
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.

Parameters
method_namethe name of the JavaScript object method.
argsthe args that have been passed to the method.
Returns
a JavaScript value or an error that represents the result of the method execution.
See also
JsError.

◆ enumerateMembers()

virtual std::vector< std::string > molybden::JsProxyObject::enumerateMembers ( )
pure virtual

Called when the JavaScript engine requests all the members of the proxy object.

Returns
the vector of the proxy object members' names.

◆ getProperty()

virtual JsValue molybden::JsProxyObject::getProperty ( const std::string &  property_name)
pure virtual

Called when the JavaScript proxy object property with property_name has been accessed to read.

Parameters
property_namethe name of the JavaScript object property.
Returns
JsValue that represents the JavaScript property value.

◆ getType()

virtual std::string molybden::JsProxyObject::getType ( )
pure virtual

Called when the JavaScript engine requests the proxy object type name.

Returns
the object type name.

◆ hasMethod()

virtual bool molybden::JsProxyObject::hasMethod ( const std::string &  method_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>&).

Parameters
method_namethe name of the requested method.
Returns
true if such method exists.

◆ hasProperty()

virtual bool molybden::JsProxyObject::hasProperty ( const std::string &  property_name)
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&).

Parameters
property_namethe name of the requested property.
Returns
true if such property exists.

◆ setProperty()

virtual bool molybden::JsProxyObject::setProperty ( const std::string &  property_name,
const JsValue value 
)
pure virtual

Called when the JavaScript proxy object property with property_name has been accessed to write.

Parameters
property_namethe name of the JavaScript object property.
valuethe value to set as the JavaScript object property with property_name.
Returns
JsValue that represents the JavaScript property value.

The documentation for this class was generated from the following files: