-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbufferBlocksCodeGen.js
150 lines (128 loc) · 6.47 KB
/
bufferBlocksCodeGen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Blockly.JavaScript['buffer_from_string'] = function(block) {
const string_input = Blockly.JavaScript.valueToCode(block, 'STRING_INPUT', Blockly.JavaScript.ORDER_ATOMIC) || '""';
const encoding = block.getFieldValue('ENCODING');
const code = 'Buffer.from(' + string_input + ', "' + encoding + '")';
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.JavaScript['buffer_to_string'] = function(block) {
const buffer_input = Blockly.JavaScript.valueToCode(block, 'BUFFER_INPUT', Blockly.JavaScript.ORDER_ATOMIC);
const encoding = block.getFieldValue('ENCODING');
const code = buffer_input + '.toString("' + encoding + '")';
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';
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.JavaScript['buffer_alloc'] = function(block) {
const length = Blockly.JavaScript.valueToCode(block, 'LENGTH', Blockly.JavaScript.ORDER_ATOMIC);
const code = 'Buffer.alloc(' + length + ')';
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.JavaScript['buffer_fill'] = function(block) {
const buffer_input = Blockly.JavaScript.valueToCode(block, 'BUFFER_INPUT', Blockly.JavaScript.ORDER_ATOMIC);
const fill_value = Blockly.JavaScript.valueToCode(block, 'FILL_VALUE', Blockly.JavaScript.ORDER_ATOMIC) || 0;
const code = buffer_input + '.fill(' + fill_value + ');\n';
return code;
};
Blockly.JavaScript['buffer_check'] = function(block) {
const input_object = Blockly.JavaScript.valueToCode(block, 'INPUT', Blockly.JavaScript.ORDER_ATOMIC);
const code = 'Buffer.isBuffer(' + input_object + ')';
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.JavaScript['buffer_copy'] = function(block) {
const buffer_source = Blockly.JavaScript.valueToCode(block, 'BUFFER_SOURCE', Blockly.JavaScript.ORDER_ATOMIC);
const buffer_destination = Blockly.JavaScript.valueToCode(block, 'BUFFER_DESTINATION', Blockly.JavaScript.ORDER_ATOMIC);
const code = buffer_source + '.copy(' + buffer_destination + ');\n';
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;
//console.log(index);
const buffer = Blockly.JavaScript.valueToCode(block, 'BUFFER', Blockly.JavaScript.ORDER_ATOMIC);
var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC);
// Get the data type checks offered by the block that is connected to our VALUE field
var targetCheck = block.getInput('VALUE').connection.targetConnection.getCheck();
// Find the type of value that has been used as input.
// See https://groups.google.com/g/blockly/c/9fEPSGFarNM
// Find the type of value that has been used as input.
// See https://groups.google.com/g/blockly/c/9fEPSGFarNM
var dataType = null;
if (targetCheck && Array.isArray(targetCheck) && targetCheck.length === 1) {
dataType = targetCheck[0];
}
if ((dataType === null) || ((dataType == 'String') && (value.charAt(0) == '(') && (value.charAt(value.length - 1) == ')') )) {
// We assume the input is a variable
// Let Blockly generate a function 'convertValueToByte' (shared by all buffer_set_index blocks that have a variable as input).
// Blockly will generate this function once, since all buffer_set_byte blocks will register the same name, thus overwriting each others function...
var functionName = Blockly.JavaScript.provideFunction_(
'convertValueToByte',
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(value) {',
' if (isNaN(value)) {',
' return value.charCodeAt(0);',
' } else {',
' if (Buffer.isBuffer(value)) {',
' return value[0];',
' } else {',
//JS thinks a single " " is !isNan so we need to treat it as a special case
' if (value === " ") {',
' return value.charCodeAt(0);',
' } else {',
' return Math.min(Math.max(value, 0),255);',
' }',
' }',
' }',
'}']);
// Call the same function for every buffer_set_byte block
var code = buffer + '[' + index + ' - 1] = ' + functionName + '(' + value + ');\n';
return code;
}
switch (dataType) {
case "Number":
value = Math.min(Math.max(value, 0),255);
break;
case "String":
// Get the first character of the input string
value = value.replace(/^'(.*)'$/, '$1'); //remove any quotes
value = value.charCodeAt(0);
break;
case "Buffer":
// Get the first element from the input buffer
value = value + '[0]';
break;
}
var code = buffer + '[' + index + ' - 1] = ' + value + ';\n';
return code;
};
Blockly.JavaScript['buffer_get_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 code = buffer + '[' + index + ' - 1]';
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.JavaScript['buffer_byte'] = function(block) {
const value = block.getFieldValue('BYTE_VALUE');
const code = 'Buffer.alloc(1, ' + value + ')';
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.JavaScript['buffer_empty'] = function(block) {
const code = 'Buffer.alloc(0)';
return [code, Blockly.JavaScript.ORDER_NONE];
};