szieke / ScriptCommunicator_serial-terminal

Scriptable cross-platform data terminal which supports: serial port, UDP, TCP, SPI, I2C and CAN.
https://sourceforge.net/projects/scriptcommunicator/
717 stars 187 forks source link

Question: intended behavior scriptThread.stopScript() #38

Closed flo80 closed 2 years ago

flo80 commented 2 years ago

When developing a script today, I wanted to stop execution when the user cancels a selection in a combo box. I called scriptThread.stopScript() but the execution did not stop (ran into an exception since the lookup of the non existent / empty selected value failed)

What is the intended way to completely stop the execution of a script? Is this possible at all? Or do I need to build a big chain of if/else to make sure I do not run into issues with not selected values?

szieke commented 2 years ago

scriptThread.stopScript() is the function to stop a script. Maybe you can upload your script and I can debug it.

flo80 commented 2 years ago

Here a simple example

const list = ["a", "b", "c"]

function getListItem() {
  const input_string = scriptThread.showGetItemDialog("Select", "test", list)
  if (input_string == "") {
    scriptThread.messageBox("Error", "Select", "Required")
    scriptThread.stopScript()
    return null
  }

  return list.indexOf(input_string)
}

var t = getListItem()
scriptThread.messageBox("Info", "Selected", t.toString())

Note: when I am not using a function the script also doesn't stop immediately (in this case it would show index -1 and not have an exception)

https://user-images.githubusercontent.com/12986622/180719636-54278aae-545b-418f-90da-b4f0fcf1c085.mov

szieke commented 2 years ago

The reason for the exception is return null. null is an undefined object. If you call a function (toString()) from an undefined object an exception is thrown. If you change null for example to -1 (number object) then the exception does not occur.

flo80 commented 2 years ago

My understanding was, the script would immediately abort after the stop and would never reach the line where it tries to deference the null? But from your explanation it sounds like the script will always continue to the end and there is no early exit? (This was not clear to me from the documentation and examples)

So if it is like this, calling stop in the middle of a script is not a pattern, just at the end is sufficient (given it had no endless loop). Correct?

On Mon 25. Jul 2022 at 17:50 Stefan Zieker @.***> wrote:

The reason for the exception is return null. null is an undefined object. If you call a function (toString()) from an undefined object an exception is thrown. If you change null for example to -1 (number object) then the exception does not occur.

— Reply to this email directly, view it on GitHub https://github.com/szieke/ScriptCommunicator_serial-terminal/issues/38#issuecomment-1194257500, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADDCR7UOJ6NWDM234OYMCALVV2ZURANCNFSM54JGFE4Q . You are receiving this because you authored the thread.Message ID: @.*** com>

szieke commented 2 years ago

To be honest, I thought stopScript would exit the script directly because it calls the exit function of the thread which executes the script (every script is executes in its own thread). But the code that is not in a slot function (slot functions are called if a signal is emitted (e.g. from a timer or data reception)) is executed by the script interpreter before the exec function of the thread is called. In other words, if you call stop script in code that is not in a slot function then the script does stop if the end of the script is reached, if called in a slot function the script stops immediately. I will change this behavior in the next release (if I cannot change it then I will describe it in the documentation).

szieke commented 2 years ago

Hi, I fixed this issue (on the master branch). If a script calls stopScript then the execution is stopped immediately. I will create a new release next week. Sorry for fixing this so late, I was really busy the last months.