【问题标题】:Adding API call to JS-interpreter for use with custom blockly block将 API 调用添加到 JS 解释器以与自定义块块一起使用
【发布时间】:2019-03-01 07:47:52
【问题描述】:

我想先说一下我是 JavaScript 新手,而且肯定是使用 Neil Fraser JS-interpreter。

我制作了一些自定义块来简单地创建 JavaScript,当 eval() 将其块类型的对象和用户输入放入数组时。

他们用来执行此操作的函数称为 pushInstruction(blockName, inputs); 其中inputs是用户输入的blocks数组,blockName是block的名字。

现在我正在尝试使用 JS 解释器,但问题在于我如何使用这些块。

我急需帮助,但终生无法找到任何资源来帮助我。这可能有点傻。

自定义块代码

Blockly.Blocks['select_hand_position'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Move");
    this.appendDummyInput()
        .appendField(new Blockly.FieldDropdown([["left hand","Left hand"], ["right hand","Right hand"]]), "handSelect")
        .appendField("to index")
        .appendField(new Blockly.FieldNumber(0), "indexSelect");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(290);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};
Blockly.JavaScript['select_hand_position'] = function(block) {
  var dropdown_handselect = block.getFieldValue('handSelect');
  var number_indexselect = block.getFieldValue('indexSelect'); 
  var input = '["'+dropdown_handselect+'",'+number_indexselect+']';
  var code = 'pushInstruction("select_hand_position",'+input+');'               
  return code;
};

我有一个全局数组来保存对象

instructionStructure = new Array();

然后在这个函数中使用它,它是块生成代码使用的那个。

function pushInstruction(blockName,inputs) {  
  var instruction = null;
  switch(blockName) {
    case "place_book":
    case "grab_book":
    case "select_hand_position":
      instruction = {
        blockName: blockName,
        hand: inputs[0],
        index: inputs[1].toString()
      };
      instructionStructure.push(instruction);
      break;

    default:
      throw 'attempted to push unknown instruction block';
  }       
} 

步骤代码

这是在按下步进按钮时运行的代码

function stepCode() {
  Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
  Blockly.JavaScript.addReservedWords('highlightBlock');
  var code = Blockly.JavaScript.workspaceToCode(workspace);
  var myInterpreter = new Interpreter(code, initApi);
  function nextStep() {
    if (myInterpreter.step()) {
      window.setTimeout(nextStep, 1000);
    }
  }
  nextStep();
  alert(instructionStructure);
}

initAPI 函数

这是我不断得到的地方

未捕获的 TypeError:Interpreter.setProperty 不是函数

上线

Interpreter.setProperty(范围, 'pushInstruction', 解释器.createNativeFunction(wrapper));

function initApi(interpreter, scope) {

  var wrapper = function(id) {
    id = id ? id.toString() : '';
    return interpreter.createPrimitive(highlightBlock(id));
  };
  interpreter.setProperty(scope, 'highlightBlock',
      interpreter.createNativeFunction(wrapper));


  wrapper = function(blockName, inputs) {
    return pushInstruction(blockName,inputs);
  };
  Interpreter.setProperty(scope, 'pushInstruction',
      interpreter.createNativeFunction(wrapper));  
}

感谢您花时间阅读本文,非常感谢!

【问题讨论】:

    标签: javascript blockly google-blockly


    【解决方案1】:

    这里有错别字:

    Interpreter.setProperty(scope, 'pushInstruction',
      interpreter.createNativeFunction(wrapper));  
    

    =>

    interpreter.setProperty(scope, 'pushInstruction',
      interpreter.createNativeFunction(wrapper));  
    

    这是给Uncaught TypeError 的。 这是这里唯一的问题吗?因为我真的不明白

    但问题在于我如何使用这些块。

    【讨论】:

    • 谢谢!就是这样。再次抱歉,它是如此简单。但是现在我在 pushInstruction() 函数中遇到了另一个 TypeError。这是错误 - “未捕获的类型错误:无法读取未定义的属性 'toString'”行'index:inputs[1].toString()'
    • @Iamprettybadatthis 这是因为这里的inputs 不是数组,而是Interpreter.Object。您应该改用inputs.properties[1].toString()inputs.properties[0].toString()也一样
    • 非常感谢!有没有办法改变inputs,所以它被视为一个数组而不是Interpreter.Object。正如我尝试使用属性但给出类型错误Cannot read property '0' of undefined
    • @Iamprettybadatthis 嗯,我用最新版本的 blockly-demo 进行了尝试,它与properties 配合得很好。试试这个:jsfiddle.net/extempl/ym0hbtdj
    • 感谢您的帮助,经过大量的摆弄,我设法让它工作。但我不得不使用inputs.a[0].toString() 而不是属性。任何想法为什么?如果我控制台日志inputsqb {O: {…}, R: {…}, a: {…}, na: qb, K: "Array"} K: "Array" O: {} R: {} a: {0: "Left hand", 1: 0, length: 2} na: qb {O: {…}, R: {…}, a: {…}, na: qb} __proto__: Object,我也会得到这个,你知道它采用这种格式的原因,而不是像简单数组{"name",[-inputs-]} 这样的生成器函数中指定的格式吗?感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多