Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Mar 31, 2024
1 parent e94b021 commit 95825ea
Show file tree
Hide file tree
Showing 28 changed files with 67 additions and 77 deletions.
3 changes: 2 additions & 1 deletion Jint.Repl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
var defaultColor = Console.ForegroundColor;
var parserOptions = new ParserOptions
{
EcmaVersion = EcmaVersion.Experimental,
EcmaVersion = EcmaVersion.ES2023,
ExperimentalESFeatures = ExperimentalESFeatures.ImportAttributes | ExperimentalESFeatures.RegExpDuplicateNamedCapturingGroups,
Tolerant = true,
RegExpParseMode = RegExpParseMode.AdaptToInterpreted
};
Expand Down
3 changes: 2 additions & 1 deletion Jint.Tests.CommonScripts/ConcurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public class ConcurrencyTest
public void ConcurrentEnginesCanUseSameAst(bool prepared)
{
var scriptContents = SunSpiderTests.GetEmbeddedFile("babel-standalone.js");
var experimentalESFeatures = ExperimentalESFeatures.ImportAttributes | ExperimentalESFeatures.RegExpDuplicateNamedCapturingGroups;
var script = prepared
? Engine.PrepareScript(scriptContents)
: new Parser(new ParserOptions { EcmaVersion = EcmaVersion.Experimental, RegExpParseMode = RegExpParseMode.AdaptToInterpreted }).ParseScript(scriptContents);
: new Parser(new ParserOptions { EcmaVersion = EcmaVersion.ES2023, ExperimentalESFeatures = experimentalESFeatures, RegExpParseMode = RegExpParseMode.AdaptToInterpreted }).ParseScript(scriptContents);

Parallel.ForEach(Enumerable.Range(0, 3), x =>
{
Expand Down
1 change: 1 addition & 0 deletions Jint.Tests.Test262/Test262Harness.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Array.fromAsync",
"async-iteration",
"Atomics",
"decorators",
"import-assertions",
"iterator-helpers",
"regexp-lookbehind",
Expand Down
8 changes: 7 additions & 1 deletion Jint.Tests.Test262/Test262Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ private Engine BuildTestExecutor(Test262File file)
throw new Exception("only script parsing supported");
}

var options = new ParserOptions { EcmaVersion = EcmaVersion.Experimental, RegExpParseMode = RegExpParseMode.AdaptToInterpreted, Tolerant = false };
var options = new ParserOptions
{
EcmaVersion = EcmaVersion.ES2023,
ExperimentalESFeatures = ExperimentalESFeatures.ImportAttributes | ExperimentalESFeatures.RegExpDuplicateNamedCapturingGroups,
RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
Tolerant = false
};
var parser = new Parser(options);
var script = parser.ParseScript(args.At(0).AsString());

Expand Down
16 changes: 3 additions & 13 deletions Jint/AstExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Jint
{
public static class AstExtensions
{
internal static readonly SourceLocation DefaultLocation;

public static JsValue GetKey<T>(this T property, Engine engine) where T : IProperty => GetKey(property.Key, engine, property.Computed);

public static JsValue GetKey(this Expression expression, Engine engine, bool resolveComputed = false)
Expand Down Expand Up @@ -290,18 +292,6 @@ internal static void PrivateBoundIdentifiers(this Node parameter, HashSet<Privat
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static IDestructuringPattern? AsBindingPattern(this Node? node)
{
return (IDestructuringPattern?) (node as ArrayPattern) ?? node as ObjectPattern;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Node? AsBindingPatternNode(this Node? node)
{
return (Node?) (node as ArrayPattern) ?? node as ObjectPattern;
}

internal static void BindingInitialization(
this Node? expression,
EvaluationContext context,
Expand All @@ -313,7 +303,7 @@ internal static void BindingInitialization(
var catchEnvRecord = (DeclarativeEnvironment) env;
catchEnvRecord.CreateMutableBindingAndInitialize(identifier.Name, canBeDeleted: false, value);
}
else if (expression.AsBindingPattern() is { } bindingPattern)
else if (expression is DestructuringPattern bindingPattern)
{
BindingPatternAssignmentExpression.ProcessPatterns(
context,
Expand Down
3 changes: 2 additions & 1 deletion Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public sealed partial class Engine : IDisposable
{
internal static readonly ParserOptions BaseParserOptions = ParserOptions.Default with
{
EcmaVersion = EcmaVersion.Experimental,
EcmaVersion = EcmaVersion.ES2023,
ExperimentalESFeatures = ExperimentalESFeatures.ImportAttributes | ExperimentalESFeatures.RegExpDuplicateNamedCapturingGroups,
RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
};

Expand Down
9 changes: 0 additions & 9 deletions Jint/Messages.cs

This file was deleted.

4 changes: 1 addition & 3 deletions Jint/Native/Function/EvalFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ internal JsValue PerformEval(JsValue x, Realm callerRealm, bool strictCaller, bo
}
catch (ParseErrorException e)
{
// TODO: This may be not reliable as Acornima messages are translatable.
// Also, there are multiple InvalidLHS messages.
if (string.Equals(e.Description, Messages.InvalidLHSInAssignment, StringComparison.Ordinal))
if (string.Equals(e.Error.Code, "InvalidLhsInAssignment", StringComparison.Ordinal))
{
ExceptionHelper.ThrowReferenceError(callerRealm, (string?) null);
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/RegExp/RegExpConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private JsRegExp RegExpInitialize(JsRegExp r, JsValue pattern, JsValue flags)

try
{
var regExpParseResult = Tokenizer.AdaptRegExp(p, f, compiled: false, _engine.Options.Constraints.RegexTimeout, EcmaVersion.Experimental);
var regExpParseResult = Tokenizer.AdaptRegExp(p, f, compiled: false, _engine.Options.Constraints.RegexTimeout, experimentalESFeatures: Engine.BaseParserOptions.ExperimentalESFeatures);

if (!regExpParseResult.Success)
{
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/ShadowRealm/ShadowRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ internal JsValue PerformShadowRealmEval(string sourceText, Realm callerRealm)
}
catch (ParseErrorException e)
{
if (string.Equals(e.Description, Messages.InvalidLHSInAssignment, StringComparison.Ordinal))
if (string.Equals(e.Error.Code, "InvalidLhsInAssignment", StringComparison.Ordinal))
{
ExceptionHelper.ThrowReferenceError(callerRealm, Messages.InvalidLHSInAssignment);
ExceptionHelper.ThrowReferenceError(callerRealm, e.Description);
}
else
{
Expand Down
11 changes: 6 additions & 5 deletions Jint/Runtime/CallStack/CallStackElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ public CallStackElement(
public readonly JintExpression? Expression;
public readonly CallStackExecutionContext CallingExecutionContext;

public SourceLocation Location
public ref readonly SourceLocation Location
{
get
{
var expressionLocation = Expression?._expression.Location;
if (expressionLocation != null && expressionLocation.Value != default)
ref readonly var expressionLocation = ref (Expression is not null ? ref Expression._expression.LocationRef : ref AstExtensions.DefaultLocation);
if (expressionLocation != default)
{
return expressionLocation.Value;
return ref expressionLocation;
}

return ((Node?) Function._functionDefinition?.Function)?.Location ?? default;
var function = (Node?) Function._functionDefinition?.Function;
return ref (function is not null ? ref function.LocationRef : ref AstExtensions.DefaultLocation);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Completion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Completion(CompletionType type, JsValue value, Node source)

public readonly CompletionType Type;
public readonly JsValue Value;
public readonly SourceLocation Location => _source.Location;
public readonly ref readonly SourceLocation Location => ref _source.LocationRef;

public static ref readonly Completion Empty() => ref _emptyCompletion;

Expand Down
7 changes: 4 additions & 3 deletions Jint/Runtime/Debugger/CallFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ namespace Jint.Runtime.Debugger
public sealed class CallFrame
{
private readonly CallStackExecutionContext _context;
private SourceLocation _location;
private readonly CallStackElement? _element;
private readonly Lazy<DebugScopes> _scopeChain;

internal CallFrame(
CallStackElement? element,
in CallStackExecutionContext context,
SourceLocation location,
in SourceLocation location,
JsValue? returnValue)
{
_element = element;
_context = context;
Location = location;
_location = location;
ReturnValue = returnValue;

_scopeChain = new Lazy<DebugScopes>(() => new DebugScopes(Environment));
Expand All @@ -43,7 +44,7 @@ internal CallFrame(
/// <summary>
/// Currently executing source location in this call frame.
/// </summary>
public SourceLocation Location { get; }
public ref SourceLocation Location => ref _location;

/// <summary>
/// The scope chain of this call frame.
Expand Down
4 changes: 2 additions & 2 deletions Jint/Runtime/Debugger/DebugHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ internal void OnReturnPoint(Node functionBody, JsValue returnValue)

private void CheckBreakPointAndPause(
Node? node,
SourceLocation location,
in SourceLocation location,
JsValue? returnValue = null)
{
CurrentLocation = location;
Expand Down Expand Up @@ -216,7 +216,7 @@ private void CheckBreakPointAndPause(
private void Pause(
PauseType type,
Node? node,
SourceLocation location,
in SourceLocation location,
JsValue? returnValue = null,
BreakPoint? breakPoint = null)
{
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Debugger/DebugInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal DebugInformation(
/// The current source Location.
/// For return points, this starts and ends at the end of the function body.
/// </summary>
public SourceLocation Location => CurrentCallFrame.Location;
public ref readonly SourceLocation Location => ref CurrentCallFrame.Location;

/// <summary>
/// Not implemented. Will always return 0.
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Environments/FunctionEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private void HandleRestElementArray(
{
SetItemSafely(restIdentifier.Name, array, initiallyEmpty);
}
else if (restElement.Argument.AsBindingPatternNode() is { } bindingPattern)
else if (restElement.Argument is DestructuringPattern bindingPattern)
{
SetFunctionParameter(context, bindingPattern, new [] { array }, 0, initiallyEmpty);
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/ExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void ThrowSyntaxError(Realm realm, string? message = null)
}

[DoesNotReturn]
public static void ThrowSyntaxError(Realm realm, string message, SourceLocation location)
public static void ThrowSyntaxError(Realm realm, string message, in SourceLocation location)
{
throw new JavaScriptException(realm.Intrinsics.SyntaxError, message).SetJavaScriptLocation(location);
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/IScriptOrModule.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Jint.Runtime;

internal static class ScriptOrModuleExtensions
{
public static Module AsModule(this IScriptOrModule? scriptOrModule, Engine engine, SourceLocation location)
public static Module AsModule(this IScriptOrModule? scriptOrModule, Engine engine, in SourceLocation location)
{
if (scriptOrModule is not Module module)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace Jint.Runtime.Interpreter.Expressions
{
internal sealed class BindingPatternAssignmentExpression : JintExpression
{
private readonly IDestructuringPattern _pattern;
private readonly DestructuringPattern _pattern;
private JintExpression _right = null!;
private bool _initialized;

public BindingPatternAssignmentExpression(AssignmentExpression expression) : base(expression)
{
_pattern = (IDestructuringPattern) expression.Left;
_pattern = (DestructuringPattern) expression.Left;
}

protected override object EvaluateInternal(EvaluationContext context)
Expand Down Expand Up @@ -43,7 +43,7 @@ protected override object EvaluateInternal(EvaluationContext context)

internal static JsValue ProcessPatterns(
EvaluationContext context,
IDestructuringPattern pattern,
DestructuringPattern pattern,
JsValue argument,
Environment? environment,
bool checkPatternPropertyReference = true)
Expand Down Expand Up @@ -158,7 +158,7 @@ private static JsValue HandleArrayPattern(

AssignToReference(engine, reference, value, environment);
}
else if (left.AsBindingPattern() is { } bindingPattern)
else if (left is DestructuringPattern bindingPattern)
{
JsValue value;
if (arrayOperations != null)
Expand Down Expand Up @@ -216,9 +216,9 @@ private static JsValue HandleArrayPattern(
{
AssignToIdentifier(engine, leftIdentifier.Name, array, environment, checkReference);
}
else if ((bindingPattern = restElement.Argument.AsBindingPattern()) is not null)
else if (restElement.Argument is DestructuringPattern bp)
{
ProcessPatterns(context, bindingPattern, array, environment);
ProcessPatterns(context, bp, array, environment);
}
else
{
Expand Down Expand Up @@ -257,9 +257,9 @@ private static JsValue HandleArrayPattern(

AssignToIdentifier(engine, leftIdentifier.Name, value, environment, checkReference);
}
else if ((bindingPattern = assignmentPattern.Left.AsBindingPattern()) is not null)
else if (assignmentPattern.Left is DestructuringPattern bp)
{
ProcessPatterns(context, bindingPattern, value, environment);
ProcessPatterns(context, bp, value, environment);
}
}
else
Expand Down Expand Up @@ -299,7 +299,6 @@ private static JsValue HandleObjectPattern(
: null;

var source = TypeConverter.ToObject(context.Engine.Realm, argument);
IDestructuringPattern? bindingPattern;
for (var i = 0; i < pattern.Properties.Count; i++)
{
if (pattern.Properties[i] is AssignmentProperty p)
Expand Down Expand Up @@ -336,9 +335,9 @@ private static JsValue HandleObjectPattern(
value = completion;
}

if ((bindingPattern = assignmentPattern.Left.AsBindingPattern()) is not null)
if (assignmentPattern.Left is DestructuringPattern bp)
{
ProcessPatterns(context, bindingPattern, value, environment);
ProcessPatterns(context, bp, value, environment);
continue;
}

Expand All @@ -351,7 +350,7 @@ private static JsValue HandleObjectPattern(

AssignToIdentifier(context.Engine, target!.Name, value, environment, checkReference);
}
else if ((bindingPattern = p.Value.AsBindingPattern()) is not null)
else if (p.Value is DestructuringPattern bindingPattern)
{
var value = source.Get(sourceKey);
ProcessPatterns(context, bindingPattern, value, environment);
Expand Down Expand Up @@ -380,9 +379,9 @@ private static JsValue HandleObjectPattern(
source.CopyDataProperties(rest, processedProperties);
AssignToIdentifier(context.Engine, leftIdentifier.Name, rest, environment, checkReference);
}
else if ((bindingPattern = restElement.Argument.AsBindingPattern()) is not null)
else if (restElement.Argument is DestructuringPattern bp)
{
ProcessPatterns(context, bindingPattern, argument, environment);
ProcessPatterns(context, bp, argument, environment);
}
else if (restElement.Argument is MemberExpression memberExpression)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static JintExpression Build(AssignmentExpression expression)
{
if (expression.Operator == Operator.Assignment)
{
if (expression.Left.AsBindingPatternNode() is not null)
if (expression.Left is DestructuringPattern)
{
return new BindingPatternAssignmentExpression(expression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class JintForInForOfStatement : JintStatement<Statement>

private ProbablyBlockStatement _body;
private JintExpression? _expr;
private IDestructuringPattern? _assignmentPattern;
private DestructuringPattern? _assignmentPattern;
private JintExpression _right = null!;
private List<Key>? _tdzNames;
private bool _destructuring;
Expand All @@ -46,7 +46,6 @@ protected override void Initialize(EvaluationContext context)
{
_lhsKind = LhsKind.Assignment;
var engine = context.Engine;
IDestructuringPattern? bindingPattern;
if (_leftNode is VariableDeclaration variableDeclaration)
{
_lhsKind = variableDeclaration.Kind == VariableDeclarationKind.Var
Expand All @@ -61,7 +60,7 @@ protected override void Initialize(EvaluationContext context)
id.GetBoundNames(_tdzNames);
}

if ((bindingPattern = id.AsBindingPattern()) is not null)
if (id is DestructuringPattern bindingPattern)
{
_destructuring = true;
_assignmentPattern = bindingPattern;
Expand All @@ -72,7 +71,7 @@ protected override void Initialize(EvaluationContext context)
_expr = new JintIdentifierExpression(identifier);
}
}
else if ((bindingPattern = _leftNode.AsBindingPattern()) is not null)
else if (_leftNode is DestructuringPattern bindingPattern)
{
_destructuring = true;
_assignmentPattern = bindingPattern;
Expand Down
Loading

0 comments on commit 95825ea

Please sign in to comment.