#include <sstream>
#include <vector>
-#ifdef __APPLE__
+#include <dlfcn.h>
+
+#if defined(__APPLE__) && !defined(__arm__)
#include <JavaVM/jni.h>
#else
#include <jni.h>
#endif
+#ifdef __ANDROID__
+// XXX: this is deprecated?!?!?!?!?!?!
+#include <sys/system_properties.h>
+#endif
+
#include "cycript.hpp"
#include "Error.hpp"
#include "Execute.hpp"
return value; \
}
-extern "C" {
- // Android's jni.h seriously doesn't declare these :/
- jint JNI_CreateJavaVM(JavaVM **, void **, void *);
- jint JNI_GetCreatedJavaVMs(JavaVM **, jsize, jsize *);
-}
-
-JNIEnv *GetJNI(JSContextRef context);
-
#define CYJavaForEachPrimitive \
CYJavaForEachPrimitive_(Z, z, Boolean, Boolean, boolean) \
CYJavaForEachPrimitive_(B, b, Byte, Byte, byte) \
#undef CYJavaForEachPrimitive_
};
+template <typename Type_>
+struct IsJavaPrimitive { static const bool value = false; };
+
+#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
+ template <> \
+ struct IsJavaPrimitive<j ## type> { static const bool value = true; };
+CYJavaForEachPrimitive
+#undef CYJavaForEachPrimitive_
+
// Java References {{{
template <typename Value_>
struct CYJavaRef {
return jni_;
}
+ // XXX: this is only needed to support CYJavaEnv relying on C variadics
_finline Value_ get() const {
return value_;
}
return {jni_, static_cast<Other_>(value_)};
}
+ // XXX: this should be tied into CYJavaFrame
Value_ leak() {
Value_ value(value_);
value_ = NULL;
{
}
- ~CYJavaDelete() {
+ void clear() {
if (this->value_ != NULL)
(this->jni_->*Delete_)(this->value_);
+ this->value_ = NULL;
+ }
+
+ ~CYJavaDelete() {
+ clear();
}
};
{
}
- CYJavaLocal(CYJavaLocal &&value) :
- CYJavaLocal(value.jni_, value.value_)
+ template <typename Other_>
+ CYJavaLocal(CYJavaRef<Other_> &&other) :
+ CYJavaLocal(other.jni_, other.value_)
+ {
+ other.value_ = NULL;
+ }
+
+ CYJavaLocal(CYJavaLocal &&other) :
+ CYJavaLocal(static_cast<CYJavaRef<Value_> &&>(other))
{
- value.value_ = NULL;
}
- CYJavaLocal &operator =(CYJavaLocal<Value_> &&other) {
- std::swap(this->jni_, other.jni_);
- std::swap(this->value_, other.value_);
+ template <typename Other_>
+ CYJavaLocal &operator =(CYJavaLocal<Other_> &&other) {
+ this->clear();
+ this->jni_ = other.jni_;
+ this->value_ = other.value_;
+ other.value_ = NULL;
return *this;
}
};
public CYUTF8String
{
private:
- const CYJavaRef<jstring> *value_;
+ const CYJavaRef<jstring> &value_;
public:
CYJavaUTF8String(const CYJavaRef<jstring> &value) :
- value_(&value)
+ value_(value)
{
_assert(value);
JNIEnv *jni(value.jni());
data = jni->GetStringUTFChars(value, NULL);
}
- CYJavaUTF8String(const CYJavaRef<jobject> &value) :
- CYJavaUTF8String(CYCastJavaString(value))
- {
- }
+ CYJavaUTF8String(CYJavaRef<jstring> &&) = delete;
~CYJavaUTF8String() {
- if (value_ != NULL) {
- JNIEnv *jni(value_->jni());
- jni->ReleaseStringUTFChars(*value_, data);
- }
+ JNIEnv *jni(value_.jni());
+ jni->ReleaseStringUTFChars(value_, data);
}
CYJavaUTF8String(const CYJavaUTF8String &) = delete;
-
- CYJavaUTF8String(CYJavaUTF8String &&rhs) :
- value_(rhs.value_)
- {
- rhs.value_ = NULL;
- }
};
CYJavaUTF8String CYCastUTF8String(const CYJavaRef<jstring> &value) {
- return CYJavaUTF8String(value);
+ return {value};
}
JSStringRef CYCopyJSString(const CYJavaRef<jstring> &value) {
}
virtual const char *PoolCString(CYPool &pool) const {
- return CYPoolCString(pool, CYJavaUTF8String(value_.cast<jobject>()));
+ auto string(CYCastJavaString(value_.cast<jobject>()));
+ return CYPoolCString(pool, CYJavaUTF8String(string));
}
virtual JSValueRef CastJSValue(JSContextRef context, const char *name) const;
operator ()(NULL);
}
+ operator JNIEnv *() const {
+ return jni_;
+ }
+
jobject operator ()(jobject object) {
JNIEnv *jni(jni_);
jni_ = NULL;
#define CYJavaEnv_(Code) \
template <typename... Args_> \
- auto Code(Args_ &&... args) const -> decltype(jni->Code(args...)) { \
+ auto Code(Args_ &&... args) const -> decltype(jni->Code(cy::Forward<Args_>(args)...)) { \
return _envcall(jni, Code(cy::Forward<Args_>(args)...)); \
}
CYJavaEnv_(GetMethodID)
CYJavaEnv_(GetStaticMethodID)
CYJavaEnv_(IsSameObject)
+ CYJavaEnv_(RegisterNatives)
#undef CYJavaEnv_
#define CYJavaEnv_(Code) \
return jni.CallObjectMethod<jstring>(value, Object$toString);
}
-template <typename Internal_, typename Value_>
-struct CYJavaValue :
- CYPrivate<Internal_>
-{
- CYJavaGlobal<Value_> value_;
-
- CYJavaValue(const CYJavaRef<Value_> &value) :
- value_(value)
- {
- }
-
- CYJavaValue(const CYJavaValue &) = delete;
-};
-
static JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef<jobject> &value);
template <typename Other_>
{
}
- CYJavaSignature(unsigned count) :
- shorty_(count)
- {
- }
-
+ // XXX: the shorty doesn't store enough information
bool operator <(const CYJavaSignature &rhs) const {
- return shorty_.size() < rhs.shorty_.size();
+ return shorty_ < rhs.shorty_;
}
};
-typedef std::multiset<CYJavaSignature> CYJavaOverload;
+typedef std::set<CYJavaSignature> CYJavaOverload;
+typedef std::map<unsigned, CYJavaOverload> CYJavaOverloads;
struct CYJavaMethod :
- CYPrivate<CYJavaMethod>
+ CYRoot
{
- CYJavaOverload overload_;
-
- CYJavaMethod(const CYJavaOverload &overload) :
- overload_(overload)
+ CYUTF8String type_;
+ CYUTF8String name_;
+ CYJavaOverloads overloads_;
+
+ CYJavaMethod(CYUTF8String type, CYUTF8String name, const CYJavaOverloads &overloads) :
+ type_(CYPoolUTF8String(*pool_, type)),
+ name_(CYPoolUTF8String(*pool_, name)),
+ overloads_(overloads)
{
}
};
-struct CYJavaStaticMethod :
- CYPrivate<CYJavaStaticMethod>
+struct CYJavaInstanceMethod :
+ CYJavaMethod
{
- CYJavaOverload overload_;
+ using CYJavaMethod::CYJavaMethod;
+};
- CYJavaStaticMethod(const CYJavaOverload &overload) :
- overload_(overload)
- {
- }
+struct CYJavaStaticMethod :
+ CYJavaMethod
+{
+ using CYJavaMethod::CYJavaMethod;
};
struct CYJavaClass :
- CYJavaValue<CYJavaClass, jclass>
+ CYRoot
{
+ CYJavaGlobal<jclass> value_;
bool interface_;
CYJavaFieldMap static_;
CYJavaFieldMap instance_;
- CYJavaOverload overload_;
+ CYJavaOverloads overloads_;
CYJavaClass(const CYJavaRef<jclass> &value, bool interface) :
- CYJavaValue(value),
+ value_(value),
interface_(interface)
{
}
static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef<jclass> &_class);
struct CYJavaObject :
- CYJavaValue<CYJavaObject, jobject>
+ CYRoot
{
+ CYJavaGlobal<jobject> value_;
CYJavaClass *table_;
CYJavaObject(const CYJavaRef<jobject> &value, CYJavaClass *table) :
- CYJavaValue(value),
+ value_(value),
table_(table)
{
}
};
struct CYJavaInterior :
- CYJavaValue<CYJavaInterior, jobject>
+ CYRoot
{
+ CYJavaGlobal<jobject> value_;
CYJavaClass *table_;
CYJavaInterior(const CYJavaRef<jobject> &value, CYJavaClass *table) :
- CYJavaValue(value),
+ value_(value),
table_(table)
{
}
};
struct CYJavaStaticInterior :
- CYJavaValue<CYJavaStaticInterior, jclass>
+ CYRoot
{
+ CYJavaGlobal<jclass> value_;
CYJavaClass *table_;
CYJavaStaticInterior(const CYJavaRef<jclass> &value, CYJavaClass *table) :
- CYJavaValue(value),
+ value_(value),
table_(table)
{
}
};
struct CYJavaArray :
- CYJavaValue<CYJavaArray, jarray>
+ CYRoot
{
+ CYJavaGlobal<jarray> value_;
CYJavaPrimitive primitive_;
CYJavaArray(const CYJavaRef<jarray> &value, CYJavaPrimitive primitive) :
- CYJavaValue(value),
+ value_(value),
primitive_(primitive)
{
}
};
struct CYJavaPackage :
- CYPrivate<CYJavaPackage>
+ CYRoot
{
+ JNIEnv *jni_;
+
typedef std::vector<std::string> Path;
Path package_;
- _finline CYJavaPackage(const Path &package) :
+ _finline CYJavaPackage(JNIEnv *jni, const Path &package) :
+ jni_(jni),
package_(package)
{
}
auto Class$getComponentType(jni.GetMethodID(Class$, "getComponentType", "()Ljava/lang/Class;"));
auto component(jni.CallObjectMethod<jclass>(_class, Class$getComponentType));
auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;"));
- return CYJavaArray::Make(context, value.cast<jarray>(), CYJavaGetPrimitive(context, component, Class$getName));
+ return CYPrivate<CYJavaArray>::Make(context, value.cast<jarray>(), CYJavaGetPrimitive(context, component, Class$getName));
}
auto Wrapper$(jni.FindClass("Cycript$Wrapper"));
}
CYJavaClass *table(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(CYGetJavaClass(context, _class))));
- return CYJavaObject::Make(context, value, table);
+ return CYPrivate<CYJavaObject>::Make(context, value, table);
}
static _finline JSObjectRef CYCastJSObject(JSContextRef context, const CYJavaRef<jobject> &value) {
CYCastJava$(D, Double, jdouble, CYCastDouble)
static CYJavaClass *CYGetJavaTable(JSContextRef context, JSObjectRef object) {
- if (!JSValueIsObjectOfClass(context, object, CYJavaClass::Class_))
+ if (!JSValueIsObjectOfClass(context, object, CYPrivate<CYJavaClass>::Class_))
return NULL;
return reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object));
}
static CYJavaObject *CYGetJavaObject(JSContextRef context, JSObjectRef object) {
- if (!JSValueIsObjectOfClass(context, object, CYJavaObject::Class_))
+ if (!JSValueIsObjectOfClass(context, object, CYPrivate<CYJavaObject>::Class_))
return NULL;
return reinterpret_cast<CYJavaObject *>(JSObjectGetPrivate(object));
}
auto Class$(jni.FindClass("java/lang/Class"));
auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;"));
- CYJSString name(jni.CallObjectMethod<jstring>(value, Class$getName));
+ auto string(jni.CallObjectMethod<jstring>(value, Class$getName));
+ CYJavaUTF8String utf8(string);
+ CYJSString name(utf8);
+
JSValueRef cached(CYGetProperty(context, cy, name));
if (!JSValueIsUndefined(context, cached))
return CYCastJSObject(context, cached);
auto modifiers(jni.CallIntMethod(field, Field$getModifiers));
auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers));
auto &map(instance ? table->instance_ : table->static_);
- CYJavaUTF8String name(jni.CallObjectMethod<jstring>(field, Field$getName));
+ auto string(jni.CallObjectMethod<jstring>(field, Field$getName));
+ CYJavaUTF8String name(string);
auto id(jni.FromReflectedField(field));
auto type(jni.CallObjectMethod<jclass>(field, Field$getType));
map.insert(std::make_pair(std::string(name), CYJavaField{id, CYJavaGetPrimitive(context, type, Class$getName)}));
}
}
- constructor = JSObjectMake(context, CYJavaClass::Class_, table);
+ constructor = JSObjectMake(context, CYPrivate<CYJavaClass>::Class_, table);
prototype = JSObjectMake(context, NULL, NULL);
CYSetProperty(context, constructor, prototype_s, prototype, kJSPropertyAttributeDontEnum);
auto parameters(jni.CallObjectMethod<jobjectArray>(constructor, Constructor$getParameterTypes));
CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName));
auto id(jni.FromReflectedMethod(constructor));
- table->overload_.insert(CYJavaSignature(constructor, id, CYJavaPrimitiveObject, shorty));
+ table->overloads_[shorty.size()].insert(CYJavaSignature(constructor, id, CYJavaPrimitiveObject, shorty));
}
- auto methods(jni.CallObjectMethod<jobjectArray>(value, Class$getDeclaredMethods));
+ std::map<std::pair<bool, std::string>, CYJavaOverloads> entries;
+
+ bool base(false);
+ for (CYJavaLocal<jclass> prototype(value); prototype; prototype = jni.GetSuperclass(prototype)) {
+ auto methods(jni.CallObjectMethod<jobjectArray>(prototype, Class$getDeclaredMethods));
- std::map<std::pair<bool, std::string>, CYJavaOverload> entries;
+ for (jsize i(0), e(jni.GetArrayLength(methods)); i != e; ++i) {
+ auto method(jni.GetObjectArrayElement<jobject>(methods, i));
+ auto modifiers(jni.CallIntMethod(method, Method$getModifiers));
+ auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers));
+ auto string(jni.CallObjectMethod<jstring>(method, Method$getName));
+ CYJavaUTF8String name(string);
+
+ auto parameters(jni.CallObjectMethod<jobjectArray>(method, Method$getParameterTypes));
+ CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName));
+
+ CYJavaOverload *overload;
+ if (!base)
+ overload = &entries[std::make_pair(instance, std::string(name))][shorty.size()];
+ else {
+ auto entry(entries.find(std::make_pair(instance, std::string(name))));
+ if (entry == entries.end())
+ continue;
+ overload = &entry->second[shorty.size()];
+ }
+
+ auto type(jni.CallObjectMethod<jclass>(method, Method$getReturnType));
+ auto primitive(CYJavaGetPrimitive(context, type, Class$getName));
+ auto id(jni.FromReflectedMethod(method));
+ overload->insert(CYJavaSignature(method, id, primitive, shorty));
+ }
- for (jsize i(0), e(jni.GetArrayLength(methods)); i != e; ++i) {
- auto method(jni.GetObjectArrayElement<jobject>(methods, i));
- auto modifiers(jni.CallIntMethod(method, Method$getModifiers));
- auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers));
- CYJavaUTF8String name(jni.CallObjectMethod<jstring>(method, Method$getName));
- auto parameters(jni.CallObjectMethod<jobjectArray>(method, Method$getParameterTypes));
- CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName));
- auto type(jni.CallObjectMethod<jclass>(method, Method$getReturnType));
- auto primitive(CYJavaGetPrimitive(context, type, Class$getName));
- auto id(jni.FromReflectedMethod(method));
- entries[std::make_pair(instance, std::string(name))].insert(CYJavaSignature(method, id, primitive, shorty));
+ base = true;
}
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, CYJavaMethod::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete);
+ CYSetProperty(context, prototype, method, CYPrivate<CYJavaInstanceMethod>::Make(context, utf8, entry.first.second.c_str(), overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete);
else
- CYSetProperty(context, constructor, name, CYJavaStaticMethod::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete);
+ CYSetProperty(context, constructor, method, CYPrivate<CYJavaStaticMethod>::Make(context, utf8, entry.first.second.c_str(), overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete);
}
}
}
}
-static bool CYCastJavaArguments(const CYJavaEnv &jni, const CYJavaShorty &shorty, JSContextRef context, const JSValueRef arguments[], jvalue *array) {
+static bool CYCastJavaArguments(const CYJavaFrame &frame, const CYJavaShorty &shorty, JSContextRef context, const JSValueRef arguments[], jvalue *array) {
+ CYJavaEnv jni(frame);
+
for (size_t index(0); index != shorty.size(); ++index) {
JSValueRef argument(arguments[index]);
JSType type(JSValueGetType(context, argument));
switch (CYJavaPrimitive primitive = shorty[index]) {
case CYJavaPrimitiveObject:
+ // XXX: figure out a way to tie this in to the CYJavaFrame
value.l = CYCastJavaObject(jni, context, argument).leak();
break;
return true;
}
-static JSValueRef JavaMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- CYJavaMethod *internal(reinterpret_cast<CYJavaMethod *>(JSObjectGetPrivate(object)));
+static JSValueRef JavaInstanceMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ auto internal(CYPrivate<CYJavaInstanceMethod>::Get(context, object));
CYJavaObject *self(CYGetJavaObject(context, _this));
+ _assert(self != NULL);
CYJavaEnv jni(self->value_);
- CYJavaSignature bound(count);
- for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) {
- CYJavaFrame(jni, count + 16);
+ auto overload(internal->overloads_.find(count));
+ if (overload != internal->overloads_.end())
+ for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) {
+ CYJavaFrame frame(jni, count + 16);
jvalue array[count];
- if (!CYCastJavaArguments(jni, overload->shorty_, context, arguments, array))
+ if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array))
continue;
jvalue *values(array);
- switch (overload->primitive_) {
+ switch (signature->primitive_) {
case CYJavaPrimitiveObject:
- return CYCastJSValue(context, jni.CallObjectMethodA<jobject>(self->value_, overload->method_, values));
+ return CYCastJSValue(context, jni.CallObjectMethodA<jobject>(self->value_, signature->method_, values));
case CYJavaPrimitiveVoid:
- jni.CallVoidMethodA(self->value_, overload->method_, values);
+ jni.CallVoidMethodA(self->value_, signature->method_, values);
return CYJSUndefined(context);
#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
case CYJavaPrimitive ## Type: \
- return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, overload->method_, values));
+ return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, signature->method_, values));
CYJavaForEachPrimitive
#undef CYJavaForEachPrimitive_
default: _assert(false);
} CYCatch(NULL) }
static JSValueRef JavaStaticMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- CYJavaMethod *internal(reinterpret_cast<CYJavaMethod *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaStaticMethod>::Get(context, object));
CYJavaClass *table(CYGetJavaTable(context, _this));
CYJavaEnv jni(table->value_);
- CYJavaSignature bound(count);
- for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) {
- CYJavaFrame(jni, count + 16);
+ auto overload(internal->overloads_.find(count));
+ if (overload != internal->overloads_.end())
+ for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) {
+ CYJavaFrame frame(jni, count + 16);
jvalue array[count];
- if (!CYCastJavaArguments(jni, overload->shorty_, context, arguments, array))
+ if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array))
continue;
jvalue *values(array);
- switch (overload->primitive_) {
+ switch (signature->primitive_) {
case CYJavaPrimitiveObject:
- return CYCastJSValue(context, jni.CallStaticObjectMethodA<jobject>(table->value_, overload->method_, values));
+ return CYCastJSValue(context, jni.CallStaticObjectMethodA<jobject>(table->value_, signature->method_, values));
case CYJavaPrimitiveVoid:
- jni.CallStaticVoidMethodA(table->value_, overload->method_, values);
+ jni.CallStaticVoidMethodA(table->value_, signature->method_, values);
return CYJSUndefined(context);
#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
case CYJavaPrimitive ## Type: \
- return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, overload->method_, values));
+ return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, signature->method_, values));
CYJavaForEachPrimitive
#undef CYJavaForEachPrimitive_
default: _assert(false);
} CYCatch(NULL) }
static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- CYJavaClass *table(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)));
+ auto table(CYPrivate<CYJavaClass>::Get(context, object));
CYJavaEnv jni(table->value_);
jclass _class(table->value_);
if (table->interface_ && count == 1) {
- JSObjectRef target(CYCastJSObject(context, arguments[0]));
auto Cycript$(jni.FindClass("Cycript"));
- auto Cycript$Make(jni.GetStaticMethodID(Cycript$, "proxy", "(Ljava/lang/Class;J)Ljava/lang/Object;"));
- auto protect(new CYProtect(context, target));
- return CYCastJSObject(context, jni.CallObjectMethod<jobject>(Cycript$, Cycript$Make, _class, reinterpret_cast<jlong>(protect)));
+ auto Cycript$Make(jni.GetStaticMethodID(Cycript$, "proxy", "(Ljava/lang/Class;LCycript$Wrapper;)Ljava/lang/Object;"));
+ return CYCastJSObject(context, jni.CallObjectMethod<jobject>(Cycript$, Cycript$Make, _class, CYCastJavaObject(jni, context, CYCastJSObject(context, arguments[0])).get()));
}
- CYJavaSignature bound(count);
- for (auto overload(table->overload_.lower_bound(bound)), e(table->overload_.upper_bound(bound)); overload != e; ++overload) {
- CYJavaFrame(jni, count + 16);
+ auto overload(table->overloads_.find(count));
+ if (overload != table->overloads_.end())
+ for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) {
+ CYJavaFrame frame(jni, count + 16);
jvalue array[count];
- if (!CYCastJavaArguments(jni, overload->shorty_, context, arguments, array))
+ if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array))
continue;
jvalue *values(array);
- auto object(jni.NewObjectA(_class, overload->method_, values));
+ auto object(jni.NewObjectA(_class, signature->method_, values));
return CYCastJSObject(context, object);
}
CYThrow("invalid constructor call");
-} CYCatch(NULL) }
+} CYCatchObject() }
static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
- CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object));
CYJavaClass *table(internal->table_);
CYPool pool;
auto name(CYPoolUTF8String(pool, context, property));
}
static JSValueRef JavaStaticInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object));
CYJavaClass *table(internal->table_);
CYJavaEnv jni(table->value_);
CYPool pool;
} CYCatch(NULL) }
static bool JavaStaticInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
- CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object));
CYJavaClass *table(internal->table_);
CYJavaEnv jni(table->value_);
CYPool pool;
} CYCatch(false) }
static void JavaStaticInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
- CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object));
CYJavaClass *table(internal->table_);
for (const auto &field : table->static_)
JSPropertyNameAccumulatorAddName(names, CYJSString(field.first));
}
static JSValueRef JavaClass_getProperty_class(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaClass *table(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)));
+ auto table(CYPrivate<CYJavaClass>::Get(context, object));
return CYCastJSValue(context, table->value_);
} CYCatch(NULL) }
static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
- CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaInterior>::Get(context, object));
CYJavaClass *table(internal->table_);
CYPool pool;
auto name(CYPoolUTF8String(pool, context, property));
}
static JSValueRef JavaInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaInterior>::Get(context, object));
CYJavaEnv jni(internal->value_);
CYJavaClass *table(internal->table_);
CYPool pool;
} CYCatch(NULL) }
static bool JavaInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
- CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaInterior>::Get(context, object));
CYJavaEnv jni(internal->value_);
CYJavaClass *table(internal->table_);
CYPool pool;
} CYCatch(false) }
static void JavaInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
- CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaInterior>::Get(context, object));
CYJavaClass *table(internal->table_);
for (const auto &field : table->instance_)
JSPropertyNameAccumulatorAddName(names, CYJSString(field.first));
}
static JSValueRef JavaObject_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaObject *internal(reinterpret_cast<CYJavaObject *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaObject>::Get(context, object));
CYJavaEnv jni(internal->value_);
return CYGetJavaClass(context, jni.GetObjectClass(internal->value_));
} CYCatch(NULL) }
static JSValueRef JavaClass_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaClass *internal(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)));
- return CYJavaStaticInterior::Make(context, internal->value_, internal);
+ auto internal(CYPrivate<CYJavaClass>::Get(context, object));
+ return CYPrivate<CYJavaStaticInterior>::Make(context, internal->value_, internal);
} CYCatch(NULL) }
static JSValueRef JavaObject_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaObject *internal(reinterpret_cast<CYJavaObject *>(JSObjectGetPrivate(object)));
- return CYJavaInterior::Make(context, internal->value_, internal->table_);
+ auto internal(CYPrivate<CYJavaObject>::Get(context, object));
+ return CYPrivate<CYJavaInterior>::Make(context, internal->value_, internal->table_);
} CYCatch(NULL) }
static JSValueRef JavaClass_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- CYJavaClass *internal(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(_this)));
+ auto internal(CYPrivate<CYJavaClass>::Get(context, _this));
CYJavaEnv jni(internal->value_);
auto Class$(jni.FindClass("java/lang/Class"));
auto Class$getCanonicalName(jni.GetMethodID(Class$, "getCanonicalName", "()Ljava/lang/String;"));
} 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<CYJavaMethod>::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) }
static JSValueRef JavaArray_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaArray *internal(reinterpret_cast<CYJavaArray *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaArray>::Get(context, object));
CYJavaEnv jni(internal->value_);
if (JSStringIsEqual(property, length_s))
return CYCastJSValue(context, jni.GetArrayLength(internal->value_));
} CYCatch(NULL) }
static bool JavaArray_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
- CYJavaArray *internal(reinterpret_cast<CYJavaArray *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaArray>::Get(context, object));
CYJavaEnv jni(internal->value_);
CYPool pool;
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 {
- CYJavaPackage *internal(reinterpret_cast<CYJavaPackage *>(JSObjectGetPrivate(_this)));
+ auto internal(CYPrivate<CYJavaPackage>::Get(context, _this));
std::ostringstream name;
for (auto &package : internal->package_)
name << package << '.';
}
static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
- CYJavaPackage *internal(reinterpret_cast<CYJavaPackage *>(JSObjectGetPrivate(object)));
+ auto internal(CYPrivate<CYJavaPackage>::Get(context, object));
CYJavaPackage::Path package(internal->package_);
CYPool pool;
name << package << '/';
name << next;
- JNIEnv *jni(GetJNI(context));
+ 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();
package.push_back(next);
- return CYJavaPackage::Make(context, package);
+ return CYPrivate<CYJavaPackage>::Make(context, jni, package);
} CYCatch(NULL) }
static void Cycript_delete(JNIEnv *env, jclass api, jlong jprotect) { CYJavaTry {
size_t count(jarguments == NULL ? 0 : jni.GetArrayLength(jarguments));
JSValueRef arguments[count];
- for (size_t index(0); index != count; ++index) {
+ for (size_t index(0); index != count; ++index)
arguments[index] = CYCastJSValue(context, jni.GetObjectArrayElement<jobject>(jarguments, index));
- }
- return CYCastJavaObject(jni, context, CYCallAsFunction(context, CYCastJSObject(context, function), object, count, arguments));
+ return CYCastJavaObject(jni, context, CYCallAsFunction(context, CYCastJSObject(context, function), object, count, arguments)).leak();
} CYJavaCatch(NULL) }
static JNINativeMethod Cycript_[] = {
{(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle},
};
-JNIEnv *GetJNI(JSContextRef context) {
- static JavaVM *jvm(NULL);
- static JNIEnv *jni(NULL);
+template <typename Type_>
+static _finline void dlset(Type_ &function, const char *name, void *handle) {
+ function = reinterpret_cast<Type_>(dlsym(handle, name));
+}
- if (jni != NULL)
- return jni;
- jint version(JNI_VERSION_1_4);
+jint CYJavaVersion(JNI_VERSION_1_4);
+static JavaVM *CYGetJavaVM(jint (*$JNI_GetCreatedJavaVMs)(JavaVM **, jsize, jsize *)) {
jsize capacity(16);
JavaVM *jvms[capacity];
jsize size;
- _jnicall(JNI_GetCreatedJavaVMs(jvms, capacity, &size));
+ _jnicall($JNI_GetCreatedJavaVMs(jvms, capacity, &size));
+ if (size == 0)
+ return NULL;
+ return jvms[0];
+}
+
+static JavaVM *CYGetJavaVM(JSContextRef context) {
+ CYPool pool;
+ void *handle(RTLD_DEFAULT);
+ std::string library;
+
+ jint (*$JNI_GetCreatedJavaVMs)(JavaVM **jvms, jsize capacity, jsize *size);
+ dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle);
- if (size != 0) {
- jvm = jvms[0];
- _jnicall(jvm->GetEnv(reinterpret_cast<void **>(&jni), version));
+ if ($JNI_GetCreatedJavaVMs != NULL) {
+ if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs))
+ return jvm;
} else {
- CYPool pool;
- std::vector<JavaVMOption> options;
-
- {
- std::ostringstream option;
- option << "-Djava.class.path=";
- option << CYPoolLibraryPath(pool) << "/libcycript.jar";
- if (const char *classpath = getenv("CLASSPATH"))
- option << ':' << classpath;
- options.push_back(JavaVMOption{pool.strdup(option.str().c_str()), NULL});
+ std::vector<const char *> guesses;
+
+#ifdef __ANDROID__
+ char android[PROP_VALUE_MAX];
+ if (__system_property_get("persist.sys.dalvik.vm.lib", android) != 0)
+ guesses.push_back(android);
+#endif
+
+ guesses.push_back("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib");
+ //guesses.push_back("/System/Library/Frameworks/JavaVM.framework/JavaVM");
+ guesses.push_back("libjvm.dylib");
+
+ guesses.push_back("libart.so");
+ guesses.push_back("libdvm.so");
+ guesses.push_back("libjvm.so");
+
+ for (const char *guess : guesses) {
+ handle = dlopen(guess, RTLD_LAZY | RTLD_GLOBAL);
+ if (handle != NULL) {
+ library = guess;
+ break;
+ }
}
- JavaVMInitArgs args;
- memset(&args, 0, sizeof(args));
- args.version = version;
- args.nOptions = options.size();
- args.options = options.data();
- _jnicall(JNI_CreateJavaVM(&jvm, reinterpret_cast<void **>(&jni), &args));
+ if (library.size() == 0)
+ return NULL;
+
+ dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle);
+ if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs))
+ return jvm;
+ }
+
+ std::vector<JavaVMOption> options;
+
+ {
+ std::ostringstream option;
+ option << "-Djava.class.path=";
+ option << CYPoolLibraryPath(pool) << "/libcycript.jar";
+ if (const char *classpath = getenv("CLASSPATH"))
+ option << ':' << classpath;
+ options.push_back(JavaVMOption{pool.strdup(option.str().c_str()), NULL});
+ }
+
+ // To use libnativehelper to access JNI_GetCreatedJavaVMs, you need JniInvocation.
+ // ...but there can only be one JniInvocation, and assuradely the other VM has it.
+ // Essentially, this API makes no sense. We need it for AndroidRuntime, though :/.
+
+ if (void *libnativehelper = dlopen("libnativehelper.so", RTLD_LAZY | RTLD_GLOBAL)) {
+ class JniInvocation$;
+ JniInvocation$ *(*JniInvocation$$init$)(JniInvocation$ *self)(NULL);
+ bool (*JniInvocation$Init)(JniInvocation$ *self, const char *library)(NULL);
+ JniInvocation$ *(*JniInvocation$finalize)(JniInvocation$ *self)(NULL);
+
+ dlset(JniInvocation$$init$, "_ZN13JniInvocationC1Ev", libnativehelper);
+ dlset(JniInvocation$Init, "_ZN13JniInvocation4InitEPKc", libnativehelper);
+ dlset(JniInvocation$finalize, "_ZN13JniInvocationD1Ev", libnativehelper);
+
+ if (JniInvocation$$init$ == NULL)
+ dlclose(libnativehelper);
+ else {
+ // XXX: we should attach a pool to the VM itself and deallocate this there
+ //auto invocation(pool.calloc<JniInvocation$>(1, 1024));
+ //_assert(JniInvocation$finalize != NULL);
+ //pool.atexit(reinterpret_cast<void (*)(void *)>(JniInvocation$finalize), invocation);
+
+ auto invocation(static_cast<JniInvocation$ *>(calloc(1, 1024)));
+ JniInvocation$$init$(invocation);
+
+ _assert(JniInvocation$Init != NULL);
+ JniInvocation$Init(invocation, NULL);
+
+ dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", libnativehelper);
+ if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs))
+ return jvm;
+ }
+ }
+
+ JavaVM *jvm;
+ JNIEnv *env;
+
+ if (void *libandroid_runtime = dlopen("libandroid_runtime.so", RTLD_LAZY | RTLD_GLOBAL)) {
+ class AndroidRuntime$;
+ AndroidRuntime$ *(*AndroidRuntime$$init$)(AndroidRuntime$ *self, char *args, unsigned int size)(NULL);
+ int (*AndroidRuntime$startVm)(AndroidRuntime$ *self, JavaVM **jvm, JNIEnv **env)(NULL);
+ int (*AndroidRuntime$startReg)(JNIEnv *env)(NULL);
+ int (*AndroidRuntime$addOption)(AndroidRuntime$ *self, const char *option, void *extra)(NULL);
+ int (*AndroidRuntime$addVmArguments)(AndroidRuntime$ *self, int, const char *const argv[])(NULL);
+ AndroidRuntime$ *(*AndroidRuntime$finalize)(AndroidRuntime$ *self)(NULL);
+
+ dlset(AndroidRuntime$$init$, "_ZN7android14AndroidRuntimeC1EPcj", libandroid_runtime);
+ dlset(AndroidRuntime$startVm, "_ZN7android14AndroidRuntime7startVmEPP7_JavaVMPP7_JNIEnv", libandroid_runtime);
+ dlset(AndroidRuntime$startReg, "_ZN7android14AndroidRuntime8startRegEP7_JNIEnv", libandroid_runtime);
+ dlset(AndroidRuntime$addOption, "_ZN7android14AndroidRuntime9addOptionEPKcPv", libandroid_runtime);
+ dlset(AndroidRuntime$addVmArguments, "_ZN7android14AndroidRuntime14addVmArgumentsEiPKPKc", libandroid_runtime);
+ dlset(AndroidRuntime$finalize, "_ZN7android14AndroidRuntimeD1Ev", libandroid_runtime);
+
+ // XXX: it would also be interesting to attach this to a global pool
+ AndroidRuntime$ *runtime(pool.calloc<AndroidRuntime$>(1, 1024));
+
+ _assert(AndroidRuntime$$init$ != NULL);
+ AndroidRuntime$$init$(runtime, NULL, 0);
+
+ if (AndroidRuntime$addOption == NULL) {
+ _assert(AndroidRuntime$addVmArguments != NULL);
+ std::vector<const char *> arguments;
+ for (const auto &option : options)
+ arguments.push_back(option.optionString);
+ AndroidRuntime$addVmArguments(runtime, arguments.size(), arguments.data());
+ } else for (const auto &option : options)
+ AndroidRuntime$addOption(runtime, option.optionString, option.extraInfo);
+
+ int failure;
+
+ _assert(AndroidRuntime$startVm != NULL);
+ failure = AndroidRuntime$startVm(runtime, &jvm, &env);
+ _assert(failure == 0);
+
+ _assert(AndroidRuntime$startReg != NULL);
+ failure = AndroidRuntime$startReg(env);
+ _assert(failure == 0);
+
+ return jvm;
}
- auto Cycript$(CYJavaEnv(jni).FindClass("Cycript"));
- _envcall(jni, RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0])));
+ jint (*$JNI_CreateJavaVM)(JavaVM **jvm, void **, void *);
+ dlset($JNI_CreateJavaVM, "JNI_CreateJavaVM", handle);
+
+ JavaVMInitArgs args;
+ memset(&args, 0, sizeof(args));
+ args.version = CYJavaVersion;
+ args.nOptions = options.size();
+ args.options = options.data();
+ _jnicall($JNI_CreateJavaVM(&jvm, reinterpret_cast<void **>(&env), &args));
+ return jvm;
+}
+
+static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env) {
+ auto jvm(CYGetJavaVM(context));
+ _assert(jvm != NULL);
+ _jnicall(jvm->GetEnv(reinterpret_cast<void **>(&env), CYJavaVersion));
+ CYJavaEnv jni(env);
+
+ // XXX: this happens once per stub :/
- return jni;
+ 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;
}
static JSStaticValue JavaClass_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}
};
definition.staticFunctions = JavaClass_staticFunctions;
definition.callAsConstructor = &JavaClass_callAsConstructor;
definition.finalize = &CYFinalize;
- CYJavaClass::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaClass>::Class_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.attributes = kJSClassAttributeNoAutomaticPrototype;
definition.setProperty = &JavaInterior_setProperty;
definition.getPropertyNames = &JavaInterior_getPropertyNames;
definition.finalize = &CYFinalize;
- CYJavaInterior::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaInterior>::Class_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "JavaMethod";
- definition.staticFunctions = JavaMethod_staticFunctions;
- definition.callAsFunction = &JavaMethod_callAsFunction;
definition.finalize = &CYFinalize;
- CYJavaMethod::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaMethod>::Class_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "JavaInstanceMethod";
+ definition.parentClass = CYPrivate<CYJavaMethod>::Class_;
+ definition.staticFunctions = JavaInstanceMethod_staticFunctions;
+ definition.callAsFunction = &JavaInstanceMethod_callAsFunction;
+ CYPrivate<CYJavaInstanceMethod>::Class_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "JavaStaticMethod";
+ definition.parentClass = CYPrivate<CYJavaMethod>::Class_;
definition.staticFunctions = JavaStaticMethod_staticFunctions;
definition.callAsFunction = &JavaStaticMethod_callAsFunction;
- definition.finalize = &CYFinalize;
- CYJavaStaticMethod::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaStaticMethod>::Class_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.attributes = kJSClassAttributeNoAutomaticPrototype;
definition.className = "JavaObject";
definition.staticValues = JavaObject_staticValues;
definition.finalize = &CYFinalize;
- CYJavaObject::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaObject>::Class_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "JavaArray";
definition.getProperty = &JavaArray_getProperty;
definition.setProperty = &JavaArray_setProperty;
definition.finalize = &CYFinalize;
- CYJavaArray::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaArray>::Class_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "JavaPackage";
definition.hasProperty = &CYJavaPackage_hasProperty;
definition.getProperty = &CYJavaPackage_getProperty;
definition.finalize = &CYFinalize;
- CYJavaPackage::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaPackage>::Class_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.attributes = kJSClassAttributeNoAutomaticPrototype;
definition.setProperty = &JavaStaticInterior_setProperty;
definition.getPropertyNames = &JavaStaticInterior_getPropertyNames;
definition.finalize = &CYFinalize;
- CYJavaStaticInterior::Class_ = JSClassCreate(&definition);
+ CYPrivate<CYJavaStaticInterior>::Class_ = JSClassCreate(&definition);
}
void CYJava_SetupContext(JSContextRef context) {
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, CYJavaPackage::Path()));
- CYSetProperty(context, all, CYJSString("Packages"), Packages);
+ JSObjectRef Packages(CYPrivate<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, CYPrivate<CYJavaPackage>::Make(context, nullptr, CYJavaPackage::Path(1, name)), kJSPropertyAttributeDontEnum);
}
}