Question

I'm making a site that teaches beginners programming. They will write some code in an editor, click a button to run it, after which a web worker starts to run t heir code.

I have answered in the comments for this question why I can't use eval().

My question now is, I have defined functions that expose the alert(), prompt() and confirm() dialogs, which works fine. However, the return values of the latter two are not returned to the web worker - even if I add return, I get Cannot read property 'toString' of undefined Is it possible to modify the code to handle their return values?

Full code: http://pastebin.com/bnE128DE

Relevant parts are in the runCode() method:

var userCode = editor.getValue();
        // append functions and variables for drawing on Canvas
var codeWithMessages = '\n\
    function print  (data) { postMessage(["print", data.toString()]); } \n\
    function println(data) { postMessage(["print", data.toString() + "\\n"]); } \n\
    function alert  (data) { postMessage(["alert", data.toString()]); } \n\
    function confirm(data) { return postMessage(["confirm", data.toString()]); } \n\
    function prompt (data) { return postMessage(["prompt", data.toString()]); } \n\
    \n\
    ...
' + userCode;
        var codeWithMessagesAndExitValues = codeWithMessages + "..."

    // handle messages, calling window functions or exit and handle user return vars
    outer.worker.addEventListener('message', function (event) {
        var method = event.data[0] || null;
        var data   = event.data[1] || null;


        if (method == "canvas") {
            // call canvas methods
        } 
        else if(method == "exit") {
            // close the worker and  handle user variables
        } 
        else if(method == "alert") {
            alert(data);
        } 
        else if(method == "confirm") {
            confirm(data);
        }
        else if(method == "prompt") {
            prompt(data);
        } 
        else if(method == "print") {
            if ($("#run-output").text() == editorController.noOutputMsg)
                $("#run-output").text("");
            $("#run-output").append(data);
        } 

Minimum example of user code:

print( prompt("Enter your name:") );

(entering anything will give the above error)

Était-ce utile?

La solution

I get Cannot read property 'toString' of undefined

That looks like you called the in-webworker prompt/confirm methods without a message, so that data was undefined. Not sure to say without the used code sample and the line number of the exception, though.

the return values of the latter two are not returned to the web worker

Well, why would they? Not even your message event handler does attempt to do anything with their return values.

Is it possible to modify the code to handle their return values?

No, since the worker-communication is asynchronous you never will be able to synchronous return values from a message.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top