Class WinccoaCtrlScript

Class that represents a CTRL script that can be called from JavaScript/TypeScript. The necessary steps to call a CTRL function are:

  1. Create an instance of WinccoaCtrlScript.
  2. Call a function in the script with start. This can be done several times, also for different functions contained in the script.
import {
WinccoaCtrlScript,
WinccoaCtrlType,
WinccoaManager,
} from 'winccoa-manager';
const winccoa = new WinccoaManager();

async function ctrlScriptExample() {
// create instance of script with CTRL code to execute
const script = new WinccoaCtrlScript(
winccoa,
`dyn_string main(string pattern, bool empty = true)
{
return dpTypes(pattern, getSystemId(), empty);
}`
);

try {
// call the script with two parameters
const types = await script.start(
'main',
['Ex*', false],
[WinccoaCtrlType.string, WinccoaCtrlType.bool]
) as string[];

// show result
console.warn(types);
} catch (exc) {
console.error(exc);
}
}

Constructors

  • Constructor. Creates an instance and initializes it with the CTRL code that will be executed. A function in the script must then be started with a call to start. For an example, see WinccoaCtrlScript.

    Parameters

    • manager: WinccoaManager

      Manager instance that is used for getting the user ID and options to use for the subsequent call of start

    • ctrlCode: string

      CTRL code for the script. Must contain at least one CTRL function.

    Returns WinccoaCtrlScript

    WinccoaError when manager is not the correct type or ctrlCode contains a syntax error.

    WinccoaOptions

