|
34 | 34 | #include "JSFunction.h" |
35 | 35 |
|
36 | 36 | 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; |
41 | 41 | return definition; |
42 | 42 | } |
43 | 43 |
|
44 | 44 | JSFunction::JSFunction(JNIEnv* env, jobject thiz, JSContextRef ctx, JSStringRef name) |
45 | | - : Instance(env, thiz, ctx, JSFunctionClassDefinition(), name) |
| 45 | + : Instance(env, thiz, ctx, JSFunctionClassDefinition(), name) |
46 | 46 | { |
47 | 47 | } |
48 | 48 |
|
49 | 49 | JSFunction::~JSFunction() { |
50 | 50 | } |
51 | 51 |
|
52 | 52 | 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) |
54 | 54 | { |
55 | 55 | JSFunction *thiz = (JSFunction *)getInstance(function); |
56 | 56 |
|
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; |
61 | 61 | } |
62 | 62 |
|
63 | 63 | 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) |
65 | 65 | { |
66 | 66 | JSFunction *thiz = (JSFunction *)getInstance(constructor); |
67 | 67 |
|
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; |
72 | 72 | } |
73 | 73 |
|
74 | 74 | bool JSFunction::StaticHasInstanceCallback(JSContextRef ctx, JSObjectRef constructor, |
75 | 75 | JSValueRef possibleInstance, JSValueRef* exception) |
76 | 76 | { |
77 | 77 | JSFunction *thiz = (JSFunction *)getInstance(constructor); |
78 | 78 |
|
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; |
83 | 83 | } |
84 | 84 |
|
85 | 85 | 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) |
87 | 87 | { |
88 | | - JNIEnv *env; |
| 88 | + JNIEnv *env; |
89 | 89 | int getEnvStat = jvm->GetEnv((void**)&env, JNI_VERSION_1_6); |
90 | 90 | if (getEnvStat == JNI_EDETACHED) { |
91 | | - jvm->AttachCurrentThread(&env, NULL); |
| 91 | + jvm->AttachCurrentThread(&env, NULL); |
92 | 92 | } |
93 | 93 |
|
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 | + |
122 | 123 | if (getEnvStat == JNI_EDETACHED) { |
123 | | - jvm->DetachCurrentThread(); |
| 124 | + jvm->DetachCurrentThread(); |
124 | 125 | } |
125 | | - return (JSObjectRef)objret; |
| 126 | + return (JSObjectRef)objret; |
126 | 127 | } |
127 | 128 |
|
128 | 129 | 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) |
130 | 131 | { |
131 | | - JNIEnv *env; |
| 132 | + JNIEnv *env; |
132 | 133 | int getEnvStat = jvm->GetEnv((void**)&env, JNI_VERSION_1_6); |
133 | 134 | 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]; |
135 | 157 | } |
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 | + |
164 | 166 | if (getEnvStat == JNI_EDETACHED) { |
165 | | - jvm->DetachCurrentThread(); |
| 167 | + jvm->DetachCurrentThread(); |
166 | 168 | } |
167 | | - return (JSObjectRef)objret; |
| 169 | + return (JSObjectRef)objret; |
168 | 170 | } |
169 | 171 |
|
170 | 172 | bool JSFunction::HasInstanceCallback(JSContextRef ctx, JSObjectRef constructor, |
171 | 173 | JSValueRef possibleInstance, JSValueRef* exception) |
172 | 174 | { |
173 | | - JNIEnv *env; |
| 175 | + JNIEnv *env; |
174 | 176 | int getEnvStat = jvm->GetEnv((void**)&env, JNI_VERSION_1_6); |
175 | 177 | if (getEnvStat == JNI_EDETACHED) { |
176 | | - jvm->AttachCurrentThread(&env, NULL); |
| 178 | + jvm->AttachCurrentThread(&env, NULL); |
177 | 179 | } |
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); |
197 | 199 |
|
198 | 200 | if (getEnvStat == JNI_EDETACHED) { |
199 | | - jvm->DetachCurrentThread(); |
| 201 | + jvm->DetachCurrentThread(); |
200 | 202 | } |
201 | | - return ret; |
| 203 | + return ret; |
202 | 204 | } |
0 commit comments