staticFields
This option was added in version 7.21.0.
Description
When set to true, this option enables static scoping for strongly typed fields and file definitions within the top-level function of a program.
Static scoping means that definitions remain in memory within the session and are reused if the program exits and is run again within the same session and activation group. All field and file state is retained between calls.
When this option is not set, top-level definitions are in automatic scope, meaning they are created and destroyed every time the function is called and returns. Definitions in other functions in the program are always in automatic scope.
This option affects the top-level function of all programs. Automatic scoping can still be selected for individual programs by adding a comment to the source code, as shown below.
Enabling in config.js
staticFields: trueExample: Static Fields Behavior
Because myStatic is static-scoped, its value survives between calls within the same session and activation group:
exports.activationGroup = "MYACTGRP";
function myProgram() {
pjs.define("myStatic", { type: "integer", length: 10, initValue: 1 });
myStatic += 1;
// Outputs 2 the first time the program is run.
// Then 3, 4, 5, etc. on subsequent calls within the same session.
console.log(myStatic);
}
exports.default = myProgram;Example: Destroying Static Fields With the LR Flag
A program with static-scoped fields can destroy them by setting flags["LR"] on before returning, which resets the field to its initial value on the next call:
exports.activationGroup = "MYACTGRP";
function myProgram() {
pjs.define("myStatic", { type: "integer", length: 10, initValue: 1 });
myStatic += 1;
console.log(myStatic); // Outputs 2 every time the program is run.
// Setting LR on before returning destroys the static fields.
flags["LR"] = true;
}
exports.default = myProgram;Example: Selecting Automatic Scope for an Individual Program
function myProgram() {
// The comment below enables automatic storage scope for top-level program fields.
// The comment can be placed anywhere in the source file.
// PROFOUNDJS_fieldsScope = automatic
pjs.define("myAuto", { type: "integer", length: 10, initValue: 1 });
myAuto += 1;
console.log(myAuto); // Outputs 2 every time the program is run.
}
exports.default = myProgram;For information on how to modify this setting, see Configuration File.