1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 01:14:58 +00:00
serenity/Libraries/LibJS/Tests/runtime-error-call-stack-size.js
Linus Groh a02b9983f9 LibJS: Throw RuntimeError when reaching the end of the stack
This prevents stack overflows when calling infinite/deep recursive
functions, e.g.:

    const f = () => f(); f();
    JSON.stringify({}, () => ({ foo: "bar" }));
    new Proxy({}, { get: (_, __, p) => p.foo }).foo;

The VM caches a StackInfo object to not slow down function calls
considerably. VM::push_call_frame() will throw an exception if
necessary (plain Error with "RuntimeError" as its .name).
2020-11-08 16:51:54 +01:00

21 lines
610 B
JavaScript

test("infinite recursion", () => {
function infiniteRecursion() {
infiniteRecursion();
}
try {
infiniteRecursion();
} catch (e) {
expect(e).toBeInstanceOf(Error);
expect(e.name).toBe("RuntimeError");
expect(e.message).toBe("Call stack size limit exceeded");
}
expect(() => {
JSON.stringify({}, () => ({ foo: "bar" }));
}).toThrowWithMessage(Error, "Call stack size limit exceeded");
expect(() => {
new Proxy({}, { get: (_, __, p) => p.foo }).foo;
}).toThrowWithMessage(Error, "Call stack size limit exceeded");
});