Xbasic

Xbasic_execute_Javascript Function

Syntax

dim result as p = Xbasic_execute_Javascript(jscode as c, [l flagSandbox [, l validateCode[, l openConsoleWindow]]])

Arguments

jscodeCharacter

The JavaScript code to execute.

flagSandboxLogical

Default = .f.. If .t., the JavaScript executes in a sandbox that does not permit access to the filesystem or using the Node.js require() function.

validateCodeLogical

Default = .f.. If .t. the syntax of the JavaScript to be executed is validated and any syntax errors are reported.

openConsoleWindowLogical

Default = .f.. If .t., a window is opened and all output generated by console.log() functions in the JavaScript to be executed are shown.

Returns

resultPointer

Returns an object with the following properties:

errorLogical

True/false indicating if there was an error.

errorTextCharacter

Error text if there was an error.

resultAny Type

The value returned by the Javascript code if there was no error.

Description

Execute JavaScript code on the server side using the JavaScript engine in Node.js.

Discussion

The Xbasic_execute_Javascript() function can be used to execute JavaScript from within an Xbasic script using the Node.js engine.

If you want the JavaScript code to return a value, the JavaScript code must emit the value by specifying the name of the variable to emit as the last line in the JavaScript code. For example:

Example

dim js as c
js = <<%str%
var arr = [];
for(var i = 0; i < 1000; i++) {
    arr.push('String: ' + i );
}
var txt = arr.join('\n');

//we want to return the value of the 'txt' variable, so we emit it by specifying
//the variable name as the list line in the JavaScript code
txt;
%str%

pr = xbasic_execute_javascript(js)

if pr.error = .f. then
    showvar(pr.result)
else
    showvar(pr.errorText)
end if