]> git.saurik.com Git - cycript.git/blob - libcycript.cy
88fe069ee7769faa2086ff0eeed24c4728ba99a2
[cycript.git] / libcycript.cy
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
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
22 var process = {
23 env: {},
24 };
25
26 (function() {
27
28 this.typeid = function(object) {
29 return object.$cyt;
30 };
31
32 let $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
42 $cy_set(Boolean.prototype, {
43 toCYON: function() {
44 return `new Boolean(${this.toString()})`;
45 },
46 });
47
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() {
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}`;
69 },
70 });
71
72 $cy_set(Number.prototype, {
73 toCYON: function() {
74 if ("$cyt" in this)
75 return `${this.$cyt.toCYON()}(${this.toString()})`;
76 return `new Number(${this.toString()})`;
77 },
78 });
79
80 $cy_set(RegExp.prototype, {
81 toCYON: function() {
82 return this.toString();
83 },
84 });
85
86 if ("Java" in Cycript) {
87 $cy_set(java.lang.Boolean.prototype, {
88 toCYON: function() {
89 return `new java.lang.Boolean(${this.value})`;
90 },
91 });
92
93 $cy_set(java.lang.Byte.prototype, {
94 toCYON: function() {
95 return `new java.lang.Byte(${this.value})`;
96 },
97 });
98
99 $cy_set(java.lang.Character.prototype, {
100 toCYON: function() {
101 return `new java.lang.Character(${this.value})`;
102 },
103 });
104
105 $cy_set(java.lang.Short.prototype, {
106 toCYON: function() {
107 return `new java.lang.Short(${this.value})`;
108 },
109 });
110
111 $cy_set(java.lang.Integer.prototype, {
112 toCYON: function() {
113 return `new java.lang.Integer(${this.value})`;
114 },
115 });
116
117 $cy_set(java.lang.Long.prototype, {
118 toCYON: function() {
119 return `new java.lang.Long(${this.value})`;
120 },
121 });
122
123 $cy_set(java.lang.Float.prototype, {
124 toCYON: function() {
125 return `new java.lang.Float(${this.value})`;
126 },
127 });
128
129 $cy_set(java.lang.Double.prototype, {
130 toCYON: function() {
131 return `new java.lang.Double(${this.value})`;
132 },
133 });
134 }
135
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;
139 };
140
141 let StartsWith = function(lhs, rhs) {
142 return lhs.substring(0, rhs.length) == rhs;
143 };
144
145 let ResolveFile = function(exact, name) {
146 if (exact && IsFile(name))
147 return name;
148 for (let suffix of ['.js', '.json'])
149 if (IsFile(name + suffix))
150 return name + suffix;
151 return null;
152 };
153
154
155 let GetLibraryPath = function() {
156 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
157 if (handle == null)
158 return null;
159
160 try {
161 let CYHandleServer = dlsym(handle, "CYHandleServer");
162 if (CYHandleServer == null)
163 return null;
164
165 let info = new Dl_info;
166 if (dladdr(CYHandleServer, info) == 0)
167 return null;
168
169 let path = info->dli_fname;
170 let slash = path.lastIndexOf('/');
171 if (slash == -1)
172 return null;
173
174 path = path.substr(0, slash);
175
176 GetLibraryPath = function() {
177 return path;
178 };
179
180 return GetLibraryPath();
181 } finally {
182 dlclose(handle);
183 }
184 };
185
186 let ResolveFolder = function(name) {
187 if (access(name + '/', F_OK) == -1)
188 return null;
189
190 if (IsFile(name + "/package.json")) {
191 let package = require(name + "/package.json");
192 let path = ResolveFile(true, name + "/" + package.main);
193 if (path != null)
194 return path;
195 }
196
197 return ResolveFile(false, name + "/index");
198 };
199
200 let ResolveEither = function(name) {
201 let path = null;
202 if (path == null)
203 path = ResolveFile(true, name);
204 if (path == null)
205 path = ResolveFolder(name);
206 return path;
207 };
208
209 require.resolve = function(name) {
210 if (StartsWith(name, '/')) {
211 let path = ResolveEither(name);
212 if (path != null)
213 return path;
214 } else {
215 let cwd = new (typedef char[1024]);
216 cwd = getcwd(cwd, cwd.length).toString();
217 cwd = cwd.split('/');
218
219 if (StartsWith(name, './') || StartsWith(name, '../')) {
220 let path = ResolveEither(cwd + '/' + name);
221 if (path != null)
222 return path;
223 } else {
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);
227 if (path != null)
228 return path;
229 }
230
231 let library = GetLibraryPath();
232 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
233 if (path != null)
234 return path;
235 }
236 }
237
238 throw new Error("Cannot find module '" + name + "'");
239 };
240
241 var _syscall = function(value) {
242 if (value == -1)
243 throw new Error(strerror(errno));
244 };
245
246 var info = *new (struct stat);
247 if (false) {
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";
256 } else {
257 var st_atime = "st_atime";
258 var st_mtime = "st_mtime";
259 var st_ctime = "st_ctime";
260 }
261
262 var toDate = function(timespec) {
263 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
264 };
265
266 var bindings = {};
267
268 process.binding = function(name) {
269 let binding = bindings[name];
270 if (typeof binding != 'undefined')
271 return binding;
272
273 switch (name) {
274 case 'buffer': binding = {
275 setupBufferJS() {
276 },
277 }; break;
278
279 case 'cares_wrap': binding = {
280 }; break;
281
282 case 'constants': binding = {
283 }; break;
284
285 case 'fs': binding = {
286 FSInitialize() {
287 },
288
289 lstat(path) {
290 var info = new (struct stat);
291 _syscall(lstat(path, info));
292
293 return {
294 dev: info->st_dev,
295 mode: info->st_mode,
296 nlink: info->st_nlink,
297 uid: info->st_uid,
298 gid: info->st_gid,
299 rdev: info->st_rdev,
300 blksize: info->st_blksize,
301 ino: info->st_ino,
302 size: info->st_size,
303 blocks: info->st_blocks,
304
305 atime: toDate(info->[st_atime]),
306 mtime: toDate(info->[st_mtime]),
307 ctime: toDate(info->[st_ctime]),
308
309 isSymbolicLink() {
310 return S_ISLNK(this.mode);
311 },
312 };
313 },
314 }; break;
315
316 case 'pipe_wrap': binding = {
317 }; break;
318
319 case 'smalloc': binding = {
320 alloc() {
321 },
322 }; break;
323
324 case 'stream_wrap': binding = {
325 }; break;
326
327 case 'tcp_wrap': binding = {
328 }; break;
329
330 case 'timer_wrap': binding = {
331 kOnTimeout: 0,
332 Timer: {
333 },
334 }; break;
335
336 case 'tty_wrap': binding = {
337 }; break;
338
339 case 'uv': binding = {
340 }; break;
341
342 default:
343 throw new Error('No such module: ' + name);
344 }
345
346 bindings[name] = binding;
347 return binding;
348 };
349
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;
357 }
358
359 process.pid = getpid();
360
361 })();