This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathContext.cs
More file actions
270 lines (234 loc) · 9.11 KB
/
Copy pathContext.cs
File metadata and controls
270 lines (234 loc) · 9.11 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Copyright 2014 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Jurassic;
using Jurassic.Library;
using SharpJS.Encodings;
using SharpJS.Libraries;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
namespace SharpJS
{
public class Context
{
public ScriptEngine Engine;
private Dictionary<string, object> Modules = new Dictionary<string, object>();
public Module ExecutingModule = null;
public bool Initialized = false;
private Queue<FunctionInstance> callbacks = new Queue<FunctionInstance>();
public Libraries.Assert Assert;
public Libraries.Util Util;
public Libraries.Path Path;
public Libraries.Events Events;
public Libraries.Timers Timers;
public Libraries.Buffer Buffer;
public Libraries.StringDecoder StringDecoder;
public Libraries.QueryString QueryString;
// public Libraries.Stream Stream;
public Libraries.Fs Fs;
public Libraries.Console Console;
public Libraries.Process Process;
public Context() {
Engine = new ScriptEngine();
Engine.Global = new ContextGlobalObject(this);
Modules.Add("assert", Assert = new Libraries.Assert(this));
Modules.Add("util", Util = new Libraries.Util(this));
Modules.Add("path", Path = new Libraries.Path(this));
Modules.Add("events", Events = new Libraries.Events(this));
Modules.Add("timers", Timers = new Libraries.Timers(this));
Modules.Add("buffer", Buffer = new Libraries.Buffer(this));
Modules.Add("string_decoder", StringDecoder = new Libraries.StringDecoder(this));
Modules.Add("querystring", QueryString = new Libraries.QueryString(this));
// Modules.Add("stream", Stream = new Libraries.Stream(this));
Modules.Add("fs", Fs = new Libraries.Fs(this));
Modules.Add("console", Console = new Libraries.Console(this));
Process = new Libraries.Process(this);
RegisterGlobals();
}
public event ExitEventHandler Exit;
public delegate void ExitEventHandler(object sender, int code);
public object Run(Module main) {
ExecutingModule = main;
try {
object exports = main.Run();
Initialized = true;
return exports;
} catch (Exception ex) {
if (!HandleUncaughtException(ex))
throw;
} finally {
ExecutingModule = null;
}
return null;
}
public void RegisterGlobals() {
Engine.PopulateGlobals();
GlobalObject global = Engine.Global;
global.SetPropertyValue("global", global, true);
global.SetPropertyValue("process", Process.Instance, true);
foreach (KeyValuePair<string,object> kv in Modules) {
if (kv.Value is Module)
(kv.Value as Module).RegisterGlobals();
}
}
public bool AddModule(string name, object module) {
if (Modules.ContainsKey(name))
return false;
Modules.Add(name, module);
return true;
}
public object GetModule(string name) {
object module = null;
Modules.TryGetValue(name, out module);
return module;
}
public bool MoveModule(string fromName, string toName) {
object module = GetModule(fromName);
if (module == null)
return false;
if (AddModule(toName, module)) {
Modules.Remove(fromName);
return true;
}
return false;
}
public bool ReplaceModule(string name, object replacementModule) {
object module = GetModule(name);
if (module == null)
return false;
Modules.Remove(name);
if (AddModule(name, replacementModule)) {
return true;
}
return false;
}
public bool RemoveModule(string name) {
return Modules.Remove(name);
}
private string TimersRef = "timers";
public bool MayExit() {
Timers timers = (GetModule(TimersRef) as Timers);
if (timers == null) {
foreach (var module in Modules) {
if (module.Value is Timers) {
TimersRef = module.Key;
timers = module.Value as Timers;
}
}
}
if (timers /* still */ == null) {
return true;
}
return timers.MayExit();
}
public void OnExit() {
Process.Instance.Emit("exit");
}
public object TryRequire(string name) {
object exports = null;
if (Modules.TryGetValue(name, out exports))
return exports;
return null;
}
public bool HandleUncaughtException(Exception ex) {
if (Initialized && ex is JavaScriptException) {
if (Process.Instance.HasListener("uncaughtException")) {
Process.Instance.Emit("uncaughtException", ex);
return true;
}
} else if (ex is SharpJS.Libraries.Process.ExitException) {
OnExit();
if (Exit != null)
Exit(this, (ex as SharpJS.Libraries.Process.ExitException).Code);
return true;
}
return false;
}
// Ticking
public delegate void TickEventHandler(object sender, EventArgs e);
public event TickEventHandler Ticked;
public void Tick() {
try {
ProcessCallbacks();
if (Ticked != null)
Ticked(this, EventArgs.Empty);
ProcessCallbacks();
} catch (Exception ex) {
if (!HandleUncaughtException(ex))
throw;
}
}
// Callback queue
public void NextTick(FunctionInstance callback) {
lock (callbacks) {
callbacks.Enqueue(callback);
}
}
private void ProcessCallbacks() {
FunctionInstance[] cbs;
lock (callbacks) {
cbs = callbacks.ToArray();
callbacks.Clear();
}
foreach (FunctionInstance cb in cbs) {
try {
cb.CallLateBound(this);
} catch (Exception ex) {
if (!HandleUncaughtException(ex))
throw;
}
}
}
// Encodings
private static Dictionary<string, Encoding> Encodings = null;
public static Dictionary<string, Encoding> GetEncodings() {
if (Encodings == null) {
Encodings = new Dictionary<string, Encoding>();
Encodings.Add("utf8", System.Text.Encoding.UTF8);
Encodings.Add("hex", new Hex());
Encodings.Add("binary", new Binary());
// Encodings.Add("base64", new Base64());
}
return Encodings;
}
public static Encoding GetEncoding(string name) {
if (Encodings == null)
GetEncodings();
System.Text.Encoding encoding = null;
Encodings.TryGetValue(name.ToLower(), out encoding);
return encoding;
}
}
public class ContextGlobalObject : GlobalObject
{
public ContextGlobalObject(Context context) : base(context.Engine.Object.InstancePrototype) {
}
/* public override bool DefineProperty(string propertyName, PropertyDescriptor descriptor, bool throwOnError) {
// System.Console.WriteLine("define: " + propertyName + " / " + descriptor);
return base.DefineProperty(propertyName, descriptor, throwOnError);
}
public override void SetPropertyValue(uint index, object value, bool throwOnError) {
StackTrace st = new StackTrace();
MethodBase caller = st.GetFrame(1).GetMethod();
if (caller.DeclaringType != typeof(Context)) {
// System.Console.WriteLine("intercepted: " + caller.Name);
return;
}
// System.Console.WriteLine("not intercepted: "+caller.Name);
base.SetPropertyValue(index, value, throwOnError);
} */
}
}