Our JavaScript engine for Xojo

  1. 7 weeks ago

    Christian S

    Dec 9 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    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.

    Dim j As New JavaScriptEngineMBS
    Dim v As Variant = j.Evaluate("2+3")
    MsgBox v.StringValue

    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:

    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)

    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.

    Dim j As New JavaScriptEngineMBS
    j.GlobalProperty("Hello") = "World"
    MsgBox j.GlobalProperty("Hello")
    j.GlobalPropertyJSON("test") = "[1,2,3]"
    MsgBox j.GlobalPropertyJSON("test")

    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!

  2. Hans-Norbert G

    is not verified Dec 9 Pre-Release Testers, Xojo Pro

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

  3. Christian S

    Dec 9 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    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.

  4. 6 weeks ago

    Christian S

    Dec 10 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    Available in 19.6pr2 now.


  5. Nice...

    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)

  6. Christian S

    Dec 11 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany
    Edited 6 weeks ago

    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

or Sign Up to reply!