-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
KrollBindings.cpp
362 lines (300 loc) Β· 11.3 KB
/
KrollBindings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2011-2016 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#include <dlfcn.h>
#include <map>
#include <string.h>
#include <vector>
#include <v8.h>
#include <jni.h>
#include "AndroidUtil.h"
#include "EventEmitter.h"
#include "JNIUtil.h"
#include "JSException.h"
#include "Proxy.h"
#include "ProxyFactory.h"
#include "V8Runtime.h"
#include "V8Util.h"
#include "TypeConverter.h"
#include "KrollBindings.h"
// Generated Javascript -> C++ code
#include "KrollJS.cpp"
// Generated perfect hash for native bindings
#include "KrollNativeBindings.cpp"
// Generated perfect hash for generated bindings
#include "KrollGeneratedBindings.cpp"
#define TAG "KrollBindings"
namespace titanium {
using namespace v8;
std::map<std::string, bindings::BindEntry*> KrollBindings::externalBindings;
std::map<std::string, jobject> KrollBindings::externalCommonJsModules;
std::map<std::string, jmethodID> KrollBindings::commonJsSourceRetrievalMethods;
std::vector<LookupFunction> KrollBindings::externalLookups;
std::map<std::string, bindings::BindEntry*> KrollBindings::externalLookupBindings;
void KrollBindings::initFunctions(Local<Object> exports, Local<Context> context)
{
Isolate* isolate = context->GetIsolate();
SetMethod(isolate, exports, "binding", KrollBindings::getBinding);
SetMethod(isolate, exports, "externalBinding", KrollBindings::getExternalBinding);
SetMethod(isolate, exports, "isExternalCommonJsModule", KrollBindings::isExternalCommonJsModule);
SetMethod(isolate, exports, "getExternalCommonJsModule", KrollBindings::getExternalCommonJsModule);
}
void KrollBindings::initNatives(Local<Object> exports, Local<Context> context)
{
Isolate* isolate = context->GetIsolate();
HandleScope scope(isolate);
for (int i = 0; natives[i].name; ++i) {
if (natives[i].source == kroll_native) continue;
Local<String> name = String::NewFromUtf8(isolate, natives[i].name);
Local<String> source = IMMUTABLE_STRING_LITERAL_FROM_ARRAY(isolate, natives[i].source, natives[i].source_length);
exports->Set(name, source);
}
}
void KrollBindings::initTitanium(Local<Object> exports, Local<Context> context)
{
Isolate* isolate = context->GetIsolate();
HandleScope scope(isolate);
JNIEnv *env = JNIScope::getEnv();
if (!env) {
LOGE(TAG, "Couldn't initialize JNIEnv");
return;
}
Proxy::bindProxy(exports, context);
KrollProxy::bindProxy(exports, context);
KrollModule::bindProxy(exports, context);
TitaniumModule::bindProxy(exports, context);
}
void KrollBindings::disposeTitanium(Isolate* isolate)
{
Proxy::dispose(isolate);
KrollProxy::dispose(isolate);
KrollModule::dispose(isolate);
TitaniumModule::dispose(isolate);
}
static Persistent<Object> bindingCache;
void KrollBindings::getBinding(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
if (args.Length() == 0 || !(args[0]->IsString())) {
JSException::Error(isolate, "Invalid arguments to binding, expected String");
return;
}
Local<Object> binding = getBinding(isolate, args[0].As<String>());
if (binding.IsEmpty()) {
return;
}
args.GetReturnValue().Set(binding);
}
void KrollBindings::getExternalBinding(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
if (args.Length() == 0 || !(args[0]->IsString())) {
JSException::Error(args.GetIsolate(), "Invalid arguments to externalBinding, expected String");
return;
}
Local<String> binding = args[0].As<String>();
Local<Object> cache = bindingCache.Get(isolate);
if (cache->Has(binding)) {
args.GetReturnValue().Set(cache->Get(binding)->ToObject(isolate));
return;
}
titanium::Utf8Value bindingValue(binding);
std::string key(*bindingValue);
struct bindings::BindEntry *externalBinding = externalBindings[key];
if (externalBinding) {
Local<Object> exports = Object::New(isolate);
Local<Context> context = isolate->GetCurrentContext();
externalBinding->bind(exports, context);
cache->Set(binding, exports);
args.GetReturnValue().Set(exports);
}
}
void KrollBindings::addExternalBinding(const char *name, struct bindings::BindEntry *binding)
{
externalBindings[std::string(name)] = binding;
}
void KrollBindings::addExternalLookup(LookupFunction lookup)
{
externalLookups.push_back(lookup);
}
Local<Object> KrollBindings::getBinding(v8::Isolate* isolate, Local<String> binding)
{
Local<Object> cache;
if (bindingCache.IsEmpty()) {
cache = Object::New(isolate);
bindingCache.Reset(isolate, cache);
} else {
cache = bindingCache.Get(isolate);
}
if (cache->Has(binding)) {
return cache->Get(binding)->ToObject(isolate);
}
titanium::Utf8Value bindingValue(binding);
int length = bindingValue.length();
// TODO Combine all this duplicated code here...
struct bindings::BindEntry *native = bindings::native::lookupBindingInit(*bindingValue, length);
if (native) {
Local<Object> exports = Object::New(isolate);
native->bind(exports, isolate->GetCurrentContext());
cache->Set(binding, exports);
return exports;
}
struct bindings::BindEntry* generated = bindings::generated::lookupGeneratedInit(*bindingValue, length);
if (generated) {
Local<Object> exports = Object::New(isolate);
generated->bind(exports, isolate->GetCurrentContext());
cache->Set(binding, exports);
return exports;
}
for (int i = 0; i < KrollBindings::externalLookups.size(); i++) {
titanium::LookupFunction lookupFunction = KrollBindings::externalLookups[i];
struct bindings::BindEntry* external = (*lookupFunction)(*bindingValue, length);
if (external) {
Local<Object> exports = Object::New(isolate);
external->bind(exports, isolate->GetCurrentContext());
cache->Set(binding, exports);
externalLookupBindings[*bindingValue] = external;
return exports;
}
}
return Local<Object>();
}
// Dispose of all static function templates
// in the generated and native bindings. This
// clears out the module lookup cache
void KrollBindings::dispose(v8::Isolate* isolate)
{
JNIEnv *env = JNIScope::getEnv();
std::map<std::string, jobject>::iterator iterMods;
for (iterMods = externalCommonJsModules.begin(); iterMods != externalCommonJsModules.end(); ++iterMods) {
jobject obj = iterMods->second;
env->DeleteGlobalRef(obj);
}
externalCommonJsModules.clear();
commonJsSourceRetrievalMethods.clear();
// Dispose all external bindings
std::map<std::string, bindings::BindEntry *>::iterator iter;
for (iter = externalBindings.begin(); iter != externalBindings.end(); ++iter) {
bindings::BindEntry *external = iter->second;
if (external && external->dispose) {
external->dispose(isolate);
}
}
if (bindingCache.IsEmpty()) {
return;
}
Local<Object> cache = bindingCache.Get(isolate);
Local<Array> propertyNames = cache->GetPropertyNames();
uint32_t length = propertyNames->Length();
for (uint32_t i = 0; i < length; i++) {
titanium::Utf8Value binding(propertyNames->Get(i));
int bindingLength = binding.length();
struct titanium::bindings::BindEntry *generated = bindings::generated::lookupGeneratedInit(*binding, bindingLength);
if (generated && generated->dispose) {
generated->dispose(isolate);
continue;
}
struct titanium::bindings::BindEntry *native = bindings::native::lookupBindingInit(*binding, bindingLength);
if (native && native->dispose) {
native->dispose(isolate);
continue;
}
struct titanium::bindings::BindEntry *lookup = externalLookupBindings[*binding];
if (lookup && lookup->dispose) {
lookup->dispose(isolate);
continue;
}
}
externalLookupBindings.clear();
bindingCache.Reset();
}
/*
* Stores a java KrollSourceCodeProvider instance and the id of its getSourceCode method
* for an external CommonJS module that is stored in a java external module.
*/
void KrollBindings::addExternalCommonJsModule(const char *name, jobject sourceProvider, jmethodID sourceRetrievalMethod)
{
std::string stringName(name);
externalCommonJsModules[stringName] = sourceProvider;
commonJsSourceRetrievalMethods[stringName] = sourceRetrievalMethod;
}
/*
* Checks if an external CommonJS module with given name has been registered
* here.
*/
void KrollBindings::isExternalCommonJsModule(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if (args.Length() == 0 || !(args[0]->IsString())) {
JSException::Error(isolate, "Invalid arguments to isExternalCommonJsModule, expected String");
return;
}
v8::Local<v8::String> name = args[0].As<String>();
titanium::Utf8Value nameVal(name);
std::string nameKey(*nameVal);
bool exists = (externalCommonJsModules.count(nameKey) > 0);
args.GetReturnValue().Set(exists);
}
/*
* Makes the KrollSourceCodeProvider's getSourceCode method call to grab
* the source code of a CommonJS module stored in a native (java) external module.
*/
void KrollBindings::getExternalCommonJsModule(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
EscapableHandleScope scope(args.GetIsolate());
if (args.Length() == 0 || !(args[0]->IsString())) {
JSException::Error(isolate, "Invalid arguments to getExternalCommonJsBinding, expected String");
return;
}
v8::Local<v8::String> name = args[0].As<String>();
titanium::Utf8Value nameVal(name);
std::string nameKey(*nameVal);
std::string moduleRoot = nameKey;
std::string subPath = nameKey;
int slashPos = nameKey.find("/", 0);
if (slashPos != std::string::npos) {
moduleRoot = nameKey.substr(0, slashPos);
subPath = nameKey.substr(slashPos + 1);
}
bool exists = (externalCommonJsModules.count(moduleRoot) > 0);
if (!exists) {
args.GetReturnValue().Set(v8::Undefined(isolate));
return;
}
JNIEnv *env = JNIScope::getEnv();
jobject sourceProvider = externalCommonJsModules[moduleRoot];
jmethodID sourceRetrievalMethod = commonJsSourceRetrievalMethods[moduleRoot];
// The older version of KrollSourceCodeProvider.getSourceCode() (the method being called
// below) took no arguments, because we only
// supported one possible CommonJS module file packaged in a native module. There could be some
// modules out there that were created during the time when we only had that no-arg version of
// getSourceCode(), so we have to continue to support that. But we first try the newer version:
// getSourceCode(String), which allows you to get any CommonJS module packaged with the native
// module since we now support multiple CommonJS modules.
jstring sourceJavaString = (jstring) env->CallObjectMethod(sourceProvider,
sourceRetrievalMethod, env->NewStringUTF(subPath.c_str()));
jthrowable exc = env->ExceptionOccurred();
if (exc && slashPos == std::string::npos) {
// An exception occurred trying the newer getSourceCode(String).
// Try the old, no-arg way of getting source, but only if indeed the
// root module is being requested (i.e., no slashes in path).
env->ExceptionClear();
sourceRetrievalMethod = env->GetMethodID(env->GetObjectClass(sourceProvider),
"getSourceCode", "()Ljava/lang/String;");
if (sourceRetrievalMethod) {
sourceJavaString = (jstring) env->CallObjectMethod(sourceProvider, sourceRetrievalMethod);
}
}
v8::Local<v8::Value> sourceCode = TypeConverter::javaStringToJsString(isolate, env, sourceJavaString);
args.GetReturnValue().Set(scope.Escape(sourceCode));
}
Local<String> KrollBindings::getMainSource(v8::Isolate* isolate)
{
return IMMUTABLE_STRING_LITERAL_FROM_ARRAY(isolate, kroll_native, sizeof(kroll_native)-1);
}
}