]> git.saurik.com Git - cycript.git/blame - libcycript.cy
Also use CXType walker to for function prototypes.
[cycript.git] / libcycript.cy
CommitLineData
7341eedb
JF
1/* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
bf998c10
JF
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
22(function() {
23
574d4720
JF
24this.typeid = function(object) {
25 return object.$cyt;
26};
27
3f7ac48b
JF
28let $cy_set = function(object, properties) {
29 for (const name in properties)
30 Object.defineProperty(object, name, {
31 configurable: true,
32 enumerable: false,
33 writable: true,
34 value: properties[name],
35 });
36};
37
a4e76c78
JF
38$cy_set(Boolean.prototype, {
39 toCYON: function() {
40 return `new Boolean(${this.toString()})`;
41 },
42});
43
3f7ac48b
JF
44$cy_set(Date.prototype, {
45 toCYON: function() {
46 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
47 },
48});
49
50$cy_set(Error.prototype, {
51 toCYON: function() {
f348c890
JF
52 let stack = this.stack;
53 if (typeof stack == 'undefined')
54 stack = '';
55 else {
56 stack = stack.split('\n');
57 if (stack.slice(-1)[0] == "global code")
58 stack = stack.slice(0, -1);
59 for (let i = 0; i != stack.length; ++i)
60 stack[i] = '\n ' + stack[i];
0a4d6bce
JF
61 if (stack.length == 0)
62 stack = '';
63 else {
64 stack = stack.join('');
65 stack = ` /*${stack} */`;
66 }
f348c890
JF
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
f45374f4 86// XXX: Java should just always be available
dbf05bfd 87if ("Java" in Cycript) {
f45374f4
JF
88
89// XXX: this is a half-assed EventEmitter
90// XXX: this doesn't even have the same semantics
91
92Java.handlers_ = {};
93
94Java.on = function(event, handler) {
95 var handlers;
96 if (event in this.handlers_)
97 handlers = this.handlers_[event];
98 else {
99 handlers = [];
100 this.handlers_[event] = handlers;
101 }
102
103 if (this.handlers_ == null)
104 handler();
105 else
106 handlers.push(handler);
107};
108
109Java.emit = function(event) {
110 if (event in this.handlers_) {
111 var handlers = this.handlers_[event];
112 if (handlers != null)
113 for (var handler of handlers)
114 handler();
115 }
116
117 this.handlers_[event] = null;
118};
119
120Java.on('setup', function() {
dbf05bfd
JF
121 $cy_set(java.lang.Boolean.prototype, {
122 toCYON: function() {
8effd381 123 return `new java.lang.Boolean(${this->value})`;
dbf05bfd
JF
124 },
125 });
126
127 $cy_set(java.lang.Byte.prototype, {
128 toCYON: function() {
8effd381 129 return `new java.lang.Byte(${this->value})`;
dbf05bfd
JF
130 },
131 });
132
133 $cy_set(java.lang.Character.prototype, {
134 toCYON: function() {
8effd381 135 return `new java.lang.Character(${this->value})`;
dbf05bfd
JF
136 },
137 });
138
139 $cy_set(java.lang.Short.prototype, {
140 toCYON: function() {
8effd381 141 return `new java.lang.Short(${this->value})`;
dbf05bfd
JF
142 },
143 });
144
145 $cy_set(java.lang.Integer.prototype, {
146 toCYON: function() {
8effd381 147 return `new java.lang.Integer(${this->value})`;
dbf05bfd
JF
148 },
149 });
150
151 $cy_set(java.lang.Long.prototype, {
152 toCYON: function() {
8effd381 153 return `new java.lang.Long(${this->value})`;
dbf05bfd
JF
154 },
155 });
156
157 $cy_set(java.lang.Float.prototype, {
158 toCYON: function() {
8effd381 159 return `new java.lang.Float(${this->value})`;
dbf05bfd
JF
160 },
161 });
162
163 $cy_set(java.lang.Double.prototype, {
164 toCYON: function() {
8effd381 165 return `new java.lang.Double(${this->value})`;
dbf05bfd
JF
166 },
167 });
e56c2499
JF
168
169 $cy_set(java.lang.Object.prototype, {
4b645e23
JF
170 toCYON: function(key) {
171 return "#" + this.toString().toCYON();
172 },
173
e56c2499
JF
174 // XXX: due to lack of interface prototypes :(
175 $cyg: function(key) {
176 return this.get(key);
177 },
178
179 // XXX: due to lack of interface prototypes :(
180 $cys: function(key, value) {
181 if ("set" in this)
182 this.set(key, value);
183 else
184 this.put(key, value);
185 },
186 });
f45374f4
JF
187});
188
e56c2499
JF
189}
190
191if ("ObjectiveC" in Cycript) {
192 $cy_set(NSArray.prototype, {
193 $cyg: function(key) {
194 return objc_msgSend(this, "objectAtIndex:", key);
195 },
196
197 $cys: function(key, value) {
198 return objc_msgSend(this, "setObject:atIndex:", value, key);
199 },
200 });
201
202 $cy_set(NSDictionary.prototype, {
203 $cyg: function(key) {
204 return objc_msgSend(this, "objectForKey:", key);
205 },
206
207 $cys: function(key, value) {
208 return objc_msgSend(this, "setObject:forKey:", value, key);
209 },
210 });
dbf05bfd
JF
211}
212
0f557ff2
JF
213let IsFile = function(path) {
214 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
215 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
216};
217
218let StartsWith = function(lhs, rhs) {
219 return lhs.substring(0, rhs.length) == rhs;
220};
221
222let ResolveFile = function(exact, name) {
223 if (exact && IsFile(name))
224 return name;
225 for (let suffix of ['.js', '.json'])
226 if (IsFile(name + suffix))
227 return name + suffix;
228 return null;
229};
230
231
232let GetLibraryPath = function() {
233 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
234 if (handle == null)
235 return null;
236
237 try {
238 let CYHandleServer = dlsym(handle, "CYHandleServer");
239 if (CYHandleServer == null)
240 return null;
241
242 let info = new Dl_info;
243 if (dladdr(CYHandleServer, info) == 0)
244 return null;
245
246 let path = info->dli_fname;
247 let slash = path.lastIndexOf('/');
248 if (slash == -1)
249 return null;
250
79858e0a
JF
251 path = path.substr(0, slash);
252
253 GetLibraryPath = function() {
254 return path;
255 };
256
257 return GetLibraryPath();
0f557ff2
JF
258 } finally {
259 dlclose(handle);
260 }
261};
262
263let ResolveFolder = function(name) {
264 if (access(name + '/', F_OK) == -1)
265 return null;
266
267 if (IsFile(name + "/package.json")) {
268 let package = require(name + "/package.json");
269 let path = ResolveFile(true, name + "/" + package.main);
270 if (path != null)
271 return path;
272 }
273
274 return ResolveFile(false, name + "/index");
275};
276
277let ResolveEither = function(name) {
278 let path = null;
279 if (path == null)
280 path = ResolveFile(true, name);
281 if (path == null)
282 path = ResolveFolder(name);
283 return path;
284};
285
286require.resolve = function(name) {
287 if (StartsWith(name, '/')) {
288 let path = ResolveEither(name);
289 if (path != null)
290 return path;
291 } else {
292 let cwd = new (typedef char[1024]);
293 cwd = getcwd(cwd, cwd.length).toString();
294 cwd = cwd.split('/');
295
296 if (StartsWith(name, './') || StartsWith(name, '../')) {
297 let path = ResolveEither(cwd + '/' + name);
298 if (path != null)
299 return path;
300 } else {
301 for (let i = cwd.length; i != 0; --i) {
302 let modules = cwd.slice(0, i).concat("node_modules").join('/');
303 let path = ResolveEither(modules + "/" + name);
304 if (path != null)
305 return path;
306 }
307
308 let library = GetLibraryPath();
309 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
310 if (path != null)
311 return path;
312 }
313 }
314
315 throw new Error("Cannot find module '" + name + "'");
316};
317
d82e4c2a
JF
318var _syscall = function(value) {
319 if (value == -1)
19e13c2d 320 throw new Error(strerror(errno));
d82e4c2a
JF
321};
322
323var info = *new (struct stat);
324if (false) {
325} else if ("st_atim" in info) {
326 var st_atime = "st_atim";
327 var st_mtime = "st_mtim";
328 var st_ctime = "st_ctim";
329} else if ("st_atimespec" in info) {
330 var st_atime = "st_atimespec";
331 var st_mtime = "st_mtimespec";
332 var st_ctime = "st_ctimespec";
333} else {
334 var st_atime = "st_atime";
335 var st_mtime = "st_mtime";
336 var st_ctime = "st_ctime";
337}
338
339var toDate = function(timespec) {
340 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
341};
342
0f557ff2
JF
343var bindings = {};
344
345process.binding = function(name) {
346 let binding = bindings[name];
347 if (typeof binding != 'undefined')
348 return binding;
349
350 switch (name) {
351 case 'buffer': binding = {
352 setupBufferJS() {
353 },
354 }; break;
355
356 case 'cares_wrap': binding = {
357 }; break;
358
359 case 'constants': binding = {
360 }; break;
361
362 case 'fs': binding = {
363 FSInitialize() {
364 },
365
d82e4c2a
JF
366 lstat(path) {
367 var info = new (struct stat);
368 _syscall(lstat(path, info));
369
370 return {
371 dev: info->st_dev,
372 mode: info->st_mode,
373 nlink: info->st_nlink,
374 uid: info->st_uid,
375 gid: info->st_gid,
376 rdev: info->st_rdev,
377 blksize: info->st_blksize,
378 ino: info->st_ino,
379 size: info->st_size,
380 blocks: info->st_blocks,
381
382 atime: toDate(info->[st_atime]),
383 mtime: toDate(info->[st_mtime]),
384 ctime: toDate(info->[st_ctime]),
385
386 isSymbolicLink() {
387 return S_ISLNK(this.mode);
388 },
389 };
0f557ff2
JF
390 },
391 }; break;
392
393 case 'pipe_wrap': binding = {
394 }; break;
395
396 case 'smalloc': binding = {
397 alloc() {
398 },
399 }; break;
400
401 case 'stream_wrap': binding = {
402 }; break;
403
404 case 'tcp_wrap': binding = {
405 }; break;
406
407 case 'timer_wrap': binding = {
408 kOnTimeout: 0,
409 Timer: {
410 },
411 }; break;
412
413 case 'tty_wrap': binding = {
414 }; break;
415
416 case 'uv': binding = {
417 }; break;
418
419 default:
420 throw new Error('No such module: ' + name);
421 }
422
423 bindings[name] = binding;
424 return binding;
425};
426
89a95d47
JF
427process.env = {};
428
0f557ff2
JF
429let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
430for (let i = 0; environ[i] != null; ++i) {
431 let assign = environ[i];
432 let equal = assign.indexOf('=');
433 let name = assign.substr(0, equal);
434 let value = assign.substr(equal + 1);
435 process.env[name.toString()] = value;
436}
437
5c9d18fc
JF
438process.cwd = function() {
439 let cwd = new (typedef char[1024]);
440 return getcwd(cwd, cwd.length).toString();
441};
442
0f557ff2
JF
443process.pid = getpid();
444
445})();