Topics   All   MacOS (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Guides   Examples
New in version: 9.0   9.1   9.2   9.3   9.4   9.5   10.0   10.1   10.2   10.3    Statistic  

JS.Evaluate

Evaluates a JavaScript expression.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
JavaScript 10.0 Yes Yes Yes Yes Yes
MBS( "JS.Evaluate"; JSRef; JavaScript )   More

Parameters

Parameter Description Example value
JSRef The JavaScript reference number. $js
JavaScript A JavaScript expression. "1+2"

Result

Returns JSON or error.

Description

Evaluates a JavaScript expression.
This may include definition of functions and objects and using them.
Result is converted to JSON and returned.

Examples

Evaluate some expressions:

Set Variable [ $js ; Value: MBS( "JS.New" ) ]
Set Variable [ $r ; Value: MBS( "JS.Evaluate"; $js; "4+5") ]
Show Custom Dialog [ "Result" ; $r ]
Set Variable [ $r ; Value: MBS( "JS.SetGlobalProperty"; $JS; "test"; "\"Hello World\"" ) ]
Set Variable [ $r ; Value: MBS( "JS.Evaluate"; $js; "test + test;") ]
Show Custom Dialog [ "Result" ; $r ]
Set Variable [ $r ; Value: MBS( "JS.Free"; $JS ) ]

Defines and runs a function in JavaScript:

Let([
f = "
function makeCRCTable(){
        var c;
        var crcTable = [];
        for(var n =0; n < 256; n++){
                c = n;
                for(var k =0; k < 8; k++){
                        c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
                }
                crcTable[n] = c;
        }
        return crcTable;
}

function crc32(str) {
        var crcTable = crcTable || (crcTable = makeCRCTable());
        var crc = 0 ^ (-1);

        for (var i = 0; i < str.length; i++ ) {
                crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
        }

        return (crc ^ (-1)) >>> 0;
};

crc32(InputValue);";
j = MBS( "JS.New" );
r = MBS( "JS.SetGlobalPropertyValue"; j; "InputValue"; "Hello World" );
r = MBS( "JS.Evaluate"; j; f );
x = MBS( "JS.Free"; j )
];r)

Initialize JavaScript environment with two functions:

If [ Length($$js) = 0 ]
    Set Variable [ $$js ; Value: MBS( "JS.New" ) ]
    Set Variable [ $r ; Value: MBS( "JS.Evaluate"; $$js;
"function makeCRCTable(){
                var c;
                var crcTable = [];
                for(var n =0; n < 256; n++){
                                c = n;
                                for(var k =0; k < 8; k++){
                                                c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
                                }
                                crcTable[n] = c;
                }
                return crcTable;
}

function crc32(str) {
                var crcTable = crcTable || (crcTable = makeCRCTable());
                var crc = 0 ^ (-1);

                for (var i = 0; i < str.length; i++ ) {
                                crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
                }

                return (crc ^ (-1)) >>> 0;
};") ]
End If

List global properties:

MBS( "JS.Evaluate"; $$JS; "Object.keys(globalThis);" )

Query now in JavaScript:

MBS( "JS.Evaluate"; $$JS; "performance.now();" )

Measure tikem for a test function:

MBS("JS.Evaluate"; $js; "function testFunction() {
        for (var i = 0; i < 1e6; i++) {}
}

var t1 = performance.now();
testFunction();
var t2 = performance.now();

('test took: ' + (t2 - t1) + ' milliseconds');")

See also

Example Databases

Blog Entries

Created 7th December 2019, last changed 30th January 2020


JS.CallFunction   -   JS.EvaluateToString

Feedback: Report problem or ask question.




Links
MBS Xojo tutorial videos