libadblockplus
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
AdblockPlus::JsEngine Class Reference

JavaScript engine used by FilterEngine, wraps v8. More...

#include <JsEngine.h>

Inheritance diagram for AdblockPlus::JsEngine:
Inheritance graph
[legend]
Collaboration diagram for AdblockPlus::JsEngine:
Collaboration graph
[legend]

Classes

class  JsWeakValuesID
 An opaque structure representing ID of stored JsValueList. More...
 

Public Types

typedef std::function< void(JsValueList
&&params)> 
EventCallback
 Event callback function.
 
typedef std::map< std::string,
EventCallback
EventMap
 Maps events to callback functions.
 

Public Member Functions

void SetEventCallback (const std::string &eventName, const EventCallback &callback)
 Registers the callback function for an event. More...
 
void RemoveEventCallback (const std::string &eventName)
 Removes the callback function for an event. More...
 
void TriggerEvent (const std::string &eventName, JsValueList &&params)
 Triggers an event. More...
 
JsValue Evaluate (const std::string &source, const std::string &filename="")
 Evaluates a JavaScript expression. More...
 
void Gc ()
 Initiates a garbage collection.
 
JsValue NewObject ()
 Creates a new JavaScript object. More...
 
JsValue NewCallback (const v8::FunctionCallback &callback)
 Creates a JavaScript function that invokes a C++ callback. More...
 
JsWeakValuesID StoreJsValues (const JsValueList &values)
 Stores JsValues in a way they don't keep a strong reference to JsEngine and which are destroyed when JsEngine is destroyed. More...
 
JsValueList TakeJsValues (const JsWeakValuesID &id)
 Extracts and removes from JsEngine earlier stored JsValues. More...
 
JsValueList GetJsValues (const JsWeakValuesID &id)
 Extracts earlier stored JsValues from JsEgnine but does not remove them, one still must call TakeJsValues. More...
 
JsValueList ConvertArguments (const v8::FunctionCallbackInfo< v8::Value > &arguments)
 Converts v8 arguments to JsValue objects. More...
 
void SetGlobalProperty (const std::string &name, const AdblockPlus::JsValue &value)
 Sets a global property that can be accessed by all the scripts. More...
 
v8::Isolate * GetIsolate ()
 Returns a pointer to associated v8::Isolate.
 
void NotifyLowMemory ()
 Notifies JS engine about critically low memory what should cause a garbage collection.
 
PlatformGetPlatform ()
 Private functionality.
 
JsValue NewValue (const std::string &val)
 Creates a new JavaScript value. More...
 
JsValue NewValue (int64_t val)
 Creates a new JavaScript value. More...
 
JsValue NewValue (bool val)
 Creates a new JavaScript value. More...
 
JsValue NewValue (const char *val)
 Creates a new JavaScript value. More...
 
JsValue NewValue (int val)
 Creates a new JavaScript value. More...
 

Static Public Member Functions

static JsEnginePtr New (const AppInfo &appInfo, Platform &platform, std::unique_ptr< IV8IsolateProvider > isolate=nullptr)
 Creates a new JavaScript engine instance. More...
 
static JsEnginePtr FromArguments (const v8::FunctionCallbackInfo< v8::Value > &arguments)
 Returns a JsEngine instance contained in a v8::FunctionCallbackInfo object. More...
 
static void ScheduleTimer (const v8::FunctionCallbackInfo< v8::Value > &arguments)
 
static void ScheduleWebRequest (const v8::FunctionCallbackInfo< v8::Value > &arguments)
 

Friends

class JsValue
 
class JsContext
 

Detailed Description

JavaScript engine used by FilterEngine, wraps v8.

Member Function Documentation

JsValueList AdblockPlus::JsEngine::ConvertArguments ( const v8::FunctionCallbackInfo< v8::Value > &  arguments)

Converts v8 arguments to JsValue objects.

Parameters
argumentsv8::FunctionCallbackInfo object containing the arguments to convert.
Returns
List of arguments converted to const JsValue objects.
JsValue AdblockPlus::JsEngine::Evaluate ( const std::string &  source,
const std::string &  filename = "" 
)

Evaluates a JavaScript expression.

