Loading...
Searching...
No Matches
juce::JavascriptEngine Class Referencefinal

`#include <juce_Javascript.h>`

## Public Member Functions

| JavascriptEngine ()
| ~JavascriptEngine ()
Result | execute (const String &javascriptCode)
var | evaluate (const String &javascriptCode, Result *errorMessage=nullptr)
var | callFunction (const Identifier &function, const var::NativeFunctionArgs &args, Result *errorMessage=nullptr)
var | callFunctionObject (DynamicObject *objectScope, const var &functionObject, const var::NativeFunctionArgs &args, Result *errorMessage=nullptr)
void | registerNativeObject (const Identifier &objectName, DynamicObject *object)
void | stop () noexcept
const NamedValueSet & | getRootObjectProperties () const noexcept

## Public Attributes

RelativeTime | maximumExecutionTime

## Detailed Description

A simple javascript interpreter!

It's not fully standards-compliant, and won't 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. If you need a few simple bits of scripting in your app, and want to be able to easily let the JS work with native objects defined as DynamicObject subclasses, then this might do the job.

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

One caveat: Because the values and objects that the engine works with are DynamicObject and var objects, they use reference-counting rather than garbage-collection, so if your script creates complex connections between objects, you run the risk of creating cyclic dependencies and hence leaking.

Definition at line 47 of file juce_Javascript.h.

## Constructor & Destructor Documentation

## JavascriptEngine()

juce::JavascriptEngine::JavascriptEngine | ( |  | ) |

Creates an instance of the engine. This creates a root namespace and defines some basic Object, String, Array and Math library methods.

Definition at line 1824 of file juce_Javascript.cpp.

Referenced by JavascriptEngine().

## ~JavascriptEngine()

juce::JavascriptEngine::~JavascriptEngine | ( |  | ) |

Destructor.

Definition at line 1834 of file juce_Javascript.cpp.

## 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.

Definition at line 1875 of file juce_Javascript.cpp.

## callFunctionObject()

var juce::JavascriptEngine::callFunctionObject | ( | DynamicObject * | objectScope,
|  | const var & | functionObject,
|  | const var::NativeFunctionArgs & | args,
|  | Result * | errorMessage = nullptr )

Calls a function object in the namespace of a dynamic object, and returns the result. The function arguments are passed in the same format as used by native methods in the var class.

Definition at line 1893 of file juce_Javascript.cpp.

## 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. 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.

Definition at line 1859 of file juce_Javascript.cpp.

## 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.

Definition at line 1844 of file juce_Javascript.cpp.

## getRootObjectProperties()

const NamedValueSet & juce::JavascriptEngine::getRootObjectProperties | ( |  | ) | const

noexcept

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

Definition at line 1914 of file juce_Javascript.cpp.

## 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.

Definition at line 1839 of file juce_Javascript.cpp.

Referenced by JavascriptEngine().

## stop()

void juce::JavascriptEngine::stop | ( |  | ) |

noexcept

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

Definition at line 1837 of file juce_Javascript.cpp.

## Member Data Documentation

## 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.

Definition at line 106 of file juce_Javascript.h.

Referenced by JavascriptEngine().

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

- juce_Javascript.h
- juce_Javascript.cpp