]> git.saurik.com Git - cycript.git/blob - libcycript.cy
Convert static inline functions into bridge stubs.
[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 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 const X_OK = (1<<0);
39 const W_OK = (1<<1);
40 const R_OK = (1<<2);
41
42 $cy_set(Date.prototype, {
43 toCYON: function() {
44 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
45 },
46 });
47
48 $cy_set(Error.prototype, {
49 toCYON: function() {
50 let stack = this.stack.split('\n');
51 if (stack.slice(-1)[0] == "global code")
52 stack = stack.slice(0, -1);
53 for (let i = 0; i != stack.length; ++i)
54 stack[i] = '\n ' + stack[i];
55 return `new ${this.constructor.name}(${this.message.toCYON()}) /*${stack.join('')} */`;
56 },
57 });
58
59 let IsFile = function(path) {
60 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
61 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
62 };
63
64 let StartsWith = function(lhs, rhs) {
65 return lhs.substring(0, rhs.length) == rhs;
66 };
67
68 let ResolveFile = function(exact, name) {
69 if (exact && IsFile(name))
70 return name;
71 for (let suffix of ['.js', '.json'])
72 if (IsFile(name + suffix))
73 return name + suffix;
74 return null;
75 };
76
77
78 let GetLibraryPath = function() {
79 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
80 if (handle == null)
81 return null;
82
83 try {
84 let CYHandleServer = dlsym(handle, "CYHandleServer");
85 if (CYHandleServer == null)
86 return null;
87
88 let info = new Dl_info;
89 if (dladdr(CYHandleServer, info) == 0)
90 return null;
91
92 let path = info->dli_fname;
93 let slash = path.lastIndexOf('/');
94 if (slash == -1)
95 return null;
96
97 path = path.substr(0, slash);
98
99 GetLibraryPath = function() {
100 return path;
101 };
102
103 return GetLibraryPath();
104 } finally {
105 dlclose(handle);
106 }
107 };
108
109 let ResolveFolder = function(name) {
110 if (access(name + '/', F_OK) == -1)
111 return null;
112
113 if (IsFile(name + "/package.json")) {
114 let package = require(name + "/package.json");
115 let path = ResolveFile(true, name + "/" + package.main);
116 if (path != null)
117 return path;
118 }
119
120 return ResolveFile(false, name + "/index");
121 };
122
123 let ResolveEither = function(name) {
124 let path = null;
125 if (path == null)
126 path = ResolveFile(true, name);
127 if (path == null)
128 path = ResolveFolder(name);
129 return path;
130 };
131
132 require.resolve = function(name) {
133 if (StartsWith(name, '/')) {
134 let path = ResolveEither(name);
135 if (path != null)
136 return path;
137 } else {
138 let cwd = new (typedef char[1024]);
139 cwd = getcwd(cwd, cwd.length).toString();
140 cwd = cwd.split('/');
141
142 if (StartsWith(name, './') || StartsWith(name, '../')) {
143 let path = ResolveEither(cwd + '/' + name);
144 if (path != null)
145 return path;
146 } else {
147 for (let i = cwd.length; i != 0; --i) {
148 let modules = cwd.slice(0, i).concat("node_modules").join('/');
149 let path = ResolveEither(modules + "/" + name);
150 if (path != null)
151 return path;
152 }
153
154 let library = GetLibraryPath();
155 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
156 if (path != null)
157 return path;
158 }
159 }
160
161 throw new Error("Cannot find module '" + name + "'");
162 };
163
164 var bindings = {};
165
166 process.binding = function(name) {
167 let binding = bindings[name];
168 if (typeof binding != 'undefined')
169 return binding;
170
171 switch (name) {
172 case 'buffer': binding = {
173 setupBufferJS() {
174 },
175 }; break;
176
177 case 'cares_wrap': binding = {
178 }; break;
179
180 case 'constants': binding = {
181 }; break;
182
183 case 'fs': binding = {
184 FSInitialize() {
185 },
186
187 lstat() {
188 throw new Error("stat(" + arguments[0] + ")");
189 },
190 }; break;
191
192 case 'pipe_wrap': binding = {
193 }; break;
194
195 case 'smalloc': binding = {
196 alloc() {
197 },
198 }; break;
199
200 case 'stream_wrap': binding = {
201 }; break;
202
203 case 'tcp_wrap': binding = {
204 }; break;
205
206 case 'timer_wrap': binding = {
207 kOnTimeout: 0,
208 Timer: {
209 },
210 }; break;
211
212 case 'tty_wrap': binding = {
213 }; break;
214
215 case 'uv': binding = {
216 }; break;
217
218 default:
219 throw new Error('No such module: ' + name);
220 }
221
222 bindings[name] = binding;
223 return binding;
224 };
225
226 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
227 for (let i = 0; environ[i] != null; ++i) {
228 let assign = environ[i];
229 let equal = assign.indexOf('=');
230 let name = assign.substr(0, equal);
231 let value = assign.substr(equal + 1);
232 process.env[name.toString()] = value;
233 }
234
235 process.pid = getpid();
236
237 })();