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 pathBuiltinScript.cs
More file actions
237 lines (199 loc) · 7.41 KB
/
BuiltinScript.cs
File metadata and controls
237 lines (199 loc) · 7.41 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//------------------------------------------------------------------------------
// <license file="NativeScript.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;
namespace EcmaScript.NET.Types
{
/// <summary>
/// The JavaScript Script object.
///
/// Note that the C version of the engine uses XDR as the format used
/// by freeze and thaw. Since this depends on the internal format of
/// structures in the C runtime, we cannot duplicate it.
///
/// Since we cannot replace 'this' as a result of the compile method,
/// will forward requests to execute to the nonnull 'script' field.
///
/// </summary>
class BuiltinScript : BaseFunction
{
/// <summary> Returns the name of this JavaScript class, "Script".</summary>
override public string ClassName
{
get
{
return "Script";
}
}
override public int Length
{
get
{
return 0;
}
}
override public int Arity
{
get
{
return 0;
}
}
private static readonly object SCRIPT_TAG = new object ();
internal static new void Init (IScriptable scope, bool zealed)
{
BuiltinScript obj = new BuiltinScript (null);
obj.ExportAsJSClass (MAX_PROTOTYPE_ID, scope, zealed);
}
private BuiltinScript (IScript script)
{
this.script = script;
}
public override object Call (Context cx, IScriptable scope, IScriptable thisObj, object [] args)
{
if (script != null) {
return script.Exec (cx, scope);
}
return Undefined.Value;
}
public override IScriptable Construct (Context cx, IScriptable scope, object [] args)
{
throw Context.ReportRuntimeErrorById ("msg.script.is.not.constructor");
}
internal override string Decompile (int indent, int flags)
{
if (script is BuiltinFunction) {
return ((BuiltinFunction)script).Decompile (indent, flags);
}
return base.Decompile (indent, flags);
}
protected internal override void InitPrototypeId (int id)
{
string s;
int arity;
switch (id) {
case Id_constructor:
arity = 1;
s = "constructor";
break;
case Id_toString:
arity = 0;
s = "toString";
break;
case Id_exec:
arity = 0;
s = "exec";
break;
case Id_compile:
arity = 1;
s = "compile";
break;
default:
throw new ArgumentException (Convert.ToString (id));
}
InitPrototypeMethod (SCRIPT_TAG, id, s, arity);
}
public override object ExecIdCall (IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args)
{
if (!f.HasTag (SCRIPT_TAG)) {
return base.ExecIdCall (f, cx, scope, thisObj, args);
}
int id = f.MethodId;
switch (id) {
case Id_constructor: {
string source = (args.Length == 0) ? "" : ScriptConvert.ToString (args [0]);
IScript script = compile (cx, source);
BuiltinScript nscript = new BuiltinScript (script);
ScriptRuntime.setObjectProtoAndParent (nscript, scope);
return nscript;
}
case Id_toString: {
BuiltinScript real = realThis (thisObj, f);
IScript realScript = real.script;
if (realScript == null) {
return "";
}
return cx.DecompileScript (realScript, 0);
}
case Id_exec: {
throw Context.ReportRuntimeErrorById ("msg.cant.call.indirect", "exec");
}
case Id_compile: {
BuiltinScript real = realThis (thisObj, f);
string source = ScriptConvert.ToString (args, 0);
real.script = compile (cx, source);
return real;
}
}
throw new ArgumentException (Convert.ToString (id));
}
private static BuiltinScript realThis (IScriptable thisObj, IdFunctionObject f)
{
if (!(thisObj is BuiltinScript))
throw IncompatibleCallError (f);
return (BuiltinScript)thisObj;
}
private static IScript compile (Context cx, string source)
{
int [] linep = new int [] { 0 };
string filename = Context.GetSourcePositionFromStack (linep);
if (filename == null) {
filename = "<Script object>";
linep [0] = 1;
}
ErrorReporter reporter;
reporter = DefaultErrorReporter.ForEval (cx.ErrorReporter);
return cx.CompileString (source, null, reporter, filename, linep [0], (object)null);
}
protected internal override int FindPrototypeId (string s)
{
int id;
#region Generated PrototypeId Switch
L0: {
id = 0;
string X = null;
L:
switch (s.Length) {
case 4:
X = "exec";
id = Id_exec;
break;
case 7:
X = "compile";
id = Id_compile;
break;
case 8:
X = "toString";
id = Id_toString;
break;
case 11:
X = "constructor";
id = Id_constructor;
break;
}
if (X != null && X != s && !X.Equals (s))
id = 0;
}
EL0:
#endregion
return id;
}
#region PrototypeIds
private const int Id_constructor = 1;
private const int Id_toString = 2;
private const int Id_compile = 3;
private const int Id_exec = 4;
private const int MAX_PROTOTYPE_ID = 4;
#endregion
private IScript script;
}
}