-
-
Notifications
You must be signed in to change notification settings - Fork 547
/
JintStatementList.cs
144 lines (133 loc) · 5.07 KB
/
JintStatementList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System.Collections.Generic;
using Esprima.Ast;
using Jint.Native;
using Jint.Runtime.Environments;
using Jint.Runtime.Interpreter.Statements;
namespace Jint.Runtime.Interpreter
{
internal class JintStatementList
{
private class Pair
{
internal JintStatement Statement;
internal Completion? Value;
}
private readonly Engine _engine;
private readonly Statement _statement;
private readonly NodeList<Statement> _statements;
private Pair[] _jintStatements;
private bool _initialized;
public JintStatementList(Engine engine, Statement statement, NodeList<Statement> statements)
{
_engine = engine;
_statement = statement;
_statements = statements;
}
private void Initialize()
{
var jintStatements = new Pair[_statements.Count];
for (var i = 0; i < jintStatements.Length; i++)
{
var esprimaStatement = _statements[i];
jintStatements[i] = new Pair
{
Statement = JintStatement.Build(_engine, esprimaStatement),
Value = JintStatement.FastResolve(esprimaStatement)
};
}
_jintStatements = jintStatements;
}
public Completion Execute()
{
if (!_initialized)
{
Initialize();
_initialized = true;
}
if (_statement != null)
{
_engine._lastSyntaxNode = _statement;
_engine.RunBeforeExecuteStatementChecks(_statement);
}
JintStatement s = null;
var c = new Completion(CompletionType.Normal, null, null, _engine._lastSyntaxNode?.Location ?? default);
Completion sl = c;
// The value of a StatementList is the value of the last value-producing item in the StatementList
JsValue lastValue = null;
try
{
foreach (var pair in _jintStatements)
{
s = pair.Statement;
c = pair.Value ?? s.Execute();
if (c.Type != CompletionType.Normal)
{
return new Completion(
c.Type,
c.Value ?? sl.Value,
c.Identifier,
c.Location);
}
sl = c;
lastValue = c.Value ?? lastValue;
}
}
catch (JavaScriptException v)
{
var location = v.Location == default ? s.Location : v.Location;
var completion = new Completion(CompletionType.Throw, v.Error, null, location);
return completion;
}
catch (TypeErrorException e)
{
var error = _engine.TypeError.Construct(new JsValue[]
{
e.Message
});
return new Completion(CompletionType.Throw, error, null, s.Location);
}
catch (RangeErrorException e)
{
var error = _engine.RangeError.Construct(new JsValue[]
{
e.Message
});
c = new Completion(CompletionType.Throw, error, null, s.Location);
}
return new Completion(c.Type, lastValue ?? JsValue.Undefined, c.Identifier, c.Location);
}
/// <summary>
/// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
/// </summary>
internal static void BlockDeclarationInstantiation(
LexicalEnvironment env,
List<VariableDeclaration> lexicalDeclarations)
{
var envRec = env._record;
var boundNames = new List<string>();
for (var i = 0; i < lexicalDeclarations.Count; i++)
{
var variableDeclaration = lexicalDeclarations[i];
boundNames.Clear();
variableDeclaration.GetBoundNames(boundNames);
for (var j = 0; j < boundNames.Count; j++)
{
var dn = boundNames[j];
if (variableDeclaration.Kind == VariableDeclarationKind.Const)
{
envRec.CreateImmutableBinding(dn, strict: true);
}
else
{
envRec.CreateMutableBinding(dn, canBeDeleted: false);
}
}
/* If d is a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration, then
* Let fn be the sole element of the BoundNames of d.
* Let fo be the result of performing InstantiateFunctionObject for d with argument env.
* Perform envRec.InitializeBinding(fn, fo).
*/
}
}
}
}