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