Platforms to show: All Mac Windows Linux Cross-Platform

JavaScriptEngineMBS.AddFunction(Name as String, JavaScript as String, FileName as String = "")
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Adds a global function defined as a text string.
Example:
Dim j As New JavaScriptEngineMBS
j.AddFunction "adder", "function (x,y) { return x+y; }"
Notes:
Optional provide a file name to show in stack traces.
Function source code may contain a function name, but we use only the name parameter to register the function as global one.

Feedback, Comments & Corrections

JavaScriptEngineMBS.CallFunction(Name as String, ParamArray Params as Variant) as Variant
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Calls a global function given the name.
Example:
Dim j As New JavaScriptEngineMBS

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

// call it
Dim r As Variant = j.CallFunction("adder", 2, 3)
Break // check result in debugger
Notes:
Parameters are passed to the function.
If the function needs more parameter, undefined values are used to fill up.
If the function needs less parameters, additional ones are dropped.

Returns the result of the function converted to a variant.

See also:

Feedback, Comments & Corrections

JavaScriptEngineMBS.CallFunction(Name as String, Params() as Variant) as Variant
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Calls a global function given the name.
Example:
Dim j As New JavaScriptEngineMBS

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

// call it
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)
Break // check result in debugger
Notes:
Parameters are passed to the function.
If the function needs more parameter, undefined values are used to fill up.
If the function needs less parameters, additional ones are dropped.

Returns the result of the function converted to a variant.

See also:

Feedback, Comments & Corrections

JavaScriptEngineMBS.CallFunctionMT(Name as String, ParamArray Params as Variant) as Variant
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.1 Yes Yes Yes Yes No
Function: Calls a global function given the name.
Notes:
Threaded version of CallFunction. If events or delegates are called on main thread, the preemptive thread waits for answer.

Parameters are passed to the function.
If the function needs more parameter, undefined values are used to fill up.
If the function needs less parameters, additional ones are dropped.

Returns the result of the function converted to a variant.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.

See also:

Feedback, Comments & Corrections

JavaScriptEngineMBS.CallFunctionMT(Name as String, Params() as Variant) as Variant
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.1 Yes Yes Yes Yes No
Function: Calls a global function given the name.
Notes:
Threaded version of CallFunction. If events or delegates are called on main thread, the preemptive thread waits for answer.

Parameters are passed to the function.
If the function needs more parameter, undefined values are used to fill up.
If the function needs less parameters, additional ones are dropped.

Returns the result of the function converted to a variant.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.

See also:

Feedback, Comments & Corrections

JavaScriptEngineMBS.DateComponentsToTime(date as JavaScriptDateComponentsMBS) as Double
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.1 Yes Yes Yes Yes No
Function: Converts date components to time.
Example:
Dim j As New JavaScriptEngineMBS

Dim d As New JavaScriptDateComponentsMBS
d.Year = 1970
d.Month = 1
d.day = 1

// shows zero as that's the start point in time for unix
MsgBox "Start Time: "+Str(j.DateComponentsToTime(d))
Notes:
Convert components (year, month, day, etc), interpreted in UTC, into a time value. The weekday argument is ignored in the conversion. If the component values are invalid, an error is thrown.

There are some differences to the ECMAScript Date.UTC() built-in:

  • There's no special handling of two-digit years. For example, Date.UTC(99, 0, 1) gets interpreted as 1999-01-01. If time is 99, it's interpreted as the year 99.
  • The milliseconds component is allowed fractions (sub-millisecond resolution) so that the resulting time value may have fractions.

Like the ECMAScript primitives, the components can exceed their natural range and are normalized. For example, specifying minute as 120 is interpreted as adding 2 hours to the time value. The components are expressed as Integers to allow large and negative values to be used.

Feedback, Comments & Corrections

JavaScriptEngineMBS.Destructor
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: The destructor.

Feedback, Comments & Corrections

JavaScriptEngineMBS.Evaluate(JavaScript as String) as Variant
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Evaluates a JavaScript expression.
Example:
Dim j As New JavaScriptEngineMBS

Dim v As Variant = j.Evaluate("2+3")
MsgBox v.StringValue
Notes:
This may include definition of functions and objects and using them.
Result is converted to variant and returned.

Feedback, Comments & Corrections

JavaScriptEngineMBS.EvaluateMT(JavaScript as String) as Variant
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.1 Yes Yes Yes Yes No
Function: Evaluates a JavaScript expression.
Notes:
Threaded version of Evaluate. If events or delegates are called on main thread, the preemptive thread waits for answer.

This may include definition of functions and objects and using them.
Result is converted to variant and returned.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.

Feedback, Comments & Corrections

JavaScriptEngineMBS.EvaluateToString(JavaScript as String) as String
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Evaluates a JavaScript expression.
Example:
Dim j As New JavaScriptEngineMBS

Dim s As String = j.EvaluateToString("2+3")
MsgBox s
Notes:
This may include definition of functions and objects and using them.
Result is converted to string in JavaScript and returned as Xojo string.

Feedback, Comments & Corrections

