Returning values from the JS-interpreter

197 views
Skip to first unread message

Richard M

unread,
Jun 2, 2019, 7:45:45 PM6/2/19
to Blockly
Hi all

I'm really stuck. I need some help with returning a value from an async function called from the JS-interpreter. ( https://neil.fraser.name/software/JS-Interpreter/docs.html  )

I am using the async-interpreter demo, modifying the main html to load a new javascript file and to run the code. The new block can be placed and the prompt shows the code:

block.jpg


prompt.jpg












This is where the interpreter is falling over when run:
error.jpg










This is what I have:


//////////////////////////////////
// within index.html: (this loads fine)
...
<script src="url_block.js"></script>
...
function initApi(interpreter, scope) {
// Add an API for the url block. See get_url_block.js
initInterpreterGetURL
(interpreter, scope);
...


This is the javascript file loaded:

(The URL is just one I found to test stuff like POSTs and GETs, etc.)

//////////////////////////////////
// get_url_block.js:  (this loads fine, but crashes when run)

Blockly.defineBlocksWithJsonArray([{
 
"type": "url_get",
 
"message0": "Return a URL GET",
 
"output": "String",
 
"colour": 60,
 
"tooltip": "",
 
"helpUrl": ""
}]);

Blockly.JavaScript['url_get'] = function(block) {
 
// this is a url test site:
 
var url = "https://postman-echo.com/get?foo1=123";
 
var code = 'getURL(' + url + ');\n';
 
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};

function initInterpreterGetURL(interpreter, scope) {

 
Blockly.JavaScript.addReservedWords('getURL');
 
var wrapper = interpreter.createAsyncFunction(
   
function(href, callback) {
     
var req = new XMLHttpRequest();
      req
.open('GET', href, true);
      req
.onreadystatechange = function() {
       
if (req.readyState == 4 && req.status == 200) {
          callback
(req.responseText);
       
}
   
};
 
});
  interpreter
.setProperty(scope, 'getURL', wrapper);
}


I'm out of ideas how to debug this further. Is there anyone here able to lend some help? I get the feeling the solution is somewhere on the spectrum from "Really Easy" to "Really Hard" :-(
Thanks.

Neil Fraser

unread,
Jun 2, 2019, 10:19:14 PM6/2/19
to blo...@googlegroups.com
Acorn reporting a syntax error just means that the code you are attempting to execute with the JS Interpreter is syntactically malformed.  Do a console.log(code) just before creating the Interpreter and find out what you are attempting to execute.  Once you can see that, fixing this particular issue should be "really easy".  :)

--
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/e54ec7d3-b0a0-475e-ab45-38d62eed80c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Neil Fraser

unread,
Jun 2, 2019, 10:22:24 PM6/2/19
to blo...@googlegroups.com
Oh, and I can see the syntax error. Your block is a value block, meaning it returns a value.  But your generated code is a statement.  Just trim off the semicolon and new line here:

var code = 'getURL(' + url + ');\n';

Casper

unread,
Jun 3, 2019, 5:47:23 AM6/3/19
to Blockly
Ahh... thank you so much. From the console.log, I saw the url wasn't a string - it had no quotes. I think that was where it was initially breaking, although I would have taken me ages to figure out the value vs. statement part. 

I'm not out of the woods yet, the code seems to be hanging. As I said, I'm building it over the async demo. Maybe I should get away from that and make my own html code because I think that's the problem, so I'll persevere for a while before I pester the forum. 

Thanks once again. The last time I did a lot of Javascript I don't remember having console logs and such debugging.  (That was around 1998!)



On Monday, June 3, 2019 at 2:22:24 PM UTC+12, Neil Fraser wrote:
Oh, and I can see the syntax error. Your block is a value block, meaning it returns a value.  But your generated code is a statement.  Just trim off the semicolon and new line here:

var code = 'getURL(' + url + ');\n';

On Sun, 2 Jun 2019 at 19:18, Neil Fraser <ro...@neil.fraser.name> wrote:
Acorn reporting a syntax error just means that the code you are attempting to execute with the JS Interpreter is syntactically malformed.  Do a console.log(code) just before creating the Interpreter and find out what you are attempting to execute.  Once you can see that, fixing this particular issue should be "really easy".  :)

To unsubscribe from this group and stop receiving emails from it, send an email to blo...@googlegroups.com.

Mark Friedman

unread,
Jun 3, 2019, 10:35:55 PM6/3/19
to blo...@googlegroups.com
There may be other issues, but syntactically one thing that stands out to me is that your generated code string will contain an unquoted URL, so you'll need to change that so that the generated code has quotes around the URL.

--
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.
Reply all
Reply to author
Forward
0 new messages