This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathInterpretedFunction.cs
More file actions
186 lines (158 loc) · 6.14 KB
/
InterpretedFunction.cs
File metadata and controls
186 lines (158 loc) · 6.14 KB
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
//------------------------------------------------------------------------------
// <license file="InterpretedFunction.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
using EcmaScript.NET.Debugging;
using EcmaScript.NET.Types;
namespace EcmaScript.NET
{
sealed class InterpretedFunction : BuiltinFunction, IScript
{
override public string FunctionName
{
get
{
return (idata.itsName == null) ? "" : idata.itsName;
}
}
override public string EncodedSource
{
get
{
return Interpreter.GetEncodedSource (idata);
}
}
override public DebuggableScript DebuggableView
{
get
{
return idata;
}
}
override protected internal Context.Versions LanguageVersion
{
get
{
return idata.languageVersion;
}
}
override protected internal int ParamCount
{
get
{
return idata.argCount;
}
}
override protected internal int ParamAndVarCount
{
get
{
return idata.argNames.Length;
}
}
internal InterpreterData idata;
internal SecurityController securityController;
internal object securityDomain;
internal IScriptable [] functionRegExps;
private InterpretedFunction (InterpreterData idata, object staticSecurityDomain)
{
this.idata = idata;
// Always get Context from the current thread to
// avoid security breaches via passing mangled Context instances
// with bogus SecurityController
Context cx = Context.CurrentContext;
SecurityController sc = cx.SecurityController;
object dynamicDomain;
if (sc != null) {
dynamicDomain = sc.getDynamicSecurityDomain (staticSecurityDomain);
}
else {
if (staticSecurityDomain != null) {
throw new ArgumentException ();
}
dynamicDomain = null;
}
this.securityController = sc;
this.securityDomain = dynamicDomain;
}
private InterpretedFunction (InterpretedFunction parent, int index)
{
this.idata = parent.idata.itsNestedFunctions [index];
this.securityController = parent.securityController;
this.securityDomain = parent.securityDomain;
}
/// <summary> Create script from compiled bytecode.</summary>
internal static InterpretedFunction createScript (InterpreterData idata, object staticSecurityDomain)
{
InterpretedFunction f;
f = new InterpretedFunction (idata, staticSecurityDomain);
return f;
}
/// <summary> Create function compiled from Function(...) constructor.</summary>
internal static InterpretedFunction createFunction (Context cx, IScriptable scope, InterpreterData idata, object staticSecurityDomain)
{
InterpretedFunction f;
f = new InterpretedFunction (idata, staticSecurityDomain);
f.initInterpretedFunction (cx, scope);
return f;
}
/// <summary> Create function embedded in script or another function.</summary>
internal static InterpretedFunction createFunction (Context cx, IScriptable scope, InterpretedFunction parent, int index)
{
InterpretedFunction f = new InterpretedFunction (parent, index);
f.initInterpretedFunction (cx, scope);
return f;
}
internal IScriptable [] createRegExpWraps (Context cx, IScriptable scope)
{
if (idata.itsRegExpLiterals == null)
Context.CodeBug ();
RegExpProxy rep = cx.RegExpProxy;
int N = idata.itsRegExpLiterals.Length;
IScriptable [] array = new IScriptable [N];
for (int i = 0; i != N; ++i) {
array [i] = rep.Wrap (cx, scope, idata.itsRegExpLiterals [i]);
}
return array;
}
private void initInterpretedFunction (Context cx, IScriptable scope)
{
initScriptFunction (cx, scope);
if (idata.itsRegExpLiterals != null) {
functionRegExps = createRegExpWraps (cx, scope);
}
}
public override object Call (Context cx, IScriptable scope, IScriptable thisObj, object [] args)
{
if (!ScriptRuntime.hasTopCall (cx)) {
return ScriptRuntime.DoTopCall (this, cx, scope, thisObj, args);
}
return Interpreter.Interpret (this, cx, scope, thisObj, args);
}
public object Exec (Context cx, IScriptable scope)
{
if (idata.itsFunctionType != 0) {
// Can only be applied to scripts
throw new Exception ();
}
if (!ScriptRuntime.hasTopCall (cx)) {
// It will go through "call" path. but they are equivalent
return ScriptRuntime.DoTopCall (this, cx, scope, scope, ScriptRuntime.EmptyArgs);
}
return Interpreter.Interpret (this, cx, scope, scope, ScriptRuntime.EmptyArgs);
}
protected internal override string getParamOrVarName (int index)
{
return idata.argNames [index];
}
}
}