X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/8effd3814c3f69bc2628345eef1accd89e936ddc..925360811f44b121001797130ca89fc777072ae7:/libcycript.cy?ds=inline diff --git a/libcycript.cy b/libcycript.cy index f5f63ea..8240a44 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 {{{ */ @@ -19,10 +19,6 @@ **/ /* }}} */ -var process = { - env: {}, -}; - (function() { this.typeid = function(object) { @@ -62,8 +58,12 @@ $cy_set(Error.prototype, { stack = stack.slice(0, -1); for (let i = 0; i != stack.length; ++i) stack[i] = '\n ' + stack[i]; - stack = stack.join(''); - stack = ` /*${stack} */`; + if (stack.length == 0) + stack = ''; + else { + stack = stack.join(''); + stack = ` /*${stack} */`; + } } return `new ${this.constructor.name}(${this.message.toCYON()})${stack}`; }, @@ -83,7 +83,41 @@ $cy_set(RegExp.prototype, { }, }); +// XXX: Java should just always be available if ("Java" in Cycript) { + +// XXX: this is a half-assed EventEmitter +// XXX: this doesn't even have the same semantics + +Java.handlers_ = {}; + +Java.on = function(event, handler) { + var handlers; + if (event in this.handlers_) + handlers = this.handlers_[event]; + else { + handlers = []; + this.handlers_[event] = handlers; + } + + if (this.handlers_ == null) + handler(); + else + handlers.push(handler); +}; + +Java.emit = function(event) { + if (event in this.handlers_) { + var handlers = this.handlers_[event]; + if (handlers != null) + for (var handler of handlers) + handler(); + } + + this.handlers_[event] = null; +}; + +Java.on('setup', function() { $cy_set(java.lang.Boolean.prototype, { toCYON: function() { return `new java.lang.Boolean(${this->value})`; @@ -133,6 +167,10 @@ if ("Java" in Cycript) { }); $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); @@ -146,6 +184,28 @@ if ("Java" in Cycript) { this.put(key, value); }, }); + + $cy_set(java.lang.Throwable.prototype, { + toCYON: function() { + var message = this.getMessage(); + if (message == null) + message = ''; + else + message = message.toCYON(); + + let stack = this.getStackTrace(); + if (stack.length == 0) + stack = ''; + else { + stack = stack.join('\n '); + stack = ` /*\n ${stack} */`; + } + + return `new ${this.constructor.class.getName()}(${message})${stack}`; + }, + }); +}); + } if ("ObjectiveC" in Cycript) { @@ -384,6 +444,8 @@ process.binding = function(name) { return binding; }; +process.env = {}; + let environ = *(typedef char ***)(dlsym(RTLD_DEFAULT, "environ")); for (let i = 0; environ[i] != null; ++i) { let assign = environ[i]; @@ -393,6 +455,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(); })();