Draft Re: How to get variable name and value set for the variable using JavaScript code generator

397 views
Skip to first unread message

Andrew n marshall

unread,
Apr 6, 2018, 6:23:25 PM4/6/18
to blo...@googlegroups.com
Hi Khushbu,

To get the value of a variable generated within the interpreter, use interpretter.getValueFromScope(varName)?
While this is a public function, it is not currently marked for name preservation, so you'll have to use an unminified version of the jsinterpreter (clone the repo and build yourself).

Warning: this function is not sufficiently protected for bad input. varName must be a previously defined variable name, or things will blow up in very bad ways.

To that point, Blockly sometimes needs to rename variables when generating a script (for example to avoid conflict with a language's keywords; you can't have a javascript variable named var). If you're using developer defined variables, I presume this isn't be an issue since you'll choose valid js variables.

Unfortunately, for user variables, we haven't kept a mapping of Blockly variables to the variable names in the code. The closest we have is the defvars list inside Blockly.JavaScript.init(). You could rework this loop to create a map of variable id to JS variable name, though the details may change in future versions of Blockly. The variables code is still settling down after the introduction of variable types.




On Wed, Apr 4, 2018 at 11:21 PM, Khushbu Yeole <khushb...@gmail.com> wrote:
HI,

I am trying to get value and name for the variable using code generator of JavaScript.
PFA for my workspace blocks i have used.


Code generated by my blocks is :
var ultra, led;
/**
 * Describe this function...
 */
function do_something() {
  ultra = 20;
  if (ultra == 20) {
    led = true;
  } else {
    led = false;
  }
  return led;
}


This is Interpreter generated value: Interpreter.Primitive {data: undefined, type: "undefined"}

My code for Interpreter is:
var code = Blockly.JavaScript.workspaceToCode(Blockly.getMainWorkspace());
var myInterpretre = new Interpreter(code);
myInterpretre.run();
console.log(myInterpretre.value);

Please help me to get variable name with value return as output.

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

Reply all
Reply to author
Forward
0 new messages