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