]> git.saurik.com Git - cycript.git/blob - libcycript.cy
20a101710b7cfff1b12eb1a4a66085396f386205
[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 $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() {
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('')} */`;
52 },
53 });
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
93 path = path.substr(0, slash);
94
95 GetLibraryPath = function() {
96 return path;
97 };
98
99 return GetLibraryPath();
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
160 var bindings = {};
161
162 process.binding = function(name) {
163 let binding = bindings[name];
164 if (typeof binding != 'undefined')
165 return binding;
166
167 switch (name) {
168 case 'buffer': binding = {
169 setupBufferJS() {
170 },
171 }; break;
172
173 case 'cares_wrap': binding = {
174 }; break;
175
176 case 'constants': binding = {
177 }; break;
178
179 case 'fs': binding = {
180 FSInitialize() {
181 },
182
183 lstat() {
184 throw new Error("stat(" + arguments[0] + ")");
185 },
186 }; break;
187
188 case 'pipe_wrap': binding = {
189 }; break;
190
191 case 'smalloc': binding = {
192 alloc() {
193 },
194 }; break;
195
196 case 'stream_wrap': binding = {
197 }; break;
198
199 case 'tcp_wrap': binding = {
200 }; break;
201
202 case 'timer_wrap': binding = {
203 kOnTimeout: 0,
204 Timer: {
205 },
206 }; break;
207
208 case 'tty_wrap': binding = {
209 }; break;
210
211 case 'uv': binding = {
212 }; break;
213
214 default:
215 throw new Error('No such module: ' + name);
216 }
217
218 bindings[name] = binding;
219 return binding;
220 };
221
222 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
223 for (let i = 0; environ[i] != null; ++i) {
224 let assign = environ[i];
225 let equal = assign.indexOf('=');
226 let name = assign.substr(0, equal);
227 let value = assign.substr(equal + 1);
228 process.env[name.toString()] = value;
229 }
230
231 process.pid = getpid();
232
233 })();