"Deprecated call to Blockly.Names.prototype.getName without defining a variable map."

380 views
Skip to first unread message

Coda Highland

unread,
Feb 4, 2019, 12:36:45 AM2/4/19
to Blockly
I'm trying to integrate Blockly into a new project I'm working on, and for the most part it's been working, but now that I'm trying to use the built-in variables_set block in a program I'm getting the following warning in the console:

Deprecated call to Blockly.Names.prototype.getName without defining a variable map. To fix, add the folowing code in your generator's init() function:
Blockly.YourGeneratorName.variableDB_.setVariableMap(workspace.getVariableMap());

I'm using the JavaScript generator, and I'm definitely calling Blockly.JavaScript.init(workspace), and the above line is definitely present inside the JavaScript generator.

In trying to debug it, I found this behavior strange:

    console.log('before', Blockly.JavaScript.variableDB_.variableMap_);
    const workspaceCode = Blockly.JavaScript.workspaceToCode(this.workspace);
    console.log('after', Blockly.JavaScript.variableDB_.variableMap_);

The "before" line shows a proper variableMap. The "after" line returns null. This causes any later calls into the generator to exhibit the issue. In this case, I show the user what code a selected block generates using blockToCode, and it fails. If I remove the call to workspaceToCode, it succeeds.

Why would this behavior be present? What do I need to do to make this work correctly?

/s/ Adam

Beka Westberg

unread,
Feb 4, 2019, 3:26:26 PM2/4/19
to Blockly
Hello Adam,
 
Would you mind navigating to the demos > code > index.html page in your instance of blockly, and see if that gives the same error when you generate some javascript?

If it does, could you post a full stack trace of the error it's throwing?

If it does not throw an error, would you mind posting a bit more of your code generation... code...?

It's probably just something not getting initialized quite correctly, once we have a bit more information I'm sure someone here can help tackle it :D
Thanks!
Beka

Coda Highland

unread,
Feb 4, 2019, 5:12:53 PM2/4/19
to blo...@googlegroups.com
It's not throwing; it's logging a warning and going through a
different code path. But that's okay; I can set a breakpoint and get a
trace that way:

(anonymous)@VM177:1
Blockly.Names.getNameForUserVariable_@blockly_compressed.js:formatted:24568
Blockly.Names.getName@blockly_compressed.js:formatted:24574
Blockly.JavaScript.variables_set@javascript_compressed.js:95
Blockly.Generator.blockToCode@blockly_compressed.js:formatted:24450
Blockly.Generator.statementToCode@blockly_compressed.js:formatted:24499
Blockly.JavaScript.controls_if@javascript_compressed.js:41
Blockly.Generator.blockToCode@blockly_compressed.js:formatted:24450
(anonymous)@VM155:1

I'm using current git master, unmodified, and I can reproduce it using
demos/code/index.html. It doesn't happen with the demo as-is since the
demo only ever uses Blockly.JavaScript.workspaceToCode to generate
code, but the following steps reproduce it consistently:

1. Open the demo.
2. Create a variable named "foo".
3. Add a "set foo to" block to the workspace.
4. Attach a "123" block from Math.
5. Switch to the JavaScript tab. Observe that the code is generated correctly.
6. Return to the Workspace tab.
7. Open the Inspector console.
8. Execute this statement:
> Blockly.JavaScript.blockToCode(Blockly.mainWorkspace.topBlocks_[0])
9. Observe the warning:
> Deprecated call to Blockly.Names.prototype.getName without defining a variable map. To fix, add the folowing code in your generator's init() function:
> Blockly.YourGeneratorName.variableDB_.setVariableMap(workspace.getVariableMap());
10. Observe that the generated code uses a unique ID instead of the
human-readable variable name.

This is not limited to the JS generator; I can reproduce it with the
other generators as well.

Note that if you skip step 5, you instead get the following exception:

Uncaught TypeError: Cannot read property 'getName' of undefined at
Blockly.BlockSvg.Blockly.JavaScript.variables_set
(javascript_compressed.js:95) at Blockly.Generator.blockToCode
(blockly_compressed.js:1585) at <anonymous>:1:20
Blockly.JavaScript.variables_set@javascript_compressed.js:95
Blockly.Generator.blockToCode@blockly_compressed.js:1585
(anonymous)@VM410:1

On the other hand, if you skip step 5, but you invoke this statement:
> Blockly.JavaScript.init(Blockly.mainWorkspace)
before step 8, the warning does not trigger and the generated code is correct.

/s/ Adam
> --
> 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.

Abby Schmiedt

unread,
Feb 4, 2019, 5:51:25 PM2/4/19
to Blockly

HI Adam!


So it looks like the workspaceToCode method is calling the finish method which resets the variable map. The reason it does this is to make sure that the variable map is always updated with the most recent values (for instance if someone adds or removes a new variable between calls). The fix for this specific case is somewhat annoying, but is to call the Blockly.Javscript.init() method before the codeToBlock method is called. 


Let me know if this helps!


Abby


On Sunday, February 3, 2019 at 9:36:45 PM UTC-8, Coda Highland wrote:

Coda Highland

unread,
Feb 4, 2019, 9:24:58 PM2/4/19
to blo...@googlegroups.com
Okay, that makes a lot of sense. It's good to know there's a rationale
behind the behavior.

I had actually come across that workaround experimenting with things
to see if I could bludgeon it into working. It felt kludgey so I
wanted to verify the behavior was intended. Good to see that it is.

Thanks!
/s/ Adam
Reply all
Reply to author
Forward
0 new messages