X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/19e13c2de7065201c9a5a54e9a1a5409ed4a4cdc..447b698cea96010d0850e056348ef6cd085b96d9:/libcycript.cy diff --git a/libcycript.cy b/libcycript.cy index 79a1735..6f798ea 100644 --- a/libcycript.cy +++ b/libcycript.cy @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2015 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -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,127 @@ $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})`; + }, + }); + + $cy_set(java.lang.Object.prototype, { + toCYON: function(key) { + return "#" + this.toString().toCYON(); + }, + + // XXX: due to lack of interface prototypes :( + $cyg: function(key) { + return this.get(key); + }, + + // XXX: due to lack of interface prototypes :( + $cys: function(key, value) { + if ("set" in this) + this.set(key, value); + else + this.put(key, value); + }, + }); +} + +if ("ObjectiveC" in Cycript) { + $cy_set(NSArray.prototype, { + $cyg: function(key) { + return objc_msgSend(this, "objectAtIndex:", key); + }, + + $cys: function(key, value) { + return objc_msgSend(this, "setObject:atIndex:", value, key); + }, + }); + + $cy_set(NSDictionary.prototype, { + $cyg: function(key) { + return objc_msgSend(this, "objectForKey:", key); + }, + + $cys: function(key, value) { + return objc_msgSend(this, "setObject:forKey:", value, key); + }, + }); +} + 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; @@ -275,6 +397,11 @@ for (let i = 0; environ[i] != null; ++i) { process.env[name.toString()] = value; } +process.cwd = function() { + let cwd = new (typedef char[1024]); + return getcwd(cwd, cwd.length).toString(); +}; + process.pid = getpid(); })();