Skip to content

Commit

Permalink
cleanup after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Oct 27, 2024
1 parent b6223aa commit 5dd33d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Jint.Tests.PublicInterface/CallStackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void ShouldReturnTheSourceMapStack()
{
var sourceMap = SourceMapParser.Parse("""{"version":3,"file":"custom.js","sourceRoot":"","sources":["custom.ts"],"names":[],"mappings":"AAEA,SAAS,CAAC,CAAC,CAAM;IAChB,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC;AAED,IAAI,CAAC,GAAG,UAAU,CAAM;IACvB,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AACb,CAAC,CAAA;AAED,CAAC,CAAC,CAAC,CAAC,CAAC"}""");

string BuildCallStackHandler(string description, SourceLocation location, List<string> arguments)
string BuildCallStackHandler(string description, SourceLocation location, string[] arguments)
{
if (location.SourceFile != sourceMap.File)
{
Expand Down
4 changes: 2 additions & 2 deletions Jint/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Options

public delegate bool ExceptionHandlerDelegate(Exception exception);

public delegate string? BuildCallStackDelegate(string shortDescription, SourceLocation location, List<string>? arguments);
public delegate string? BuildCallStackDelegate(string shortDescription, SourceLocation location, string[]? arguments);

/// <summary>
/// Execution constraints for the engine.
Expand Down Expand Up @@ -304,7 +304,7 @@ public class InteropOptions
/// stack traces to code different from the code being executed, eg. when
/// executing code transpiled from TypeScript.
/// </summary>
public BuildCallStackDelegate BuildCallStackHandler { get; set; } = static (string description, SourceLocation location, List<string>? arguments) => null;
public BuildCallStackDelegate? BuildCallStackHandler { get; set; }

/// <summary>
///
Expand Down
62 changes: 42 additions & 20 deletions Jint/Runtime/CallStack/JintCallStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,10 @@ static void AppendLocation(
string shortDescription,
in SourceLocation loc,
in CallStackElement? element,
Engine engine)
Options.BuildCallStackDelegate? callStackBuilder)
{
List<string>? arguments = null;

if (element?.Arguments is not null)
{
arguments = element.Value.Arguments.Value.Select(GetPropertyKey).ToList();
}

var str = engine.Options.Interop.BuildCallStackHandler.Invoke(shortDescription, loc, arguments);
if (!string.IsNullOrEmpty(str))
if (callStackBuilder != null && TryInvokeCustomCallStackHandler(callStackBuilder, element, shortDescription, loc, ref sb))
{
sb.Append(str);
return;
}

Expand All @@ -145,16 +136,19 @@ static void AppendLocation(
sb.Append(shortDescription);
}

if (arguments is not null)
if (element?.Arguments is not null)
{
// it's a function
sb.Append(" (");
for (var index = 0; index < arguments.Count; index++)
var arguments = element.Value.Arguments.Value;
for (var i = 0; i < arguments.Count; i++)
{
if (index != 0)
if (i != 0)
{
sb.Append(", ");
}
sb.Append(arguments[index]);

sb.Append(GetPropertyKey(arguments[i]));
}
sb.Append(')');
}
Expand All @@ -168,24 +162,25 @@ static void AppendLocation(
sb.Append(System.Environment.NewLine);
}

var customCallStackBuilder = engine.Options.Interop.BuildCallStackHandler;
var builder = new ValueStringBuilder();

// stack is one frame behind function-wise when we start to process it from expression level
var index = _stack._size - 1 - excludeTop;
var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
var shortDescription = element?.ToString() ?? "";

AppendLocation(ref builder, shortDescription, location, element, engine);
AppendLocation(ref builder, shortDescription, location, element, customCallStackBuilder);

location = element?.Location ?? default;
index--;

while (index >= -1)
{
element = index >= 0 ? _stack[index] : null;
shortDescription = element?.ToString() ?? string.Empty;
shortDescription = element?.ToString() ?? "";

AppendLocation(ref builder, shortDescription, location, element, engine);
AppendLocation(ref builder, shortDescription, location, element, customCallStackBuilder);

location = element?.Location ?? default;
index--;
Expand All @@ -198,6 +193,34 @@ static void AppendLocation(
return result;
}

private static bool TryInvokeCustomCallStackHandler(
Options.BuildCallStackDelegate handler,
CallStackElement? element,
string shortDescription,
SourceLocation loc,
ref ValueStringBuilder sb)
{
string[]? arguments = null;
if (element?.Arguments is not null)
{
var args = element.Value.Arguments.Value;
arguments = args.Count > 0 ? new string[args.Count] : [];
for (var i = 0; i < arguments.Length; i++)
{
arguments[i] = GetPropertyKey(args[i]);
}
}

var str = handler(shortDescription, loc, arguments);
if (!string.IsNullOrEmpty(str))
{
sb.Append(str);
return true;
}

return false;
}

/// <summary>
/// A version of <see cref="AstExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
/// </summary>
Expand All @@ -215,8 +238,7 @@ private static string GetPropertyKey(Node expression)

if (expression is MemberExpression { Computed: false } staticMemberExpression)
{
return GetPropertyKey(staticMemberExpression.Object) + "." +
GetPropertyKey(staticMemberExpression.Property);
return $"{GetPropertyKey(staticMemberExpression.Object)}.{GetPropertyKey(staticMemberExpression.Property)}";
}

return "?";
Expand Down

0 comments on commit 5dd33d2

Please sign in to comment.