]> git.saurik.com Git - cycript.git/blame - libcycript.cy
Lazy load JVM, as it is slow and crashes on iOS 9.
[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
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
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() {
121 system.print("JVM\n");
dbf05bfd
JF
122 $cy_set(java.lang.Boolean.prototype, {
123 toCYON: function() {
8effd381 124 return `new java.lang.Boolean(${this->value})`;
dbf05bfd
JF
125 },
126 });
127
128 $cy_set(java.lang.Byte.prototype, {
129 toCYON: function() {
8effd381 130 return `new java.lang.Byte(${this->value})`;
dbf05bfd
JF
131 },
132 });
133
134 $cy_set(java.lang.Character.prototype, {
135 toCYON: function() {
8effd381 136 return `new java.lang.Character(${this->value})`;
dbf05bfd
JF
137 },
138 });
139
140 $cy_set(java.lang.Short.prototype, {
141 toCYON: function() {
8effd381 142 return `new java.lang.Short(${this->value})`;
dbf05bfd
JF
143 },
144 });
145
146 $cy_set(java.lang.Integer.prototype, {
147 toCYON: function() {
8effd381 148 return `new java.lang.Integer(${this->value})`;
dbf05bfd
JF
149 },
150 });
151
152 $cy_set(java.lang.Long.prototype, {
153 toCYON: function() {
8effd381 154 return `new java.lang.Long(${this->value})`;
dbf05bfd
JF
155 },
156 });
157
158 $cy_set(java.lang.Float.prototype, {
159 toCYON: function() {
8effd381 160 return `new java.lang.Float(${this->value})`;
dbf05bfd
JF
161 },
162 });
163
164 $cy_set(java.lang.Double.prototype, {
165 toCYON: function() {
8effd381 166 return `new java.lang.Double(${this->value})`;
dbf05bfd
JF
167 },
168 });
e56c2499
JF
169
170 $cy_set(java.lang.Object.prototype, {
4b645e23
JF
171 toCYON: function(key) {
172 return "#" + this.toString().toCYON();
173 },
174
e56c2499
JF
175 // XXX: due to lack of interface prototypes :(
176 $cyg: function(key) {
177 return this.get(key);
178 },
179
180 // XXX: due to lack of interface prototypes :(
181 $cys: function(key, value) {
182 if ("set" in this)
183 this.set(key, value);
184 else
185 this.put(key, value);
186 },
187 });
f45374f4
JF
188});
189
e56c2499
JF
190}
191
192if ("ObjectiveC" in Cycript) {
193 $cy_set(NSArray.prototype, {
194 $cyg: function(key) {
195 return objc_msgSend(this, "objectAtIndex:", key);
196 },
197
198 $cys: function(key, value) {
199 return objc_msgSend(this, "setObject:atIndex:", value, key);
200 },
201 });
202
203 $cy_set(NSDictionary.prototype, {
204 $cyg: function(key) {
205 return objc_msgSend(this, "objectForKey:", key);
206 },
207
208 $cys: function(key, value) {
209 return objc_msgSend(this, "setObject:forKey:", value, key);
210 },
211 });
dbf05bfd
JF
212}
213
0f557ff2
JF
214let IsFile = function(path) {
215 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
216 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
217};
218
219let StartsWith = function(lhs, rhs) {
220 return lhs.substring(0, rhs.length) == rhs;
221};
222
223let ResolveFile = function(exact, name) {
224 if (exact && IsFile(name))
225 return name;
226 for (let suffix of ['.js', '.json'])
227 if (IsFile(name + suffix))
228 return name + suffix;
229 return null;
230};
231
232
233let GetLibraryPath = function() {
234 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
235 if (handle == null)
236 return null;
237
238 try {
239 let CYHandleServer = dlsym(handle, "CYHandleServer");
240 if (CYHandleServer == null)
241 return null;
242
243 let info = new Dl_info;
244 if (dladdr(CYHandleServer, info) == 0)
245 return null;
246
247 let path = info->dli_fname;
248 let slash = path.lastIndexOf('/');
249 if (slash == -1)
250 return null;
251
79858e0a
JF
252 path = path.substr(0, slash);
253
254 GetLibraryPath = function() {
255 return path;
256 };
257
258 return GetLibraryPath();
0f557ff2
JF
259 } finally {
260 dlclose(handle);
261 }
262};
263
264let ResolveFolder = function(name) {
265 if (access(name + '/', F_OK) == -1)
266 return null;
267
268 if (IsFile(name + "/package.json")) {
269 let package = require(name + "/package.json");
270 let path = ResolveFile(true, name + "/" + package.main);
271 if (path != null)
272 return path;
273 }
274
275 return ResolveFile(false, name + "/index");
276};
277
278let ResolveEither = function(name) {
279 let path = null;
280 if (path == null)
281 path = ResolveFile(true, name);
282 if (path == null)
283 path = ResolveFolder(name);
284 return path;
285};
286
287require.resolve = function(name) {
288 if (StartsWith(name, '/')) {
289 let path = ResolveEither(name);
290 if (path != null)
291 return path;
292 } else {
293 let cwd = new (typedef char[1024]);
294 cwd = getcwd(cwd, cwd.length).toString();
295 cwd = cwd.split('/');
296
297 if (StartsWith(name, './') || StartsWith(name, '../')) {
298 let path = ResolveEither(cwd + '/' + name);
299 if (path != null)
300 return path;
301 } else {
302 for (let i = cwd.length; i != 0; --i) {
303 let modules = cwd.slice(0, i).concat("node_modules").join('/');
304 let path = ResolveEither(modules + "/" + name);
305 if (path != null)
306 return path;
307 }
308
309 let library = GetLibraryPath();
310 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
311 if (path != null)
312 return path;
313 }
314 }
315
316 throw new Error("Cannot find module '" + name + "'");
317};
318
d82e4c2a
JF
319var _syscall = function(value) {
320 if (value == -1)
19e13c2d 321 throw new Error(strerror(errno));
d82e4c2a
JF
322};
323
324var info = *new (struct stat);
325if (false) {
326} else if ("st_atim" in info) {
327 var st_atime = "st_atim";
328 var st_mtime = "st_mtim";
329 var st_ctime = "st_ctim";
330} else if ("st_atimespec" in info) {
331 var st_atime = "st_atimespec";
332 var st_mtime = "st_mtimespec";
333 var st_ctime = "st_ctimespec";
334} else {
335 var st_atime = "st_atime";
336 var st_mtime = "st_mtime";
337 var st_ctime = "st_ctime";
338}
339
340var toDate = function(timespec) {
341 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
342};
343
0f557ff2
JF
344var bindings = {};
345
346process.binding = function(name) {
347 let binding = bindings[name];
348 if (typeof binding != 'undefined')
349 return binding;
350
351 switch (name) {
352 case 'buffer': binding = {
353 setupBufferJS() {
354 },
355 }; break;
356
357 case 'cares_wrap': binding = {
358 }; break;
359
360 case 'constants': binding = {
361 }; break;
362
363 case 'fs': binding = {
364 FSInitialize() {
365 },
366
d82e4c2a
JF
367 lstat(path) {
368 var info = new (struct stat);
369 _syscall(lstat(path, info));
370
371 return {
372 dev: info->st_dev,
373 mode: info->st_mode,
374 nlink: info->st_nlink,
375 uid: info->st_uid,
376 gid: info->st_gid,
377 rdev: info->st_rdev,
378 blksize: info->st_blksize,
379 ino: info->st_ino,
380 size: info->st_size,
381 blocks: info->st_blocks,
382
383 atime: toDate(info->[st_atime]),
384 mtime: toDate(info->[st_mtime]),
385 ctime: toDate(info->[st_ctime]),
386
387 isSymbolicLink() {
388 return S_ISLNK(this.mode);
389 },
390 };
0f557ff2
JF
391 },
392 }; break;
393
394 case 'pipe_wrap': binding = {
395 }; break;
396
397 case 'smalloc': binding = {
398 alloc() {
399 },
400 }; break;
401
402 case 'stream_wrap': binding = {
403 }; break;
404
405 case 'tcp_wrap': binding = {
406 }; break;
407
408 case 'timer_wrap': binding = {
409 kOnTimeout: 0,
410 Timer: {
411 },
412 }; break;
413
414 case 'tty_wrap': binding = {
415 }; break;
416
417 case 'uv': binding = {
418 }; break;
419
420 default:
421 throw new Error('No such module: ' + name);
422 }
423
424 bindings[name] = binding;
425 return binding;
426};
427
428let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
429for (let i = 0; environ[i] != null; ++i) {
430 let assign = environ[i];
431 let equal = assign.indexOf('=');
432 let name = assign.substr(0, equal);
433 let value = assign.substr(equal + 1);
434 process.env[name.toString()] = value;
435}
436
5c9d18fc
JF
437process.cwd = function() {
438 let cwd = new (typedef char[1024]);
439 return getcwd(cwd, cwd.length).toString();
440};
441
0f557ff2
JF
442process.pid = getpid();
443
444})();