Repository navigation
Expand file tree
/
Copy pathUtils.cs
More file actions
371 lines (312 loc) · 11.8 KB
/
Copy pathUtils.cs
File metadata and controls
371 lines (312 loc) · 11.8 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Program.cs - MiniJs: JS-like-ish interpreter with CLR interop + WinForms helpers + tasks
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.Loader;
namespace MiniJS
{
// ---------------- Script runtime types ----------------
sealed class JsArray { public List<object?> Items = new(); }
sealed class JsObject
{
public Dictionary<string, object?> Props = new();
public List<string> Order = new();
public ClassDef? Klass;
}
public class Env
{
private readonly object _lock = new();
public Env? Parent;
public Dictionary<string, object?> Vars = new();
public Env(Env? p = null) { Parent = p; }
public object? Get(string k)
{
lock (_lock)
{
if (Vars.TryGetValue(k, out var v)) return v;
}
if (Parent != null) return Parent.Get(k);
throw new Exception("ReferenceError: " + k + " is not defined");
}
public bool TryGetHere(string k, out object? v)
{
lock (_lock) return Vars.TryGetValue(k, out v);
}
public bool ContainsHere(string k)
{
lock (_lock) return Vars.ContainsKey(k);
}
public void Set(string k, object? v)
{
lock (_lock)
{
if (Vars.ContainsKey(k)) { Vars[k] = v; return; }
}
if (Parent != null) { Parent.Set(k, v); return; }
lock (_lock) Vars[k] = v;
}
public void Declare(string k, object? v)
{
lock (_lock) Vars[k] = v;
}
}
public class Function
{
public List<string> Params = new();
public Node? Body; // Block node
public Env? Closure;
public bool IsNative = false;
public Func<List<object?>, object?, object?>? Native;
}
public class ClassDef
{
public string Name = "";
public Dictionary<string, Function> Methods = new(); // includes "constructor"
public List<(string name, Node? initExpr)> Fields = new(); // (name, initExpr)
public Env? Closure; // for field init eval
}
sealed class ReturnSignal : Exception { public object? Value; public ReturnSignal(object? v) { Value = v; } }
sealed class BreakSignal : Exception { }
sealed class ContinueSignal : Exception { }
sealed class ClrNamespace
{
public string Name;
public ClrNamespace(string n) { Name = n; }
public override string ToString() => $"[namespace {Name}]";
}
sealed class ClrCallable
{
public object? Target; // instance or Type
public string Name;
public ClrCallable(object? target, string name) { Target = target; Name = name; }
public override string ToString() => "[clr-callable]";
}
sealed class JsTask
{
private readonly Task<object?> _task;
public JsTask(Task<object?> task) { _task = task; }
public bool Done => _task.IsCompleted;
public string Status => _task.Status.ToString();
public object? Result => _task.IsCompletedSuccessfully ? _task.Result : null;
public object? Wait() => _task.GetAwaiter().GetResult();
internal Task<object?> Task => _task;
public override string ToString() => "[task]";
}
// ---------------- Helpers ----------------
public class Rt
{
public static bool IsTruthy(object? v) => v switch
{
null => false,
bool b => b,
double d => d != 0.0,
float f => f != 0.0f,
int i => i != 0,
long l => l != 0,
string s => !string.IsNullOrEmpty(s),
_ => true
};
static string EscapeJsString(string s)
{
return s
.Replace("\\", "\\\\")
.Replace("\"", "\\\"")
.Replace("\r", "\\r")
.Replace("\n", "\\n")
.Replace("\t", "\\t");
}
public static string ToJsString(object? v)
{
if (v is null) return "null";
if (v is bool b) return b ? "true" : "false";
if (v is string s) return s;
if (v is double d) return d.ToString(CultureInfo.InvariantCulture);
if (v is float f) return f.ToString(CultureInfo.InvariantCulture);
if (v is int i) return i.ToString(CultureInfo.InvariantCulture);
if (v is long l) return l.ToString(CultureInfo.InvariantCulture);
if (v is Enum en) return en.ToString();
if (v is JsArray a)
{
/*
List<object?> snap;
lock (a) snap = a.Items.ToList();
return "[" + string.Join(", ", snap.Select(ToJsString)) + "]";
*/
List<object?> snap;
lock (a) snap = a.Items.ToList();
var parts = new List<string>(snap.Count);
foreach (var elem in snap)
{
parts.Add(ToJsString(elem));
}
return "[" + string.Join(", ", parts) + "]";
}
if (v is JsObject o)
{
List<string> keys;
Dictionary<string, object?> propsSnap;
lock (o)
{
keys = (o.Order.Count > 0 ? o.Order : o.Props.Keys.ToList()).ToList();
propsSnap = new Dictionary<string, object?>(o.Props);
}
var parts = new List<string>();
foreach (var k in keys)
{
if (!propsSnap.TryGetValue(k, out var vv)) continue;
if(vv is string)
parts.Add(k + ": \"" + ToJsString(vv) + "\"");
else
parts.Add(k + ": " + ToJsString(vv));
}
return "{" + string.Join(", ", parts) + "}";
}
if (v is Function) return "[function]";
if (v is ClassDef) return "[class]";
if (v is ClrNamespace ns) return ns.ToString();
if (v is Type t) return $"[MiniJS.Type {t.FullName}]";
if (v is ClrCallable) return "[clr-callable]";
if (v is JsTask) return "[task]";
return v.ToString() ?? "";
}
public static bool IsNumeric(object? v) =>
v is byte or sbyte or short or ushort or int or uint or long or ulong or float or double or decimal;
public static double ToNumber(object? v)
{
try
{
return v switch
{
null => 0.0,
double d => d,
float f => f,
decimal m => (double)m,
int i => i,
long l => l,
bool b => b ? 1.0 : 0.0,
string s => string.IsNullOrEmpty(s) ? 0.0 : double.Parse(s, CultureInfo.InvariantCulture),
Enum e => Convert.ToDouble(e, CultureInfo.InvariantCulture),
_ => double.Parse(ToJsString(v), CultureInfo.InvariantCulture)
};
}
catch
{
throw;
}
}
public static int ToInt32(object? v)
{
double d = ToNumber(v);
if (double.IsNaN(d) || double.IsInfinity(d) || d == 0.0) return 0;
double two32 = 4294967296.0;
double n = d >= 0 ? Math.Floor(d) : Math.Ceiling(d);
n = n % two32;
if (n < 0) n += two32;
uint u = (uint)n;
return unchecked((int)u);
}
}
// ---------------- Task manager (queue + limit) ----------------
sealed class TaskManager
{
private readonly object _cfgLock = new();
private SemaphoreSlim _sem;
private int _maxConcurrency;
private int _queueLimit;
private int _queued;
public TaskManager(int maxConcurrency, int queueLimit)
{
_maxConcurrency = Math.Max(1, maxConcurrency);
_queueLimit = Math.Max(1, queueLimit);
_sem = new SemaphoreSlim(_maxConcurrency, _maxConcurrency);
}
public int MaxConcurrency { get { lock (_cfgLock) return _maxConcurrency; } }
public int QueueLimit { get { lock (_cfgLock) return _queueLimit; } }
public int Queued => Volatile.Read(ref _queued);
public void SetMaxConcurrency(int n)
{
n = Math.Max(1, n);
lock (_cfgLock)
{
if (n == _maxConcurrency) return;
_maxConcurrency = n;
_sem = new SemaphoreSlim(_maxConcurrency, _maxConcurrency);
}
}
public void SetQueueLimit(int n)
{
n = Math.Max(1, n);
lock (_cfgLock) _queueLimit = n;
}
public Task<object?> Enqueue(Func<object?> work)
{
int q = Interlocked.Increment(ref _queued);
int limit = QueueLimit;
if (q > limit)
{
Interlocked.Decrement(ref _queued);
throw new Exception($"Task queue overflow (limit={limit})");
}
SemaphoreSlim sem;
lock (_cfgLock) sem = _sem;
return Task.Run(async () =>
{
await sem.WaitAsync().ConfigureAwait(false);
try
{
return work();
}
finally
{
sem.Release();
Interlocked.Decrement(ref _queued);
}
});
}
}
// ---------------- AST dump (optional) ----------------
static class AstDump
{
public static void Dump(Node? n, int indent = 0)
{
if (n == null)
{
Console.WriteLine(new string(' ', indent) + "(null)");
return;
}
string pad = new string(' ', indent);
Console.Write(pad + n.Type);
if (n.Tok.Type != TokType.EOF_TOK || !string.IsNullOrEmpty(n.Tok.Lexeme))
Console.Write($" tok={Tok.Name(n.Tok.Type)} '{n.Tok.Lexeme}' @{n.Tok.Pos}");
if (!string.IsNullOrEmpty(n.Text))
Console.Write($" text='{n.Text}'");
Console.WriteLine();
foreach (var k in n.Kids) Dump(k, indent + 2);
}
}
// ---------------- Interpreter ----------------
readonly struct EventKey
{
public readonly object Target;
public readonly string EventName;
public EventKey(object target, string eventName) { Target = target; EventName = eventName; }
}
sealed class EventKeyComparer : IEqualityComparer<EventKey>
{
public bool Equals(EventKey x, EventKey y) =>
ReferenceEquals(x.Target, y.Target) && StringComparer.Ordinal.Equals(x.EventName, y.EventName);
public int GetHashCode(EventKey obj) =>
(RuntimeHelpers.GetHashCode(obj.Target) * 397) ^ StringComparer.Ordinal.GetHashCode(obj.EventName);
}
}