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