Methods

  • Returns the number of currently open Promises.

    Returns number

    Number of Promises currently waiting for a function call to terminate.

  • Calls a CTRL function in the script set with the constructor. For each call, a new CTRL thread is created. For an example, see WinccoaCtrlScript.

    Parameters

    • functionName: string = 'main'

      Name of the CTRL function to call.

    • functionParams: unknown[] = []

      Parameters for the CTRL function.

    • paramTypes: unknown[] | WinccoaCtrlType[] = []

      Types of the parameters as expected by the CTRL function. Note that in this context, it is not possible to determine the required parameter types automatically, therefore it is necessary to specify them explicitely.

      For CTRL types that can contain different types (e.g. anytype, mixed, mapping), the concrete types to which the values have to be converted before they are passed to CTRL have to be provided.

      For dynamic arrays containing a single datatype, it is recommended and more efficient to use the specific type, e.g. WinccoaCtrlType.dyn_float.

    • Optionalcallback: WinccoaCtrlCallback

      If given, this function can be called from the CTRL script by using the CTRL function callbackToJavaScript(). See WinccoaCtrlCallback for details and examples.

    Returns Promise<unknown>

    Promise for the return value. Resolved when the function returns or rejected when the script is stopped (see stop).

    WinccoaError when parameters cannot be properly converted or when a severe error happened in the script (which also stops the execution of the script). Note that CTRL warnings are only logged, but do not stop the execution of a script.

    import {
    WinccoaCtrlScript,
    WinccoaCtrlType,
    WinccoaManager,
    } from 'winccoa-manager';
    const winccoa = new WinccoaManager();

    // for a simple argument list, just list the argument types
    const ctrlCode = `double simpleArgs(int x, double y, string s)
    {
    return (x * y) + s.length();
    }`;
    const script = new WinccoaCtrlScript(winccoa, ctrlCode);
    const result = await script.start(
    'simpleArgs',
    [12, 34.56, 'some text'],
    [WinccoaCtrlType.int, WinccoaCtrlType.double, WinccoaCtrlType.string]
    ) as number;
    import {
    WinccoaCtrlScript,
    WinccoaCtrlType,
    WinccoaManager,
    } from 'winccoa-manager';
    const winccoa = new WinccoaManager();

    // for a dynamic array with a fixed type, use the corresponding type.
    const ctrlCode = `int dynArg(dyn_string strings)
    {
    for (int i = 0; i < strings.count(); i++)
    if (strings.at(i) == "found")
    return i;
    return -1;
    }`;
    const script = new WinccoaCtrlScript(winccoa, ctrlCode);
    const result = await script.start(
    'dynArg',
    [['can', 'this', 'thing', 'be', 'found', 'in', 'here']],
    [WinccoaCtrlType.dyn_string]
    ) as number;
    import {
    WinccoaCtrlScript,
    WinccoaCtrlType,
    WinccoaManager,
    } from 'winccoa-manager';
    const winccoa = new WinccoaManager();

    // for a dyn_anytype or dyn_mixed, list the types of all items
    const ctrlCode = `string dynAnytypeArg(dyn_anytype things, bool other)
    {
    if (!ignore)
    return "[" + things.count() + "] " + things;
    else
    return "";
    }`;
    const script = new WinccoaCtrlScript(winccoa, ctrlCode);
    const result = await script.start(
    'dynAnytypeArg',
    [['title', 4711], false],
    [[WinccoaCtrlType.string, WinccoaCtrlType.int], WinccoaCtrlType.bool]
    ) as string;
    import {
    WinccoaCtrlScript,
    WinccoaCtrlType,
    WinccoaManager,
    } from 'winccoa-manager';
    const winccoa = new WinccoaManager();

    // for a mapping, list the types of all fields
    const ctrlCode = `mapping mappingArg(mapping values)
    {
    values["square"] = values["a"] * values["a"];
    values["result"] = values["title"] + ": " + values["square"];
    return values;
    }`;
    const script = new WinccoaCtrlScript(winccoa, ctrlCode);
    const result = await script.start(
    'mappingArg',
    [{ title: 'This is the square', a: 3.01 }],
    [{ title: WinccoaCtrlType.string, a: WinccoaCtrlType.float }]
    );
    import {
    WinccoaCtrlScript,
    WinccoaCtrlType,
    WinccoaManager,
    } from 'winccoa-manager';
    const winccoa = new WinccoaManager();

    // for nested arguments, repeat the structure of the arguments,
    replacing the values with their types
    const ctrlCode = `void nestedArg(dyn_anytype things)
    {
    DebugTN(things);
    }`;
    const script = new WinccoaCtrlScript(winccoa, ctrlCode);
    await script.start(
    'nestedArg',
    [
    [
    [1, 2, 3],
    { a: 4.3, b: 'text' },
    ['something', 12.34, { yes: true, no: false }],
    ],
    ],
    [
    [
    WinccoaCtrlType.dyn_int,
    { a: WinccoaCtrlType.float, b: WinccoaCtrlType.string },
    [
    WinccoaCtrlType.string,
    WinccoaCtrlType.float,
    { yes: WinccoaCtrlType.bool, no: WinccoaCtrlType.bool },
    ],
    ],
    ]
    );
    import {
    WinccoaManager,
    WinccoaCtrlScript,
    WinccoaCtrlType,
    } from 'winccoa-manager';
    const winccoa = new WinccoaManager();

    async function parallelCtrlThreads() {
    const script = new WinccoaCtrlScript(
    winccoa,
    `int countMilliSeconds(int count, int delayMs)
    {
    for (int i = 1; i <= count; i++)
    delay(0, delayMs);
    return count * delayMs;
    }

    int countSeconds(int count, int delaySec)
    {
    for (int i = 1; i <= count; i++)
    delay(delaySec);
    return count * delaySec * 1000;
    }`,
    );

    // start three parallel CTRL threads and collect all results
    const result = await Promise.allSettled([
    script.start(
    'countMilliSeconds',
    [10, 250],
    [WinccoaCtrlType.int, WinccoaCtrlType.int],
    ),
    script.start(
    'countSeconds',
    [5, 1],
    [WinccoaCtrlType.int, WinccoaCtrlType.int],
    ),
    script.start(
    'countSeconds',
    [3, 2],
    [WinccoaCtrlType.int, WinccoaCtrlType.int],
    ),
    ]);

    console.info(result);
    }
  • Stops all threads currently running in this instance.

    For stopping running threads, the call to stop must come from a different part of the code than the await start. (e.g. from a setTimeout callback). An alternative to that is to use start without await and handle the Promise later (e. g. with Promise.allSettled());

    Returns void

    import {
    WinccoaCtrlScript,
    WinccoaCtrlType,
    WinccoaManager,
    } from 'winccoa-manager';
    const winccoa = new WinccoaManager();

    const ctrlScript = `int count(int tenths)
    {
    int i;
    for(i = 1; i <= tenths; i++)
    delay(0, 100);
    return i - 1;
    }`;
    const script = new WinccoaCtrlScript(winccoa, ctrlScript);

    // stop script after 2 seconds
    setTimeout(() => script.stop(), 2000);

    try {
    result = await script.start('count', [500], [WinccoaCtrlType.int]) as number;
    } catch (exc) {
    // interrupted
    }