Parameters
sourceJavaScript expression to evaluate.
filenameOptional file name for the expression, used in error messages.
Returns
Result of the evaluated expression.
static JsEnginePtr AdblockPlus::JsEngine::FromArguments ( const v8::FunctionCallbackInfo< v8::Value > &  arguments)
static

Returns a JsEngine instance contained in a v8::FunctionCallbackInfo object.

Use this in callbacks created via NewCallback() to retrieve the current JsEngine.

Parameters
argumentsv8::FunctionCallbackInfo object containing the JsEngine instance.
Returns
JsEngine instance from v8::FunctionCallbackInfo.
JsValueList AdblockPlus::JsEngine::GetJsValues ( const JsWeakValuesID id)

Extracts earlier stored JsValues from JsEgnine but does not remove them, one still must call TakeJsValues.

Parameters
idJsWeakValuesID of values.
Returns
JsValueList of stored values.
static JsEnginePtr AdblockPlus::JsEngine::New ( const AppInfo appInfo,
Platform platform,
std::unique_ptr< IV8IsolateProvider isolate = nullptr 
)
static

Creates a new JavaScript engine instance.

Parameters
appInfoInformation about the app.
platformAdblockPlus platform providing with necessary dependencies.
isolateA provider of v8::Isolate, if the value is nullptr then a default implementation is used.
Returns
New JsEngine instance.
JsValue AdblockPlus::JsEngine::NewCallback ( const v8::FunctionCallback &  callback)

Creates a JavaScript function that invokes a C++ callback.

Parameters
callbackC++ callback to invoke. The callback receives a v8::FunctionCallbackInfo object and can use FromArguments() to retrieve the current JsEngine.
Returns
New JsValue instance.
JsValue AdblockPlus::JsEngine::NewObject ( )

Creates a new JavaScript object.

Returns
New JsValue instance.
JsValue AdblockPlus::JsEngine::NewValue ( const std::string &  val)

Creates a new JavaScript value.

Parameters
valValue to convert.
Returns
New JsValue instance.
JsValue AdblockPlus::JsEngine::NewValue ( int64_t  val)

Creates a new JavaScript value.

Parameters
valValue to convert.
Returns
New JsValue instance.
JsValue AdblockPlus::JsEngine::NewValue ( bool  val)

Creates a new JavaScript value.

Parameters
valValue to convert.
Returns
New JsValue instance.
JsValue AdblockPlus::JsEngine::NewValue ( const char *  val)
inline

Creates a new JavaScript value.

Parameters
valValue to convert.
Returns
New JsValue instance.
JsValue AdblockPlus::JsEngine::NewValue ( int  val)
inline

Creates a new JavaScript value.

Parameters
valValue to convert.
Returns
New JsValue instance.
void AdblockPlus::JsEngine::RemoveEventCallback ( const std::string &  eventName)

Removes the callback function for an event.

Parameters
eventNameEvent name.
void AdblockPlus::JsEngine::SetEventCallback ( const std::string &  eventName,
const EventCallback callback 
)

Registers the callback function for an event.

Parameters
eventNameEvent name. Note that this can be any string - it's a general purpose event handling mechanism.
callbackEvent callback function.
void AdblockPlus::JsEngine::SetGlobalProperty ( const std::string &  name,
const AdblockPlus::JsValue value 
)

Sets a global property that can be accessed by all the scripts.

Parameters
nameName of the property to set.
valueValue of the property to set.
JsWeakValuesID AdblockPlus::JsEngine::StoreJsValues ( const JsValueList values)

Stores JsValues in a way they don't keep a strong reference to JsEngine and which are destroyed when JsEngine is destroyed.

These methods should be used when one needs to carry a JsValue in a callback directly or indirectly passed to JsEngine. The method is thread-safe.

Parameters
<tt>JsValueList</tt>to store.
Returns
JsWeakValuesID of stored values which allows to restore them later.
JsValueList AdblockPlus::JsEngine::TakeJsValues ( const JsWeakValuesID id)

Extracts and removes from JsEngine earlier stored JsValues.

The method is thread-safe.

Parameters
idJsWeakValuesID of values.
Returns
JsValueList of stored values.
void AdblockPlus::JsEngine::TriggerEvent ( const std::string &  eventName,
JsValueList &&  params 
)

Triggers an event.

Parameters
eventNameEvent name.
paramsEvent parameters.

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