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.split('\n');
53 if (stack.slice(-1)[0] == "global code")
54 stack = stack.slice(0, -1);
55 for (let i = 0; i != stack.length; ++i)
56 stack[i] = '\n ' + stack[i];
57 return `new ${this.constructor.name}(${this.message.toCYON()}) /*${stack.join('')} */`;
61 $cy_set(Number.prototype, {
63 return `new Number(${this.toString()})`;
67 $cy_set(RegExp.prototype, {
69 return this.toString();
73 let IsFile = function(path) {
74 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
75 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
78 let StartsWith = function(lhs, rhs) {
79 return lhs.substring(0, rhs.length) == rhs;
82 let ResolveFile = function(exact, name) {
83 if (exact && IsFile(name))
85 for (let suffix of ['.js', '.json'])
86 if (IsFile(name + suffix))
92 let GetLibraryPath = function() {
93 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
98 let CYHandleServer = dlsym(handle, "CYHandleServer");
99 if (CYHandleServer == null)
102 let info = new Dl_info;
103 if (dladdr(CYHandleServer, info) == 0)
106 let path = info->dli_fname;
107 let slash = path.lastIndexOf('/');
111 path = path.substr(0, slash);
113 GetLibraryPath = function() {
117 return GetLibraryPath();
123 let ResolveFolder = function(name) {
124 if (access(name + '/', F_OK) == -1)
127 if (IsFile(name + "/package.json")) {
128 let package = require(name + "/package.json");
129 let path = ResolveFile(true, name + "/" + package.main);
134 return ResolveFile(false, name + "/index");
137 let ResolveEither = function(name) {
140 path = ResolveFile(true, name);
142 path = ResolveFolder(name);
146 require.resolve = function(name) {
147 if (StartsWith(name, '/')) {
148 let path = ResolveEither(name);
152 let cwd = new (typedef char[1024]);
153 cwd = getcwd(cwd, cwd.length).toString();
154 cwd = cwd.split('/');
156 if (StartsWith(name, './') || StartsWith(name, '../')) {
157 let path = ResolveEither(cwd + '/' + name);
161 for (let i = cwd.length; i != 0; --i) {
162 let modules = cwd.slice(0, i).concat("node_modules").join('/');
163 let path = ResolveEither(modules + "/" + name);
168 let library = GetLibraryPath();
169 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
175 throw new Error("Cannot find module '" + name + "'");
178 var _syscall = function(value) {
180 throw new Error(strerror(errno));
183 var info = *new (struct stat);
185 } else if ("st_atim" in info) {
186 var st_atime = "st_atim";
187 var st_mtime = "st_mtim";
188 var st_ctime = "st_ctim";
189 } else if ("st_atimespec" in info) {
190 var st_atime = "st_atimespec";
191 var st_mtime = "st_mtimespec";
192 var st_ctime = "st_ctimespec";
194 var st_atime = "st_atime";
195 var st_mtime = "st_mtime";
196 var st_ctime = "st_ctime";
199 var toDate = function(timespec) {
200 return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
205 process.binding = function(name) {
206 let binding = bindings[name];
207 if (typeof binding != 'undefined')
211 case 'buffer': binding = {
216 case 'cares_wrap': binding = {
219 case 'constants': binding = {
222 case 'fs': binding = {
227 var info = new (struct stat);
228 _syscall(lstat(path, info));
233 nlink: info->st_nlink,
237 blksize: info->st_blksize,
240 blocks: info->st_blocks,
242 atime: toDate(info->[st_atime]),
243 mtime: toDate(info->[st_mtime]),
244 ctime: toDate(info->[st_ctime]),
247 return S_ISLNK(this.mode);
253 case 'pipe_wrap': binding = {
256 case 'smalloc': binding = {
261 case 'stream_wrap': binding = {
264 case 'tcp_wrap': binding = {
267 case 'timer_wrap': binding = {
273 case 'tty_wrap': binding = {
276 case 'uv': binding = {
280 throw new Error('No such module: ' + name);
283 bindings[name] = binding;
287 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
288 for (let i = 0; environ[i] != null; ++i) {
289 let assign = environ[i];
290 let equal = assign.indexOf('=');
291 let name = assign.substr(0, equal);
292 let value = assign.substr(equal + 1);
293 process.env[name.toString()] = value;
296 process.pid = getpid();