X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/7752205a17e5ea74ed609e029169a633c25d7621..dbf05bfd2ab79568fe706daa9112037192d4d9dd:/libcycript.cy diff --git a/libcycript.cy b/libcycript.cy index 20a1017..88fe069 100644 --- a/libcycript.cy +++ b/libcycript.cy @@ -25,6 +25,10 @@ var process = { (function() { +this.typeid = function(object) { + return object.$cyt; +}; + let $cy_set = function(object, properties) { for (const name in properties) Object.defineProperty(object, name, { @@ -35,6 +39,12 @@ let $cy_set = function(object, properties) { }); }; +$cy_set(Boolean.prototype, { + toCYON: function() { + return `new Boolean(${this.toString()})`; + }, +}); + $cy_set(Date.prototype, { toCYON: function() { return `new ${this.constructor.name}(${this.toUTCString().toCYON()})`; @@ -43,15 +53,86 @@ $cy_set(Date.prototype, { $cy_set(Error.prototype, { toCYON: function() { - 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('')} */`; + let stack = this.stack; + if (typeof stack == 'undefined') + stack = ''; + else { + stack = 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]; + stack = stack.join(''); + stack = ` /*${stack} */`; + } + return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`; + }, +}); + +$cy_set(Number.prototype, { + toCYON: function() { + if ("$cyt" in this) + return `${this.$cyt.toCYON()}(${this.toString()})`; + return `new Number(${this.toString()})`; }, }); +$cy_set(RegExp.prototype, { + toCYON: function() { + return this.toString(); + }, +}); + +if ("Java" in Cycript) { + $cy_set(java.lang.Boolean.prototype, { + toCYON: function() { + return `new java.lang.Boolean(${this.value})`; + }, + }); + + $cy_set(java.lang.Byte.prototype, { + toCYON: function() { + return `new java.lang.Byte(${this.value})`; + }, + }); + + $cy_set(java.lang.Character.prototype, { + toCYON: function() { + return `new java.lang.Character(${this.value})`; + }, + }); + + $cy_set(java.lang.Short.prototype, { + toCYON: function() { + return `new java.lang.Short(${this.value})`; + }, + }); + + $cy_set(java.lang.Integer.prototype, { + toCYON: function() { + return `new java.lang.Integer(${this.value})`; + }, + }); + + $cy_set(java.lang.Long.prototype, { + toCYON: function() { + return `new java.lang.Long(${this.value})`; + }, + }); + + $cy_set(java.lang.Float.prototype, { + toCYON: function() { + return `new java.lang.Float(${this.value})`; + }, + }); + + $cy_set(java.lang.Double.prototype, { + toCYON: function() { + return `new java.lang.Double(${this.value})`; + }, + }); +} + let IsFile = function(path) { // XXX: this doesn't work on symlinks, but I don't want to fix stat :/ return access(path, F_OK) == 0 && access(path + '/', F_OK) == -1; @@ -157,6 +238,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) { @@ -180,8 +286,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;