From 7ab9e6779d33d38b29b313153a6b109574699617 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 10 Jan 2016 03:17:50 -0800 Subject: [PATCH] CYONify the contents of a JavaMethod override set. --- Java/Execute.cpp | 71 ++++++++++++++++++++++++++++++++---------------- JavaScript.hpp | 13 +++------ 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/Java/Execute.cpp b/Java/Execute.cpp index ee2d2be..98bf103 100644 --- a/Java/Execute.cpp +++ b/Java/Execute.cpp @@ -515,23 +515,28 @@ typedef std::map CYJavaOverloads; struct CYJavaMethod : CYRoot { + CYUTF8String type_; + CYUTF8String name_; CYJavaOverloads overloads_; - CYJavaMethod(const CYJavaOverloads &overloads) : + CYJavaMethod(CYUTF8String type, CYUTF8String name, const CYJavaOverloads &overloads) : + type_(CYPoolUTF8String(*pool_, type)), + name_(CYPoolUTF8String(*pool_, name)), overloads_(overloads) { } }; -struct CYJavaStaticMethod : - CYRoot +struct CYJavaInstanceMethod : + CYJavaMethod { - CYJavaOverloads overloads_; + using CYJavaMethod::CYJavaMethod; +}; - CYJavaStaticMethod(const CYJavaOverloads &overloads) : - overloads_(overloads) - { - } +struct CYJavaStaticMethod : + CYJavaMethod +{ + using CYJavaMethod::CYJavaMethod; }; struct CYJavaClass : @@ -743,7 +748,10 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef auto Class$(jni.FindClass("java/lang/Class")); auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;")); - CYJSString name(jni.CallObjectMethod(value, Class$getName)); + auto string(jni.CallObjectMethod(value, Class$getName)); + CYJavaUTF8String utf8(string); + CYJSString name(utf8); + JSValueRef cached(CYGetProperty(context, cy, name)); if (!JSValueIsUndefined(context, cached)) return CYCastJSObject(context, cached); @@ -848,12 +856,12 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef for (const auto &entry : entries) { bool instance(entry.first.first); - CYJSString name(entry.first.second); + CYJSString method(entry.first.second); auto &overload(entry.second); if (instance) - CYSetProperty(context, prototype, name, CYPrivate::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); + CYSetProperty(context, prototype, method, CYPrivate::Make(context, utf8, entry.first.second.c_str(), overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); else - CYSetProperty(context, constructor, name, CYPrivate::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); + CYSetProperty(context, constructor, method, CYPrivate::Make(context, utf8, entry.first.second.c_str(), overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); } } @@ -937,8 +945,8 @@ static bool CYCastJavaArguments(const CYJavaFrame &frame, const CYJavaShorty &sh return true; } -static JSValueRef JavaMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { - auto internal(CYPrivate::Get(context, object)); +static JSValueRef JavaInstanceMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + auto internal(CYPrivate::Get(context, object)); CYJavaObject *self(CYGetJavaObject(context, _this)); _assert(self != NULL); CYJavaEnv jni(self->value_); @@ -1186,12 +1194,22 @@ static JSValueRef JavaClass_callAsFunction_toCYON(JSContextRef context, JSObject } CYCatch(NULL) } static JSValueRef JavaMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + auto internal(CYPrivate::Get(context, _this)); std::ostringstream cyon; - return CYCastJSValue(context, CYJSString(cyon.str())); -} CYCatch(NULL) } - -static JSValueRef JavaStaticMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { - std::ostringstream cyon; + if (false) + cyon << internal->type_ << "." << internal->name_; + else { + bool comma(false); + for (auto overload(internal->overloads_.begin()); overload != internal->overloads_.end(); ++overload) + for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { + if (comma) + cyon << std::endl; + else + comma = true; + auto string(CYCastJavaString(signature->reflected_)); + cyon << CYJavaUTF8String(string); + } + } return CYCastJSValue(context, CYJSString(cyon.str())); } CYCatch(NULL) } @@ -1509,13 +1527,13 @@ static JSStaticValue JavaObject_staticValues[3] = { {NULL, NULL, NULL, 0} }; -static JSStaticFunction JavaMethod_staticFunctions[2] = { +static JSStaticFunction JavaInstanceMethod_staticFunctions[2] = { {"toCYON", &JavaMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; static JSStaticFunction JavaStaticMethod_staticFunctions[2] = { - {"toCYON", &JavaStaticMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toCYON", &JavaMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; @@ -1553,16 +1571,21 @@ CYJavaForEachPrimitive definition = kJSClassDefinitionEmpty; definition.className = "JavaMethod"; - definition.staticFunctions = JavaMethod_staticFunctions; - definition.callAsFunction = &JavaMethod_callAsFunction; definition.finalize = &CYFinalize; CYPrivate::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.className = "JavaInstanceMethod"; + definition.parentClass = CYPrivate::Class_; + definition.staticFunctions = JavaInstanceMethod_staticFunctions; + definition.callAsFunction = &JavaInstanceMethod_callAsFunction; + CYPrivate::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "JavaStaticMethod"; + definition.parentClass = CYPrivate::Class_; definition.staticFunctions = JavaStaticMethod_staticFunctions; definition.callAsFunction = &JavaStaticMethod_callAsFunction; - definition.finalize = &CYFinalize; CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; diff --git a/JavaScript.hpp b/JavaScript.hpp index 8b58024..fcefd7b 100644 --- a/JavaScript.hpp +++ b/JavaScript.hpp @@ -39,6 +39,7 @@ #include "Pooling.hpp" #include "String.hpp" +#include "Utility.hpp" extern JSStringRef Array_s; extern JSStringRef constructor_s; @@ -194,15 +195,9 @@ class CYJSString { { } - template - CYJSString(Arg0_ arg0) : - string_(CYCopyJSString(arg0)) - { - } - - template - CYJSString(Arg0_ arg0, Arg1_ arg1) : - string_(CYCopyJSString(arg0, arg1)) + template + CYJSString(Args_ &&... args) : + string_(CYCopyJSString(cy::Forward(args)...)) { } -- 2.45.2