1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
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.
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/>.
24 Number.prototype.__defineGetter__('$cyt', function() {
31 this.typeid = function(object) {
35 let $cy_set = function(object, properties) {
36 for (const name in properties)
37 if ("defineProperty" in Object)
38 Object.defineProperty(object, name, {
42 value: properties[name],
43 }); else object[name] = properties[name];
46 $cy_set(Boolean.prototype, {
48 return `new Boolean(${this.toString()})`;
52 $cy_set(Date.prototype, {
54 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
58 $cy_set(Error.prototype, {
60 let stack = this.stack;
61 if (typeof stack == 'undefined')
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)
72 stack = stack.join('');
73 stack = ` /*${stack} */`;
76 return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`;
80 $cy_set(Number.prototype, {
83 //return `${this.$cyt.toCYON()}(${this.toString()})`;
84 return this.toString();
85 return `new Number(${this.toString()})`;
89 $cy_set(RegExp.prototype, {
91 return this.toString();
95 // XXX: Java should just always be available
96 if ("Java" in Cycript) {
98 // XXX: this is a half-assed EventEmitter
99 // XXX: this doesn't even have the same semantics
103 Java.on = function(event, handler) {
105 if (event in this.handlers_)
106 handlers = this.handlers_[event];
109 this.handlers_[event] = handlers;
112 if (this.handlers_ == null)
115 handlers.push(handler);
118 Java.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)
126 this.handlers_[event] = null;
129 Java.on('setup', function() {
130 $cy_set(java.lang.Boolean.prototype, {
132 return `new java.lang.Boolean(${this->value})`;
136 $cy_set(java.lang.Byte.prototype, {
138 return `new java.lang.Byte(${this->value})`;
142 $cy_set(java.lang.Character.prototype, {
144 return `new java.lang.Character(${this->value})`;
148 $cy_set(java.lang.Short.prototype, {
150 return `new java.lang.Short(${this->value})`;
154 $cy_set(java.lang.Integer.prototype, {
156 return `new java.lang.Integer(${this->value})`;
160 $cy_set(java.lang.Long.prototype, {
162 return `new java.lang.Long(${this->value})`;
166 $cy_set(java.lang.Float.prototype, {
168 return `new java.lang.Float(${this->value})`;
172 $cy_set(java.lang.Double.prototype, {
174 return `new java.lang.Double(${this->value})`;
178 $cy_set(java.lang.Object.prototype, {
179 toCYON: function(key) {
180 return "#" + this.toString().toCYON();
183 // XXX: due to lack of interface prototypes :(
184 $cyg: function(key) {
185 return this.get(key);
188 // XXX: due to lack of interface prototypes :(
189 $cys: function(key, value) {
191 this.set(key, value);
193 this.put(key, value);
197 $cy_set(java.lang.Throwable.prototype, {
199 var message = this.getMessage();
203 message = message.toCYON();
205 let stack = this.getStackTrace();
206 if (stack.length == 0)
209 stack = stack.join('\n ');
210 stack = ` /*\n ${stack} */`;
213 return `new ${this.constructor.class.getName()}(${message})${stack}`;
220 if ("ObjectiveC" in Cycript) {
221 $cy_set(NSArray.prototype, {
222 $cyg: function(key) {
223 return objc_msgSend(this, "objectAtIndex:", key);
226 $cys: function(key, value) {
227 return objc_msgSend(this, "setObject:atIndex:", value, key);
231 $cy_set(NSDictionary.prototype, {
232 $cyg: function(key) {
233 return objc_msgSend(this, "objectForKey:", key);
236 $cys: function(key, value) {
237 return objc_msgSend(this, "setObject:forKey:", value, key);
242 let 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;
247 let StartsWith = function(lhs, rhs) {
248 return lhs.substring(0, rhs.length) == rhs;
251 let ResolveFile = function(exact, name) {
252 if (exact && IsFile(name))
254 for (let suffix of ['.js', '.json'])
255 if (IsFile(name + suffix))
256 return name + suffix;
261 let GetLibraryPath = function() {
262 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
267 let CYHandleServer = dlsym(handle, "CYHandleServer");
268 if (CYHandleServer == null)
271 let info = new Dl_info;
272 if (dladdr(CYHandleServer, info) == 0)
275 let path = info->dli_fname;
276 let slash = path.lastIndexOf('/');
280 path = path.substr(0, slash);
282 GetLibraryPath = function() {
286 return GetLibraryPath();
292 let ResolveFolder = function(name) {
293 if (access(name + '/', F_OK) == -1)
296 if (IsFile(name + "/package.json")) {
297 let package = require(name + "/package.json");
298 let path = ResolveFile(true, name + "/" + package.main);
303 return ResolveFile(false, name + "/index");
306 let ResolveEither = function(name) {
309 path = ResolveFile(true, name);
311 path = ResolveFolder(name);
315 require.resolve = function(name) {
316 if (StartsWith(name, '/')) {
317 let path = ResolveEither(name);
321 let cwd = *new (typedef char[1024]);
322 cwd = getcwd(cwd, cwd.length).toString();
323 cwd = cwd.split('/');
325 if (StartsWith(name, './') || StartsWith(name, '../')) {
326 let path = ResolveEither(cwd + '/' + name);
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);
337 let library = GetLibraryPath();
338 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
344 throw new Error("Cannot find module '" + name + "'");
347 var _syscall = function(value) {
349 throw new Error(strerror(errno));
352 var info = *new (struct stat);
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";
363 var st_atime = "st_atime";
364 var st_mtime = "st_mtime";
365 var st_ctime = "st_ctime";
368 var toDate = function(timespec) {
369 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
374 process.binding = function(name) {
375 let binding = bindings[name];
376 if (typeof binding != 'undefined')
380 case 'buffer': binding = {
385 case 'cares_wrap': binding = {
388 case 'constants': binding = {
391 case 'fs': binding = {
396 var info = new (struct stat);
397 _syscall(lstat(path, info));
402 nlink: info->st_nlink,
406 blksize: info->st_blksize,
409 blocks: info->st_blocks,
411 atime: toDate(info->[st_atime]),
412 mtime: toDate(info->[st_mtime]),
413 ctime: toDate(info->[st_ctime]),
416 return S_ISLNK(this.mode);
422 case 'pipe_wrap': binding = {
425 case 'smalloc': binding = {
430 case 'stream_wrap': binding = {
433 case 'tcp_wrap': binding = {
436 case 'timer_wrap': binding = {
442 case 'tty_wrap': binding = {
445 case 'uv': binding = {
449 throw new Error('No such module: ' + name);
452 bindings[name] = binding;
458 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
459 for (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;
467 process.cwd = function() {
468 let cwd = new (typedef char[1024]);
469 return getcwd(cwd, cwd.length).toString();
472 process.pid = getpid();