bool IsInheritTemplate(Handle<Value> val,uint tid){ if(!val->IsObject()) return false; Local<Function> func = GetEnv()->GetTemplate(tid)->GetFunction(); Local<Object> obj = val->ToObject()->GetPrototype()->ToObject(); while(true){ if(obj->GetPrototype()->IsNull()) return false; if(obj->GetConstructorName()==func->GetName()->ToString()) return true; obj = obj->GetPrototype()->ToObject(); } }
TEST_F(JSDeviceTest, createIstance) { V8_SETUP EXPECT_CALL(*zDevices.get(), exists(extAddress)).WillOnce(Return(true)); v8::Local<v8::Value> result = runScript(creatingZDeviceScript + "a;"); String::Utf8Value utf8(result); ASSERT_THAT(result.IsEmpty(), false); ASSERT_THAT(result->IsObject(), true); Local<Object> object = result->ToObject(); ASSERT_THAT(object->GetConstructorName(), IsString(JSZDEVICE)); }
TEST_F( JSEndpointTest, createIstance) { ZEndpoint zEndpoint { NWK_ADDRESS, ENDPOINT_ID, PROFILE_ID, DEVICE_ID, DEVICE_VER, IN_CLUSTERS, OUT_CLUSTERS }; ZDevice zDevice { extAddress, NWK_ADDRESS, 0, { zEndpoint } }; std::stringstream stream { }; stream << JSZENDPOINT << "('" << EXTENDED_ADDRESS << "', " << ENDPOINT_ID << ");"; V8_SETUP jsEndpoint->initJsObjectsTemplate(isolate, global); EXPECT_CALL(*zDevices.get(), exists(extAddress)).WillOnce(Return(true)); EXPECT_CALL(*zDevices.get(), getDevice(extAddress)).WillOnce(Return(&zDevice)); v8::Local<v8::Value> result = runScript(stream.str()); ASSERT_THAT(result.IsEmpty(), false); ASSERT_THAT(result->IsObject(), true); Local<Object> object = result->ToObject(); ASSERT_THAT(object->GetConstructorName(), IsString(JSZENDPOINT)); }
void JSAttributeTest::createIstanceTest(const std::string &attributeName, std::shared_ptr<JSZAttribute> &jsZAttribute, std::shared_ptr<ZCLAttribute> attributeMock) { ZEndpoint zEndpoint{NWK_ADDRESS, ENDPOINT_ID, PROFILE_ID, DEVICE_ID, DEVICE_VER, IN_CLUSTERS, OUT_CLUSTERS}; ZDevice zDevice{extAddress, NWK_ADDRESS, 0, {zEndpoint}}; std::stringstream stream{}; stream << attributeName << "('" << EXTENDED_ADDRESS << "', " << ENDPOINT_ID << ", " << CLUSTER_ID << "," << ATTRIBUTE0_ID << ");"; V8_SETUP jsZAttribute->initJsObjectsTemplate(isolate, global); EXPECT_CALL(*zDevices, exists(extAddress)).WillOnce(Return(true)); EXPECT_CALL(*zDevices, getDevice(extAddress)).WillOnce(Return(&zDevice)); EXPECT_CALL(*clusterTypeFactoryMock, getCluster(CLUSTER_ID, zigbeeDevice, ENDPOINT_ID, NWK_ADDRESS)).WillOnce(Return(cluster)); EXPECT_CALL(*cluster, getAttribute(ATTRIBUTE0_ID)).WillOnce(Return(attributeMock)); v8::Local<v8::Value> result = runScript(stream.str()); ASSERT_THAT(result.IsEmpty(), false); ASSERT_THAT(result->IsObject(), true); Local<Object> object = result->ToObject(); ASSERT_THAT(object->GetConstructorName(), IsString(attributeName)); }
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)); }