Skip to content

Commit

Permalink
faster list hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Sep 10, 2024
1 parent 179030a commit dce5eea
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,10 @@ class JSGenerator {
case 'list.get': {
const index = this.descendInput(node.index);
if (environment.supportsNullishCoalescing) {
if (index.isAlwaysNumberOrNaN()) {
return new TypedInput(`(${this.referenceVariable(node.list)}.value[(${index.asNumber()} | 0) - 1] ?? "")`, TYPE_UNKNOWN);
}
if (index instanceof ConstantInput && index.constantValue === 'last') {
return new TypedInput(`(${this.referenceVariable(node.list)}.value[${this.referenceVariable(node.list)}.value.length - 1] ?? "")`, TYPE_UNKNOWN);
}
return new TypedInput(`(${this.referenceVariable(node.list)}.value[(${index.asNumber()} | 0) - 1] ?? "")`, TYPE_UNKNOWN);
}
return new TypedInput(`listGet(${this.referenceVariable(node.list)}.value, ${index.asUnknown()})`, TYPE_UNKNOWN);
}
Expand Down Expand Up @@ -959,7 +957,8 @@ class JSGenerator {
break;
}
case 'list.replace':
this.source += `listReplace(${this.referenceVariable(node.list)}, ${this.descendInput(node.index).asUnknown()}, ${this.descendInput(node.item).asSafe()});\n`;
this.source += `${this.referenceVariable(node.list)}.value[(${this.descendInput(node.index).asNumber()} | 0) - 1] = ${this.descendInput(node.item).asSafe()};\n`;
this.source += `${this.referenceVariable(node.list)}._monitorUpToDate = false;\n`;
break;
case 'list.show':
this.source += `runtime.monitorBlocks.changeBlock({ id: "${sanitize(node.list.id)}", element: "checkbox", value: true }, runtime);\n`;
Expand Down

0 comments on commit dce5eea

Please sign in to comment.