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})`;
136 let IsFile = function(path) {
137 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
138 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
141 let StartsWith = function(lhs, rhs) {
142 return lhs.substring(0, rhs.length) == rhs;
145 let ResolveFile = function(exact, name) {
146 if (exact && IsFile(name))
148 for (let suffix of ['.js', '.json'])
149 if (IsFile(name + suffix))
150 return name + suffix;
155 let GetLibraryPath = function() {
156 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
161 let CYHandleServer = dlsym(handle, "CYHandleServer");
162 if (CYHandleServer == null)
165 let info = new Dl_info;
166 if (dladdr(CYHandleServer, info) == 0)
169 let path = info->dli_fname;
170 let slash = path.lastIndexOf('/');
174 path = path.substr(0, slash);
176 GetLibraryPath = function() {
180 return GetLibraryPath();
186 let ResolveFolder = function(name) {
187 if (access(name + '/', F_OK) == -1)
190 if (IsFile(name + "/package.json")) {
191 let package = require(name + "/package.json");
192 let path = ResolveFile(true, name + "/" + package.main);
197 return ResolveFile(false, name + "/index");
200 let ResolveEither = function(name) {
203 path = ResolveFile(true, name);
205 path = ResolveFolder(name);
209 require.resolve = function(name) {
210 if (StartsWith(name, '/')) {
211 let path = ResolveEither(name);
215 let cwd = new (typedef char[1024]);
216 cwd = getcwd(cwd, cwd.length).toString();
217 cwd = cwd.split('/');
219 if (StartsWith(name, './') || StartsWith(name, '../')) {
220 let path = ResolveEither(cwd + '/' + name);
224 for (let i = cwd.length; i != 0; --i) {
225 let modules = cwd.slice(0, i).concat("node_modules").join('/');
226 let path = ResolveEither(modules + "/" + name);
231 let library = GetLibraryPath();
232 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
238 throw new Error("Cannot find module '" + name + "'");
241 var _syscall = function(value) {
243 throw new Error(strerror(errno));
246 var info = *new (struct stat);
248 } else if ("st_atim" in info) {
249 var st_atime = "st_atim";
250 var st_mtime = "st_mtim";
251 var st_ctime = "st_ctim";
252 } else if ("st_atimespec" in info) {
253 var st_atime = "st_atimespec";
254 var st_mtime = "st_mtimespec";
255 var st_ctime = "st_ctimespec";
257 var st_atime = "st_atime";
258 var st_mtime = "st_mtime";
259 var st_ctime = "st_ctime";
262 var toDate = function(timespec) {
263 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
268 process.binding = function(name) {
269 let binding = bindings[name];
270 if (typeof binding != 'undefined')
274 case 'buffer': binding = {
279 case 'cares_wrap': binding = {
282 case 'constants': binding = {
285 case 'fs': binding = {
290 var info = new (struct stat);
291 _syscall(lstat(path, info));
296 nlink: info->st_nlink,
300 blksize: info->st_blksize,
303 blocks: info->st_blocks,
305 atime: toDate(info->[st_atime]),
306 mtime: toDate(info->[st_mtime]),
307 ctime: toDate(info->[st_ctime]),
310 return S_ISLNK(this.mode);
316 case 'pipe_wrap': binding = {
319 case 'smalloc': binding = {
324 case 'stream_wrap': binding = {
327 case 'tcp_wrap': binding = {
330 case 'timer_wrap': binding = {
336 case 'tty_wrap': binding = {
339 case 'uv': binding = {
343 throw new Error('No such module: ' + name);
346 bindings[name] = binding;
350 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
351 for (let i = 0; environ[i] != null; ++i) {
352 let assign = environ[i];
353 let equal = assign.indexOf('=');
354 let name = assign.substr(0, equal);
355 let value = assign.substr(equal + 1);
356 process.env[name.toString()] = value;
359 process.pid = getpid();