]> git.saurik.com Git - cycript.git/blobdiff - libcycript.cy
Provide access to errno (though not from console).
[cycript.git] / libcycript.cy
index 6a751aa38ddd2a2ebc7c52e1bda835d67c612d87..79a1735279be94a6e900f1af24ca3b47f50c0d92 100644 (file)
@@ -1,3 +1,24 @@
+/* Cycript - Optimizing JavaScript Compiler/Runtime
+ * Copyright (C) 2009-2015  Jay Freeman (saurik)
+*/
+
+/* GNU Affero General Public License, Version 3 {{{ */
+/*
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+**/
+/* }}} */
+
 var process = {
     env: {},
 };
@@ -14,17 +35,6 @@ let $cy_set = function(object, properties) {
         });
 };
 
-const F_OK = 0;
-const X_OK = (1<<0);
-const W_OK = (1<<1);
-const R_OK = (1<<2);
-
-typedef long size_t;
-
-extern "C" int access(const char *path, int amode);
-extern "C" char *getcwd(char *buf, size_t size);
-extern "C" int getpid();
-
 $cy_set(Date.prototype, {
     toCYON: function() {
         return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`;
@@ -33,7 +43,12 @@ $cy_set(Date.prototype, {
 
 $cy_set(Error.prototype, {
     toCYON: function() {
-        return `new ${this.constructor.name}(${this.message.toCYON()})`;
+        let stack = this.stack.split('\n');
+        if (stack.slice(-1)[0] == "global code")
+            stack = stack.slice(0, -1);
+        for (let i = 0; i != stack.length; ++i)
+            stack[i] = '\n    ' + stack[i];
+        return `new ${this.constructor.name}(${this.message.toCYON()}) /*${stack.join('')} */`;
     },
 });
 
@@ -75,7 +90,13 @@ let GetLibraryPath = function() {
         if (slash == -1)
             return null;
 
-        return path.substr(0, slash);
+        path = path.substr(0, slash);
+
+        GetLibraryPath = function() {
+            return path;
+        };
+
+        return GetLibraryPath();
     } finally {
         dlclose(handle);
     }
@@ -136,6 +157,31 @@ require.resolve = function(name) {
     throw new Error("Cannot find module '" + name + "'");
 };
 
+var _syscall = function(value) {
+    if (value == -1)
+        throw new Error(strerror(errno));
+};
+
+var info = *new (struct stat);
+if (false) {
+} else if ("st_atim" in info) {
+    var st_atime = "st_atim";
+    var st_mtime = "st_mtim";
+    var st_ctime = "st_ctim";
+} else if ("st_atimespec" in info) {
+    var st_atime = "st_atimespec";
+    var st_mtime = "st_mtimespec";
+    var st_ctime = "st_ctimespec";
+} else {
+    var st_atime = "st_atime";
+    var st_mtime = "st_mtime";
+    var st_ctime = "st_ctime";
+}
+
+var toDate = function(timespec) {
+    return new Date(timespec.tv_sec * 1000 + timespec.tv_nsec / 1000);
+};
+
 var bindings = {};
 
 process.binding = function(name) {
@@ -159,8 +205,30 @@ process.binding = function(name) {
             FSInitialize() {
             },
 
-            lstat() {
-                throw new Error("stat(" + arguments[0] + ")");
+            lstat(path) {
+                var info = new (struct stat);
+                _syscall(lstat(path, info));
+
+                return {
+                    dev: info->st_dev,
+                    mode: info->st_mode,
+                    nlink: info->st_nlink,
+                    uid: info->st_uid,
+                    gid: info->st_gid,
+                    rdev: info->st_rdev,
+                    blksize: info->st_blksize,
+                    ino: info->st_ino,
+                    size: info->st_size,
+                    blocks: info->st_blocks,
+
+                    atime: toDate(info->[st_atime]),
+                    mtime: toDate(info->[st_mtime]),
+                    ctime: toDate(info->[st_ctime]),
+
+                    isSymbolicLink() {
+                        return S_ISLNK(this.mode);
+                    },
+                };
             },
         }; break;