JUCE v8.0.9
JUCE API
 
Loading...
Searching...
No Matches
juce::JavascriptEngine Class Referencefinal

This class is a wrapper around QuickJS, an ES2023 compliant, embeddable javascript engine. More...

#include <juce_JavascriptEngine.h>

Collaboration diagram for juce::JavascriptEngine:

Public Member Functions

 JavascriptEngine ()
 Creates an instance of the engine.
 
 ~JavascriptEngine ()
 Destructor.
 
var callFunction (const Identifier &function, const var::NativeFunctionArgs &args, Result *errorMessage=nullptr)
 Calls a function in the root namespace, and returns the result.
 
var evaluate (const String &javascriptCode, Result *errorMessage=nullptr)
 Attempts to parse and run a javascript expression, and returns the result.
 
Result execute (const String &javascriptCode)
 Attempts to parse and run a block of javascript code.
 
JSObject getRootObject () const
 Returns the object from which all Javascript objects are reachable in the engine's context.
 
NamedValueSet getRootObjectProperties () const
 Provides access to the set of properties of the root namespace object.
 
void registerNativeObject (const Identifier &objectName, DynamicObject *object)
 Adds a native object to the root namespace.
 
void stop () noexcept
 When called from another thread, causes the interpreter to time-out as soon as possible.
 

Public Attributes

RelativeTime maximumExecutionTime
 This value indicates how long a call to one of the evaluate methods is permitted to run before timing-out and failing.
 

Private Attributes

std::unique_ptr< Impl > impl
 

Detailed Description

This class is a wrapper around QuickJS, an ES2023 compliant, embeddable javascript engine.

It may not be as fast as the fancy JIT-compiled engines that you get in browsers, but this is an extremely compact, low-overhead javascript interpreter, which is integrated with the juce var and DynamicObject classes. It allows you to easily let the JS work with native objects defined as DynamicObject subclasses.

To use, simply create an instance of this class and call execute() to run your code. Variables that the script sets can be retrieved with evaluate(), and if you need to provide native objects for the script to use, you can add them with registerNativeObject().

@tags{Core}

Constructor & Destructor Documentation

◆ JavascriptEngine()

juce::JavascriptEngine::JavascriptEngine ( )

Creates an instance of the engine.

◆ ~JavascriptEngine()

juce::JavascriptEngine::~JavascriptEngine ( )

Destructor.

Member Function Documentation

◆ callFunction()

var juce::JavascriptEngine::callFunction ( const Identifier function,
const var::NativeFunctionArgs args,
Result errorMessage = nullptr 
)

Calls a function in the root namespace, and returns the result.

The function arguments are passed in the same format as used by native methods in the var class.

◆ evaluate()

var juce::JavascriptEngine::evaluate ( const String javascriptCode,
Result errorMessage = nullptr 
)

Attempts to parse and run a javascript expression, and returns the result.

If there's a syntax error, or the expression can't be evaluated, the return value will be var::undefined(). The errorMessage parameter gives you a way to find out any parsing errors. If the expression is successfully evaluated but yields no result the return value will be a void var. You can specify a maximum time for which the program is allowed to run, and it'll return with an error message if this time is exceeded.

◆ execute()

Result juce::JavascriptEngine::execute ( const String javascriptCode)

Attempts to parse and run a block of javascript code.

If there's a parse or execution error, the error description is returned in the result. You can specify a maximum time for which the program is allowed to run, and it'll return with an error message if this time is exceeded.

◆ getRootObject()

JSObject juce::JavascriptEngine::getRootObject ( ) const

Returns the object from which all Javascript objects are reachable in the engine's context.

◆ getRootObjectProperties()

NamedValueSet juce::JavascriptEngine::getRootObjectProperties ( ) const

Provides access to the set of properties of the root namespace object.

◆ registerNativeObject()

void juce::JavascriptEngine::registerNativeObject ( const Identifier objectName,
DynamicObject object 
)

Adds a native object to the root namespace.

The object passed-in is reference-counted, and will be retained by the engine until the engine is deleted. The name must be a simple JS identifier, without any dots.

◆ stop()

void juce::JavascriptEngine::stop ( )
noexcept

When called from another thread, causes the interpreter to time-out as soon as possible.

Member Data Documentation

◆ impl

std::unique_ptr<Impl> juce::JavascriptEngine::impl
private

◆ maximumExecutionTime

RelativeTime juce::JavascriptEngine::maximumExecutionTime

This value indicates how long a call to one of the evaluate methods is permitted to run before timing-out and failing.

The default value is a number of seconds, but you can change this to whatever value suits your application.


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