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