]> git.saurik.com Git - cycript.git/blame - libcycript.cy
Logically order type specifier cases in sig/parse.
[cycript.git] / libcycript.cy
CommitLineData
bf998c10
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3*/
4
5/* GNU Affero General Public License, Version 3 {{{ */
6/*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19**/
20/* }}} */
21
0f557ff2
JF
22var process = {
23 env: {},
24};
25
26(function() {
27
574d4720
JF
28this.typeid = function(object) {
29 return object.$cyt;
30};
31
3f7ac48b
JF
32let $cy_set = function(object, properties) {
33 for (const name in properties)
34 Object.defineProperty(object, name, {
35 configurable: true,
36 enumerable: false,
37 writable: true,
38 value: properties[name],
39 });
40};
41
a4e76c78
JF
42$cy_set(Boolean.prototype, {
43 toCYON: function() {
44 return `new Boolean(${this.toString()})`;
45 },
46});
47
3f7ac48b
JF
48$cy_set(Date.prototype, {
49 toCYON: function() {
50 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
51 },
52});
53
54$cy_set(Error.prototype, {
55 toCYON: function() {
f348c890
JF
56 let stack = this.stack;
57 if (typeof stack == 'undefined')
58 stack = '';
59 else {
60 stack = stack.split('\n');
61 if (stack.slice(-1)[0] == "global code")
62 stack = stack.slice(0, -1);
63 for (let i = 0; i != stack.length; ++i)
64 stack[i] = '\n ' + stack[i];
65 stack = stack.join('');
66 stack = ` /*${stack} */`;
67 }
68 return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`;
3f7ac48b
JF
69 },
70});
0f557ff2 71
a4e76c78
JF
72$cy_set(Number.prototype, {
73 toCYON: function() {
574d4720
JF
74 if ("$cyt" in this)
75 return `${this.$cyt.toCYON()}(${this.toString()})`;
a4e76c78
JF
76 return `new Number(${this.toString()})`;
77 },
78});
79
80$cy_set(RegExp.prototype, {
81 toCYON: function() {
82 return this.toString();
83 },
84});
85
0f557ff2
JF
86let IsFile = function(path) {
87 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
88 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
89};
90
91let StartsWith = function(lhs, rhs) {
92 return lhs.substring(0, rhs.length) == rhs;
93};
94
95let ResolveFile = function(exact, name) {
96 if (exact && IsFile(name))
97 return name;
98 for (let suffix of ['.js', '.json'])
99 if (IsFile(name + suffix))
100 return name + suffix;
101 return null;
102};
103
104
105let GetLibraryPath = function() {
106 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
107 if (handle == null)
108 return null;
109
110 try {
111 let CYHandleServer = dlsym(handle, "CYHandleServer");
112 if (CYHandleServer == null)
113 return null;
114
115 let info = new Dl_info;
116 if (dladdr(CYHandleServer, info) == 0)
117 return null;
118
119 let path = info->dli_fname;
120 let slash = path.lastIndexOf('/');
121 if (slash == -1)
122 return null;
123
79858e0a
JF
124 path = path.substr(0, slash);
125
126 GetLibraryPath = function() {
127 return path;
128 };
129
130 return GetLibraryPath();
0f557ff2
JF
131 } finally {
132 dlclose(handle);
133 }
134};
135
136let ResolveFolder = function(name) {
137 if (access(name + '/', F_OK) == -1)
138 return null;
139
140 if (IsFile(name + "/package.json")) {
141 let package = require(name + "/package.json");
142 let path = ResolveFile(true, name + "/" + package.main);
143 if (path != null)
144 return path;
145 }
146
147 return ResolveFile(false, name + "/index");
148};
149
150let ResolveEither = function(name) {
151 let path = null;
152 if (path == null)
153 path = ResolveFile(true, name);
154 if (path == null)
155 path = ResolveFolder(name);
156 return path;
157};
158
159require.resolve = function(name) {
160 if (StartsWith(name, '/')) {
161 let path = ResolveEither(name);
162 if (path != null)
163 return path;
164 } else {
165 let cwd = new (typedef char[1024]);
166 cwd = getcwd(cwd, cwd.length).toString();
167 cwd = cwd.split('/');
168
169 if (StartsWith(name, './') || StartsWith(name, '../')) {
170 let path = ResolveEither(cwd + '/' + name);
171 if (path != null)
172 return path;
173 } else {
174 for (let i = cwd.length; i != 0; --i) {
175 let modules = cwd.slice(0, i).concat("node_modules").join('/');
176 let path = ResolveEither(modules + "/" + name);
177 if (path != null)
178 return path;
179 }
180
181 let library = GetLibraryPath();
182 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
183 if (path != null)
184 return path;
185 }
186 }
187
188 throw new Error("Cannot find module '" + name + "'");
189};
190
d82e4c2a
JF
191var _syscall = function(value) {
192 if (value == -1)
19e13c2d 193 throw new Error(strerror(errno));
d82e4c2a
JF
194};
195
196var info = *new (struct stat);
197if (false) {
198} else if ("st_atim" in info) {
199 var st_atime = "st_atim";
200 var st_mtime = "st_mtim";
201 var st_ctime = "st_ctim";
202} else if ("st_atimespec" in info) {
203 var st_atime = "st_atimespec";
204 var st_mtime = "st_mtimespec";
205 var st_ctime = "st_ctimespec";
206} else {
207 var st_atime = "st_atime";
208 var st_mtime = "st_mtime";
209 var st_ctime = "st_ctime";
210}
211
212var toDate = function(timespec) {
213 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
214};
215
0f557ff2
JF
216var bindings = {};
217
218process.binding = function(name) {
219 let binding = bindings[name];
220 if (typeof binding != 'undefined')
221 return binding;
222
223 switch (name) {
224 case 'buffer': binding = {
225 setupBufferJS() {
226 },
227 }; break;
228
229 case 'cares_wrap': binding = {
230 }; break;
231
232 case 'constants': binding = {
233 }; break;
234
235 case 'fs': binding = {
236 FSInitialize() {
237 },
238
d82e4c2a
JF
239 lstat(path) {
240 var info = new (struct stat);
241 _syscall(lstat(path, info));
242
243 return {
244 dev: info->st_dev,
245 mode: info->st_mode,
246 nlink: info->st_nlink,
247 uid: info->st_uid,
248 gid: info->st_gid,
249 rdev: info->st_rdev,
250 blksize: info->st_blksize,
251 ino: info->st_ino,
252 size: info->st_size,
253 blocks: info->st_blocks,
254
255 atime: toDate(info->[st_atime]),
256 mtime: toDate(info->[st_mtime]),
257 ctime: toDate(info->[st_ctime]),
258
259 isSymbolicLink() {
260 return S_ISLNK(this.mode);
261 },
262 };
0f557ff2
JF
263 },
264 }; break;
265
266 case 'pipe_wrap': binding = {
267 }; break;
268
269 case 'smalloc': binding = {
270 alloc() {
271 },
272 }; break;
273
274 case 'stream_wrap': binding = {
275 }; break;
276
277 case 'tcp_wrap': binding = {
278 }; break;
279
280 case 'timer_wrap': binding = {
281 kOnTimeout: 0,
282 Timer: {
283 },
284 }; break;
285
286 case 'tty_wrap': binding = {
287 }; break;
288
289 case 'uv': binding = {
290 }; break;
291
292 default:
293 throw new Error('No such module: ' + name);
294 }
295
296 bindings[name] = binding;
297 return binding;
298};
299
300let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
301for (let i = 0; environ[i] != null; ++i) {
302 let assign = environ[i];
303 let equal = assign.indexOf('=');
304 let name = assign.substr(0, equal);
305 let value = assign.substr(equal + 1);
306 process.env[name.toString()] = value;
307}
308
309process.pid = getpid();
310
311})();