skulpt / skulpt

Skulpt is a Javascript implementation of the Python programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Delay when stopping a python program

Matthieu-BRANTHOME opened this issue · comments

Hi,
I'm using skulpt to control a character with Python code in a plateform game.
I need to stop the execution when clicking on a "stop" button.
Based on this example: https://jsfiddle.net/sc549/bjLuq5s7/
I've done this:

   Sk.configure({
        output: outputAlert,
        killableWhile: true,
        killableFor: true,        
        __future__: Sk.python3
    });
[...]
     // Set stop execution flag to false
     stopExecution = false;
    // Empty the alert container 
    emptyAlertContainer();
    // Get the code form the code editor
    var code = codeEditor.getValue();
    console.log("Function evaluatePython - Code from editor: \n" + code);
    // Replace tab caracters by double spaces
    code = code.replaceAll("\t", "  ");
    console.log("Function evaluatePython - After tab replacing: \n" + code);
    // Check syntax errors with tigerPython Parser
    var err = TPyParser.checkSyntax(code);
    if (err !== null) {
        manageSyntaxicError(err);
    }
    else {
        outputAlert("Execution lancée");
        Sk.misceval.asyncToPromise(() =>
            Sk.importMainWithBody("<stdin>", false, code, true), {
            // handle a suspension of the executing code
            // "*" says handle all types of suspensions
            "*": () => {
                if (stopExecution) throw "stopExecution"
            }
        }).then(resolve => {
            manageExecutionEnding();
        }).catch(err => {
            if (err == "stopExecution") {
                manageStopExecution();
            }
            else {
                manageSemanticError(err.toString());
            }
        }).finally(() => {})
    }

A click on the stop button set stopExecution variable to true.

The problem is that when I set the stopExecution variable to true, the current instruction is not the last one, the next one is always executed before the stop.

Can anything be done about it?

Thank you in advance for your help!

Matthieu

I can add a clarification :
when executing a loop, for example:

for i in range(12): 
   instr1

there is no additional instruction executed.
On the other hand, when executing sequential instructions, for example:

intr1
intr2
intr3

there is an additional instruction executed...

I can add another element:
Behind the Python instructions (for example "gauche()" which means "left" in french) there is this:

    
    Sk.builtins.gauche = new Sk.builtin.func(function (param) {
        if(mountedApp.levelsDescription[mountedApp.currentLevel-1].allowedFunctions.indexOf("gauche")==-1){
            throw new Error("L'utilisation de la fonction gauche n'est pas autorisée pour ce niveau.");
        }
        else if (param != undefined) {
            throw new Error("La fonction gauche ne prend pas de paramètre.");
        }
        else {
            return new Sk.misceval.promiseToSuspension(promisePirate1Left().then(() => Sk.builtin.none.none$));
        }
    });
[...]
function promisePirate1Left() {
    return new Promise((resolve, reject) => {
        console.log("xPosition before left: " + pirate1.x);
        pirate1.setFlipX(true);
        setTimeout(() => {
            console.log("xPosition after left: " + pirate1.x);
            resolve("Pirate 1 turn left successfully");
        }, endLeftTO);
    });
}

By adding this parameter:

    Sk.configure({
        debugging: true,

I manage to get a possible stop after each instruction. My stop button now works perfectly :)

I would like to be able to set up a step-by-step execution, or at least to get the line being executed to highlight it in my code editor. Is it possible to do this?

Thank you for your help,

Matthieu

commented

There is an implementation of a debugger. But it isn't documented and might not be working on master (there are no tests for it).

There's a file here you can find
https://github.com/skulpt/skulpt/blob/9b6402f8be903ee5e2053b6acbfb9a108130f8be/doc/static/debugger/debugger_cm.js
With the original example implementation. But I don't know much else about it.

Closing this issue since the original question seems to be resolved.