1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
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.
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/>.
28 let $cy_set = function(object, properties) {
29 for (const name in properties)
30 Object.defineProperty(object, name, {
34 value: properties[name],
38 $cy_set(Boolean.prototype, {
40 return `new Boolean(${this.toString()})`;
44 $cy_set(Date.prototype, {
46 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
50 $cy_set(Error.prototype, {
52 let stack = this.stack;
53 if (typeof stack == 'undefined')
56 stack = stack.split('\n');
57 if (stack.slice(-1)[0] == "global code")
58 stack = stack.slice(0, -1);
59 for (let i = 0; i != stack.length; ++i)
60 stack[i] = '\n ' + stack[i];
61 stack = stack.join('');
62 stack = ` /*${stack} */`;
64 return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`;
68 $cy_set(Number.prototype, {
70 return `new Number(${this.toString()})`;
74 $cy_set(RegExp.prototype, {
76 return this.toString();
80 let IsFile = function(path) {
81 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
82 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
85 let StartsWith = function(lhs, rhs) {
86 return lhs.substring(0, rhs.length) == rhs;
89 let ResolveFile = function(exact, name) {
90 if (exact && IsFile(name))
92 for (let suffix of ['.js', '.json'])
93 if (IsFile(name + suffix))
99 let GetLibraryPath = function() {
100 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
105 let CYHandleServer = dlsym(handle, "CYHandleServer");
106 if (CYHandleServer == null)
109 let info = new Dl_info;
110 if (dladdr(CYHandleServer, info) == 0)
113 let path = info->dli_fname;
114 let slash = path.lastIndexOf('/');
118 path = path.substr(0, slash);
120 GetLibraryPath = function() {
124 return GetLibraryPath();
130 let ResolveFolder = function(name) {
131 if (access(name + '/', F_OK) == -1)
134 if (IsFile(name + "/package.json")) {
135 let package = require(name + "/package.json");
136 let path = ResolveFile(true, name + "/" + package.main);
141 return ResolveFile(false, name + "/index");
144 let ResolveEither = function(name) {
147 path = ResolveFile(true, name);
149 path = ResolveFolder(name);
153 require.resolve = function(name) {
154 if (StartsWith(name, '/')) {
155 let path = ResolveEither(name);
159 let cwd = new (typedef char[1024]);
160 cwd = getcwd(cwd, cwd.length).toString();
161 cwd = cwd.split('/');
163 if (StartsWith(name, './') || StartsWith(name, '../')) {
164 let path = ResolveEither(cwd + '/' + name);
168 for (let i = cwd.length; i != 0; --i) {
169 let modules = cwd.slice(0, i).concat("node_modules").join('/');
170 let path = ResolveEither(modules + "/" + name);
175 let library = GetLibraryPath();
176 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
182 throw new Error("Cannot find module '" + name + "'");
185 var _syscall = function(value) {
187 throw new Error(strerror(errno));
190 var info = *new (struct stat);
192 } else if ("st_atim" in info) {
193 var st_atime = "st_atim";
194 var st_mtime = "st_mtim";
195 var st_ctime = "st_ctim";
196 } else if ("st_atimespec" in info) {
197 var st_atime = "st_atimespec";
198 var st_mtime = "st_mtimespec";
199 var st_ctime = "st_ctimespec";
201 var st_atime = "st_atime";
202 var st_mtime = "st_mtime";
203 var st_ctime = "st_ctime";
206 var toDate = function(timespec) {
207 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
212 process.binding = function(name) {
213 let binding = bindings[name];
214 if (typeof binding != 'undefined')
218 case 'buffer': binding = {
223 case 'cares_wrap': binding = {
226 case 'constants': binding = {
229 case 'fs': binding = {
234 var info = new (struct stat);
235 _syscall(lstat(path, info));
240 nlink: info->st_nlink,
244 blksize: info->st_blksize,
247 blocks: info->st_blocks,
249 atime: toDate(info->[st_atime]),
250 mtime: toDate(info->[st_mtime]),
251 ctime: toDate(info->[st_ctime]),
254 return S_ISLNK(this.mode);
260 case 'pipe_wrap': binding = {
263 case 'smalloc': binding = {
268 case 'stream_wrap': binding = {
271 case 'tcp_wrap': binding = {
274 case 'timer_wrap': binding = {
280 case 'tty_wrap': binding = {
283 case 'uv': binding = {
287 throw new Error('No such module: ' + name);
290 bindings[name] = binding;
294 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
295 for (let i = 0; environ[i] != null; ++i) {
296 let assign = environ[i];
297 let equal = assign.indexOf('=');
298 let name = assign.substr(0, equal);
299 let value = assign.substr(equal + 1);
300 process.env[name.toString()] = value;
303 process.pid = getpid();