Hi,
see
https://mbs-plugins.de/archive/2019-12-09/Our_JavaScript_engine_for_Xojo/monkeybreadsoftware_blog_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.
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!
Sincerely
Christian
--
Read our blog about news on our plugins:
http://www.mbsplugins.de/
_______________________________________________
[email protected] mailing list
%(list_address)s
https://ml-cgn08.ispgateway.de/mailman/listinfo/mbsplugins_monkeybreadsoftware.info