Generating code with variables that are not declared

525 views
Skip to first unread message

Tim Shnaider

unread,
Nov 1, 2017, 4:25:34 AM11/1/17
to Blockly
I've had a look through a number of posts and none seem to simply answer this.

I'm using Blockly as a rules editor.
I want to pass in x and y values (example names) to generated JS code.

The generated JS code already generates declarations for all variables e.g. 'var x,y'.

Is there a way to turn off declarations, or a custom variable block that doesn't result in var declarations?

The generated code will then be wrapped in a "function(x,y) { <code> }" and called with input var.

Looking through javascript generator source it looks like variables returned from getAllVariables() always results in declarations in Blockly.Javascript.init().

Thinking I might have to modify Blockly.Javascript.init() to achieve this, testing for a special var type maybe?

Alternatively I guess I can just strip out var declarations from the generated code, using Esprima/Acorn maybe..





Tim Shnaider

unread,
Nov 1, 2017, 5:10:34 AM11/1/17
to Blockly
Posted a comment here as related:

Rachel Fenichel

unread,
Nov 1, 2017, 7:36:53 PM11/1/17
to Blockly
No, we don't have a way to mark variables as not needing declarations.  I can see how this is useful in your case, though.

I think the best way to do it is to edit Blockly.JavaScript.init, as you mentioned.  I would add an array of "predeclared" variable names to the generator.  In init you would check whether a variable is in the "predeclared" list, and not add it to the defvars array if it's predeclared.  

Note that you will want to use defvars.push() instead of defvars[i], since you'll be skipping some indices.

Hope this helps,
Rachel

Tim Shnaider

unread,
Nov 1, 2017, 7:47:11 PM11/1/17
to Blockly
Great, thanks for the tip.

Me doing a PR worthwhile? 
Bake support for marking a variable non-declared into core and generators.

I'm a little surprised it's never been asked about before.


Tim Shnaider

unread,
Nov 1, 2017, 8:10:30 PM11/1/17
to Blockly
Just realised I will have to prevent rename or delete or create get/set for predefined variables in the editor.


Any Blockly features that make this easy without mods - custom block?

I'm not lazy to research, just a lot of API reference and guides to get through and real world testing, 
so any tips, yes/no will be a massive time saver.

Thanks,
Tim

Rachel Fenichel

unread,
Nov 1, 2017, 8:32:05 PM11/1/17
to Blockly
We don't have a builtin way to prevent deletion, no.  I think you'd have to reach into blockly core or create custom blocks that use distinct variable types.  

If you want to do it with custom blocks, the link above is a good place to start.

If you want to do it in core: 
1) Prevent renaming away from these names, by checking variable name against a list here: https://github.com/google/blockly/blob/master/core/variables.js#L284
2) When you create your workspace, call myWorkspace.createVariable(variableName) for each predeclared variable that you need, and that will make them show up in the flyout
3) In field_variable.js, check the selected variable and don't push the delete variable option into the dropdown if the variable is in the predeclared list.

I'm not sure about a PR (I need to think through some edge cases), but at a minimum you should open an issue on our repo for each of these two feature requests (predeclared variables and nondeletable variables) so that we don't forget about them and have a place to discuss.

Cheers,
Rachel

Tim Shnaider

unread,
Nov 1, 2017, 8:34:54 PM11/1/17
to Blockly
Awesome, thanks Rachel.

Rachel Fenichel

unread,
Nov 2, 2017, 4:47:45 PM11/2/17
to Blockly
No problem!  Please go ahead and file those two feature requests as well.

Cheers,
Rachel

On Wednesday, November 1, 2017 at 5:34:54 PM UTC-7, Tim Shnaider wrote:
Awesome, thanks Rachel.

Tim Shnaider

unread,
Nov 2, 2017, 5:18:21 PM11/2/17
to Blockly
I will.

But I have also forked and starting on my own branches as I need support today.

My intention is to do two branches:
* Predefined variable support (easy)

* Augmenting the Variable model to support "permissions" - e.g. ['get','set','create-getter','create-setter','rename','delete'] and whatever else might be relevant 
(a bit of work)
It's possible that custom blocks already provide a framework to support this to some extent.
I will start digging into the guides and API and code shortly
If you have any quick thoughts on that please do share.

Tim Shnaider

unread,
Nov 2, 2017, 5:57:10 PM11/2/17
to Blockly
Does an extension of the setXXXX e.g. setMovable limited controls to a generalised permissions framework seem like a good idea?

I will create issues on repo next week after I've done my needed dev for a deadline.
Also will be much more informed at that point.

Tim Shnaider

unread,
Nov 3, 2017, 4:46:32 AM11/3/17
to Blockly
Rachel I've implemented my changes to variables and works.
I'll push a PR in case it's useful anyway.

Though having explored more, in hindsight a custom block for each read only predefined var, copying random_number as the simplest block def possible,
looks like it would have done the job nicely!

Thanks for all your help.

Nicely architected code, very readable and easy to follow.


On Friday, November 3, 2017 at 9:47:45 AM UTC+13, Rachel Fenichel wrote:

Rachel Fenichel

unread,
Nov 3, 2017, 1:36:59 PM11/3/17
to Blockly
Thanks Tim, I'll take a look.  Note that variables are a pretty touchy part of Blockly, so we may be slow to add more features to them.

Cheers,
Rachel

Tim Shnaider

unread,
Nov 3, 2017, 6:48:32 PM11/3/17
to Blockly
Thanks again for your help Rachel.  
Last update, no need to reply.

I don't regret doing the Variable mods as it helped understand Blockly a lot more quickly, 
but a custom block was exactly all I needed.

Something to recommend if anyone else ever asks about custom variables.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages