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