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],
42 $cy_set(Date.prototype, {
44 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
48 $cy_set(Error.prototype, {
50 let stack = this.stack.split('\n');
51 if (stack.slice(-1)[0] == "global code")
52 stack = stack.slice(0, -1);
53 for (let i = 0; i != stack.length; ++i)
54 stack[i] = '\n ' + stack[i];
55 return `new ${this.constructor.name}(${this.message.toCYON()}) /*${stack.join('')} */`;
59 let IsFile = function(path) {
60 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
61 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
64 let StartsWith = function(lhs, rhs) {
65 return lhs.substring(0, rhs.length) == rhs;
68 let ResolveFile = function(exact, name) {
69 if (exact && IsFile(name))
71 for (let suffix of ['.js', '.json'])
72 if (IsFile(name + suffix))
78 let GetLibraryPath = function() {
79 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
84 let CYHandleServer = dlsym(handle, "CYHandleServer");
85 if (CYHandleServer == null)
88 let info = new Dl_info;
89 if (dladdr(CYHandleServer, info) == 0)
92 let path = info->dli_fname;
93 let slash = path.lastIndexOf('/');
97 path = path.substr(0, slash);
99 GetLibraryPath = function() {
103 return GetLibraryPath();
109 let ResolveFolder = function(name) {
110 if (access(name + '/', F_OK) == -1)
113 if (IsFile(name + "/package.json")) {
114 let package = require(name + "/package.json");
115 let path = ResolveFile(true, name + "/" + package.main);
120 return ResolveFile(false, name + "/index");
123 let ResolveEither = function(name) {
126 path = ResolveFile(true, name);
128 path = ResolveFolder(name);
132 require.resolve = function(name) {
133 if (StartsWith(name, '/')) {
134 let path = ResolveEither(name);
138 let cwd = new (typedef char[1024]);
139 cwd = getcwd(cwd, cwd.length).toString();
140 cwd = cwd.split('/');
142 if (StartsWith(name, './') || StartsWith(name, '../')) {
143 let path = ResolveEither(cwd + '/' + name);
147 for (let i = cwd.length; i != 0; --i) {
148 let modules = cwd.slice(0, i).concat("node_modules").join('/');
149 let path = ResolveEither(modules + "/" + name);
154 let library = GetLibraryPath();
155 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
161 throw new Error("Cannot find module '" + name + "'");
166 process.binding = function(name) {
167 let binding = bindings[name];
168 if (typeof binding != 'undefined')
172 case 'buffer': binding = {
177 case 'cares_wrap': binding = {
180 case 'constants': binding = {
183 case 'fs': binding = {
188 throw new Error("stat(" + arguments[0] + ")");
192 case 'pipe_wrap': binding = {
195 case 'smalloc': binding = {
200 case 'stream_wrap': binding = {
203 case 'tcp_wrap': binding = {
206 case 'timer_wrap': binding = {
212 case 'tty_wrap': binding = {
215 case 'uv': binding = {
219 throw new Error('No such module: ' + name);
222 bindings[name] = binding;
226 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
227 for (let i = 0; environ[i] != null; ++i) {
228 let assign = environ[i];
229 let equal = assign.indexOf('=');
230 let name = assign.substr(0, equal);
231 let value = assign.substr(equal + 1);
232 process.env[name.toString()] = value;
235 process.pid = getpid();