forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
V8Runtime.cpp
444 lines (358 loc) · 14.1 KB
/
V8Runtime.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* 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 <jni.h>
#include <stdio.h>
#include <string.h>
#include <v8.h>
#include <libplatform/libplatform.h>
#include <v8-debug.h>
#include "AndroidUtil.h"
#include "EventEmitter.h"
#include "JavaObject.h"
#include "JNIUtil.h"
#include "JSDebugger.h"
#include "JSException.h"
#include "KrollBindings.h"
#include "ProxyFactory.h"
#include "ScriptsModule.h"
#include "TypeConverter.h"
#include "V8Util.h"
#include "V8Runtime.h"
#include "org_appcelerator_kroll_runtime_v8_V8Runtime.h"
#define TAG "V8Runtime"
// The port number on which the V8 debugger will listen on.
#define V8_DEBUGGER_PORT 9999
namespace titanium {
Persistent<Context> V8Runtime::globalContext;
Persistent<Object> V8Runtime::krollGlobalObject;
Persistent<Array> V8Runtime::moduleContexts;
Persistent<Object> V8Runtime::moduleObject;
Persistent<Function> V8Runtime::runModuleFunction;
jobject V8Runtime::javaInstance;
Platform* V8Runtime::platform = nullptr;
Isolate* V8Runtime::v8_isolate = nullptr;
bool V8Runtime::debuggerEnabled = false;
bool V8Runtime::DBG = false;
bool V8Runtime::initialized = false;
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) { return calloc(length, 1); }
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
// Make allocator global so it sticks around?
ArrayBufferAllocator allocator;
/* static */
void V8Runtime::collectWeakRef(Persistent<Value> ref, void *parameter)
{
jobject v8Object = (jobject) parameter;
ref.Reset();
JNIScope::getEnv()->DeleteGlobalRef(v8Object);
}
Local<Object> V8Runtime::Global()
{
return krollGlobalObject.Get(v8_isolate);
}
Local<Context> V8Runtime::GlobalContext()
{
return globalContext.Get(v8_isolate);
}
Local<Object> V8Runtime::ModuleObject()
{
return moduleObject.Get(v8_isolate);
}
Local<Function> V8Runtime::RunModuleFunction()
{
return runModuleFunction.Get(v8_isolate);
}
Local<Array> V8Runtime::ModuleContexts()
{
return moduleContexts.Get(v8_isolate);
}
// Minimalistic logging function for internal JS
static void krollLog(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
uint32_t len = args.Length();
if (len < 2) {
JSException::Error(isolate, "log: missing required tag and message arguments");
return;
}
Local<String> tag = args[0].As<String>();
Local<String> message = args[1].As<String>();
Local<String> space = STRING_NEW(isolate, " ");
for (uint32_t i = 2; i < len; ++i) {
message = String::Concat(String::Concat(message, space), args[i].As<String>());
}
titanium::Utf8Value tagValue(tag);
titanium::Utf8Value messageValue(message);
__android_log_print(ANDROID_LOG_DEBUG, *tagValue, *messageValue);
}
/* static */
void V8Runtime::bootstrap(Local<Context> context)
{
Isolate* isolate = context->GetIsolate();
EventEmitter::initTemplate(context);
Local<Object> global = Object::New(isolate);
krollGlobalObject.Reset(isolate, global);
Local<Array> mc = Array::New(isolate);
moduleContexts.Reset(isolate, mc);
KrollBindings::initFunctions(global, context);
SetMethod(isolate, global, "log", krollLog);
// Move this into the EventEmitter::initTemplate call?
Local<FunctionTemplate> eect = Local<FunctionTemplate>::New(isolate, EventEmitter::constructorTemplate);
global->Set(NEW_SYMBOL(isolate, "EventEmitter"), eect->GetFunction());
global->Set(NEW_SYMBOL(isolate, "runtime"), STRING_NEW(isolate, "v8"));
global->Set(NEW_SYMBOL(isolate, "DBG"), v8::Boolean::New(isolate, V8Runtime::DBG));
global->Set(NEW_SYMBOL(isolate, "moduleContexts"), mc);
LOG_TIMER(TAG, "Executing kroll.js");
TryCatch tryCatch(isolate);
Local<Value> result = V8Util::executeString(isolate, KrollBindings::getMainSource(isolate), STRING_NEW(isolate, "ti:/kroll.js"));
if (tryCatch.HasCaught()) {
V8Util::reportException(isolate, tryCatch, true);
}
if (!result->IsFunction()) {
LOGF(TAG, "kroll.js result is not a function");
V8Util::reportException(isolate, tryCatch, true);
}
Local<Function> mainFunction = result.As<Function>();
Local<Value> args[] = { global };
mainFunction->Call(context, context->Global(), 1, args);
if (tryCatch.HasCaught()) {
V8Util::reportException(isolate, tryCatch, true);
LOGE(TAG, "Caught exception while bootstrapping Kroll");
}
}
static void logV8Exception(Local<Message> msg, Local<Value> data)
{
HandleScope scope(V8Runtime::v8_isolate);
// Log reason and location of the error.
LOGD(TAG, *titanium::Utf8Value(msg->Get()));
LOGD(TAG, "%s @ %d >>> %s",
*titanium::Utf8Value(msg->GetScriptResourceName()),
msg->GetLineNumber(),
*titanium::Utf8Value(msg->GetSourceLine()));
}
} // namespace titanium
#ifdef __cplusplus
extern "C" {
#endif
using namespace titanium;
/*
* Class: org_appcelerator_kroll_runtime_v8_V8Runtime
* Method: nativeInit
* Signature: (Lorg/appcelerator/kroll/runtime/v8/V8Runtime;)J
*/
JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeInit(JNIEnv *env, jobject self, jboolean useGlobalRefs, jobject debugger, jboolean DBG, jboolean profilerEnabled)
{
if (!V8Runtime::initialized) {
// Initialize V8.
V8::InitializeICU();
// TODO Enable this when we use snapshots?
//V8::InitializeExternalStartupData(argv[0]);
V8Runtime::platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(V8Runtime::platform);
V8::Initialize();
V8Runtime::initialized = true;
}
if (profilerEnabled) {
char* argv[] = { const_cast<char*>(""), const_cast<char*>("--expose-gc") };
int argc = sizeof(argv)/sizeof(*argv);
V8::SetFlagsFromCommandLine(&argc, argv, false);
}
titanium::JNIScope jniScope(env);
JavaObject::useGlobalRefs = useGlobalRefs;
V8Runtime::DBG = DBG;
V8Runtime::javaInstance = env->NewGlobalRef(self);
JNIUtil::initCache();
Isolate* isolate;
if (V8Runtime::v8_isolate == nullptr) {
// Create a new Isolate and make it the current one.
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
isolate = Isolate::New(create_params);
isolate->Enter();
V8Runtime::v8_isolate = isolate;
// Log all uncaught V8 exceptions.
V8::AddMessageListener(&logV8Exception);
V8::SetCaptureStackTraceForUncaughtExceptions(true);
} else {
isolate = V8Runtime::v8_isolate;
isolate->Enter();
}
HandleScope scope(isolate);
Local<Context> context = Context::New(isolate);
context->Enter();
V8Runtime::globalContext.Reset(isolate, context);
JSDebugger::init(env, isolate, debugger);
if (debugger != nullptr) {
V8Runtime::debuggerEnabled = true;
}
V8Runtime::bootstrap(context);
LOG_HEAP_STATS(isolate, TAG);
}
/*
* Class: org_appcelerator_kroll_runtime_v8_V8Runtime
* Method: nativeRunModule
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeRunModule
(JNIEnv *env, jobject self, jstring source, jstring filename, jobject activityProxy)
{
HandleScope scope(V8Runtime::v8_isolate);
titanium::JNIScope jniScope(env);
if (V8Runtime::moduleObject.IsEmpty()) {
Local<Object> module = V8Runtime::Global()->Get(STRING_NEW(V8Runtime::v8_isolate, "Module")).As<Object>();
V8Runtime::moduleObject.Reset(V8Runtime::v8_isolate, module);
V8Runtime::runModuleFunction.Reset(V8Runtime::v8_isolate,
module->Get(STRING_NEW(V8Runtime::v8_isolate, "runModule")).As<Function>());
}
Local<Value> jsSource = TypeConverter::javaStringToJsString(V8Runtime::v8_isolate, env, source);
Local<Value> jsFilename = TypeConverter::javaStringToJsString(V8Runtime::v8_isolate, env, filename);
Local<Value> jsActivity = TypeConverter::javaObjectToJsValue(V8Runtime::v8_isolate, env, activityProxy);
Local<Value> args[] = { jsSource, jsFilename, jsActivity };
TryCatch tryCatch(V8Runtime::v8_isolate);
V8Runtime::RunModuleFunction()->Call(V8Runtime::v8_isolate->GetCurrentContext(), V8Runtime::ModuleObject(), 3, args);
if (tryCatch.HasCaught()) {
V8Util::openJSErrorDialog(V8Runtime::v8_isolate, tryCatch);
V8Util::reportException(V8Runtime::v8_isolate, tryCatch, true);
}
}
JNIEXPORT jobject JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeEvalString
(JNIEnv *env, jobject self, jstring source, jstring filename)
{
HandleScope scope(V8Runtime::v8_isolate);
titanium::JNIScope jniScope(env);
Local<Value> jsSource = TypeConverter::javaStringToJsString(V8Runtime::v8_isolate, env, source);
if (jsSource.IsEmpty() || !jsSource->IsString()) {
LOGE(TAG, "Error converting Javascript string, aborting evalString");
return NULL;
}
Local<Value> jsFilename = TypeConverter::javaStringToJsString(V8Runtime::v8_isolate, env, filename);
TryCatch tryCatch(V8Runtime::v8_isolate);
Local<Script> script = Script::Compile(jsSource.As<String>(), jsFilename.As<String>());
Local<Value> result = script->Run();
if (tryCatch.HasCaught()) {
V8Util::openJSErrorDialog(V8Runtime::v8_isolate, tryCatch);
V8Util::reportException(V8Runtime::v8_isolate, tryCatch, true);
return NULL;
}
return TypeConverter::jsValueToJavaObject(V8Runtime::v8_isolate, env, result);
}
JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeProcessDebugMessages(JNIEnv *env, jobject self)
{
v8::Debug::ProcessDebugMessages();
}
JNIEXPORT jboolean JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeIdle(JNIEnv *env, jobject self)
{
// If we're closing up shop, return true, which is equivalent to V8 GC saying there's no more work to do
//if (V8Runtime::disposed) {
// return true;
//}
// FIXME What is a good value to use here? We're basically giving it 100 ms to run right now
double deadline_in_ms = (V8Runtime::platform->MonotonicallyIncreasingTime() * static_cast<double>(1000)) + 100.0;
return V8Runtime::v8_isolate->IdleNotificationDeadline(deadline_in_ms);
}
/*
* Called by V8Runtime.java, this passes a KrollSourceCodeProvider java class instance
* to KrollBindings, where it's stored and later used to retrieve an external CommonJS module's
* Javascript code when require(moduleName) occurs in Javascript.
* "External" CommonJS modules are CommonJS modules stored in external modules.
*/
JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeAddExternalCommonJsModule
(JNIEnv *env, jobject self, jstring moduleName, jobject sourceProvider)
{
const char* mName = env->GetStringUTFChars(moduleName, NULL);
jclass cls = env->GetObjectClass(sourceProvider);
if (!cls) {
LOGE(TAG, "Could not find source code provider class for module: %s", mName);
return;
}
jmethodID method = env->GetMethodID(cls, "getSourceCode", "(Ljava/lang/String;)Ljava/lang/String;");
if (!method) {
LOGE(TAG, "Could not find getSourceCode method in source code provider class for module: %s", mName);
return;
}
KrollBindings::addExternalCommonJsModule(mName, env->NewGlobalRef(sourceProvider), method);
}
// This method disposes of all native resources used by V8 when
// all activities have been destroyed by the application.
//
// When a Persistent handle is Dispose()'d in V8, the internal
// pointer is not changed, handle->IsEmpty() returns false.
// As a consequence, we have to explicitly reset the handle
// to an empty handle using Persistent<Type>()
//
// Since we use lazy initialization in a lot of our code,
// there's probably not an easier way (unless we use boolean flags)
JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeDispose(JNIEnv *env, jobject runtime)
{
JNIScope jniScope(env);
// We use a new scope here so any new handles we create
// while disposing are cleaned up before our global context
// is disposed below.
{
HandleScope scope(V8Runtime::v8_isolate);
// Any module that has been require()'d or opened via Window URL
// will be cleaned up here. We setup the initial "moduleContexts"
// Array and expose it on kroll above in nativeInit, and
// module.js will insert module contexts into this array in
// Module.prototype._runScript
uint32_t length = V8Runtime::ModuleContexts()->Length();
for (uint32_t i = 0; i < length; ++i) {
Local<Value> moduleContext = V8Runtime::ModuleContexts()->Get(i);
// WrappedContext is simply a C++ wrapper for the V8 Context object,
// and is used to expose the Context to javascript. See ScriptsModule for
// implementation details
WrappedContext *wrappedContext = WrappedContext::Unwrap(V8Runtime::v8_isolate, moduleContext.As<Object>());
ASSERT(wrappedContext != NULL);
wrappedContext->Dispose();
}
// KrollBindings
KrollBindings::dispose(V8Runtime::v8_isolate);
EventEmitter::dispose();
V8Runtime::moduleContexts.Reset();
V8Runtime::GlobalContext()->DetachGlobal();
}
// Dispose of each class' static cache / resources
V8Util::dispose();
ProxyFactory::dispose();
V8Runtime::moduleObject.Reset();
V8Runtime::runModuleFunction.Reset();
V8Runtime::krollGlobalObject.Reset();
{
HandleScope scope(V8Runtime::v8_isolate);
V8Runtime::GlobalContext()->Exit();
}
V8Runtime::globalContext.Reset();
// Removes the retained global reference to the V8Runtime
env->DeleteGlobalRef(V8Runtime::javaInstance);
V8Runtime::javaInstance = NULL;
// Whereas most calls to IdleNotification get kicked off via Java (the looper's
// idle event in V8Runtime.java), we can't count on that running anymore at this point.
// So as our last act, run IdleNotification until it returns true so we can clean up all
// the stuff we just released references for above.
while (!V8Runtime::v8_isolate->IdleNotificationDeadline(V8Runtime::platform->MonotonicallyIncreasingTime() + 1.0));
// Typically in a V8 embedded app, we'd clean everything up here. But since
// an app may just be closed/backgrounded but still alive, we can't do this
// because we can't re-initialize once it's disposed.
// Do final cleanup
V8Runtime::v8_isolate->Exit();
//V8Runtime::v8_isolate->Dispose();
//V8::Dispose();
//V8::ShutdownPlatform();
//delete V8Runtime::platform;
}
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
JNIUtil::javaVm = vm;
return JNI_VERSION_1_4;
}
#ifdef __cplusplus
}
#endif