Menu

Using a state-machine with SC

devnull
2024-01-16
2024-01-19
  • devnull

    devnull - 2024-01-16

    Hi, i'm learning SC and how to use worker-scripts (with UI) for some sniffing/debuging tasks with UART.
    I'd like to make use of some FSM (Finite State Machine) like XState. In the first i wrote a very basic approach from scratch, but feel to spend too much time doing basic stuff that it already available on SF or Github for Javascript.
    But even some codesnippets from stackoverflow won't do, maybe because the ECMA standard supported by SC is too old? (What level/year does it support anyway?)

     
  • Stefan Zieker

    Stefan Zieker - 2024-01-17

    Hi,

    JavaScript is based on ECMA. But it extends it. And on top, some browsers add there own JavaScript functions that other browsers don't have. Therefore you cannot execute all JavaScript code with ScriptCommunicator.

    Can you upload the code that does not work ?

     

    Last edit: Stefan Zieker 2024-01-17
  • devnull

    devnull - 2024-01-17

    Sure, this is a code-snippet of a super-simple FSM written as Lambda:

    function createMachine(stateMachineDefinition)
    {
        const machine = {
            value: stateMachineDefinition.initialState,
            transition(currentState, event) {
                const currentStateDefinition = stateMachineDefinition[currentState]
                const destinationTransition = currentStateDefinition.transitions[event]
                if ( ! destinationTransition) {
                    return
                }
                const destinationState = destinationTransition.target
                const destinationStateDefinition = stateMachineDefinition[destinationState]
                destinationTransition.action()
                currentStateDefinition.actions.onExit()
                destinationStateDefinition.actions.onEnter()
                machine.value = destinationState
                return machine.value
            }
        }
        return machine
    }
    

    The code runs in the browser but if put into the code editor of SC you get an error:

     

    Last edit: devnull 2024-01-17
  • devnull

    devnull - 2024-01-17

    I don't get all the fine differences in ECMA-262. Is there a page to know which command is supported in latest version of SC and which not?

    This is a code-snippet of a super-simple FSM written as Lambda which won't run in SC:

    function createMachine(stateMachineDefinition)
    {
        const machine = {
            value: stateMachineDefinition.initialState,
            transition(currentState, event) {
                const currentStateDefinition = stateMachineDefinition[currentState]
                const destinationTransition = currentStateDefinition.transitions[event]
                if ( ! destinationTransition) {
                    return
                }
                const destinationState = destinationTransition.target
                const destinationStateDefinition = stateMachineDefinition[destinationState]
                destinationTransition.action()
                currentStateDefinition.actions.onExit()
                destinationStateDefinition.actions.onEnter()
                machine.value = destinationState
                return machine.value
            }
        }
        return machine
    }
    

    The code runs in the browser but if put into the code editor of SC

     

    Last edit: devnull 2024-01-17
  • Stefan Zieker

    Stefan Zieker - 2024-01-18

    The JavaScript functions supported by ScriptCommunicator can be found here: https://wiki.qt.io/JavaScript

    The ScriptEditor is just a simple editor deleloped by me. The intention was to have an editor for simple script (like these in the example folder). I had/have no time to implement an editor that fully supports the modern JavaScript.

    ScriptCommunicator and the ScriptEditor are open source. So you can do it yourself if you like.

     
  • devnull

    devnull - 2024-01-19

    All fine. So you mean the script code i pasted above would run, even if it's not shown correctly in your editor? The good thing on your editor is the UI integration.

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.