Skip to content

Commit

Permalink
Observe gas limits in MOS declaration
Browse files Browse the repository at this point in the history
ref #327
  • Loading branch information
frostburn committed May 23, 2024
1 parent 21de136 commit d3e5a4d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/parser/__tests__/source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1823,4 +1823,24 @@ describe('SonicWeave parser', () => {
'27\\27',
]);
});

it('supports guard rails against absurd MOS declarations', () => {
const ast = parseAST('MOS 420L 69s');

const visitor = getSourceVisitor(false);
visitor.rootContext!.gas = 100;
expect(() => visitor.executeProgram(ast)).toThrow();
});

it('supports guard rails against unreasonable automos', () => {
const ast = parseAST(`
MOS 20L 20s
automos()
`);

const visitor = getSourceVisitor(false);
visitor.rootContext!.gas = 100;
visitor.visit(ast.body[0]);
expect(() => visitor.visit(ast.body[1])).toThrow();
});
});
9 changes: 9 additions & 0 deletions src/parser/mos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ export class Tardigrade {
this.subVisitor = subVisitor;
}

spendGas(amount?: number) {
this.subVisitor.spendGas(amount);
}

createMosConfig(): MosConfig {
if (!this.pattern) {
throw new Error('Mode must be given in MOS declaration.');
}
this.spendGas(this.pattern.length);
const notation = generateNotation(this.pattern);
const N = new Fraction(this.pattern.length);
const countL = new Fraction((this.pattern.match(/L/g) ?? []).length);
Expand Down Expand Up @@ -179,6 +184,7 @@ export class Tardigrade {
}

visit(node: MosExpression) {
this.spendGas();
switch (node.type) {
case 'AbstractStepPattern':
return this.visitAbstractStepPattern(node);
Expand All @@ -202,13 +208,15 @@ export class Tardigrade {
}

visitAbstractStepPattern(node: AbstractStepPattern) {
this.spendGas(node.pattern.length);
this.pattern = node.pattern.join('');
if (node.equave) {
this.equave = this.visitRationalEquave(node.equave);
}
}

visitIntegerPattern(node: IntegerPattern) {
this.spendGas(node.pattern.length);
const small = Math.min(...node.pattern);
const large = Math.max(...node.pattern);
this.pattern = node.pattern.map(i => (i === small ? 's' : 'L')).join('');
Expand All @@ -223,6 +231,7 @@ export class Tardigrade {
}

visitPatternUpDownPeriod(node: PatternUpDownPeriod) {
this.spendGas(Math.abs(node.countLarge) + Math.abs(node.countSmall));
const options: MosOptions = {};
if (node.udp) {
options.up = node.udp.up;
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,7 @@ function automos(this: ExpressionVisitor) {
if (!this.rootContext?.mosConfig || scale.length) {
return;
}
this.spendGas(this.rootContext.mosConfig.scale.size);
const J4 = this.rootContext.C4;
const monzos = scaleMonzos(this.rootContext.mosConfig).map(m => J4.mul(m));
return monzos.map(
Expand Down

0 comments on commit d3e5a4d

Please sign in to comment.