Skip to content

Commit 26b2ec7

Browse files
committed
add the runtime functions too, why not
1 parent 7b6b356 commit 26b2ec7

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

docs/website/how-it-works.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Things to notice:
137137

138138
Of course, this is a very simple script where the interpreter overhead is negligible, which is the case for most projects. It's only when you execute thousands of blocks per frame that the interpreter's overhead becomes significant.
139139

140-
Here's a more complex example: a naive sorting algorithm (bubble sort).
140+
Here's a more complex example: a sorting algorithm - bubble sort.
141141

142142
```js
143143
const length = stage.variables["O;aH~(njYNn}Bl@}!%pS-length-"];
@@ -182,6 +182,90 @@ return function fun1_sort () {
182182
};
183183
```
184184
185+
Functions such as `listGet`, `listReplace`, and `compareEqual` are part of the TurboWarp runtime and are implemented to match the strange behaviors of Scratch. The functions used by bubble sort are shown below, for your reference. Accuracy and performance are a higher priority than readability for these functions as they tend to be quite hot.
186+
187+
```js
188+
const isNotActuallyZero = val => {
189+
if (typeof val !== 'string') return false;
190+
for (let i = 0; i < val.length; i++) {
191+
const code = val.charCodeAt(i);
192+
if (code === 48 || code === 9) {
193+
return false;
194+
}
195+
}
196+
return true;
197+
};
198+
199+
const compareEqualSlow = (v1, v2) => {
200+
const n1 = +v1;
201+
if (isNaN(n1) || (n1 === 0 && isNotActuallyZero(v1))) return ('' + v1).toLowerCase() === ('' + v2).toLowerCase();
202+
const n2 = +v2;
203+
if (isNaN(n2) || (n2 === 0 && isNotActuallyZero(v2))) return ('' + v1).toLowerCase() === ('' + v2).toLowerCase();
204+
return n1 === n2;
205+
};
206+
207+
const compareEqual = (v1, v2) => (typeof v1 === 'number' && typeof v2 === 'number' && !isNaN(v1) && !isNaN(v2) || v1 === v2) ? v1 === v2 : compareEqualSlow(v1, v2);
208+
209+
const compareGreaterThanSlow = (v1, v2) => {
210+
let n1 = +v1;
211+
let n2 = +v2;
212+
if (n1 === 0 && isNotActuallyZero(v1)) {
213+
n1 = NaN;
214+
} else if (n2 === 0 && isNotActuallyZero(v2)) {
215+
n2 = NaN;
216+
}
217+
if (isNaN(n1) || isNaN(n2)) {
218+
const s1 = ('' + v1).toLowerCase();
219+
const s2 = ('' + v2).toLowerCase();
220+
return s1 > s2;
221+
}
222+
return n1 > n2;
223+
};
224+
225+
const compareGreaterThan = (v1, v2) => typeof v1 === 'number' && typeof v2 === 'number' && !isNaN(v1) ? v1 > v2 : compareGreaterThanSlow(v1, v2);
226+
227+
const listIndexSlow = (index, length) => {
228+
if (index === 'last') {
229+
return length - 1;
230+
} else if (index === 'random' || index === 'any') {
231+
if (length > 0) {
232+
return (Math.random() * length) | 0;
233+
}
234+
return -1;
235+
}
236+
index = (+index || 0) | 0;
237+
if (index < 1 || index > length) {
238+
return -1;
239+
}
240+
return index - 1;
241+
};
242+
243+
const listIndex = (index, length) => {
244+
if (typeof index !== 'number') {
245+
return listIndexSlow(index, length);
246+
}
247+
index = index | 0;
248+
return index < 1 || index > length ? -1 : index - 1;
249+
};
250+
251+
const listGet = (list, idx) => {
252+
const index = listIndex(idx, list.length);
253+
if (index === -1) {
254+
return '';
255+
}
256+
return list[index];
257+
};
258+
259+
const listReplace = (list, idx, value) => {
260+
const index = listIndex(idx, list.value.length);
261+
if (index === -1) {
262+
return;
263+
}
264+
list.value[index] = value;
265+
list._monitorUpToDate = false;
266+
};
267+
```
268+
185269
### Live script editing {#live-script-editing}
186270
187271
If you start a script using the compiler, you won't be able to move, remove, or add blocks and have the changes be reflected in real time as they would be in Scratch. The script has to be restarted for changes to apply. We believe there are some ways we could make this work, but they will hurt performance or add significant complexity. It's something we want to implement eventually, but not yet.

0 commit comments

Comments
 (0)