Skip to content

Commit

Permalink
Move expression initialize to expression implementation (#1677)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 10, 2023
1 parent bd171d0 commit 791384f
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ internal sealed class BindingPatternAssignmentExpression : JintExpression
{
private readonly BindingPattern _pattern;
private JintExpression _right = null!;
private bool _initialized;

public BindingPatternAssignmentExpression(AssignmentExpression expression) : base(expression)
{
_pattern = (BindingPattern) expression.Left;
_initialized = false;
}

protected override void Initialize(EvaluationContext context)
{
_right = Build(((AssignmentExpression) _expression).Right);
}

protected override object EvaluateInternal(EvaluationContext context)
{
if (!_initialized)
{
_right = Build(((AssignmentExpression) _expression).Right);
_initialized = true;
}

var rightValue = _right.GetValue(context);
if (context.IsAbrupt())
{
Expand Down
10 changes: 8 additions & 2 deletions Jint/Runtime/Interpreter/Expressions/JintArrayExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ internal sealed class JintArrayExpression : JintExpression
{
private JintExpression?[] _expressions = Array.Empty<JintExpression?>();
private bool _hasSpreads;
private bool _initialized;

private JintArrayExpression(ArrayExpression expression) : base(expression)
{
_initialized = false;
}

public static JintExpression Build(ArrayExpression expression)
Expand All @@ -21,7 +21,7 @@ public static JintExpression Build(ArrayExpression expression)
: new JintArrayExpression(expression);
}

protected override void Initialize(EvaluationContext context)
private void Initialize()
{
ref readonly var elements = ref ((ArrayExpression) _expression).Elements;
var expressions = _expressions = new JintExpression[((ArrayExpression) _expression).Elements.Count];
Expand All @@ -42,6 +42,12 @@ protected override void Initialize(EvaluationContext context)

protected override object EvaluateInternal(EvaluationContext context)
{
if (!_initialized)
{
Initialize();
_initialized = true;
}

var engine = context.Engine;
var a = engine.Realm.Intrinsics.Array.ArrayCreate(_hasSpreads ? 0 : (uint) _expressions.Length);

Expand Down
10 changes: 8 additions & 2 deletions Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ internal sealed class SimpleAssignmentExpression : JintExpression

private JintIdentifierExpression? _leftIdentifier;
private bool _evalOrArguments;
private bool _initialized;

public SimpleAssignmentExpression(AssignmentExpression expression) : base(expression)
{
_initialized = false;
}

protected override void Initialize(EvaluationContext context)
private void Initialize()
{
var assignmentExpression = (AssignmentExpression) _expression;
_left = Build((Expression) assignmentExpression.Left);
Expand All @@ -361,6 +361,12 @@ protected override void Initialize(EvaluationContext context)

protected override object EvaluateInternal(EvaluationContext context)
{
if (!_initialized)
{
Initialize();
_initialized = true;
}

object? completion = null;
if (_leftIdentifier != null)
{
Expand Down
12 changes: 7 additions & 5 deletions Jint/Runtime/Interpreter/Expressions/JintAwaitExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ namespace Jint.Runtime.Interpreter.Expressions;
internal sealed class JintAwaitExpression : JintExpression
{
private JintExpression _awaitExpression = null!;
private bool _initialized;

public JintAwaitExpression(AwaitExpression expression) : base(expression)
{
_initialized = false;
}

protected override void Initialize(EvaluationContext context)
{
_awaitExpression = Build(((AwaitExpression) _expression).Argument);
}

protected override object EvaluateInternal(EvaluationContext context)
{
if (!_initialized)
{
_awaitExpression = Build(((AwaitExpression) _expression).Argument);
_initialized = true;
}

var engine = context.Engine;
var asyncContext = engine.ExecutionContext;

Expand Down
41 changes: 39 additions & 2 deletions Jint/Runtime/Interpreter/Expressions/JintBinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@ internal abstract class JintBinaryExpression : JintExpression

private JintExpression _left = null!;
private JintExpression _right = null!;
private bool _initialized;

private JintBinaryExpression(BinaryExpression expression) : base(expression)
{
// TODO check https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
_initialized = false;
}

protected override void Initialize(EvaluationContext context)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EnsureInitialized()
{
if (_initialized)
{
return;
}

var expression = (BinaryExpression) _expression;
_left = Build(expression.Left);
_right = Build(expression.Right);
_initialized = true;
}

internal static bool TryOperatorOverloading(
Expand Down Expand Up @@ -196,6 +203,8 @@ public StrictlyEqualBinaryExpression(BinaryExpression expression) : base(express

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);
var equal = left == right;
Expand All @@ -211,6 +220,8 @@ public StrictlyNotEqualBinaryExpression(BinaryExpression expression) : base(expr

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);
return left == right ? JsBoolean.False : JsBoolean.True;
Expand All @@ -225,6 +236,8 @@ public LessBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand All @@ -248,6 +261,8 @@ public GreaterBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand All @@ -271,6 +286,8 @@ public PlusBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand Down Expand Up @@ -314,6 +331,8 @@ public MinusBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand Down Expand Up @@ -352,6 +371,8 @@ public TimesBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand Down Expand Up @@ -393,6 +414,8 @@ public DivideBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand All @@ -419,6 +442,8 @@ public EqualBinaryExpression(BinaryExpression expression, bool invert = false) :

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand Down Expand Up @@ -448,6 +473,8 @@ public CompareBinaryExpression(BinaryExpression expression, bool leftFirst) : ba

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var leftValue = _left.GetValue(context);
var rightValue = _right.GetValue(context);

Expand All @@ -473,6 +500,8 @@ public InstanceOfBinaryExpression(BinaryExpression expression) : base(expression

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var leftValue = _left.GetValue(context);
var rightValue = _right.GetValue(context);
return leftValue.InstanceofOperator(rightValue) ? JsBoolean.True : JsBoolean.False;
Expand All @@ -487,6 +516,8 @@ public ExponentiationBinaryExpression(BinaryExpression expression) : base(expres

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var leftReference = _left.GetValue(context);
var rightReference = _right.GetValue(context);

Expand Down Expand Up @@ -612,6 +643,8 @@ public InBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand Down Expand Up @@ -640,6 +673,8 @@ public ModuloBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var left = _left.GetValue(context);
var right = _right.GetValue(context);

Expand Down Expand Up @@ -754,6 +789,8 @@ public BitwiseBinaryExpression(BinaryExpression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
EnsureInitialized();

var lval = _left.GetValue(context);
var rval = _right.GetValue(context);

Expand Down
10 changes: 8 additions & 2 deletions Jint/Runtime/Interpreter/Expressions/JintCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ internal sealed class JintCallExpression : JintExpression

private JintExpression _calleeExpression = null!;
private bool _hasSpreads;
private bool _initialized;

public JintCallExpression(CallExpression expression) : base(expression)
{
_initialized = false;
}

protected override void Initialize(EvaluationContext context)
private void Initialize(EvaluationContext context)
{
var expression = (CallExpression) _expression;
ref readonly var expressionArguments = ref expression.Arguments;
Expand Down Expand Up @@ -78,6 +78,12 @@ static bool CanSpread(Node? e)

protected override object EvaluateInternal(EvaluationContext context)
{
if (!_initialized)
{
Initialize(context);
_initialized = true;
}

if (!context.Engine._stackGuard.TryEnterOnCurrentStack())
{
return context.Engine._stackGuard.RunOnEmptyStack(EvaluateInternal, context);
Expand Down
23 changes: 0 additions & 23 deletions Jint/Runtime/Interpreter/Expressions/JintExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ namespace Jint.Runtime.Interpreter.Expressions
{
internal abstract class JintExpression
{
// require sub-classes to set to false explicitly to skip virtual call
protected bool _initialized = true;

protected internal readonly Expression _expression;

protected JintExpression(Expression expression)
Expand Down Expand Up @@ -43,12 +40,6 @@ public object Evaluate(EvaluationContext context)
var oldSyntaxElement = context.LastSyntaxElement;
context.PrepareFor(_expression);

if (!_initialized)
{
Initialize(context);
_initialized = true;
}

var result = EvaluateInternal(context);

context.LastSyntaxElement = oldSyntaxElement;
Expand All @@ -59,23 +50,9 @@ public object Evaluate(EvaluationContext context)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal object EvaluateWithoutNodeTracking(EvaluationContext context)
{
if (!_initialized)
{
Initialize(context);
_initialized = true;
}

return EvaluateInternal(context);
}

/// <summary>
/// Opportunity to build one-time structures and caching based on lexical context.
/// </summary>
/// <param name="context"></param>
protected virtual void Initialize(EvaluationContext context)
{
}

protected abstract object EvaluateInternal(EvaluationContext context);

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions Jint/Runtime/Interpreter/Expressions/JintIdentifierExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Jint.Runtime.Interpreter.Expressions;
internal sealed class JintIdentifierExpression : JintExpression
{
private EnvironmentRecord.BindingName _identifier = null!;
private bool _initialized;

public JintIdentifierExpression(Identifier expression) : base(expression)
{
_initialized = false;
}

public EnvironmentRecord.BindingName Identifier
Expand All @@ -25,7 +25,7 @@ public EnvironmentRecord.BindingName Identifier
}
}

protected override void Initialize(EvaluationContext context)
private void Initialize()
{
EnsureIdentifier();
}
Expand All @@ -47,6 +47,12 @@ public bool HasEvalOrArguments

protected override object EvaluateInternal(EvaluationContext context)
{
if (!_initialized)
{
Initialize();
_initialized = true;
}

var engine = context.Engine;
var env = engine.ExecutionContext.LexicalEnvironment;
var strict = StrictModeScope.IsStrictModeCode;
Expand Down
Loading

0 comments on commit 791384f

Please sign in to comment.