Platforms to show: All Mac Windows Linux Cross-Platform

Back to JSContextMBS class.

JSContextMBS.CheckScriptSyntax(script as string, sourceURL as String, startingLineNumber as Integer = 1, byref JSException as JSValueMBS) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Checks for syntax errors in a string of JavaScript.
Example
dim c as new JSContextMBS
dim e as JSValueMBS
if c.CheckScriptSyntax("1+", "", e) then
MsgBox "OK"
else
// show error

MsgBox e.StringValue
end if

Script: A string containing the script to check for syntax errors.
sourceURL: A string containing a URL for the script's source file. This is only used when reporting exceptions. Pass "" if you do not care to include source file information in exceptions.
startingLineNumber: An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1.
exception: A JSValue in which to store a syntax error exception, if any.

Returns true if the script is syntactically correct, otherwise false.

JSContextMBS.Constructor

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The constructor.

Creates a global JavaScript execution context.

JSContextMBS.EvaluateScript(script as string, sourceURL as String, thisObject as JSValueMBS, startingLineNumber as Integer = 1, byref JSException as JSValueMBS) as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Evaluates a string of JavaScript.
Example
dim c as new JSContextMBS
dim e as JSValueMBS
dim v as JSValueMBS = c.EvaluateScript("1+", "", nil, e)

if e <> nil then
// show error
MsgBox e.StringValue
else
// show result
MsgBox str(v.doubleValue)
end if

script: A string containing the script to evaluate.
thisObject: The object to use as "this," or nil to use the global object as "this."
sourceURL: A string containing a URL for the script's source file. This is used by debuggers and when reporting exceptions. Pass "" if you do not care to include source file information.
startingLineNumber: An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1.
exception: A JSValueMBS in which to store an exception, if any.

Returns the JSValue that results from evaluating script, or nil if an exception is thrown.

JSContextMBS.GarbageCollect

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Performs a JavaScript garbage collection.

JavaScript values that are on the machine stack, in a register, protected by JSValueProtect, set as the global object of an execution context, or reachable from any such value will not be collected.

During JavaScript execution, you are not required to call this function; the JavaScript engine will garbage collect as needed. JavaScript values created within a context group are automatically destroyed when the last reference to the context group is released.

JSContextMBS.NewArray(arguments() as JSValueMBS, byref JSException as JSValueMBS) as JSObjectMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript Array object.
Example
dim c as new JSContextMBS

dim e as JSValueMBS
dim v as JSObjectMBS = c.NewArray(nil, e)
v.SetPropertyAtIndex(0, c.valueWithString("Hello"), e)
v.SetPropertyAtIndex(1, c.valueWithString("World"), e)
MsgBox v.JSONString

arguments: A JSValue array of data to populate the Array with.
JSException: A JSValueMBS in which to store an exception, if any.
Returns a JSObject that is an Array.
The behavior of this function does not exactly match the behavior of the built-in Array constructor. Specifically, if one argument is supplied, this function returns an array with one element.
Requires Mac OS X 10.6 or newer.

JSContextMBS.NewDate(arguments() as JSValueMBS, byref JSException as JSValueMBS) as JSObjectMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript Date object, as if by invoking the built-in Date constructor.
Example
dim c as new JSContextMBS

dim year as JSValueMBS = c.valueWithDouble(2015)
dim month as JSValueMBS = c.valueWithDouble(5)
dim day as JSValueMBS = c.valueWithDouble(12)

dim e as JSValueMBS // exception
dim d as JSValueMBS = c.NewDate(array(year, month, day), e)

MsgBox d.JSONString

arguments: A JSValue array of arguments to pass to the Date Constructor.
JSException: A JSValueMBS in which to store an exception, if any.
Returns a JSObject that is a Date.
Requires Mac OS X 10.6 or newer.

JSContextMBS.NewError(arguments() as JSValueMBS, byref JSException as JSValueMBS) as JSObjectMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript Error object, as if by invoking the built-in Error constructor.
Example
dim c as new JSContextMBS

dim parameters() as JSValueMBS
Parameters.Append c.valueWithString("Hello")

dim ex as JSValueMBS
dim e as JSValueMBS = c.NewError(Parameters, ex)
MsgBox e.StringValue

