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 this.typeid = function(object) {
32 let $cy_set = function(object, properties) {
33 for (const name in properties)
34 Object.defineProperty(object, name, {
38 value: properties[name],
42 $cy_set(Boolean.prototype, {
44 return `new Boolean(${this.toString()})`;
48 $cy_set(Date.prototype, {
50 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
54 $cy_set(Error.prototype, {
56 let stack = this.stack;
57 if (typeof stack == 'undefined')
60 stack = stack.split('\n');
61 if (stack.slice(-1)[0] == "global code")
62 stack = stack.slice(0, -1);
63 for (let i = 0; i != stack.length; ++i)
64 stack[i] = '\n ' + stack[i];
65 stack = stack.join('');
66 stack = ` /*${stack} */`;
68 return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`;
72 $cy_set(Number.prototype, {
75 return `${this.$cyt.toCYON()}(${this.toString()})`;
76 return `new Number(${this.toString()})`;
80 $cy_set(RegExp.prototype, {
82 return this.toString();
86 let IsFile = function(path) {
87 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
88 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
91 let StartsWith = function(lhs, rhs) {
92 return lhs.substring(0, rhs.length) == rhs;
95 let ResolveFile = function(exact, name) {
96 if (exact && IsFile(name))
98 for (let suffix of ['.js', '.json'])
99 if (IsFile(name + suffix))
100 return name + suffix;
105 let GetLibraryPath = function() {
106 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
111 let CYHandleServer = dlsym(handle, "CYHandleServer");
112 if (CYHandleServer == null)
115 let info = new Dl_info;
116 if (dladdr(CYHandleServer, info) == 0)
119 let path = info->dli_fname;
120 let slash = path.lastIndexOf('/');
124 path = path.substr(0, slash);
126 GetLibraryPath = function() {
130 return GetLibraryPath();
136 let ResolveFolder = function(name) {
137 if (access(name + '/', F_OK) == -1)
140 if (IsFile(name + "/package.json")) {
141 let package = require(name + "/package.json");
142 let path = ResolveFile(true, name + "/" + package.main);
147 return ResolveFile(false, name + "/index");
150 let ResolveEither = function(name) {
153 path = ResolveFile(true, name);
155 path = ResolveFolder(name);
159 require.resolve = function(name) {
160 if (StartsWith(name, '/')) {
161 let path = ResolveEither(name);
165 let cwd = new (typedef char[1024]);
166 cwd = getcwd(cwd, cwd.length).toString();
167 cwd = cwd.split('/');
169 if (StartsWith(name, './') || StartsWith(name, '../')) {
170 let path = ResolveEither(cwd + '/' + name);
174 for (let i = cwd.length; i != 0; --i) {
175 let modules = cwd.slice(0, i).concat("node_modules").join('/');
176 let path = ResolveEither(modules + "/" + name);
181 let library = GetLibraryPath();
182 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
188 throw new Error("Cannot find module '" + name + "'");
191 var _syscall = function(value) {
193 throw new Error(strerror(errno));
196 var info = *new (struct stat);
198 } else if ("st_atim" in info) {
199 var st_atime = "st_atim";
200 var st_mtime = "st_mtim";
201 var st_ctime = "st_ctim";
202 } else if ("st_atimespec" in info) {
203 var st_atime = "st_atimespec";
204 var st_mtime = "st_mtimespec";
205 var st_ctime = "st_ctimespec";
207 var st_atime = "st_atime";
208 var st_mtime = "st_mtime";
209 var st_ctime = "st_ctime";
212 var toDate = function(timespec) {
213 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
218 process.binding = function(name) {
219 let binding = bindings[name];
220 if (typeof binding != 'undefined')
224 case 'buffer': binding = {
229 case 'cares_wrap': binding = {
232 case 'constants': binding = {
235 case 'fs': binding = {
240 var info = new (struct stat);
241 _syscall(lstat(path, info));
246 nlink: info->st_nlink,
250 blksize: info->st_blksize,
253 blocks: info->st_blocks,
255 atime: toDate(info->[st_atime]),
256 mtime: toDate(info->[st_mtime]),
257 ctime: toDate(info->[st_ctime]),
260 return S_ISLNK(this.mode);
266 case 'pipe_wrap': binding = {
269 case 'smalloc': binding = {
274 case 'stream_wrap': binding = {
277 case 'tcp_wrap': binding = {
280 case 'timer_wrap': binding = {
286 case 'tty_wrap': binding = {
289 case 'uv': binding = {
293 throw new Error('No such module: ' + name);
296 bindings[name] = binding;
300 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
301 for (let i = 0; environ[i] != null; ++i) {
302 let assign = environ[i];
303 let equal = assign.indexOf('=');
304 let name = assign.substr(0, equal);
305 let value = assign.substr(equal + 1);
306 process.env[name.toString()] = value;
309 process.pid = getpid();