-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstd.smash
More file actions
230 lines (188 loc) 路 4.67 KB
/
Copy pathstd.smash
File metadata and controls
230 lines (188 loc) 路 4.67 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
// Standard operator overrides
fn __add__(a, b) {
return a + b;
}
fn __sub__(a, b) {
return a - b;
}
fn __mul__(a, b) {
return a * b;
}
fn __div__(a, b) {
return a / b;
}
// Core utility
fn print(x) {
return x; // placeholder for native print
}
// Async/await support
// This is a special function that the runtime will recognize and handle
fn __createPromise(executor) {
return __native_create_promise(executor);
}
// Convert a value to a Promise if it's not already one
fn __promisify(value) {
if (__is_promise(value)) {
return value;
}
return __createPromise(fn(resolve, _) {
resolve(value);
});
}
// Control flow support
// These are special functions that the runtime will recognize and handle
// Conditional operators
fn __equals(a, b) {
return a === b;
}
fn __not_equals(a, b) {
return a !== b;
}
fn __greater_than(a, b) {
return a > b;
}
fn __less_than(a, b) {
return a < b;
}
fn __greater_than_equals(a, b) {
return a >= b;
}
fn __less_than_equals(a, b) {
return a <= b;
}
// Logical operators
fn __and(a, b) {
return a && b;
}
fn __or(a, b) {
return a || b;
}
fn __not(a) {
return !a;
}
// Ternary operator helper
fn __ternary(condition, trueValue, falseValue) {
return condition ? trueValue : falseValue;
}
// Array and object iteration helpers
fn __iterate_array(array, callback) {
for (let i = 0; i < array.length; i++) {
callback(array[i], i, array);
}
}
fn __iterate_object(obj, callback) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
callback(key, obj[key], obj);
}
}
}
// HTTP/HTTPS functionality - fetch API compatible
fn fetch(url, options = {}) {
let method = options.method || "GET";
let headers = options.headers || {};
let body = options.body || null;
let timeout = options.timeout || 30000; // 30 seconds default
// This will be implemented natively in the runtime
// Returns a Promise-like object with .then(), .catch(), and .json() methods
return __native_fetch(url, method, headers, body, timeout);
}
// Helper methods for common HTTP verbs
fn get(url, options = {}) {
options.method = "GET";
return fetch(url, options);
}
fn post(url, data, options = {}) {
options.method = "POST";
options.body = data;
return fetch(url, options);
}
fn put(url, data, options = {}) {
options.method = "PUT";
options.body = data;
return fetch(url, options);
}
fn del(url, options = {}) {
options.method = "DELETE";
return fetch(url, options);
}
// Process-related functionality
// System environment variables
const env = {
// Core environment variables that should be available
HOME: __native_get_env("HOME"),
USER: __native_get_env("USER"),
PATH: __native_get_env("PATH"),
TEMP: __native_get_env("TEMP") || __native_get_env("TMP"),
SHELL: __native_get_env("SHELL"),
LANG: __native_get_env("LANG"),
// Helper method to get any environment variable
get: function(name) {
return __native_get_env(name);
},
// Helper method to set an environment variable
set: function(name, value) {
return __native_set_env(name, value);
}
};
// Command line arguments
// This will be populated by the runtime with the actual command line arguments
const argv = __native_get_argv();
// Current working directory operations
fn cwd() {
return __native_get_cwd();
}
fn chdir(directory) {
return __native_set_cwd(directory);
}
// Platform information
const platform = __native_get_platform(); // 'linux', 'darwin', 'win32', etc.
const arch = __native_get_arch(); // 'x64', 'arm64', etc.
// Process control
fn exit(code = 0) {
return __native_exit(code);
}
// Process information
const pid = __native_get_pid();
const ppid = __native_get_ppid();
// Memory usage
fn memoryUsage() {
return __native_memory_usage();
}
// CPU usage
fn cpuUsage() {
return __native_cpu_usage();
}
// High-resolution time measurement
fn hrtime() {
return __native_hrtime();
}
// Event handling for process events
const eventHandlers = {
exit: [],
uncaughtException: [],
unhandledRejection: []
};
fn on(event, handler) {
if (eventHandlers[event]) {
eventHandlers[event].push(handler);
}
return this; // For chaining
}
fn removeListener(event, handler) {
if (eventHandlers[event]) {
const index = eventHandlers[event].indexOf(handler);
if (index !== -1) {
eventHandlers[event].splice(index, 1);
}
}
return this; // For chaining
}
// These functions would be called by the runtime when events occur
fn __triggerEvent(event, ...args) {
if (eventHandlers[event]) {
for (const handler of eventHandlers[event]) {
handler(...args);
}
}
}