JavaScriptEngineMBS.GlobalMemoryBlock(PropertyName as String) as MemoryBlock
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
property JavaScript MBS Tools Plugin 20.1 Yes Yes Yes Yes No
Function: Query or assign a global property to reference memoryblock.
Notes:
A normal memoryblock passed as parameter to a function or property would be copied.
This method allows you to define a buffer in JavaScript to reference the bytes in a memoryblock, so all changes are visible in Xojo directly.

You can assign nil as value to clear a memoryblock.
(Read and Write computed property)

Feedback, Comments & Corrections

JavaScriptEngineMBS.GlobalProperty(PropertyName as String) as Variant
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
property JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Get or set a global property.
Example:
Dim j As New JavaScriptEngineMBS

j.GlobalProperty("Hello") = "World"
MsgBox j.GlobalProperty("Hello")
Notes:
Values are converted to/from variant.
(Read and Write computed property)

Feedback, Comments & Corrections

JavaScriptEngineMBS.GlobalPropertyJSON(PropertyName as String) as String
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
property JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Get or set a global property as JSON.
Example:
Dim j As New JavaScriptEngineMBS

j.GlobalProperty("Hello") = "World"
MsgBox j.GlobalPropertyJSON("Hello")

j.GlobalPropertyJSON("test") = "[1,2,3]"
MsgBox j.GlobalPropertyJSON("test")
Notes:
Same as GlobalProperty, but needs JSON text.
(Read and Write computed property)

Feedback, Comments & Corrections

JavaScriptEngineMBS.LoadFunction(Name as String, Data as Memoryblock) as Boolean
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Loads a global function from byte code.
Example:
Dim j As New JavaScriptEngineMBS

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

// get byte code
Dim m As MemoryBlock = j.SaveFunction("adder")

// and load back with new name
dim b as Boolean = j.LoadFunction("adder2", m)

// call it
Dim r As Variant = j.CallFunction("adder2", 2, 3)
break // check result in debugger
Notes:
Load a buffer containing bytecode, recreating the original ECMAScript function (with some limitations). You must ensure that the bytecode has been dumped with a compatible Duktape version and that the bytecode has not been modified since. Loading bytecode from an untrusted source is memory unsafe and may lead to exploitable vulnerabilities.

Returns true in case of success or false on failure.

Feedback, Comments & Corrections

JavaScriptEngineMBS.Now as Double
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.1 Yes Yes Yes Yes No
Function: Queries current time as double value.
Notes:
This is seconds since 1st January 1970 multiplied by 1000 and may contain fraction with microseconds.

Get current time in POSIX milliseconds, as seen by the ECMAScript environment. The return value matches Date.now() with the reservation that sub-millisecond resolution may be available.

Feedback, Comments & Corrections

JavaScriptEngineMBS.RegisterFunction(Name as String, theDelegate as JavaScriptEngineDelegateMBS, ArgCount as Integer = -1, tag as Variant = nil)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Registers a global function with given name to call delegate.
Example:
Dim j As New JavaScriptEngineMBS

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

// call in JavaScript
Dim x As Variant = j.Evaluate("MyFunction(123);")

// call via plugin
Dim r As Variant = j.CallFunction("MyFunction", "Hello")

Break

'Public Function testFunction(Name as String, Params() as Variant, tag as Variant) as Variant
' MsgBox params(0).StringValue
' Return "ok"
'End Function
Notes:
ArgCount can be in range from -1 to 15.
If -1 is passed, we allow variable number of arguments.
Tag is passed to delegate and allows you to pass a context object to the delegate.

Feedback, Comments & Corrections

JavaScriptEngineMBS.SaveFunction(Name as String) as MemoryBlock
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.0 Yes Yes Yes Yes No
Function: Queries byte code for global JavaScript function.
Example:
Dim j As New JavaScriptEngineMBS

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

// get byte code
Dim m As MemoryBlock = j.SaveFunction("adder")

// and load back with new name
dim b as Boolean = j.LoadFunction("adder2", m)

// call it
Dim r As Variant = j.CallFunction("adder2", 2, 3)
break // check result in debugger
Notes: Dump an ECMAScript function at stack top into bytecode, replacing the function with a buffer containing the bytecode data. The bytecode can be loaded back using LoadFunction().

Feedback, Comments & Corrections

JavaScriptEngineMBS.TimeToDateComponents(time as Double) as JavaScriptDateComponentsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method JavaScript MBS Tools Plugin 20.1 Yes Yes Yes Yes No
Function: Converts time to date components.
Example:
Dim j As New JavaScriptEngineMBS

// get now
Dim t1 As Double = j.Now

// as compoents
Dim d1 As JavaScriptDateComponentsMBS = j.TimeToDateComponents(t1)
Dim s1 As String = d1.SQLDateTime
// looks right?

Break
Notes:
Convert a time value to components (year, month, day, etc) interpreted in UTC. If the time value is invalid, e.g. beyond the valid ECMAScript time range, an error is thrown.

There are some differences to the ECMAScript Date UTC accessors like Date.prototype.getUTCMinutes():

The time value is allowed to have fractions (sub-millisecond resolution) so that the millisecond component may also have fractions.

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Tools Plugin.

The biggest plugin in space...




Links
MBS Xojo blog