Skip to content
This repository was archived by the owner on Mar 25, 2019. It is now read-only.

Commit 3eeb248

Browse files
author
ericwlange
committed
Made all JS Array methods available from Java
Added support for Typed Arrays Fixed hanging bug (for real this time) Fixed protect/unprotect mismatch Fixed constant "Attempt to remove non-JNI local reference, dumping thread" messages
1 parent 966f002 commit 3eeb248

27 files changed

Lines changed: 2152 additions & 1668 deletions

AndroidJSCore/AndroidJSCore/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ android {
77
buildToolsVersion "23.0.2"
88

99
defaultConfig {
10-
minSdkVersion 9
10+
minSdkVersion 11
1111
targetSdkVersion 23
1212
versionCode 3
13-
versionName "3.0-pre1"
13+
versionName "3.0-pre2"
1414
setProperty("archivesBaseName", "AndroidJSCore-$versionName")
1515
}
1616
buildTypes {

AndroidJSCore/AndroidJSCore/jni/JSContext.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ NATIVE(JSContext,jobject,evaluateScript) (PARAMS, jlong ctx, jlong script,
140140
fid = env->GetFieldID(ret , "exception", "J");
141141
env->SetLongField( out, fid, (jlong) exception);
142142

143-
env->DeleteLocalRef(ret);
144-
145143
return out;
146144
}
147145

@@ -166,8 +164,6 @@ NATIVE(JSContext,jobject,checkScriptSyntax) (PARAMS, jlong ctx, jlong script,
166164
fid = env->GetFieldID(ret , "exception", "J");
167165
env->SetLongField( out, fid, (jlong) exception);
168166

169-
env->DeleteLocalRef(ret);
170-
171167
return out;
172168
}
173169

AndroidJSCore/AndroidJSCore/jni/JSFunction.cpp

Lines changed: 110 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -34,169 +34,171 @@
3434
#include "JSFunction.h"
3535

3636
JSClassDefinition JSFunction::JSFunctionClassDefinition() {
37-
JSClassDefinition definition = kJSClassDefinitionEmpty;
38-
definition.callAsFunction = StaticFunctionCallback;
39-
definition.callAsConstructor = StaticConstructorCallback;
40-
definition.hasInstance = StaticHasInstanceCallback;
37+
JSClassDefinition definition = kJSClassDefinitionEmpty;
38+
definition.callAsFunction = StaticFunctionCallback;
39+
definition.callAsConstructor = StaticConstructorCallback;
40+
definition.hasInstance = StaticHasInstanceCallback;
4141
return definition;
4242
}
4343

4444
JSFunction::JSFunction(JNIEnv* env, jobject thiz, JSContextRef ctx, JSStringRef name)
45-
: Instance(env, thiz, ctx, JSFunctionClassDefinition(), name)
45+
: Instance(env, thiz, ctx, JSFunctionClassDefinition(), name)
4646
{
4747
}
4848

4949
JSFunction::~JSFunction() {
5050
}
5151

5252
JSValueRef JSFunction::StaticFunctionCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
53-
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
53+
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
5454
{
5555
JSFunction *thiz = (JSFunction *)getInstance(function);
5656

57-
if (thiz) {
58-
return thiz->FunctionCallback(ctx,function,thisObject,argumentCount,arguments,exception);
59-
}
60-
return NULL;
57+
if (thiz) {
58+
return thiz->FunctionCallback(ctx,function,thisObject,argumentCount,arguments,exception);
59+
}
60+
return NULL;
6161
}
6262

6363
JSObjectRef JSFunction::StaticConstructorCallback(JSContextRef ctx, JSObjectRef constructor,
64-
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
64+
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
6565
{
6666
JSFunction *thiz = (JSFunction *)getInstance(constructor);
6767

68-
if (thiz) {
69-
return thiz->ConstructorCallback(ctx,constructor,argumentCount,arguments,exception);
70-
}
71-
return NULL;
68+
if (thiz) {
69+
return thiz->ConstructorCallback(ctx,constructor,argumentCount,arguments,exception);
70+
}
71+
return NULL;
7272
}
7373

7474
bool JSFunction::StaticHasInstanceCallback(JSContextRef ctx, JSObjectRef constructor,
7575
JSValueRef possibleInstance, JSValueRef* exception)
7676
{
7777
JSFunction *thiz = (JSFunction *)getInstance(constructor);
7878

79-
if (thiz) {
80-
return thiz->HasInstanceCallback(ctx,constructor,possibleInstance,exception);
81-
}
82-
return false;
79+
if (thiz) {
80+
return thiz->HasInstanceCallback(ctx,constructor,possibleInstance,exception);
81+
}
82+
return false;
8383
}
8484

8585
JSValueRef JSFunction::FunctionCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
86-
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
86+
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
8787
{
88-
JNIEnv *env;
88+
JNIEnv *env;
8989
int getEnvStat = jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
9090
if (getEnvStat == JNI_EDETACHED) {
91-
jvm->AttachCurrentThread(&env, NULL);
91+
jvm->AttachCurrentThread(&env, NULL);
9292
}
9393

94-
jclass cls = env->GetObjectClass(thiz);
95-
jmethodID mid;
96-
do {
97-
mid = env->GetMethodID(cls,"functionCallback","(JJJ[JJ)J");
98-
if (!env->ExceptionCheck()) break;
99-
env->ExceptionClear();
100-
jclass super = env->GetSuperclass(cls);
101-
env->DeleteLocalRef(cls);
102-
if (super == NULL || env->ExceptionCheck()) {
103-
if (super != NULL) env->DeleteLocalRef(super);
104-
jvm->DetachCurrentThread();
105-
return NULL;
106-
}
107-
cls = super;
108-
} while (true);
109-
env->DeleteLocalRef(cls);
110-
jlongArray argsArr = env->NewLongArray(argumentCount);
111-
jlong* args = new jlong[argumentCount];
112-
for (size_t i=0; i<argumentCount; i++) {
113-
args[i] = (long) arguments[i];
114-
}
115-
env->SetLongArrayRegion(argsArr,0,argumentCount,args);
116-
117-
long objret = env->CallLongMethod(thiz, mid, (jlong)ctx, (jlong)function, (jlong)thisObject,
118-
argsArr, (jlong)exception);
119-
120-
delete args;
121-
env->DeleteLocalRef(argsArr);
94+
jclass cls = env->GetObjectClass(thiz);
95+
jmethodID mid;
96+
do {
97+
mid = env->GetMethodID(cls,"functionCallback","(JJJ[JJ)J");
98+
if (!env->ExceptionCheck()) break;
99+
env->ExceptionClear();
100+
jclass super = env->GetSuperclass(cls);
101+
env->DeleteLocalRef(cls);
102+
if (super == NULL || env->ExceptionCheck()) {
103+
if (super != NULL) env->DeleteLocalRef(super);
104+
jvm->DetachCurrentThread();
105+
return NULL;
106+
}
107+
cls = super;
108+
} while (true);
109+
env->DeleteLocalRef(cls);
110+
jlongArray argsArr = env->NewLongArray(argumentCount);
111+
jlong* args = new jlong[argumentCount];
112+
for (size_t i=0; i<argumentCount; i++) {
113+
args[i] = (long) arguments[i];
114+
}
115+
env->SetLongArrayRegion(argsArr,0,argumentCount,args);
116+
117+
long objret = env->CallLongMethod(thiz, mid, (jlong)ctx, (jlong)function, (jlong)thisObject,
118+
argsArr, (jlong)exception);
119+
120+
delete args;
121+
env->DeleteLocalRef(argsArr);
122+
122123
if (getEnvStat == JNI_EDETACHED) {
123-
jvm->DetachCurrentThread();
124+
jvm->DetachCurrentThread();
124125
}
125-
return (JSObjectRef)objret;
126+
return (JSObjectRef)objret;
126127
}
127128

128129
JSObjectRef JSFunction::ConstructorCallback(JSContextRef ctx, JSObjectRef constructor,
129-
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
130+
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
130131
{
131-
JNIEnv *env;
132+
JNIEnv *env;
132133
int getEnvStat = jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
133134
if (getEnvStat == JNI_EDETACHED) {
134-
jvm->AttachCurrentThread(&env, NULL);
135+
jvm->AttachCurrentThread(&env, NULL);
136+
}
137+
jclass cls = env->GetObjectClass(thiz);
138+
jmethodID mid;
139+
do {
140+
mid = env->GetMethodID(cls,"constructorCallback","(JJ[JJ)J");
141+
if (!env->ExceptionCheck()) break;
142+
env->ExceptionClear();
143+
jclass super = env->GetSuperclass(cls);
144+
env->DeleteLocalRef(cls);
145+
if (super == NULL || env->ExceptionCheck()) {
146+
if (super != NULL) env->DeleteLocalRef(super);
147+
jvm->DetachCurrentThread();
148+
return NULL;
149+
}
150+
cls = super;
151+
} while (true);
152+
env->DeleteLocalRef(cls);
153+
jlongArray argsArr = env->NewLongArray(argumentCount);
154+
jlong* args = new jlong[argumentCount];
155+
for (size_t i=0; i<argumentCount; i++) {
156+
args[i] = (long) arguments[i];
135157
}
136-
jclass cls = env->GetObjectClass(thiz);
137-
jmethodID mid;
138-
do {
139-
mid = env->GetMethodID(cls,"constructorCallback","(JJ[JJ)J");
140-
if (!env->ExceptionCheck()) break;
141-
env->ExceptionClear();
142-
jclass super = env->GetSuperclass(cls);
143-
env->DeleteLocalRef(cls);
144-
if (super == NULL || env->ExceptionCheck()) {
145-
if (super != NULL) env->DeleteLocalRef(super);
146-
jvm->DetachCurrentThread();
147-
return NULL;
148-
}
149-
cls = super;
150-
} while (true);
151-
env->DeleteLocalRef(cls);
152-
jlongArray argsArr = env->NewLongArray(argumentCount);
153-
jlong* args = new jlong[argumentCount];
154-
for (size_t i=0; i<argumentCount; i++) {
155-
args[i] = (long) arguments[i];
156-
}
157-
env->SetLongArrayRegion(argsArr,0,argumentCount,args);
158-
159-
long objret = env->CallLongMethod(thiz, mid, (jlong)ctx, (jlong)constructor,
160-
argsArr, (jlong)exception);
161-
162-
delete args;
163-
env->DeleteLocalRef(argsArr);
158+
env->SetLongArrayRegion(argsArr,0,argumentCount,args);
159+
160+
long objret = env->CallLongMethod(thiz, mid, (jlong)ctx, (jlong)constructor,
161+
argsArr, (jlong)exception);
162+
163+
delete args;
164+
env->DeleteLocalRef(argsArr);
165+
164166
if (getEnvStat == JNI_EDETACHED) {
165-
jvm->DetachCurrentThread();
167+
jvm->DetachCurrentThread();
166168
}
167-
return (JSObjectRef)objret;
169+
return (JSObjectRef)objret;
168170
}
169171

170172
bool JSFunction::HasInstanceCallback(JSContextRef ctx, JSObjectRef constructor,
171173
JSValueRef possibleInstance, JSValueRef* exception)
172174
{
173-
JNIEnv *env;
175+
JNIEnv *env;
174176
int getEnvStat = jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
175177
if (getEnvStat == JNI_EDETACHED) {
176-
jvm->AttachCurrentThread(&env, NULL);
178+
jvm->AttachCurrentThread(&env, NULL);
177179
}
178-
jclass cls = env->GetObjectClass(thiz);
179-
jmethodID mid;
180-
do {
181-
mid = env->GetMethodID(cls,"hasInstanceCallback","(JJJJ)Z");
182-
if (!env->ExceptionCheck()) break;
183-
env->ExceptionClear();
184-
jclass super = env->GetSuperclass(cls);
185-
env->DeleteLocalRef(cls);
186-
if (super == NULL || env->ExceptionCheck()) {
187-
if (super != NULL) env->DeleteLocalRef(super);
188-
jvm->DetachCurrentThread();
189-
return NULL;
190-
}
191-
cls = super;
192-
} while (true);
193-
env->DeleteLocalRef(cls);
194-
195-
bool ret = env->CallBooleanMethod(thiz, mid, (jlong)ctx, (jlong)constructor,
196-
(jlong)possibleInstance, (jlong)exception);
180+
jclass cls = env->GetObjectClass(thiz);
181+
jmethodID mid;
182+
do {
183+
mid = env->GetMethodID(cls,"hasInstanceCallback","(JJJJ)Z");
184+
if (!env->ExceptionCheck()) break;
185+
env->ExceptionClear();
186+
jclass super = env->GetSuperclass(cls);
187+
env->DeleteLocalRef(cls);
188+
if (super == NULL || env->ExceptionCheck()) {
189+
if (super != NULL) env->DeleteLocalRef(super);
190+
jvm->DetachCurrentThread();
191+
return NULL;
192+
}
193+
cls = super;
194+
} while (true);
195+
env->DeleteLocalRef(cls);
196+
197+
bool ret = env->CallBooleanMethod(thiz, mid, (jlong)ctx, (jlong)constructor,
198+
(jlong)possibleInstance, (jlong)exception);
197199

198200
if (getEnvStat == JNI_EDETACHED) {
199-
jvm->DetachCurrentThread();
201+
jvm->DetachCurrentThread();
200202
}
201-
return ret;
203+
return ret;
202204
}

0 commit comments

Comments
 (0)