]> git.saurik.com Git - cycript.git/blame - libcycript.cy
Avoid crash when using toCYON on String.prototype.
[cycript.git] / libcycript.cy
CommitLineData
bf998c10
JF
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
0f557ff2
JF
22var process = {
23 env: {},
24};
25
26(function() {
27
3f7ac48b
JF
28let $cy_set = function(object, properties) {
29 for (const name in properties)
30 Object.defineProperty(object, name, {
31 configurable: true,
32 enumerable: false,
33 writable: true,
34 value: properties[name],
35 });
36};
37
a4e76c78
JF
38$cy_set(Boolean.prototype, {
39 toCYON: function() {
40 return `new Boolean(${this.toString()})`;
41 },
42});
43
3f7ac48b
JF
44$cy_set(Date.prototype, {
45 toCYON: function() {
46 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
47 },
48});
49
50$cy_set(Error.prototype, {
51 toCYON: function() {
9e85a60e
JF
52 let stack = this.stack.split('\n');
53 if (stack.slice(-1)[0] == "global code")
54 stack = stack.slice(0, -1);
55 for (let i = 0; i != stack.length; ++i)
56 stack[i] = '\n ' + stack[i];
57 return `new ${this.constructor.name}(${this.message.toCYON()}) /*${stack.join('')} */`;
3f7ac48b
JF
58 },
59});
0f557ff2 60
a4e76c78
JF
61$cy_set(Number.prototype, {
62 toCYON: function() {
63 return `new Number(${this.toString()})`;
64 },
65});
66
67$cy_set(RegExp.prototype, {
68 toCYON: function() {
69 return this.toString();
70 },
71});
72
0f557ff2
JF
73let IsFile = function(path) {
74 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
75 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
76};
77
78let StartsWith = function(lhs, rhs) {
79 return lhs.substring(0, rhs.length) == rhs;
80};
81
82let ResolveFile = function(exact, name) {
83 if (exact && IsFile(name))
84 return name;
85 for (let suffix of ['.js', '.json'])
86 if (IsFile(name + suffix))
87 return name + suffix;
88 return null;
89};
90
91
92let GetLibraryPath = function() {
93 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
94 if (handle == null)
95 return null;
96
97 try {
98 let CYHandleServer = dlsym(handle, "CYHandleServer");
99 if (CYHandleServer == null)
100 return null;
101
102 let info = new Dl_info;
103 if (dladdr(CYHandleServer, info) == 0)
104 return null;
105
106 let path = info->dli_fname;
107 let slash = path.lastIndexOf('/');
108 if (slash == -1)
109 return null;
110
79858e0a
JF
111 path = path.substr(0, slash);
112
113 GetLibraryPath = function() {
114 return path;
115 };
116
117 return GetLibraryPath();
0f557ff2
JF
118 } finally {
119 dlclose(handle);
120 }
121};
122
123let ResolveFolder = function(name) {
124 if (access(name + '/', F_OK) == -1)
125 return null;
126
127 if (IsFile(name + "/package.json")) {
128 let package = require(name + "/package.json");
129 let path = ResolveFile(true, name + "/" + package.main);
130 if (path != null)
131 return path;
132 }
133
134 return ResolveFile(false, name + "/index");
135};
136
137let ResolveEither = function(name) {
138 let path = null;
139 if (path == null)
140 path = ResolveFile(true, name);
141 if (path == null)
142 path = ResolveFolder(name);
143 return path;
144};
145
146require.resolve = function(name) {
147 if (StartsWith(name, '/')) {
148 let path = ResolveEither(name);
149 if (path != null)
150 return path;
151 } else {
152 let cwd = new (typedef char[1024]);
153 cwd = getcwd(cwd, cwd.length).toString();
154 cwd = cwd.split('/');
155
156 if (StartsWith(name, './') || StartsWith(name, '../')) {
157 let path = ResolveEither(cwd + '/' + name);
158 if (path != null)
159 return path;
160 } else {
161 for (let i = cwd.length; i != 0; --i) {
162 let modules = cwd.slice(0, i).concat("node_modules").join('/');
163 let path = ResolveEither(modules + "/" + name);
164 if (path != null)
165 return path;
166 }
167
168 let library = GetLibraryPath();
169 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
170 if (path != null)
171 return path;
172 }
173 }
174
175 throw new Error("Cannot find module '" + name + "'");
176};
177
d82e4c2a
JF
178var _syscall = function(value) {
179 if (value == -1)
19e13c2d 180 throw new Error(strerror(errno));
d82e4c2a
JF
181};
182
183var info = *new (struct stat);
184if (false) {
185} else if ("st_atim" in info) {
186 var st_atime = "st_atim";
187 var st_mtime = "st_mtim";
188 var st_ctime = "st_ctim";
189} else if ("st_atimespec" in info) {
190 var st_atime = "st_atimespec";
191 var st_mtime = "st_mtimespec";
192 var st_ctime = "st_ctimespec";
193} else {
194 var st_atime = "st_atime";
195 var st_mtime = "st_mtime";
196 var st_ctime = "st_ctime";
197}
198
199var toDate = function(timespec) {
200 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
201};
202
0f557ff2
JF
203var bindings = {};
204
205process.binding = function(name) {
206 let binding = bindings[name];
207 if (typeof binding != 'undefined')
208 return binding;
209
210 switch (name) {
211 case 'buffer': binding = {
212 setupBufferJS() {
213 },
214 }; break;
215
216 case 'cares_wrap': binding = {
217 }; break;
218
219 case 'constants': binding = {
220 }; break;
221
222 case 'fs': binding = {
223 FSInitialize() {
224 },
225
d82e4c2a
JF
226 lstat(path) {
227 var info = new (struct stat);
228 _syscall(lstat(path, info));
229
230 return {
231 dev: info->st_dev,
232 mode: info->st_mode,
233 nlink: info->st_nlink,
234 uid: info->st_uid,
235 gid: info->st_gid,
236 rdev: info->st_rdev,
237 blksize: info->st_blksize,
238 ino: info->st_ino,
239 size: info->st_size,
240 blocks: info->st_blocks,
241
242 atime: toDate(info->[st_atime]),
243 mtime: toDate(info->[st_mtime]),
244 ctime: toDate(info->[st_ctime]),
245
246 isSymbolicLink() {
247 return S_ISLNK(this.mode);
248 },
249 };
0f557ff2
JF
250 },
251 }; break;
252
253 case 'pipe_wrap': binding = {
254 }; break;
255
256 case 'smalloc': binding = {
257 alloc() {
258 },
259 }; break;
260
261 case 'stream_wrap': binding = {
262 }; break;
263
264 case 'tcp_wrap': binding = {
265 }; break;
266
267 case 'timer_wrap': binding = {
268 kOnTimeout: 0,
269 Timer: {
270 },
271 }; break;
272
273 case 'tty_wrap': binding = {
274 }; break;
275
276 case 'uv': binding = {
277 }; break;
278
279 default:
280 throw new Error('No such module: ' + name);
281 }
282
283 bindings[name] = binding;
284 return binding;
285};
286
287let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
288for (let i = 0; environ[i] != null; ++i) {
289 let assign = environ[i];
290 let equal = assign.indexOf('=');
291 let name = assign.substr(0, equal);
292 let value = assign.substr(equal + 1);
293 process.env[name.toString()] = value;
294}
295
296process.pid = getpid();
297
298})();