-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from yantrajs/feature/stack-overflow
Added support for stack overflow
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
YantraJS.Core.Tests/Generator/Files/es5/Function/stack-overflow.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
let i = 0; | ||
function recursive() { | ||
i++; | ||
recursive(); | ||
} | ||
|
||
try { | ||
recursive(); | ||
} catch (error) { | ||
if (error instanceof RangeError) { | ||
if (/maximum\scall\sstack/i.test(error.message)) { | ||
console.log(`Failed after ${i} recursive calls`); | ||
return; | ||
} | ||
} | ||
throw new Error("Stack overflow expected"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
namespace YantraJS.Core.Core | ||
{ | ||
public static class JSContextExtensions | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void EnsureSufficientExecutionStack(this JSContext context) | ||
{ | ||
#if NETSTANDARD2_1_OR_GREATER | ||
if(RuntimeHelpers.TryEnsureSufficientExecutionStack()) | ||
{ | ||
return; | ||
} | ||
#else | ||
try | ||
{ | ||
RuntimeHelpers.EnsureSufficientExecutionStack(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (ex is not InsufficientExecutionStackException) | ||
{ | ||
throw ex; | ||
} | ||
} | ||
#endif | ||
throw context.NewRangeError("Maximum call stack size exceeded"); | ||
} | ||
|
||
|
||
} | ||
} |