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