Skip to content

Commit

Permalink
Guard infinite recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
iHiD committed Jan 30, 2025
1 parent fe67b79 commit b7dccb8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/javascript/interpreter/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type RuntimeErrorType =
| 'ExpectedFunctionHasWrongArguments'
| 'InvalidNestedFunction'
| 'MaxIterationsReached'
| 'InfiniteRecursion'

export type StaticErrorType =
| DisabledLanguageFeatureErrorType
Expand Down
7 changes: 5 additions & 2 deletions app/javascript/interpreter/executor/executeCallExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ export function executeCallExpression(
} catch (e) {
if (e instanceof FunctionCallTypeMismatchError) {
executor.error('FunctionCallTypeMismatch', expression.location, e.context)
}
if (e instanceof LogicError) {
} else if (e instanceof LogicError) {
executor.error('LogicError', expression.location, { message: e.message })
} else if (e instanceof RangeError) {
executor.error('InfiniteRecursion', expression.location, {
message: e.message,
})
} else {
throw e
}
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/interpreter/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"FunctionCallTypeMismatch": "Jiki expected input {{argIdx}} to be a {{expectedType}} but it was actually a {{actualType}}.",
"UnexpectedReturnOutsideOfFunction": "You used the `return` keyword, but you're not inside a function. So Jiki has nothing to return out from.",
"ExpectedFunctionNotFound": "Jiki couldn't find the `{{name}}` function. Have you defined it correctly?",
"ExpectedFunctionHasWrongArguments": "Jiki tried to run the `{{name}}` function but it had a different amount of inputs to what he expected."
"ExpectedFunctionHasWrongArguments": "Jiki tried to run the `{{name}}` function but it had a different amount of inputs to what he expected.",
"InfiniteRecursion": "Jiki kept on running the same code over and over again. Do you have a function that is somehow trying to use itself?"
},
"disabledLanguageFeature": {
"ExcludeListViolation": "Jiki doesn't know how to use `{{lexeme}}` in this exercise.",
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/interpreter/locales/system/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"ExpectedFunctionHasWrongArguments": "ExpectedFunctionHasWrongArguments: name: {{name}}",
"ExpectedFunctionNotFound": "ExpectedFunctionNotFound: name: {{name}}",
"VariableNotAccessibleInFunction": "VariableNotAccessibleInFunction: name: {{name}}",
"InvalidNestedFunction": "InvalidNestedFunction"
"InvalidNestedFunction": "InvalidNestedFunction",
"InfiniteRecursion": "InfiniteRecursion"
},
"disabledLanguageFeature": {
"ExcludeListViolation": "ExcludeListViolation: tokenType: {{tokenType}}",
Expand Down
9 changes: 9 additions & 0 deletions test/javascript/interpreter/runtimeErrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,13 @@ describe('Runtime errors', () => {
`MaxIterationsReached: maxIterations: ${maxIterations}`
)
})
test('InfiniteRecursion', () => {
const code = `function foo do
foo()
end
foo()`
const { frames } = interpret(code)
expectFrameToBeError(frames[0], 'foo()', 'InfiniteRecursion')
expect(frames[0].error!.message).toBe('InfiniteRecursion')
})
})

0 comments on commit b7dccb8

Please sign in to comment.