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 this.typeid = function(object) {
28 let $cy_set = function(object, properties) {
29 for (const name in properties)
30 Object.defineProperty(object, name, {
34 value: properties[name],
38 $cy_set(Boolean.prototype, {
40 return `new Boolean(${this.toString()})`;
44 $cy_set(Date.prototype, {
46 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
50 $cy_set(Error.prototype, {
52 let stack = this.stack;
53 if (typeof stack == 'undefined')
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];
61 if (stack.length == 0)
64 stack = stack.join('');
65 stack = ` /*${stack} */`;
68 return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`;
72 $cy_set(Number.prototype, {
75 //return `${this.$cyt.toCYON()}(${this.toString()})`;
76 return this.toString();
77 return `new Number(${this.toString()})`;
81 $cy_set(RegExp.prototype, {
83 return this.toString();
87 // XXX: Java should just always be available
88 if ("Java" in Cycript) {
90 // XXX: this is a half-assed EventEmitter
91 // XXX: this doesn't even have the same semantics
95 Java.on = function(event, handler) {
97 if (event in this.handlers_)
98 handlers = this.handlers_[event];
101 this.handlers_[event] = handlers;
104 if (this.handlers_ == null)
107 handlers.push(handler);
110 Java.emit = function(event) {
111 if (event in this.handlers_) {
112 var handlers = this.handlers_[event];
113 if (handlers != null)
114 for (var handler of handlers)
118 this.handlers_[event] = null;
121 Java.on('setup', function() {
122 $cy_set(java.lang.Boolean.prototype, {
124 return `new java.lang.Boolean(${this->value})`;
128 $cy_set(java.lang.Byte.prototype, {
130 return `new java.lang.Byte(${this->value})`;
134 $cy_set(java.lang.Character.prototype, {
136 return `new java.lang.Character(${this->value})`;
140 $cy_set(java.lang.Short.prototype, {
142 return `new java.lang.Short(${this->value})`;
146 $cy_set(java.lang.Integer.prototype, {
148 return `new java.lang.Integer(${this->value})`;
152 $cy_set(java.lang.Long.prototype, {
154 return `new java.lang.Long(${this->value})`;
158 $cy_set(java.lang.Float.prototype, {
160 return `new java.lang.Float(${this->value})`;
164 $cy_set(java.lang.Double.prototype, {
166 return `new java.lang.Double(${this->value})`;
170 $cy_set(java.lang.Object.prototype, {
171 toCYON: function(key) {
172 return "#" + this.toString().toCYON();
175 // XXX: due to lack of interface prototypes :(
176 $cyg: function(key) {
177 return this.get(key);
180 // XXX: due to lack of interface prototypes :(
181 $cys: function(key, value) {
183 this.set(key, value);
185 this.put(key, value);
189 $cy_set(java.lang.Throwable.prototype, {
191 var message = this.getMessage();
195 message = message.toCYON();
197 let stack = this.getStackTrace();
198 if (stack.length == 0)
201 stack = stack.join('\n ');
202 stack = ` /*\n ${stack} */`;
205 return `new ${this.constructor.class.getName()}(${message})${stack}`;
212 if ("ObjectiveC" in Cycript) {
213 $cy_set(NSArray.prototype, {
214 $cyg: function(key) {
215 return objc_msgSend(this, "objectAtIndex:", key);
218 $cys: function(key, value) {
219 return objc_msgSend(this, "setObject:atIndex:", value, key);
223 $cy_set(NSDictionary.prototype, {
224 $cyg: function(key) {
225 return objc_msgSend(this, "objectForKey:", key);
228 $cys: function(key, value) {
229 return objc_msgSend(this, "setObject:forKey:", value, key);
234 let IsFile = function(path) {
235 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
236 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
239 let StartsWith = function(lhs, rhs) {
240 return lhs.substring(0, rhs.length) == rhs;
243 let ResolveFile = function(exact, name) {
244 if (exact && IsFile(name))
246 for (let suffix of ['.js', '.json'])
247 if (IsFile(name + suffix))
248 return name + suffix;
253 let GetLibraryPath = function() {
254 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
259 let CYHandleServer = dlsym(handle, "CYHandleServer");
260 if (CYHandleServer == null)
263 let info = new Dl_info;
264 if (dladdr(CYHandleServer, info) == 0)
267 let path = info->dli_fname;
268 let slash = path.lastIndexOf('/');
272 path = path.substr(0, slash);
274 GetLibraryPath = function() {
278 return GetLibraryPath();
284 let ResolveFolder = function(name) {
285 if (access(name + '/', F_OK) == -1)
288 if (IsFile(name + "/package.json")) {
289 let package = require(name + "/package.json");
290 let path = ResolveFile(true, name + "/" + package.main);
295 return ResolveFile(false, name + "/index");
298 let ResolveEither = function(name) {
301 path = ResolveFile(true, name);
303 path = ResolveFolder(name);
307 require.resolve = function(name) {
308 if (StartsWith(name, '/')) {
309 let path = ResolveEither(name);
313 let cwd = new (typedef char[1024]);
314 cwd = getcwd(cwd, cwd.length).toString();
315 cwd = cwd.split('/');
317 if (StartsWith(name, './') || StartsWith(name, '../')) {
318 let path = ResolveEither(cwd + '/' + name);
322 for (let i = cwd.length; i != 0; --i) {
323 let modules = cwd.slice(0, i).concat("node_modules").join('/');
324 let path = ResolveEither(modules + "/" + name);
329 let library = GetLibraryPath();
330 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
336 throw new Error("Cannot find module '" + name + "'");
339 var _syscall = function(value) {
341 throw new Error(strerror(errno));
344 var info = *new (struct stat);
346 } else if ("st_atim" in info) {
347 var st_atime = "st_atim";
348 var st_mtime = "st_mtim";
349 var st_ctime = "st_ctim";
350 } else if ("st_atimespec" in info) {
351 var st_atime = "st_atimespec";
352 var st_mtime = "st_mtimespec";
353 var st_ctime = "st_ctimespec";
355 var st_atime = "st_atime";
356 var st_mtime = "st_mtime";
357 var st_ctime = "st_ctime";
360 var toDate = function(timespec) {
361 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
366 process.binding = function(name) {
367 let binding = bindings[name];
368 if (typeof binding != 'undefined')
372 case 'buffer': binding = {
377 case 'cares_wrap': binding = {
380 case 'constants': binding = {
383 case 'fs': binding = {
388 var info = new (struct stat);
389 _syscall(lstat(path, info));
394 nlink: info->st_nlink,
398 blksize: info->st_blksize,
401 blocks: info->st_blocks,
403 atime: toDate(info->[st_atime]),
404 mtime: toDate(info->[st_mtime]),
405 ctime: toDate(info->[st_ctime]),
408 return S_ISLNK(this.mode);
414 case 'pipe_wrap': binding = {
417 case 'smalloc': binding = {
422 case 'stream_wrap': binding = {
425 case 'tcp_wrap': binding = {
428 case 'timer_wrap': binding = {
434 case 'tty_wrap': binding = {
437 case 'uv': binding = {
441 throw new Error('No such module: ' + name);
444 bindings[name] = binding;
450 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
451 for (let i = 0; environ[i] != null; ++i) {
452 let assign = environ[i];
453 let equal = assign.indexOf('=');
454 let name = assign.substr(0, equal);
455 let value = assign.substr(equal + 1);
456 process.env[name.toString()] = value;
459 process.cwd = function() {
460 let cwd = new (typedef char[1024]);
461 return getcwd(cwd, cwd.length).toString();
464 process.pid = getpid();