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(Date.prototype, {
40 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
44 $cy_set(Error.prototype, {
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('')} */`;
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;
60 let StartsWith = function(lhs, rhs) {
61 return lhs.substring(0, rhs.length) == rhs;
64 let ResolveFile = function(exact, name) {
65 if (exact && IsFile(name))
67 for (let suffix of ['.js', '.json'])
68 if (IsFile(name + suffix))
74 let GetLibraryPath = function() {
75 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
80 let CYHandleServer = dlsym(handle, "CYHandleServer");
81 if (CYHandleServer == null)
84 let info = new Dl_info;
85 if (dladdr(CYHandleServer, info) == 0)
88 let path = info->dli_fname;
89 let slash = path.lastIndexOf('/');
93 path = path.substr(0, slash);
95 GetLibraryPath = function() {
99 return GetLibraryPath();
105 let ResolveFolder = function(name) {
106 if (access(name + '/', F_OK) == -1)
109 if (IsFile(name + "/package.json")) {
110 let package = require(name + "/package.json");
111 let path = ResolveFile(true, name + "/" + package.main);
116 return ResolveFile(false, name + "/index");
119 let ResolveEither = function(name) {
122 path = ResolveFile(true, name);
124 path = ResolveFolder(name);
128 require.resolve = function(name) {
129 if (StartsWith(name, '/')) {
130 let path = ResolveEither(name);
134 let cwd = new (typedef char[1024]);
135 cwd = getcwd(cwd, cwd.length).toString();
136 cwd = cwd.split('/');
138 if (StartsWith(name, './') || StartsWith(name, '../')) {
139 let path = ResolveEither(cwd + '/' + name);
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);
150 let library = GetLibraryPath();
151 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
157 throw new Error("Cannot find module '" + name + "'");
160 var _syscall = function(value) {
162 throw new Error("system call failed");
165 var info = *new (struct stat);
167 } else if ("st_atim" in info) {
168 var st_atime = "st_atim";
169 var st_mtime = "st_mtim";
170 var st_ctime = "st_ctim";
171 } else if ("st_atimespec" in info) {
172 var st_atime = "st_atimespec";
173 var st_mtime = "st_mtimespec";
174 var st_ctime = "st_ctimespec";
176 var st_atime = "st_atime";
177 var st_mtime = "st_mtime";
178 var st_ctime = "st_ctime";
181 var toDate = function(timespec) {
182 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
187 process.binding = function(name) {
188 let binding = bindings[name];
189 if (typeof binding != 'undefined')
193 case 'buffer': binding = {
198 case 'cares_wrap': binding = {
201 case 'constants': binding = {
204 case 'fs': binding = {
209 var info = new (struct stat);
210 _syscall(lstat(path, info));
215 nlink: info->st_nlink,
219 blksize: info->st_blksize,
222 blocks: info->st_blocks,
224 atime: toDate(info->[st_atime]),
225 mtime: toDate(info->[st_mtime]),
226 ctime: toDate(info->[st_ctime]),
229 return S_ISLNK(this.mode);
235 case 'pipe_wrap': binding = {
238 case 'smalloc': binding = {
243 case 'stream_wrap': binding = {
246 case 'tcp_wrap': binding = {
249 case 'timer_wrap': binding = {
255 case 'tty_wrap': binding = {
258 case 'uv': binding = {
262 throw new Error('No such module: ' + name);
265 bindings[name] = binding;
269 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
270 for (let i = 0; environ[i] != null; ++i) {
271 let assign = environ[i];
272 let equal = assign.indexOf('=');
273 let name = assign.substr(0, equal);
274 let value = assign.substr(equal + 1);
275 process.env[name.toString()] = value;
278 process.pid = getpid();