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],
45 extern "C" int access(const char *path, int amode);
46 extern "C" char *getcwd(char *buf, size_t size);
47 extern "C" int getpid();
49 $cy_set(Date.prototype, {
51 return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
55 $cy_set(Error.prototype, {
57 let stack = this.stack.split('\n');
58 if (stack.slice(-1)[0] == "global code")
59 stack = stack.slice(0, -1);
60 for (let i = 0; i != stack.length; ++i)
61 stack[i] = '\n ' + stack[i];
62 return `new ${this.constructor.name}(${this.message.toCYON()}) /*${stack.join('')} */`;
66 let IsFile = function(path) {
67 // XXX: this doesn't work on symlinks, but I don't want to fix stat :/
68 return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1;
71 let StartsWith = function(lhs, rhs) {
72 return lhs.substring(0, rhs.length) == rhs;
75 let ResolveFile = function(exact, name) {
76 if (exact && IsFile(name))
78 for (let suffix of ['.js', '.json'])
79 if (IsFile(name + suffix))
85 let GetLibraryPath = function() {
86 let handle = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
91 let CYHandleServer = dlsym(handle, "CYHandleServer");
92 if (CYHandleServer == null)
95 let info = new Dl_info;
96 if (dladdr(CYHandleServer, info) == 0)
99 let path = info->dli_fname;
100 let slash = path.lastIndexOf('/');
104 path = path.substr(0, slash);
106 GetLibraryPath = function() {
110 return GetLibraryPath();
116 let ResolveFolder = function(name) {
117 if (access(name + '/', F_OK) == -1)
120 if (IsFile(name + "/package.json")) {
121 let package = require(name + "/package.json");
122 let path = ResolveFile(true, name + "/" + package.main);
127 return ResolveFile(false, name + "/index");
130 let ResolveEither = function(name) {
133 path = ResolveFile(true, name);
135 path = ResolveFolder(name);
139 require.resolve = function(name) {
140 if (StartsWith(name, '/')) {
141 let path = ResolveEither(name);
145 let cwd = new (typedef char[1024]);
146 cwd = getcwd(cwd, cwd.length).toString();
147 cwd = cwd.split('/');
149 if (StartsWith(name, './') || StartsWith(name, '../')) {
150 let path = ResolveEither(cwd + '/' + name);
154 for (let i = cwd.length; i != 0; --i) {
155 let modules = cwd.slice(0, i).concat("node_modules").join('/');
156 let path = ResolveEither(modules + "/" + name);
161 let library = GetLibraryPath();
162 let path = ResolveFile(true, library + "/cycript0.9/" + name + ".cy");
168 throw new Error("Cannot find module '" + name + "'");
173 process.binding = function(name) {
174 let binding = bindings[name];
175 if (typeof binding != 'undefined')
179 case 'buffer': binding = {
184 case 'cares_wrap': binding = {
187 case 'constants': binding = {
190 case 'fs': binding = {
195 throw new Error("stat(" + arguments[0] + ")");
199 case 'pipe_wrap': binding = {
202 case 'smalloc': binding = {
207 case 'stream_wrap': binding = {
210 case 'tcp_wrap': binding = {
213 case 'timer_wrap': binding = {
219 case 'tty_wrap': binding = {
222 case 'uv': binding = {
226 throw new Error('No such module: ' + name);
229 bindings[name] = binding;
233 let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ"));
234 for (let i = 0; environ[i] != null; ++i) {
235 let assign = environ[i];
236 let equal = assign.indexOf('=');
237 let name = assign.substr(0, equal);
238 let value = assign.substr(equal + 1);
239 process.env[name.toString()] = value;
242 process.pid = getpid();