/* static */ void V8Runtime::bootstrap(Local<Context> context) { Isolate* isolate = context->GetIsolate(); EventEmitter::initTemplate(context); Local<Object> kroll = Object::New(isolate); krollGlobalObject.Reset(isolate, kroll); Local<Array> mc = Array::New(isolate); moduleContexts.Reset(isolate, mc); KrollBindings::initFunctions(kroll, context); SetMethod(isolate, kroll, "log", krollLog); // Move this into the EventEmitter::initTemplate call? Local<FunctionTemplate> eect = Local<FunctionTemplate>::New(isolate, EventEmitter::constructorTemplate); { v8::TryCatch tryCatch(isolate); Local<Function> eventEmitterConstructor; MaybeLocal<Function> maybeEventEmitterConstructor = eect->GetFunction(context); if (!maybeEventEmitterConstructor.ToLocal(&eventEmitterConstructor)) { titanium::V8Util::fatalException(isolate, tryCatch); return; } kroll->Set(NEW_SYMBOL(isolate, "EventEmitter"), eventEmitterConstructor); } kroll->Set(NEW_SYMBOL(isolate, "runtime"), STRING_NEW(isolate, "v8")); kroll->Set(NEW_SYMBOL(isolate, "DBG"), v8::Boolean::New(isolate, V8Runtime::DBG)); kroll->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); } // Add a reference to the global object Local<Object> global = context->Global(); // Expose the global object as a property on itself // (Allows you to set stuff on `global` from anywhere in JavaScript.) global->Set(NEW_SYMBOL(isolate, "global"), global); Local<Function> mainFunction = result.As<Function>(); Local<Value> args[] = { kroll }; mainFunction->Call(context, global, 1, args); if (tryCatch.HasCaught()) { V8Util::reportException(isolate, tryCatch, true); LOGE(TAG, "Caught exception while bootstrapping Kroll"); } }
LispObject GC_MakeString(char *str) { LispObject o; struct LispString *s = STRING_NEW(); s->str = StringAlloc(str); s->len = (int)strlen(str); SET_STRING(o, s); return o; }
static void setPropertyOnProxy(Isolate* isolate, Local<Name> property, Local<Value> value, Local<Object> proxy) { // Call Proxy.prototype.setProperty. Local<Value> setProperty = proxy->Get(STRING_NEW(isolate, "setProperty")); if (!setProperty.IsEmpty() && setProperty->IsFunction()) { Local<Value> argv[2] = { property, value }; setProperty.As<Function>()->Call(isolate->GetCurrentContext(), proxy, 2, argv); return; } LOGE(TAG, "Unable to lookup Proxy.prototype.setProperty"); }
ORGANIZATION *new_org(void) { static ORGANIZATION relg_zero; ORGANIZATION *pRlg; int rank;//, faction, iClass; if (org_free == NULL) pRlg = alloc_perm(sizeof(*pRlg)); else { pRlg = org_free; org_free = org_free->next; } *pRlg = relg_zero; STRING_NEW(pRlg->pmt_msg); STRING_NEW(pRlg->dmt_msg); STRING_NEW(pRlg->chan_name); STRING_NEW(pRlg->sac_msg ); STRING_NEW(pRlg->patron); STRING_NEW(pRlg->name ); for(rank = 0; rank < MAX_RELG_RANK ; rank++ ) { free_string(pRlg->rank[rank]); pRlg->rank[rank] = str_dup(orgrank_table[rank].name ); } VALIDATE(pRlg); return pRlg; }
static Local<Value> getPropertyForProxy(Isolate* isolate, Local<Name> property, Local<Object> proxy) { // Call getProperty on the Proxy to get the property. // We define this method in JavaScript on the Proxy prototype. Local<Value> getProperty = proxy->Get(STRING_NEW(isolate, "getProperty")); if (!getProperty.IsEmpty() && getProperty->IsFunction()) { Local<Value> argv[1] = { property }; MaybeLocal<Value> value = getProperty.As<Function>()->Call(isolate->GetCurrentContext(), proxy, 1, argv); if (value.IsEmpty()) { return Undefined(isolate); } return value.ToLocalChecked(); } LOGE(TAG, "Unable to lookup Proxy.prototype.getProperty"); return Undefined(isolate); }
/* 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"); } }
// 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>()); } v8::String::Utf8Value tagValue(tag); v8::String::Utf8Value messageValue(message); __android_log_print(ANDROID_LOG_DEBUG, *tagValue, *messageValue); }
void Proxy::proxyConstructor(const v8::FunctionCallbackInfo<v8::Value>& args) { LOGD(TAG, "proxy constructor callback!"); Isolate* isolate = args.GetIsolate(); EscapableHandleScope scope(isolate); JNIEnv *env = JNIScope::getEnv(); Local<Object> jsProxy = args.This(); // First things first, we need to wrap the object in case future calls need to unwrap proxy! Proxy* proxy = new Proxy(NULL); proxy->wrap(isolate, jsProxy); // every instance gets a special "_properties" object for us to use internally for get/setProperty jsProxy->DefineOwnProperty(isolate->GetCurrentContext(), propertiesSymbol.Get(isolate), Object::New(isolate), static_cast<PropertyAttribute>(DontEnum)); // Now we hook up a java Object from the JVM... jobject javaProxy = ProxyFactory::unwrapJavaProxy(args); // do we already have one that got passed in? bool deleteRef = false; if (!javaProxy) { // No passed in java object, so let's create an instance // Look up java class from prototype... Local<Object> prototype = jsProxy->GetPrototype()->ToObject(isolate); Local<Function> constructor = prototype->Get(constructorSymbol.Get(isolate)).As<Function>(); Local<External> wrap = constructor->Get(javaClassSymbol.Get(isolate)).As<External>(); jclass javaClass = static_cast<jclass>(wrap->Value()); // Now we create an instance of the class and hook it up JNIUtil::logClassName("Creating java proxy for class %s", javaClass); javaProxy = ProxyFactory::createJavaProxy(javaClass, jsProxy, args); deleteRef = true; } proxy->attach(javaProxy); int length = args.Length(); if (length > 0 && args[0]->IsObject()) { bool extend = true; Local<Object> createProperties = args[0].As<Object>(); Local<String> constructorName = createProperties->GetConstructorName(); if (strcmp(*titanium::Utf8Value(constructorName), "Arguments") == 0) { extend = false; int32_t argsLength = createProperties->Get(STRING_NEW(isolate, "length"))->Int32Value(); if (argsLength > 1) { Local<Value> properties = createProperties->Get(1); if (properties->IsObject()) { extend = true; createProperties = properties.As<Object>(); } } } if (extend) { Local<Array> names = createProperties->GetOwnPropertyNames(); int length = names->Length(); Local<Object> properties = jsProxy->Get(propertiesSymbol.Get(isolate))->ToObject(isolate); for (int i = 0; i < length; ++i) { Local<Value> name = names->Get(i); Local<Value> value = createProperties->Get(name); bool isProperty = true; if (name->IsString()) { Local<String> nameString = name.As<String>(); if (!jsProxy->HasRealNamedCallbackProperty(nameString) && !jsProxy->HasRealNamedProperty(nameString)) { jsProxy->Set(name, value); isProperty = false; } } if (isProperty) { properties->Set(name, value); } } } } if (!args.Data().IsEmpty() && args.Data()->IsFunction()) { Local<Function> proxyFn = args.Data().As<Function>(); Local<Value> *fnArgs = new Local<Value>[length]; for (int i = 0; i < length; ++i) { fnArgs[i] = args[i]; } proxyFn->Call(isolate->GetCurrentContext(), jsProxy, length, fnArgs); } if (deleteRef) { JNIEnv *env = JNIScope::getEnv(); if (env) { env->DeleteLocalRef(javaProxy); } } args.GetReturnValue().Set(scope.Escape(jsProxy)); }