Skip to content

Commit

Permalink
byte block returns buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bartbutenaers committed Jul 22, 2021
1 parent 79967f1 commit 9b63191
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
36 changes: 31 additions & 5 deletions lib/buffer/bufferBlocksCodeGen.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,40 @@ Blockly.JavaScript['buffer_copy'] = function(block) {
return code;
};

Blockly.JavaScript['buffer_concatenate'] = function(block) {
const buffer_first = Blockly.JavaScript.valueToCode(block, 'BUFFER_FIRST', Blockly.JavaScript.ORDER_ATOMIC);
const buffer_second = Blockly.JavaScript.valueToCode(block, 'BUFFER_SECOND', Blockly.JavaScript.ORDER_ATOMIC);
const code = 'Buffer.concat([' + buffer_first + ', ' + buffer_second + '])';
return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript['buffer_set_index'] = function(block) {
var index = Blockly.JavaScript.valueToCode(block, 'INDEX', Blockly.JavaScript.ORDER_ATOMIC) || 1;
const buffer = Blockly.JavaScript.valueToCode(block, 'BUFFER', Blockly.JavaScript.ORDER_ATOMIC);
const value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC);
var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC);

// Blockly is 1-based while Javascript is 0-based, so the index needs to be converted
index--;


// Find the type of value that has been used as input.
// See https://groups.google.com/g/blockly/c/9fEPSGFarNM
var dataType = block.getInput('VALUE').connection.targetConnection.getCheck()[0];

// Based on the value type, we need different ways to get the first byte
switch (dataType) {
case "Number":
// TODO convert to hexadecimal 0x...
break;
case "String":
// Get the first character of the input string
value = value + '.charAt(0)';
break;
case "Buffer":
// Get the first element from the input buffer
value = value + '[0]';
break;
}

const code = buffer + '[' + index + '] = ' + value + ';\n';
return code;
};
Expand All @@ -62,15 +88,15 @@ Blockly.JavaScript['buffer_get_index'] = function(block) {

// Blockly is 1-based while Javascript is 0-based, so the index needs to be converted
index--;

const code = buffer + '[' + index + ']';
return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript['buffer_byte'] = function(block) {
const value = block.getFieldValue('BYTE_VALUE');
const code = value;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
const code = 'Buffer.alloc(1, ' + value + ')';
return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript['buffer_empty'] = function(block) {
Expand Down
30 changes: 28 additions & 2 deletions lib/buffer/bufferBlocksDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ Blockly.Blocks['buffer_copy'] = {
}
};

Blockly.Blocks['buffer_concatenate'] = {
init: function () {
this.jsonInit({
"type": "buffer_concatenate",
"message0": Blockly.Msg.BUFFER_CONCATENATE,
"args0": [
{
"type": "input_value",
"name": "BUFFER_FIRST",
"check": "Buffer"
},
{
"type": "input_value",
"name": "BUFFER_SECOND",
"check": "Buffer"
}
],
"inputsInline": true,
"output": 'Buffer',
"colour": "#DF6C06",
"tooltip": Blockly.Msg.BUFFER_CONCATENATE_TOOLTIP,
"helpUrl": null
});
}
};

// TODO check in generator if byte value is between 0 and 255
Blockly.Blocks['buffer_set_index'] = {
init: function () {
Expand All @@ -198,7 +224,7 @@ Blockly.Blocks['buffer_set_index'] = {
{
"type": "input_value",
"name": "VALUE",
"check": "Number"
"check": ["Number", "String", "Buffer"]
}
],
"inputsInline": true,
Expand Down Expand Up @@ -252,7 +278,7 @@ Blockly.Blocks['buffer_byte'] = {
}
],
"inputsInline": true,
"output": 'Number',
"output": 'Buffer',
"colour": "#DF6C06",
"tooltip": Blockly.Msg.BUFFER_BYTE_TOOLTIP,
"helpUrl": null
Expand Down

0 comments on commit 9b63191

Please sign in to comment.