Skip to content

Commit

Permalink
Merge pull request #120 from cymplecy/bufferListConversions
Browse files Browse the repository at this point in the history
Buffer list conversions
  • Loading branch information
bartbutenaers authored Nov 6, 2023
2 parents a0828cd + 4c95302 commit ee0c6e1
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/buffer/bufferBlocksCodeGen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ Blockly.JavaScript['buffer_to_string'] = function(block) {
return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript['buffer_from_list'] = function(block) {
const list_input = Blockly.JavaScript.valueToCode(block, 'LIST_INPUT', Blockly.JavaScript.ORDER_ATOMIC) || '""';
const code = 'Buffer.from(' + list_input + ')';
return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript['buffer_to_list'] = function(block) {
const buffer_input = Blockly.JavaScript.valueToCode(block, 'BUFFER_INPUT', Blockly.JavaScript.ORDER_ATOMIC);
const code = 'Array.from(' + buffer_input + ')';
return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript['buffer_length'] = function(block) {
const buffer_input = Blockly.JavaScript.valueToCode(block, 'BUFFER_INPUT', Blockly.JavaScript.ORDER_ATOMIC);
const code = buffer_input + '.length';
Expand Down
42 changes: 42 additions & 0 deletions lib/buffer/bufferBlocksDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@ Blockly.Blocks['buffer_to_string'] = {
}
};

Blockly.Blocks['buffer_from_list'] = {
init: function () {
this.jsonInit({
"type": "buffer_from_list",
"message0": Blockly.Msg.BUFFER_FROM_LIST,
"args0": [
{
"type": "input_value",
"name": "LIST_INPUT",
'check': ['Array']
}
],
"inputsInline": true,
"output": 'Buffer',
"colour": "#DF6C06",
"tooltip": Blockly.Msg.BUFFER_FROM_LIST_TOOLTIP,
"helpUrl": null
});
}
};

Blockly.Blocks['buffer_to_list'] = {
init: function () {
this.jsonInit({
"type": "buffer_to_list",
"message0": Blockly.Msg.BUFFER_TO_LIST,
"args0": [
{
"type": "input_value",
"name": "BUFFER_INPUT",
"check": "Buffer"
}
],
"inputsInline": true,
"output": 'List',
"colour": "#DF6C06",
"tooltip": Blockly.Msg.BUFFER_TO_LIST_TOOLTIP,
"helpUrl": null
});
}
};

Blockly.Blocks['buffer_length'] = {
init: function () {
this.jsonInit({
Expand Down
9 changes: 9 additions & 0 deletions lib/buffer/toolbox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
<shadow type="buffer_empty"></shadow>
</value>
</block>
<block type="buffer_from_list">
<value name="LIST_INPUT">
</value>
</block>
<block type="buffer_to_list">
<value name="BUFFER_INPUT">
<shadow type="buffer_empty"></shadow>
</value>
</block>
<block type="buffer_alloc">
<value name="LENGTH">
<shadow type="math_number">
Expand Down
6 changes: 5 additions & 1 deletion messages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ Blockly.Msg.NODE_ENV_TOOLTIP = "Access the value of an environment variable.";
Blockly.Msg.BUFFER_FROM_STRING = "generate buffer from string %1 with encoding %2";
Blockly.Msg.BUFFER_FROM_STRING_TOOLTIP = "Generate a buffer from a string.";
Blockly.Msg.BUFFER_TO_STRING = "generate string from buffer %1 with encoding %2";
Blockly.Msg.BUFFER_TO_STRING_TOOLTIP = "Convert a buffer to a string.";
Blockly.Msg.BUFFER_TO_STRING_TOOLTIP = "Generate a string from a buffer.";
Blockly.Msg.BUFFER_FROM_LIST = "generate buffer from list %1";
Blockly.Msg.BUFFER_FROM_LIST_TOOLTIP = "Generate a buffer from a list.";
Blockly.Msg.BUFFER_TO_LIST = "generate list from buffer %1";
Blockly.Msg.BUFFER_TO_LIST_TOOLTIP = "Convert a buffer to a string.";
Blockly.Msg.BUFFER_LENGTH = "get length of buffer %1";
Blockly.Msg.BUFFER_LENGTH_TOOLTIP = "Get the buffer length in bytes.";
Blockly.Msg.BUFFER_ALLOC = "create buffer with length %1";
Expand Down
4 changes: 4 additions & 0 deletions messages/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Blockly.Msg.BUFFER_FROM_STRING = "créer un tampon avec la chaîne %1 et l’enc
Blockly.Msg.BUFFER_FROM_STRING_TOOLTIP = "Crée un tampon (buffer) à partir d’une chaîne de caractères.";
Blockly.Msg.BUFFER_TO_STRING = "créer une chaîne depuis le tampon %1 avec l’encodage %2";
Blockly.Msg.BUFFER_TO_STRING_TOOLTIP = "Conversion d’un tampon (buffer) en chaîne de caractères.";
Blockly.Msg.BUFFER_FROM_LIST = "créer un tampon à partir d'une liste %1";
Blockly.Msg.BUFFER_FROM_LIST_TOOLTIP = "créer un tampon à partir d'une liste.";
Blockly.Msg.BUFFER_TO_LIST = "créer une list à partir du tampon %1";
Blockly.Msg.BUFFER_TO_LIST_TOOLTIP = "créer une list à partir du tampon.";
Blockly.Msg.BUFFER_LENGTH = "longueur du tampon %1";
Blockly.Msg.BUFFER_LENGTH_TOOLTIP = "Obtenir la longueur du tampon (buffer) en octets.";
Blockly.Msg.BUFFER_ALLOC = "créer un tampon de longueur %1";
Expand Down
4 changes: 4 additions & 0 deletions messages/nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Blockly.Msg.BUFFER_FROM_STRING = "maak buffer vanuit tekst %1 met codering %2";
Blockly.Msg.BUFFER_FROM_STRING_TOOLTIP = "Genereer een buffer vanuit een tekst.";
Blockly.Msg.BUFFER_TO_STRING = "maak tekst van buffer %1 met codering %2";
Blockly.Msg.BUFFER_TO_STRING_TOOLTIP = "Zet de buffer om naar een tekst.";
Blockly.Msg.BUFFER_FROM_LIST = "maak buffer vanuit lijst %1";
Blockly.Msg.BUFFER_FROM_LIST_TOOLTIP = "maak buffer vanuit lijst.";
Blockly.Msg.BUFFER_TO_LIST = "maak lijst van buffer %1";
Blockly.Msg.BUFFER_TO_LIST_TOOLTIP = "maak lijst van buffer.";
Blockly.Msg.BUFFER_LENGTH = "zoek lengte van buffer %1";
Blockly.Msg.BUFFER_LENGTH_TOOLTIP = "Zoek de buffer lengte in bytes.";
Blockly.Msg.BUFFER_ALLOC = "maak buffer met lengte %1";
Expand Down

0 comments on commit ee0c6e1

Please sign in to comment.