Skip to content

Commit 791384f

Browse files
authored
Move expression initialize to expression implementation (#1677)
1 parent bd171d0 commit 791384f

17 files changed

+158
-68
lines changed

Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@ internal sealed class BindingPatternAssignmentExpression : JintExpression
1212
{
1313
private readonly BindingPattern _pattern;
1414
private JintExpression _right = null!;
15+
private bool _initialized;
1516

1617
public BindingPatternAssignmentExpression(AssignmentExpression expression) : base(expression)
1718
{
1819
_pattern = (BindingPattern) expression.Left;
19-
_initialized = false;
20-
}
21-
22-
protected override void Initialize(EvaluationContext context)
23-
{
24-
_right = Build(((AssignmentExpression) _expression).Right);
2520
}
2621

2722
protected override object EvaluateInternal(EvaluationContext context)
2823
{
24+
if (!_initialized)
25+
{
26+
_right = Build(((AssignmentExpression) _expression).Right);
27+
_initialized = true;
28+
}
29+
2930
var rightValue = _right.GetValue(context);
3031
if (context.IsAbrupt())
3132
{

Jint/Runtime/Interpreter/Expressions/JintArrayExpression.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ internal sealed class JintArrayExpression : JintExpression
88
{
99
private JintExpression?[] _expressions = Array.Empty<JintExpression?>();
1010
private bool _hasSpreads;
11+
private bool _initialized;
1112

1213
private JintArrayExpression(ArrayExpression expression) : base(expression)
1314
{
14-
_initialized = false;
1515
}
1616

1717
public static JintExpression Build(ArrayExpression expression)
@@ -21,7 +21,7 @@ public static JintExpression Build(ArrayExpression expression)
2121
: new JintArrayExpression(expression);
2222
}
2323

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

4343
protected override object EvaluateInternal(EvaluationContext context)
4444
{
45+
if (!_initialized)
46+
{
47+
Initialize();
48+
_initialized = true;
49+
}
50+
4551
var engine = context.Engine;
4652
var a = engine.Realm.Intrinsics.Array.ArrayCreate(_hasSpreads ? 0 : (uint) _expressions.Length);
4753

Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,13 @@ internal sealed class SimpleAssignmentExpression : JintExpression
343343

344344
private JintIdentifierExpression? _leftIdentifier;
345345
private bool _evalOrArguments;
346+
private bool _initialized;
346347

347348
public SimpleAssignmentExpression(AssignmentExpression expression) : base(expression)
348349
{
349-
_initialized = false;
350350
}
351351

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

362362
protected override object EvaluateInternal(EvaluationContext context)
363363
{
364+
if (!_initialized)
365+
{
366+
Initialize();
367+
_initialized = true;
368+
}
369+
364370
object? completion = null;
365371
if (_leftIdentifier != null)
366372
{

Jint/Runtime/Interpreter/Expressions/JintAwaitExpression.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ namespace Jint.Runtime.Interpreter.Expressions;
66
internal sealed class JintAwaitExpression : JintExpression
77
{
88
private JintExpression _awaitExpression = null!;
9+
private bool _initialized;
910

1011
public JintAwaitExpression(AwaitExpression expression) : base(expression)
1112
{
1213
_initialized = false;
1314
}
1415

15-
protected override void Initialize(EvaluationContext context)
16-
{
17-
_awaitExpression = Build(((AwaitExpression) _expression).Argument);
18-
}
19-
2016
protected override object EvaluateInternal(EvaluationContext context)
2117
{
18+
if (!_initialized)
19+
{
20+
_awaitExpression = Build(((AwaitExpression) _expression).Argument);
21+
_initialized = true;
22+
}
23+
2224
var engine = context.Engine;
2325
var asyncContext = engine.ExecutionContext;
2426

Jint/Runtime/Interpreter/Expressions/JintBinaryExpression.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@ internal abstract class JintBinaryExpression : JintExpression
2020

2121
private JintExpression _left = null!;
2222
private JintExpression _right = null!;
23+
private bool _initialized;
2324

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

30-
protected override void Initialize(EvaluationContext context)
30+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31+
private void EnsureInitialized()
3132
{
33+
if (_initialized)
34+
{
35+
return;
36+
}
37+
3238
var expression = (BinaryExpression) _expression;
3339
_left = Build(expression.Left);
3440
_right = Build(expression.Right);
41+
_initialized = true;
3542
}
3643

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

197204
protected override object EvaluateInternal(EvaluationContext context)
198205
{
206+
EnsureInitialized();
207+
199208
var left = _left.GetValue(context);
200209
var right = _right.GetValue(context);
201210
var equal = left == right;
@@ -211,6 +220,8 @@ public StrictlyNotEqualBinaryExpression(BinaryExpression expression) : base(expr
211220

212221
protected override object EvaluateInternal(EvaluationContext context)
213222
{
223+
EnsureInitialized();
224+
214225
var left = _left.GetValue(context);
215226
var right = _right.GetValue(context);
216227
return left == right ? JsBoolean.False : JsBoolean.True;
@@ -225,6 +236,8 @@ public LessBinaryExpression(BinaryExpression expression) : base(expression)
225236

226237
protected override object EvaluateInternal(EvaluationContext context)
227238
{
239+
EnsureInitialized();
240+
228241
var left = _left.GetValue(context);
229242
var right = _right.GetValue(context);
230243

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

249262
protected override object EvaluateInternal(EvaluationContext context)
250263
{
264+
EnsureInitialized();
265+
251266
var left = _left.GetValue(context);
252267
var right = _right.GetValue(context);
253268

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

272287
protected override object EvaluateInternal(EvaluationContext context)
273288
{
289+
EnsureInitialized();
290+
274291
var left = _left.GetValue(context);
275292
var right = _right.GetValue(context);
276293

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

315332
protected override object EvaluateInternal(EvaluationContext context)
316333
{
334+
EnsureInitialized();
335+
317336
var left = _left.GetValue(context);
318337
var right = _right.GetValue(context);
319338

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

353372
protected override object EvaluateInternal(EvaluationContext context)
354373
{
374+
EnsureInitialized();
375+
355376
var left = _left.GetValue(context);
356377
var right = _right.GetValue(context);
357378

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

394415
protected override object EvaluateInternal(EvaluationContext context)
395416
{
417+
EnsureInitialized();
418+
396419
var left = _left.GetValue(context);
397420
var right = _right.GetValue(context);
398421

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

420443
protected override object EvaluateInternal(EvaluationContext context)
421444
{
445+
EnsureInitialized();
446+
422447
var left = _left.GetValue(context);
423448
var right = _right.GetValue(context);
424449

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

449474
protected override object EvaluateInternal(EvaluationContext context)
450475
{
476+
EnsureInitialized();
477+
451478
var leftValue = _left.GetValue(context);
452479
var rightValue = _right.GetValue(context);
453480

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

474501
protected override object EvaluateInternal(EvaluationContext context)
475502
{
503+
EnsureInitialized();
504+
476505
var leftValue = _left.GetValue(context);
477506
var rightValue = _right.GetValue(context);
478507
return leftValue.InstanceofOperator(rightValue) ? JsBoolean.True : JsBoolean.False;
@@ -487,6 +516,8 @@ public ExponentiationBinaryExpression(BinaryExpression expression) : base(expres
487516

488517
protected override object EvaluateInternal(EvaluationContext context)
489518
{
519+
EnsureInitialized();
520+
490521
var leftReference = _left.GetValue(context);
491522
var rightReference = _right.GetValue(context);
492523

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

613644
protected override object EvaluateInternal(EvaluationContext context)
614645
{
646+
EnsureInitialized();
647+
615648
var left = _left.GetValue(context);
616649
var right = _right.GetValue(context);
617650

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

641674
protected override object EvaluateInternal(EvaluationContext context)
642675
{
676+
EnsureInitialized();
677+
643678
var left = _left.GetValue(context);
644679
var right = _right.GetValue(context);
645680

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

755790
protected override object EvaluateInternal(EvaluationContext context)
756791
{
792+
EnsureInitialized();
793+
757794
var lval = _left.GetValue(context);
758795
var rval = _right.GetValue(context);
759796

Jint/Runtime/Interpreter/Expressions/JintCallExpression.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ internal sealed class JintCallExpression : JintExpression
1616

1717
private JintExpression _calleeExpression = null!;
1818
private bool _hasSpreads;
19+
private bool _initialized;
1920

2021
public JintCallExpression(CallExpression expression) : base(expression)
2122
{
22-
_initialized = false;
2323
}
2424

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

7979
protected override object EvaluateInternal(EvaluationContext context)
8080
{
81+
if (!_initialized)
82+
{
83+
Initialize(context);
84+
_initialized = true;
85+
}
86+
8187
if (!context.Engine._stackGuard.TryEnterOnCurrentStack())
8288
{
8389
return context.Engine._stackGuard.RunOnEmptyStack(EvaluateInternal, context);

Jint/Runtime/Interpreter/Expressions/JintExpression.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ namespace Jint.Runtime.Interpreter.Expressions
1010
{
1111
internal abstract class JintExpression
1212
{
13-
// require sub-classes to set to false explicitly to skip virtual call
14-
protected bool _initialized = true;
15-
1613
protected internal readonly Expression _expression;
1714

1815
protected JintExpression(Expression expression)
@@ -43,12 +40,6 @@ public object Evaluate(EvaluationContext context)
4340
var oldSyntaxElement = context.LastSyntaxElement;
4441
context.PrepareFor(_expression);
4542

46-
if (!_initialized)
47-
{
48-
Initialize(context);
49-
_initialized = true;
50-
}
51-
5243
var result = EvaluateInternal(context);
5344

5445
context.LastSyntaxElement = oldSyntaxElement;
@@ -59,23 +50,9 @@ public object Evaluate(EvaluationContext context)
5950
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6051
internal object EvaluateWithoutNodeTracking(EvaluationContext context)
6152
{
62-
if (!_initialized)
63-
{
64-
Initialize(context);
65-
_initialized = true;
66-
}
67-
6853
return EvaluateInternal(context);
6954
}
7055

71-
/// <summary>
72-
/// Opportunity to build one-time structures and caching based on lexical context.
73-
/// </summary>
74-
/// <param name="context"></param>
75-
protected virtual void Initialize(EvaluationContext context)
76-
{
77-
}
78-
7956
protected abstract object EvaluateInternal(EvaluationContext context);
8057

8158
/// <summary>

Jint/Runtime/Interpreter/Expressions/JintIdentifierExpression.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ namespace Jint.Runtime.Interpreter.Expressions;
1010
internal sealed class JintIdentifierExpression : JintExpression
1111
{
1212
private EnvironmentRecord.BindingName _identifier = null!;
13+
private bool _initialized;
1314

1415
public JintIdentifierExpression(Identifier expression) : base(expression)
1516
{
16-
_initialized = false;
1717
}
1818

1919
public EnvironmentRecord.BindingName Identifier
@@ -25,7 +25,7 @@ public EnvironmentRecord.BindingName Identifier
2525
}
2626
}
2727

28-
protected override void Initialize(EvaluationContext context)
28+
private void Initialize()
2929
{
3030
EnsureIdentifier();
3131
}
@@ -47,6 +47,12 @@ public bool HasEvalOrArguments
4747

4848
protected override object EvaluateInternal(EvaluationContext context)
4949
{
50+
if (!_initialized)
51+
{
52+
Initialize();
53+
_initialized = true;
54+
}
55+
5056
var engine = context.Engine;
5157
var env = engine.ExecutionContext.LexicalEnvironment;
5258
var strict = StrictModeScope.IsStrictModeCode;

0 commit comments

Comments
 (0)