arguments: A JSValue array of arguments to pass to the Error Constructor.
JSException: A JSValueMBS in which to store an exception, if any.

Returns a JSObject that is a Error.
Requires Mac OS X 10.6 or newer.

JSContextMBS.NewFunction(name as string) as JSObjectMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Convenience method for creating a JavaScript function which raises FunctionCalled event on invokation.

name: A string containing the function's name. This will be used when converting the function to string. Pass NULL to create an anonymous function.
Returns a JSObject that is a function. The object's prototype will be the default function prototype.

See also:

Some examples using this method:

JSContextMBS.NewFunction(name as string, parameterNames() as string, Body as String, SourceURL as string = "", startingLineNumber as Integer = 0, byref JSException as JSValueMBS) as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a function with a given script as its body.
Example
dim c as new JSContextMBS

// create a function
dim parameterNames() as string = array("value")
dim body as string = "return value*value;"
dim name as string = "test"

dim e as JSValueMBS
dim v as JSValueMBS = c.NewFunction(name, parameterNames, body, e )

MsgBox v.StringValue

// put it in global memory
c.globalObject.SetProperty "test", v, e

// and call it
dim r as JSValueMBS = c.EvaluateScript("test(5)", "", nil, e)
MsgBox r.StringValue

name: A string containing the function's name. This will be used when converting the function to string. Pass "" to create an anonymous function.
parameterNames: A string array containing the names of the function's parameters.
body: A string containing the script to use as the function's body.
sourceURL: A string containing a URL for the script's source file. This is only used when reporting exceptions. Pass "" if you do not care to include source file information in exceptions.
startingLineNumber: An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1.
exception: A JSValueMBS in which to store a syntax error exception, if any. Pass nil if you do not care to store a syntax error exception.
A JSObject that is a function, or nil if either body or parameterNames contains a syntax error. The object's prototype will be the default function prototype.
Use this method when you want to execute a script repeatedly, to avoid the cost of re-parsing the script before each execution.

See also:

JSContextMBS.NewObject as JSObjectMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a new object.

JSContextMBS.NewRegExp(arguments() as JSValueMBS, byref JSException as JSValueMBS) as JSObjectMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript RegExp object, as if by invoking the built-in RegExp constructor.

arguments: A JSValue array of arguments to pass to the RegExp Constructor.
JSException: A JSValueMBS in which to store an exception, if any.
Returns a JSObject that is a RegExp.
Requires Mac OS X 10.6 or newer.

JSContextMBS.valueWithBool(value as boolean) as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript value of the boolean type.
Example
dim c as new JSContextMBS
dim v as JSValueMBS = c.valueWithBool(true)
MsgBox v.JSONString

JSContextMBS.valueWithDouble(value as Double) as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript value of the number type.
Example
dim c as new JSContextMBS

dim v as JSValueMBS = c.valueWithDouble(5.6)
MsgBox v.StringValue

JSContextMBS.valueWithJSON(JSON as string) as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript value from a JSON formatted string.
Example
dim c as new JSContextMBS

dim v as JSValueMBS = c.valueWithJSON("[1,2,3]")
dim o as JSObjectMBS = JSObjectMBS(v) // arrays are objects

dim e as JSValueMBS
dim p as JSValueMBS = o.GetProperty("length", e)

MsgBox p.StringValue // shows 3

Returns a JSValue containing the parsed value, or nil if the input is invalid.
Available on Mac OS X 10.7 and newer

JSContextMBS.valueWithNull as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript value of the null type.
Example
dim c as new JSContextMBS
dim j as JSValueMBS = c.valueWithNull

if j.Type = JSValueMBS.kJSTypeNull then
MsgBox "null"
end if

JSContextMBS.valueWithString(value as string) as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript value of the string type.
Example
dim c as new JSContextMBS

dim v as JSValueMBS = c.valueWithString("Hello")
MsgBox v.StringValue

Some examples using this method:

JSContextMBS.valueWithUndefined as JSValueMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript MBS MacFrameworks Plugin 15.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a JavaScript value of the undefined type.
Example
dim c as new JSContextMBS
dim j as JSValueMBS = c.valueWithUndefined

if j.Type = JSValueMBS.kJSTypeUndefined then
MsgBox "undefined"
end if

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


The biggest plugin in space...