]> git.saurik.com Git - cycript.git/blame - libcycript.cy
Port modules to typedef/extern instead of @encode.
[cycript.git] / libcycript.cy
CommitLineData
7341eedb
JF
1/* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
bf998c10
JF
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
0f557ff2
JF
22var process = {
23 env: {},
24};
25
26(function() {
27
574d4720
JF
28this.typeid = function(object) {
29 return object.$cyt;
30};
31
3f7ac48b
JF
32let $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
a4e76c78
JF
42$cy_set(Boolean.prototype, {
43 toCYON: function() {
44 return `new Boolean(${this.toString()})`;
45 },
46});
47
3f7ac48b
JF
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() {
f348c890
JF
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}`;
3f7ac48b
JF
69 },
70});
0f557ff2 71
a4e76c78
JF
72$cy_set(Number.prototype, {
73 toCYON: function() {
574d4720
JF
74 if ("$cyt" in this)
75 return `${this.$cyt.toCYON()}(${this.toString()})`;
a4e76c78
JF
76 return `new Number(${this.toString()})`;
77 },
78});
79
80$cy_set(RegExp.prototype, {
81 toCYON: function() {
82 return this.toString();
83 },
84});
85
dbf05bfd
JF
86if ("Java" in Cycript) {
87 $cy_set(java.lang.Boolean.prototype, {
88 toCYON: function() {
8effd381 89 return `new java.lang.Boolean(${this->value})`;
dbf05bfd
JF
90 },
91 });
92
93 $cy_set(java.lang.Byte.prototype, {
94 toCYON: function() {
8effd381 95 return `new java.lang.Byte(${this->value})`;
dbf05bfd
JF
96 },
97 });
98
99 $cy_set(java.lang.Character.prototype, {
100 toCYON: function() {
8effd381 101 return `new java.lang.Character(${this->value})`;
dbf05bfd
JF
102 },
103 });
104
105 $cy_set(java.lang.Short.prototype, {
106 toCYON: function() {
8effd381 107 return `new java.lang.Short(${this->value})`;
dbf05bfd
JF
108 },
109 });
110
111 $cy_set(java.lang.Integer.prototype, {
112 toCYON: function() {
8effd381 113 return `new java.lang.Integer(${this->value})`;
dbf05bfd
JF
114 },
115 });
116
117 $cy_set(java.lang.Long.prototype, {
118 toCYON: function() {
8effd381 119 return `new java.lang.Long(${this->value})`;
dbf05bfd
JF
120 },
121 });
122
123 $cy_set(java.lang.Float.prototype, {
124 toCYON: function() {
8effd381 125 return `new java.lang.Float(${this->value})`;
dbf05bfd
JF
126 },
127 });
128
129 $cy_set(java.lang.Double.prototype, {
130 toCYON: function() {
8effd381 131 return `new java.lang.Double(${this->value})`;
dbf05bfd
JF
132 },
133 });
e56c2499
JF
134
135 $cy_set(java.lang.Object.prototype, {
4b645e23
JF
136 toCYON: function(key) {
137 return "#" + this.toString().toCYON();
138 },
139
e56c2499
JF
140 // XXX: due to lack of interface prototypes :(
141 $cyg: function(key) {
142 return this.get(key);
143 },
144
145 // XXX: due to lack of interface prototypes :(
146 $cys: function(key, value) {
147 if ("set" in this)
148 this.set(key, value);
149 else
150 this.put(key, value);
151 },
152 });
153}
154
155if ("ObjectiveC" in Cycript) {
156 $cy_set(NSArray.prototype, {
157 $cyg: function(key) {
158 return objc_msgSend(this, "objectAtIndex:", key);
159 },
160
161 $cys: function(key, value) {
162 return objc_msgSend(this, "setObject:atIndex:", value, key);
163 },
164 });
165
166 $cy_set(NSDictionary.prototype, {
167 $cyg: function(key) {
168 return objc_msgSend(this, "objectForKey:", key);
169 },
170
171 $cys: function(key, value) {
172 return objc_msgSend(this, "setObject:forKey:", value, key);
173 },
174 });
dbf05bfd
JF
175}
176
0f557ff2
JF
177let IsFile = function(path) {
178 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
179 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
180};
181
182let StartsWith = function(lhs, rhs) {
183 return lhs.substring(0, rhs.length) == rhs;
184};
185
186let ResolveFile = function(exact, name) {
187 if (exact && IsFile(name))
188 return name;
189 for (let suffix of ['.js', '.json'])
190 if (IsFile(name + suffix))
191 return name + suffix;
192 return null;
193};
194
195
196let GetLibraryPath = function() {
197 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
198 if (handle == null)
199 return null;
200
201 try {
202 let CYHandleServer = dlsym(handle, "CYHandleServer");
203 if (CYHandleServer == null)
204 return null;
205
206 let info = new Dl_info;
207 if (dladdr(CYHandleServer, info) == 0)
208 return null;
209
210 let path = info->dli_fname;
211 let slash = path.lastIndexOf('/');
212 if (slash == -1)
213 return null;
214
79858e0a
JF
215 path = path.substr(0, slash);
216
217 GetLibraryPath = function() {
218 return path;
219 };
220
221 return GetLibraryPath();
0f557ff2
JF
222 } finally {
223 dlclose(handle);
224 }
225};
226
227let ResolveFolder = function(name) {
228 if (access(name + '/', F_OK) == -1)
229 return null;
230
231 if (IsFile(name + "/package.json")) {
232 let package = require(name + "/package.json");
233 let path = ResolveFile(true, name + "/" + package.main);
234 if (path != null)
235 return path;
236 }
237
238 return ResolveFile(false, name + "/index");
239};
240
241let ResolveEither = function(name) {
242 let path = null;
243 if (path == null)
244 path = ResolveFile(true, name);
245 if (path == null)
246 path = ResolveFolder(name);
247 return path;
248};
249
250require.resolve = function(name) {
251 if (StartsWith(name, '/')) {
252 let path = ResolveEither(name);
253 if (path != null)
254 return path;
255 } else {
256 let cwd = new (typedef char[1024]);
257 cwd = getcwd(cwd, cwd.length).toString();
258 cwd = cwd.split('/');
259
260 if (StartsWith(name, './') || StartsWith(name, '../')) {
261 let path = ResolveEither(cwd + '/' + name);
262 if (path != null)
263 return path;
264 } else {
265 for (let i = cwd.length; i != 0; --i) {
266 let modules = cwd.slice(0, i).concat("node_modules").join('/');
267 let path = ResolveEither(modules + "/" + name);
268 if (path != null)
269 return path;
270 }
271
272 let library = GetLibraryPath();
273 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
274 if (path != null)
275 return path;
276 }
277 }
278
279 throw new Error("Cannot find module '" + name + "'");
280};
281
d82e4c2a
JF
282var _syscall = function(value) {
283 if (value == -1)
19e13c2d 284 throw new Error(strerror(errno));
d82e4c2a
JF
285};
286
287var info = *new (struct stat);
288if (false) {
289} else if ("st_atim" in info) {
290 var st_atime = "st_atim";
291 var st_mtime = "st_mtim";
292 var st_ctime = "st_ctim";
293} else if ("st_atimespec" in info) {
294 var st_atime = "st_atimespec";
295 var st_mtime = "st_mtimespec";
296 var st_ctime = "st_ctimespec";
297} else {
298 var st_atime = "st_atime";
299 var st_mtime = "st_mtime";
300 var st_ctime = "st_ctime";
301}
302
303var toDate = function(timespec) {
304 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
305};
306
0f557ff2
JF
307var bindings = {};
308
309process.binding = function(name) {
310 let binding = bindings[name];
311 if (typeof binding != 'undefined')
312 return binding;
313
314 switch (name) {
315 case 'buffer': binding = {
316 setupBufferJS() {
317 },
318 }; break;
319
320 case 'cares_wrap': binding = {
321 }; break;
322
323 case 'constants': binding = {
324 }; break;
325
326 case 'fs': binding = {
327 FSInitialize() {
328 },
329
d82e4c2a
JF
330 lstat(path) {
331 var info = new (struct stat);
332 _syscall(lstat(path, info));
333
334 return {
335 dev: info->st_dev,
336 mode: info->st_mode,
337 nlink: info->st_nlink,
338 uid: info->st_uid,
339 gid: info->st_gid,
340 rdev: info->st_rdev,
341 blksize: info->st_blksize,
342 ino: info->st_ino,
343 size: info->st_size,
344 blocks: info->st_blocks,
345
346 atime: toDate(info->[st_atime]),
347 mtime: toDate(info->[st_mtime]),
348 ctime: toDate(info->[st_ctime]),
349
350 isSymbolicLink() {
351 return S_ISLNK(this.mode);
352 },
353 };
0f557ff2
JF
354 },
355 }; break;
356
357 case 'pipe_wrap': binding = {
358 }; break;
359
360 case 'smalloc': binding = {
361 alloc() {
362 },
363 }; break;
364
365 case 'stream_wrap': binding = {
366 }; break;
367
368 case 'tcp_wrap': binding = {
369 }; break;
370
371 case 'timer_wrap': binding = {
372 kOnTimeout: 0,
373 Timer: {
374 },
375 }; break;
376
377 case 'tty_wrap': binding = {
378 }; break;
379
380 case 'uv': binding = {
381 }; break;
382
383 default:
384 throw new Error('No such module: ' + name);
385 }
386
387 bindings[name] = binding;
388 return binding;
389};
390
391let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
392for (let i = 0; environ[i] != null; ++i) {
393 let assign = environ[i];
394 let equal = assign.indexOf('=');
395 let name = assign.substr(0, equal);
396 let value = assign.substr(equal + 1);
397 process.env[name.toString()] = value;
398}
399
400process.pid = getpid();
401
402})();