-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
Copy pathEvalFunction.cs
221 lines (191 loc) · 7.46 KB
/
EvalFunction.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Environments;
using Jint.Runtime.Interpreter;
using Jint.Runtime.Interpreter.Statements;
using Environment = Jint.Runtime.Environments.Environment;
namespace Jint.Native.Function;
public sealed class EvalFunction : Function
{
private static readonly JsString _functionName = new("eval");
internal EvalFunction(
Engine engine,
Realm realm,
FunctionPrototype functionPrototype)
: base(
engine,
realm,
_functionName,
StrictModeScope.IsStrictModeCode ? FunctionThisMode.Strict : FunctionThisMode.Global)
{
_prototype = functionPrototype;
_length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
}
protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
{
var callerRealm = _engine.ExecutionContext.Realm;
var x = arguments.At(0);
return PerformEval(x, callerRealm, false, false);
}
/// <summary>
/// https://tc39.es/ecma262/#sec-performeval
/// </summary>
internal JsValue PerformEval(JsValue x, Realm callerRealm, bool strictCaller, bool direct)
{
if (!x.IsString())
{
return x;
}
var evalRealm = _realm;
_engine._host.EnsureCanCompileStrings(callerRealm, evalRealm);
var inFunction = false;
var inMethod = false;
var inDerivedConstructor = false;
var inClassFieldInitializer = false;
if (direct)
{
var thisEnvRec = _engine.ExecutionContext.GetThisEnvironment();
if (thisEnvRec is FunctionEnvironment functionEnvironmentRecord)
{
var F = functionEnvironmentRecord._functionObject;
inFunction = true;
inMethod = thisEnvRec.HasSuperBinding();
if (F._constructorKind == ConstructorKind.Derived)
{
inDerivedConstructor = true;
}
var classFieldInitializerName = (F as ScriptFunction)?._classFieldInitializerName;
if (!string.IsNullOrEmpty(classFieldInitializerName?.ToString()))
{
inClassFieldInitializer = true;
}
}
}
Script? script = null;
var parserOptions = _engine.GetActiveParserOptions();
var adjustedParserOptions = parserOptions with
{
AllowReturnOutsideFunction = false,
AllowNewTargetOutsideFunction = true,
AllowSuperOutsideMethod = true,
// This is a workaround, just makes some tests pass. Actually, we need these checks (done either by the parser or by the runtime).
// TODO: implement a correct solution
CheckPrivateFields = false
};
var parser = _engine.GetParserFor(adjustedParserOptions);
script = parser.ParseScriptGuarded(_engine.Realm, x.ToString(), strict: strictCaller);
var body = script.Body;
if (body.Count == 0)
{
return Undefined;
}
var analyzer = new EvalScriptAnalyzer();
analyzer.Visit(script);
if (!inFunction)
{
// if body Contains NewTarget, throw a SyntaxError exception.
if (analyzer._containsNewTarget)
{
ExceptionHelper.ThrowSyntaxError(evalRealm, "new.target expression is not allowed here");
}
}
if (!inMethod)
{
// if body Contains SuperProperty, throw a SyntaxError exception.
if (analyzer._containsSuperProperty)
{
ExceptionHelper.ThrowSyntaxError(evalRealm, "'super' keyword unexpected here");
}
}
if (!inDerivedConstructor)
{
// if body Contains SuperCall, throw a SyntaxError exception.
if (analyzer._containsSuperCall)
{
ExceptionHelper.ThrowSyntaxError(evalRealm, "'super' keyword unexpected here");
}
}
if (inClassFieldInitializer)
{
// if ContainsArguments of body is true, throw a SyntaxError exception.
if (analyzer._containsArguments)
{
ExceptionHelper.ThrowSyntaxError(evalRealm, "'arguments' is not allowed in class field initializer or static initialization block");
}
}
var strictEval = script.Strict || _engine._isStrict;
var ctx = _engine.ExecutionContext;
using (new StrictModeScope(strictEval))
{
Environment lexEnv;
Environment varEnv;
PrivateEnvironment? privateEnv;
if (direct)
{
lexEnv = JintEnvironment.NewDeclarativeEnvironment(_engine, ctx.LexicalEnvironment);
varEnv = ctx.VariableEnvironment;
privateEnv = ctx.PrivateEnvironment;
}
else
{
lexEnv = JintEnvironment.NewDeclarativeEnvironment(_engine, evalRealm.GlobalEnv);
varEnv = evalRealm.GlobalEnv;
privateEnv = null;
}
if (strictEval)
{
varEnv = lexEnv;
}
// If ctx is not already suspended, suspend ctx.
Engine.EnterExecutionContext(lexEnv, varEnv, evalRealm, privateEnv);
try
{
Engine.EvalDeclarationInstantiation(script, varEnv, lexEnv, privateEnv, strictEval);
var statement = new JintScript(script);
var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);
var result = statement.Execute(context);
var value = result.GetValueOrDefault();
if (result.Type == CompletionType.Throw)
{
ExceptionHelper.ThrowJavaScriptException(_engine, value, result);
return null!;
}
else
{
return value;
}
}
finally
{
Engine.LeaveExecutionContext();
}
}
}
private sealed class EvalScriptAnalyzer : AstVisitor
{
public bool _containsArguments;
public bool _containsNewTarget;
public bool _containsSuperCall;
public bool _containsSuperProperty;
protected override object VisitIdentifier(Identifier identifier)
{
_containsArguments |= string.Equals(identifier.Name, "arguments", StringComparison.Ordinal);
return identifier;
}
protected override object VisitMetaProperty(MetaProperty metaProperty)
{
_containsNewTarget |= string.Equals(metaProperty.Meta.Name, "new", StringComparison.Ordinal) && string.Equals(metaProperty.Property.Name, "target", StringComparison.Ordinal);
return metaProperty;
}
protected override object? VisitMemberExpression(MemberExpression memberExpression)
{
_containsSuperProperty |= memberExpression.Object.Type == NodeType.Super;
return base.VisitMemberExpression(memberExpression);
}
protected override object? VisitCallExpression(CallExpression callExpression)
{
_containsSuperCall |= callExpression.Callee.Type == NodeType.Super;
return base.VisitCallExpression(callExpression);
}
}
}