Comparing JavaScriptEngineMBS to XojoScript

Let’s compare a bit XojoScript built-in to Xojo to JavaScriptEngineMBS class in MBS Xojo Tools Plugin. What are differences and benefits for both?

XojoScript is the scripting engine provided by Xojo and is heavily used in Xojo for IDE scripting. You can use it in your projects, compile code at runtime with a reduced Xojo language and the run it. With context object you can provide your own methods and properties and use them in the scripts. While you can precompile and then run several times, there is no way to store the compiled code anyhow. There is only one source piece containing all the code, so you can’t just predefine some routines outside source.

On the other side we have JavaScriptEngineMBS, which uses Duktape library to provide a ECMAScript engine. So the language is JavaScript and there is built-in functionality for text, array and JSON functions. You can just pass some JavaScript from your user to the engine and let it execute. Our plugin allows you to set/get global properties in the JavaScript memory. With XojoScript you would have to predefine variables either in context object or in the source code. For JavaScript you can define your own functions to be used in the script by calling AddFunction method, so you don’t need to know those at compile time and you can load them from a database at runtime. You could load a JavaScript from your user and let them write functions which you call with our CallFunction function. Finally like the XojoScript context object you can use Xojo methods to define JavaScript functions via delegates and have JavaScript call back to your Xojo application.

The JavaScript is evaluated at runtime, it may be parsed before running, but it is not compiled into machine code as with LLVM compiler in XojoScript. We don’t expect anyone to notice the difference unless you do some heavy calculations. The JavaScript function definitions in Duktape can be exported as a BLOB and stored in binary notation in a database. See SaveFunction and LoadFunction methods in JavaScriptEngineMBS class.

As we don’t know your use case, the decision which to use can be difficult. XojoScript has the big advantage that it’s built-in and uses the same Xojo language you know about. But your users may be more familiar with JavaScript, so our plugin may be a good choice, too. As our JavaScript doesn’t need to be defined as much at compile time, you can invoke it more dynamically. Like loading your context functions from a database and installing them at runtime and loading global properties from database and registering them via code. Finally you can add functions to call methods on different objects via delegates, so some methods you offer can go to the app object, some to the current window and others to your Xojo class for the data model you have.

If you have questions, please don’t hesitate to contact us. See also New in the MBS Xojo Plugins 20.0

Let us know if you see other benefits for one of them!