1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 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/>.
28 this.typeid = function(object) {
32 let $cy_set = function(object, properties) {
33 for (const name in properties)
34 Object.defineProperty(object, name, {
38 value: properties[name],
42 $cy_set(Boolean.prototype, {
44 return `new Boolean(${this.toString()})`;
48 $cy_set(Date.prototype, {
50 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
54 $cy_set(Error.prototype, {
56 let stack = this.stack;
57 if (typeof stack == 'undefined')
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} */`;
68 return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`;
72 $cy_set(Number.prototype, {
75 return `${this.$cyt.toCYON()}(${this.toString()})`;
76 return `new Number(${this.toString()})`;
80 $cy_set(RegExp.prototype, {
82 return this.toString();
86 if ("Java" in Cycript) {
87 $cy_set(java.lang.Boolean.prototype, {
89 return `new java.lang.Boolean(${this->value})`;
93 $cy_set(java.lang.Byte.prototype, {
95 return `new java.lang.Byte(${this->value})`;
99 $cy_set(java.lang.Character.prototype, {
101 return `new java.lang.Character(${this->value})`;
105 $cy_set(java.lang.Short.prototype, {
107 return `new java.lang.Short(${this->value})`;
111 $cy_set(java.lang.Integer.prototype, {
113 return `new java.lang.Integer(${this->value})`;
117 $cy_set(java.lang.Long.prototype, {
119 return `new java.lang.Long(${this->value})`;
123 $cy_set(java.lang.Float.prototype, {
125 return `new java.lang.Float(${this->value})`;
129 $cy_set(java.lang.Double.prototype, {
131 return `new java.lang.Double(${this->value})`;
135 $cy_set(java.lang.Object.prototype, {
136 // XXX: due to lack of interface prototypes :(
137 $cyg: function(key) {
138 return this.get(key);
141 // XXX: due to lack of interface prototypes :(
142 $cys: function(key, value) {
144 this.set(key, value);
146 this.put(key, value);
151 if ("ObjectiveC" in Cycript) {
152 $cy_set(NSArray.prototype, {
153 $cyg: function(key) {
154 return objc_msgSend(this, "objectAtIndex:", key);
157 $cys: function(key, value) {
158 return objc_msgSend(this, "setObject:atIndex:", value, key);
162 $cy_set(NSDictionary.prototype, {
163 $cyg: function(key) {
164 return objc_msgSend(this, "objectForKey:", key);
167 $cys: function(key, value) {
168 return objc_msgSend(this, "setObject:forKey:", value, key);
173 let IsFile = function(path) {
174 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
175 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
178 let StartsWith = function(lhs, rhs) {
179 return lhs.substring(0, rhs.length) == rhs;
182 let ResolveFile = function(exact, name) {
183 if (exact && IsFile(name))
185 for (let suffix of ['.js', '.json'])
186 if (IsFile(name + suffix))
187 return name + suffix;
192 let GetLibraryPath = function() {
193 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
198 let CYHandleServer = dlsym(handle, "CYHandleServer");
199 if (CYHandleServer == null)
202 let info = new Dl_info;
203 if (dladdr(CYHandleServer, info) == 0)
206 let path = info->dli_fname;
207 let slash = path.lastIndexOf('/');
211 path = path.substr(0, slash);
213 GetLibraryPath = function() {
217 return GetLibraryPath();
223 let ResolveFolder = function(name) {
224 if (access(name + '/', F_OK) == -1)
227 if (IsFile(name + "/package.json")) {
228 let package = require(name + "/package.json");
229 let path = ResolveFile(true, name + "/" + package.main);
234 return ResolveFile(false, name + "/index");
237 let ResolveEither = function(name) {
240 path = ResolveFile(true, name);
242 path = ResolveFolder(name);
246 require.resolve = function(name) {
247 if (StartsWith(name, '/')) {
248 let path = ResolveEither(name);
252 let cwd = new (typedef char[1024]);
253 cwd = getcwd(cwd, cwd.length).toString();
254 cwd = cwd.split('/');
256 if (StartsWith(name, './') || StartsWith(name, '../')) {
257 let path = ResolveEither(cwd + '/' + name);
261 for (let i = cwd.length; i != 0; --i) {
262 let modules = cwd.slice(0, i).concat("node_modules").join('/');
263 let path = ResolveEither(modules + "/" + name);
268 let library = GetLibraryPath();
269 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
275 throw new Error("Cannot find module '" + name + "'");
278 var _syscall = function(value) {
280 throw new Error(strerror(errno));
283 var info = *new (struct stat);
285 } else if ("st_atim" in info) {
286 var st_atime = "st_atim";
287 var st_mtime = "st_mtim";
288 var st_ctime = "st_ctim";
289 } else if ("st_atimespec" in info) {
290 var st_atime = "st_atimespec";
291 var st_mtime = "st_mtimespec";
292 var st_ctime = "st_ctimespec";
294 var st_atime = "st_atime";
295 var st_mtime = "st_mtime";
296 var st_ctime = "st_ctime";
299 var toDate = function(timespec) {
300 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
305 process.binding = function(name) {
306 let binding = bindings[name];
307 if (typeof binding != 'undefined')
311 case 'buffer': binding = {
316 case 'cares_wrap': binding = {
319 case 'constants': binding = {
322 case 'fs': binding = {
327 var info = new (struct stat);
328 _syscall(lstat(path, info));
333 nlink: info->st_nlink,
337 blksize: info->st_blksize,
340 blocks: info->st_blocks,
342 atime: toDate(info->[st_atime]),
343 mtime: toDate(info->[st_mtime]),
344 ctime: toDate(info->[st_ctime]),
347 return S_ISLNK(this.mode);
353 case 'pipe_wrap': binding = {
356 case 'smalloc': binding = {
361 case 'stream_wrap': binding = {
364 case 'tcp_wrap': binding = {
367 case 'timer_wrap': binding = {
373 case 'tty_wrap': binding = {
376 case 'uv': binding = {
380 throw new Error('No such module: ' + name);
383 bindings[name] = binding;
387 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
388 for (let i = 0; environ[i] != null; ++i) {
389 let assign = environ[i];
390 let equal = assign.indexOf('=');
391 let name = assign.substr(0, equal);
392 let value = assign.substr(equal + 1);
393 process.env[name.toString()] = value;
396 process.pid = getpid();