Skip to content

Commit ffc4769

Browse files
committed
handle cycles
1 parent 29eec3f commit ffc4769

File tree

1 file changed

+14
-0
lines changed
  • packages/svelte/src/compiler/phases

1 file changed

+14
-0
lines changed

packages/svelte/src/compiler/phases/scope.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ class Evaluation {
221221
* @param {Set<any>} values
222222
*/
223223
constructor(scope, expression, values) {
224+
current_evaluations.set(expression, this);
225+
224226
this.values = values;
225227

226228
switch (expression.type) {
@@ -543,6 +545,8 @@ class Evaluation {
543545
if (this.values.size > 1 || typeof this.value === 'symbol') {
544546
this.is_known = false;
545547
}
548+
549+
current_evaluations.delete(expression);
546550
}
547551
}
548552

@@ -734,10 +738,20 @@ export class Scope {
734738
* @param {Set<any>} [values]
735739
*/
736740
evaluate(expression, values = new Set()) {
741+
const current = current_evaluations.get(expression);
742+
if (current) return current;
743+
737744
return new Evaluation(this, expression, values);
738745
}
739746
}
740747

748+
/**
749+
* Track which expressions are currently being evaluated — this allows
750+
* us to prevent cyclical evaluations without passing the map around
751+
* @type {Map<Expression | FunctionDeclaration, Evaluation>}
752+
*/
753+
const current_evaluations = new Map();
754+
741755
/** @type {Record<BinaryOperator, (left: any, right: any) => any>} */
742756
const binary = {
743757
'!=': (left, right) => left != right,

0 commit comments

Comments
 (0)