about how to use async-execution

70 views
Skip to first unread message

Ryosuke Saruwatari

unread,
Sep 19, 2023, 4:25:44 AM9/19/23
to Blockly
HI! I have  a question about how to use async-execution.
I copied the async-execution on github.(reference)
And I changed the code in one of function to the following code.
let runnerPid = 0;

function resetStepUi(clearOutput) {
      clearTimeout(runnerPid);
    }

function runCode() {
      if (!myInterpreter) {
        // First statement of this code.
        // Clear the program output.
        resetStepUi(true);
        const latestCode = javascript.javascriptGenerator.workspaceToCode(demoWorkspace);
        // And then show generated code in an alert.
        // In a timeout to allow the outputArea.value to reset first.
        setTimeout(function() {
          // Begin execution
          myInterpreter = new Interpreter(latestCode, initApi);
          function runner() {
            if (myInterpreter) {
              const hasMore = myInterpreter.run();
              if (hasMore) {
                // Execution is currently blocked by some async call.
                // Try again later.
                runnerPid = setTimeout(runner, 10);
              } else {
                // Program is complete.
                resetStepUi(false);
              }
            }
          }
          runner();
       
        }, 1);
        return;
     }
    }
This result was Uncaught TypeError: Cannot use 'in' operator to search for 'get' in 5.
I knew that my code was wrong if I got this error. But I don't know how to fix my code.
Please tell me this answer.
Thank you.

Neil Fraser

unread,
Sep 19, 2023, 5:39:53 AM9/19/23
to blo...@googlegroups.com
The code here looks fine to me.

Can you do a console.log(latestCode) to see what the interpreter is actually running?

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/160f7a95-b603-4e02-a88f-50ab7925c7e0n%40googlegroups.com.


--
Neil Fraser, Switzerland
https://neil.fraser.name

Ryosuke Saruwatari

unread,
Sep 19, 2023, 11:10:46 PM9/19/23
to Blockly
Thank you for answering my question, and I have to apologize for not explain enough.
Actually, The code works fine when I tried by 'console.log'. However, this doesn't works fine about async-execution and I got that error. 
I'm in trouble how to solve this problem.
2023年9月19日火曜日 18:39:53 UTC+9 Neil Fraser:

Neil Fraser

unread,
Sep 20, 2023, 4:06:56 AM9/20/23
to blo...@googlegroups.com
The error message you mention is
  TypeError: Cannot use 'in' operator to search for 'get' in 5.
I don't see anything in the code you provided that could generate that error, and I don't recognize that error as something from JS-Interpreter.  Thus I need to see the rest of the code to figure out whence the error originates.

There are two missing pieces of code which could be at fault:
1) The code which JS-Interpreter is attempting to execute (which may be obtained with console.log).
2) The initApi function.

Ryosuke Saruwatari

unread,
Sep 20, 2023, 5:02:12 AM9/20/23
to Blockly
Thank you for pointing it out. 
That is  attempting to execute code. I'm trying in 'start' block and 'wash' block.

1)
Blockly.Blocks['start'] = {
  init: function () {
    this.appendDummyInput()
     
      .appendField("料理スタート");
   
    this.setNextStatement(true, null);
    this.setColour(230);
    this.setTooltip("");
    this.setHelpUrl("");
  }
};
Blockly.JavaScript['start'] = function (block) {
 const j1 ="img/スタート.png";
 const j2 ="img/スタート.png";
const code2 =   'image(\"'+ j1+'\");\n';
const code3 =   'image2(\"'+ j2+'\");\n';
const code =code2 + code3;
  return code;
};


Blockly.Blocks['wash'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(new Blockly.FieldVariable("食材"), "NAME")
        .appendField("を洗う");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(230);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};
Blockly.JavaScript['wash'] =  function(block) {
  var variable_name = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('NAME'), Blockly.Names.NameType.VARIABLE);
  // TODO: Assemble JavaScript into code variable.
  switch(variable_name){

    case "_E3_81_98_E3_82_83_E3_81_8C_E3_81_84_E3_82_82":
  j1 = "img/じゃがいも.png";
  const code1 = 'image('+ j1+');\n';
  return code1;
   break;

   case "_E3_81_AB_E3_82_93_E3_81_98_E3_82_93":
  j2 = "img/にんじん.png";
 const code2 =   'image2('+ j2+');\n';
  return code2;
  break;
 
  default:
    j = "";
  }
};

And that is initapi code.

2)
 function initApi(interpreter, scope) {
  // Add an API function for highlighting blocks.
  var wrapper = function(text) {
    return alert(arguments.length ? text : '');
  };
  interpreter.setProperty(scope, 'alert',
      interpreter.createNativeFunction(wrapper));
      wrapper = function(text) {
    return prompt(text);
  };
  interpreter.setProperty(scope, 'prompt',
      interpreter.createNativeFunction(wrapper));

 var wrapperImage = function(name){
          name = name ? name.toString() : "";
          return image(name);
        };
        interpreter.setProperty(
            scope,
            'image',
            interpreter.createNativeFunction(wrapperImage)
        )
   
    var wrapperImage2 = function(name){
          name = name ? name.toString() : '';
          return image2(name);
        };
        interpreter.setProperty(
            scope,
            'image2',
            interpreter.createNativeFunction(wrapperImage2)
        )

        javascript.javascriptGenerator.addReservedWords('waitForSeconds');
     var wrapperwait =interpreter.createAsyncFunction(
      function(timeInSeconds, callback) {
      // Delay the call to the callback.
        setTimeout(callback, timeInSeconds * 1000);
      });
  interpreter.setProperty(
scope,
'waitForSeconds',
wrapperwait
);
      }
    function image(name){
    const img =  document.getElementById("image");
      img.src = name;
    }
    function image2(name){
    const img =   document.getElementById("image2");
      img.src = name;
    }

If only I use 'start' block, I don't get error. However I use two blocks, I must get that error.
Thank you.
2023年9月20日水曜日 17:06:56 UTC+9 Neil Fraser:
Reply all
Reply to author
Forward
0 new messages