This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathProcess.cs
More file actions
233 lines (202 loc) · 8.36 KB
/
Copy pathProcess.cs
File metadata and controls
233 lines (202 loc) · 8.36 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
/*
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.Helpers;
using System;
using System.Collections;
using System.Diagnostics;
namespace SharpJS.Libraries
{
public class Process : Module {
public readonly ProcessConstructor Constructor;
public readonly ProcessInstance Instance;
public Process(Context context) : base(context) {
Constructor = new ProcessConstructor(context, "Process", new ProcessInstance(context.Events.EventEmitter.InstancePrototype, false));
Instance = Constructor.Construct();
Populate();
}
public class ProcessConstructor : ConstructorFunction
{
public ProcessConstructor(Context context, string name, ObjectInstance instancePrototype) : base(context, name, instancePrototype) {
Populate();
}
// Not callable from JS
public ProcessInstance Construct() {
return new ProcessInstance(Context, this.InstancePrototype);
}
}
public class ProcessInstance : Events.EventEmitterInstance
{
private long StartTime;
private OperatingSystem OS = Environment.OSVersion;
private string _Title = "#js";
private ArrayInstance _Argv;
private int _MaxTickDepth = 1000;
// Prototype constructor
public ProcessInstance(ObjectInstance nextPrototype, bool extended) : base(nextPrototype, true) {
if (!extended)
Populate();
}
// Instance constructor
public ProcessInstance(Context context, ObjectInstance thisPrototype) : base(context, thisPrototype) {
StartTime = DateTime.Now.Ticks;
_Argv = Engine.Array.Construct(new object[] { "sharpjs" });
}
[JSProperty(Name = "pid", IsEnumerable = true)]
public int Pid {
get {
return System.Diagnostics.Process.GetCurrentProcess().Id;
}
}
[JSProperty(Name = "title", IsEnumerable = true)]
public string Title {
get {
return _Title;
}
set {
_Title = value;
}
}
[JSProperty(Name = "arch", IsEnumerable = true)]
public string Arch {
get {
string arch = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
if (arch == null)
return "null";
switch (arch) {
case "AMD64":
case "IA64":
return "x64";
case "X86":
return "ia32";
default:
return arch.ToLower();
}
}
}
[JSProperty(Name = "platform", IsEnumerable = true)]
public string Platform {
get {
string platform = OS.Platform.ToString();
switch (platform) {
case "Win32":
case "Win32NT":
return "win32";
default:
return platform.ToLower();
}
}
}
[JSProperty(Name = "config", IsEnumerable = true)]
public ObjectInstance Config {
get {
return Engine.Object.Construct();
}
}
[JSProperty(Name = "version", IsEnumerable = true)]
public string Version {
get {
return "1.0.0";
}
}
[JSProperty(Name = "versions", IsEnumerable = true)]
public ObjectInstance Versions {
get {
ObjectInstance obj = Engine.Object.Construct();
obj.SetPropertyValue("sharpjs", Version, false);
return obj;
}
}
[JSProperty(Name = "argv", IsEnumerable = true)]
public ArrayInstance Argv {
get {
return _Argv;
}
set {
_Argv = value;
}
}
[JSProperty(Name = "maxTickDepth", IsEnumerable = true)]
public int MaxTickDepth {
get {
return _MaxTickDepth;
}
set {
_MaxTickDepth = value;
}
}
[JSFunction(Name = "cwd", IsEnumerable = true)]
public string Cwd() {
return Context.Path.Normalize(System.IO.Directory.GetCurrentDirectory());
}
[JSProperty(Name = "env", IsEnumerable = true)]
public ObjectInstance Env {
get {
ObjectInstance env = Engine.Object.Construct();
IDictionary vars = Environment.GetEnvironmentVariables();
foreach (object key in vars.Keys) {
if (key is string)
env.SetPropertyValue(key as string, vars[key], false);
}
return env;
}
}
[JSFunction(Name = "uptime", IsEnumerable = true)]
public int Uptime() {
return (int)((DateTime.Now.Ticks - StartTime) / 10000000);
}
[JSFunction(Name = "hrtime", IsEnumerable = true)]
public ArrayInstance Hrtime() {
long nanos = DateTime.Now.Ticks * 100;
int seconds = (int)(nanos / 1000000000);
nanos %= 1000000000;
return Engine.Array.Construct(new object[] { seconds, (int)nanos });
}
[JSFunction(Name = "nextTick", IsEnumerable = true)]
public void NextTick(FunctionInstance callback) {
// node.js actually performs these callbacks at the end of the current tick, which
// is nearly as fast as calling them synchronous regarding reaction time. We could
// also do this with a seperate queue, though for now it's just an immediate.
(Context.GetModule("timers") as Timers).SetImmediate(callback, new object[] { });
}
[JSFunction(Name = "memoryUsage", IsEnumerable = true)]
public ObjectInstance MemoryUsage() {
ObjectInstance obj = Engine.Object.Construct();
// rss?
obj.SetPropertyValue("heapUsed", (double)System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64, false);
obj.SetPropertyValue("heapTotal", (double)System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64, false);
return obj;
}
[JSFunction(Name = "kill", IsEnumerable = true)]
public void Kill(int pid, [DefaultParameterValue("SIGTERM")] string signal = "SIGTERM") {
throw new JavaScriptException(Engine, "Error", "not implemented");
}
[JSFunction(Name = "exit", IsEnumerable = true)]
public void Exit([DefaultParameterValue(0)] int code = 0) {
throw new ExitException(code);
}
[JSFunction(Name = "abort", IsEnumerable = true)]
public void Abort() {
throw new JavaScriptException(Engine, "Error", "not implemented");
}
}
public class ExitException : Exception
{
public int Code;
public ExitException(int code) {
Code = code;
}
}
}
}