return true;
} CYCatch(false) }
+static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env);
+
static JSValueRef JavaPackage_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
auto internal(CYJavaPackage::Get(context, _this));
std::ostringstream name;
name << package << '/';
name << next;
+ if (internal->jni_ == NULL)
+ GetJNI(context, internal->jni_);
JNIEnv *jni(internal->jni_);
+
if (auto _class = jni->FindClass(name.str().c_str()))
return CYGetJavaClass(context, CYJavaLocal<jclass>(jni, _class));
jni->ExceptionClear();
return jvm;
}
-static JNIEnv *GetJNI(JSContextRef context) {
+static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env) {
auto jvm(CYGetJavaVM(context));
- if (jvm == NULL)
- return NULL;
-
- JNIEnv *env;
+ _assert(jvm != NULL);
_jnicall(jvm->GetEnv(reinterpret_cast<void **>(&env), CYJavaVersion));
CYJavaEnv jni(env);
+ // XXX: this happens once per stub :/
+
auto Cycript$(jni.FindClass("Cycript"));
jni.RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0]));
+ JSObjectRef java(CYGetCachedObject(context, CYJSString("Java")));
+ JSValueRef arguments[1];
+ arguments[0] = CYCastJSValue(context, CYJSString("setup"));
+ CYCallAsFunction(context, CYCastJSObject(context, CYGetProperty(context, java, CYJSString("emit"))), java, 1, arguments);
+
return env;
}
}
void CYJava_SetupContext(JSContextRef context) {
- JNIEnv *jni(GetJNI(context));
- if (jni == NULL)
- return;
-
JSObjectRef global(CYGetGlobalObject(context));
- //JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
+ JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all"))));
//JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
JSObjectRef Java(JSObjectMake(context, NULL, NULL));
CYSetProperty(context, cycript, CYJSString("Java"), Java);
+ CYSetProperty(context, cy, CYJSString("Java"), Java);
- JSObjectRef Packages(CYJavaPackage::Make(context, jni, CYJavaPackage::Path()));
- CYSetProperty(context, all, CYJSString("Packages"), Packages);
+ JSObjectRef Packages(CYJavaPackage::Make(context, nullptr, CYJavaPackage::Path()));
+ CYSetProperty(context, all, CYJSString("Packages"), Packages, kJSPropertyAttributeDontEnum);
for (auto name : (const char *[]) {"java", "javax", "android", "com", "net", "org"}) {
CYJSString js(name);
- CYSetProperty(context, all, js, CYGetProperty(context, Packages, js));
+ CYSetProperty(context, all, js, CYJavaPackage::Make(context, nullptr, CYJavaPackage::Path(1, name)), kJSPropertyAttributeDontEnum);
}
}
},
});
+// 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() {
+ system.print("JVM\n");
$cy_set(java.lang.Boolean.prototype, {
toCYON: function() {
return `new java.lang.Boolean(${this->value})`;
this.put(key, value);
},
});
+});
+
}
if ("ObjectiveC" in Cycript) {