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 + "'");
162 process.binding = function(name) {
163 let binding = bindings[name];
164 if (typeof binding != 'undefined')
168 case 'buffer': binding = {
173 case 'cares_wrap': binding = {
176 case 'constants': binding = {
179 case 'fs': binding = {
184 throw new Error("stat(" + arguments[0] + ")");
188 case 'pipe_wrap': binding = {
191 case 'smalloc': binding = {
196 case 'stream_wrap': binding = {
199 case 'tcp_wrap': binding = {
202 case 'timer_wrap': binding = {
208 case 'tty_wrap': binding = {
211 case 'uv': binding = {
215 throw new Error('No such module: ' + name);
218 bindings[name] = binding;
222 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
223 for (let i = 0; environ[i] != null; ++i) {
224 let assign = environ[i];
225 let equal = assign.indexOf('=');
226 let name = assign.substr(0, equal);
227 let value = assign.substr(equal + 1);
228 process.env[name.toString()] = value;
231 process.pid = getpid();