Our JavaScript engine for Xojo

For next MBS Xojo Plugins we add a JavaScript engine to be used within your Xojo application without a HTMLViewer control. You can execute JavaScript similar to the XojoScript control and offer it to your users as a scripting language within your solution. If you need, you can run JavaScript snippets at runtime to do calculations and even integrate your own functions to call back to Xojo.

Evaluate JavaScript

You can use JavaScriptEngineMBS class in next plugin version and instantiate a JavaScript environment. Then you can evaluate some JavaScript code and get back a result. For your convenience we convert between JavaScript data types and Xojo’s variant data type.

[code]Dim j As New JavaScriptEngineMBS

Dim v As Variant = j.Evaluate(“2+3”)
MsgBox v.StringValue[/code]

The variant will now contain the number 5. Alternative you can use EvaluateToString which converts result to string. If something goes wrong you may get a JavaScriptEngineExceptionMBS exception raised, so it may be good to wrap it in a try & catch block.

For your convenience we define a Print, Input and Yield function in JavaScript. The print command will raise the Print event and allow you to implement a print command for JavaScript to write to a log window. The Input event works similar, but passes back the result from Xojo back to JavaScript and converts the types back. Third function for yield is to give time to other Xojo threads if a you run JavaScript in a thread.

Own functions

We have a method AddFunction to add a custom function defined as JavaScript. We compile the JavaScript and put it in the global name space. With RegisterFunction you can add a custom function which calls back to a Xojo function. This uses a delegate pointing to a Xojo method in your class or module. We can pass a fixed or variable number of parameters and a tag value:

[code]Dim j As New JavaScriptEngineMBS

// register a custom function
j.RegisterFunction “MessageBox”, AddressOf MessageBox, 1, Nil

// just add a function
j.AddFunction “adder”, “function (x,y) { return x+y; }”

// call it
Dim r As Variant = j.CallFunction(“adder”, 2, 3)

// call it with array:
Dim Params() As Variant
params.Append 3
params.Append 4
params.Append 5 // one too much -> will be ignored
Dim r As Variant = j.CallFunction(“adder”, params)[/code]

As an advantage function we have functions to save the byte code for the JavaScript functions and restore it later with LoadFunction method.

If you need, you can call a function explicitly without evaluating using the CallFunction

Global properties

You can use GlobalProperty(name) to get and set values in variables linked to the global namespace in JavaScript. As an option we have GlobalPropertyJSON to pass data as JSON to/from JavaScript.

[code]Dim j As New JavaScriptEngineMBS

j.GlobalProperty(“Hello”) = “World”
MsgBox j.GlobalProperty(“Hello”)

j.GlobalPropertyJSON(“test”) = “[1,2,3]”
MsgBox j.GlobalPropertyJSON(“test”)[/code]

As long as you keep the engine object alive, the properties are stored and you can use them in your JavaScript code.

We hope you enjoy the new JavaScript functions. Let us know if you have any questions!

Will the latest JavaScript standard be supported?
class, properties, async/await, web workers?
Is it based on V8?

We use DukTape, a light weight implementation.
It’s much smaller and easier to embed then V8.

Classes and properties work.
Async won’t work as the JavaScript runs only short time when you call evaluate function.
No file access or processes support.

Available in 19.6pr2 now.



I have my own branch of MUJS, which is also synchronous and meant for embedding. DuckTape has more ES6 features.

In my version I added http, url, file system objects similar to corresponding Node.Js modules.

I also modified the engine to allow to add new language functionalities, it also has a better api to define objects (is actually similar to RB’s api)

Well, with Register function you can connect a Xojo function to JavaScript, so you can add file IO and other functions if needed.

j.RegisterFunction "MyFunction", AddressOf testFunction, 1, Nil