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