mussonindustrial / embr

Modules for Ignition by Inductive Automation 🔥
MIT License
11 stars 1 forks source link

Add ability to use functions in scriptable properties #37

Closed calebmusson closed 5 months ago

calebmusson commented 5 months ago
function parseFunctionString(
    value: string
): CallableFunction | undefined | string {
    if (
        typeof value === 'string' &&
        value &&
        (value.startsWith('function (') || value.startsWith('function('))
    ) {
        // eslint-disable-next-line prefer-const
        let fn: CallableFunction | undefined = undefined
        console.log(`function found: ${value}`)
        eval('fn = ' + value)
        return fn
    } else {
        console.log(`function not found: ${value}`)
        return value
    }
}
benmusson commented 5 months ago

Draft of function support added in b38b44cbe02a405ae159b97d4ca568ab1ed344c1. An alternative syntax return was used to search for scripts, for the following reasons:

  1. eval is very unsafe (as opposed to new Function() which is just normal unsafe).
  2. writing function (context, options){} takes up a lot of precious space in the property editor.

Currently all properties (including bound data) is scanned for scripts. Let's see what performance looks like, but I think we should at least black-list datasets[x].data from scanning.