Unable to generate JavaScript code for an function block

544 views
Skip to first unread message

Lucas Demarais

unread,
Feb 26, 2019, 2:00:32 PM2/26/19
to Blockly
Hello,

I am currently working on an app using Blockly.

In that app I need to generate JavaScript code for separate blocks instead of the whole workspace.
Here, I try to generate JavaScript for a function block.

I define the function in the Blockly workspace like this:

bloc_def.png








Then, I get the block like this:

getBloc.png








Now, I want to generate the JavaScript code of this block. I use the JavaScript.blockToCode function.
But when I do it I get this error message:

errorJS.png










I have no idea how to deal with that error, maybe I missed a step in the generating process ?
Does anyone know how I could resolve this ?

Thank you in advance.

Amber B

unread,
Feb 26, 2019, 2:24:36 PM2/26/19
to Blockly
Looks like Blockly.JavaScript.definitions_ was never inited. This gets inited when you call Blockly.JavaScript.init(workspace). Make sure to call that on your main workspace before you try to generate your code and you should be good.

Lucas Demarais

unread,
Feb 26, 2019, 2:40:23 PM2/26/19
to Blockly
Thank you for your quick response ! 

That was actually the problem, I have no error message anymore but now the only output that I get is an empty string and not the generated code...

Maybe you know why ?

Amber B

unread,
Feb 26, 2019, 2:45:45 PM2/26/19
to Blockly
*checks* Oh, that's interesting. It looks like the block to code definition for procedures actually doesn't return anything. It builds the code and sets it into Blockly.JavaScript.definitions_ and then returns null. There are a few options for how you could proceed, but it depends what all you need to do with the generated code from this block/what you're expecting it to look like (for example, are you expecting something that looks like myFunction(), or function(myFunction) {/*function info*/}, or?)

Lucas Demarais

unread,
Feb 26, 2019, 3:03:30 PM2/26/19
to Blockly
I expect something like :
function myFunction{
//Code of the inner blocks
}
Basically the same function definition I get when I generate the whole workspace code.

Amber B

unread,
Feb 26, 2019, 3:09:28 PM2/26/19
to Blockly
In your case, that would be in Blockly.JavaScript.definitions_['myFunction'] after you run blockToCode. I believe you can get the function name in general by using Blockly.JavaScript.variableDB_.getName(
      myFunctionBloc.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE), then use that as the key in Blockly.JavaScript.definitions_.

Coda Highland

unread,
Feb 26, 2019, 3:23:01 PM2/26/19
to blo...@googlegroups.com
I was dealing with this myself a few weeks ago. It's a real pain and while I think I understand why it's built this way from a correctness standpoint I find it frustrating that (1) the solution involves accessing an internal property and (2) the behavior violates the API documentation.

At first I was working around it by generating code for the inner blocks and synthesizing the function syntax around it. In the end I decided it wasn't worth maintaining that clunky code and switched to using the undocumented stuff.

/s/ Adam

On Tue, Feb 26, 2019 at 2:09 PM Amber B <abla...@citizendeveloper.com> wrote:
In your case, that would be in Blockly.JavaScript.definitions_['myFunction'] after you run blockToCode. I believe you can get the function name in general by using Blockly.JavaScript.variableDB_.getName(
      myFunctionBloc.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE), then use that as the key in Blockly.JavaScript.definitions_.

--
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.
For more options, visit https://groups.google.com/d/optout.

Amber B

unread,
Feb 26, 2019, 3:34:44 PM2/26/19
to Blockly
Oh yeah, I had to make peace with that long ago for using our own functions implementation (which we're going to have to completely redo at some point anyway because it turns out our implementation of local variables -- necessary for some parallel asynchronous functionality we added -- completely does not work with this function setup.) There's a reason that I immediately knew where to look for these problems, LOL. I agree that I wish that there were some more documented public-facing APIs for managing and accessing Blockly function definitions.

Lucas Demarais

unread,
Feb 26, 2019, 4:10:21 PM2/26/19
to Blockly
It worked ! Thank you very much !

Erik Pasternak

unread,
Feb 26, 2019, 6:58:26 PM2/26/19
to Blockly
The single block generation path should probably return code for functions. Filed https://github.com/google/blockly/issues/2317 to track.
Reply all
Reply to author
Forward
0 new messages