Exemplo n.º 1
0
int SerializePart(google::protobuf::Message *message, Handle<Object> subj) {
	NanScope();

	// get a reflection
	const Reflection *r = message->GetReflection();
	const Descriptor *d = message->GetDescriptor();
	
	// build a list of required properties
	vector<string> required;
	for (int i = 0; i < d->field_count(); i++) {
		const FieldDescriptor *field = d->field(i);
		if (field->is_required())
			required.push_back(field->name());
	}

	// build a reflection
	// get properties of passed object
	Local<Array> properties = subj->GetPropertyNames();
	uint32_t len = properties->Length();
	
	// check that all required properties are present
	for (uint32_t i = 0; i < required.size(); i++) {
		Handle<String> key = String::New(required.at(i).c_str());
		if (!subj->Has(key))
			return -1;
	}

	for (uint32_t i = 0; i < len; i++) {
		Local<Value> property = properties->Get(i);
		Local<String> property_s = property->ToString();

		if (*property_s == NULL)
			continue;

		String::Utf8Value temp(property);
		std::string propertyName = std::string(*temp);

		const FieldDescriptor *field = d->FindFieldByName(propertyName);
		if (field == NULL) continue;

		Local<Value> val = subj->Get(property);

		if (field->is_repeated()) {
			if (!val->IsArray())
				continue;

			Handle<Array> array = val.As<Array>();
			int len = array->Length();

			for (int i = 0; i < len; i++)
				SerializeField(message, r, field, array->Get(i));

		} else {
			SerializeField(message, r, field, val);
		}
	}
	
	return 0;
}
void Ti::TiProxy::initWithObject(Handle<Object> obj)
{
	HandleScope scope;
	Local<Array> props = obj->GetPropertyNames();
	for(int i = 0, len = props->Length(); i < len; i++) {
		Local<String> key = props->Get(i)->ToString();
		Local<Value> val = obj->Get(key);
		_jsObject->Set(key, val);
	}
}
Exemplo n.º 3
0
	void CloneObject(Handle<Object> src, Handle<Object> dest){

		HandleScope scope; 
		v8::Local<v8::Array> keys = src->GetPropertyNames();
		TryCatch try_catch; 
		int ignored = 0; 
		for (uint32_t i = 0; i < keys->Length(); i++) {
			v8::Handle<v8::String> key = keys->Get(i)->ToString();
			if (key->Length() > 0){
				v8::Handle<v8::Value> value = src->Get(key);
				dest->Set(key, value);
			} 
		}
	}
void ContactsPersonProxy::_setEmail(void* userContext, Handle<Value> value)
{
    ContactsPersonProxy *obj = (ContactsPersonProxy*) userContext;

    if(!value->IsObject()) return;

	Handle<Object> emailObject = value->ToObject();
	Local<Array> emailProperties = emailObject->GetPropertyNames();
	for(int i = 0, len = emailProperties->Length(); i < len; i++)
	{
		Local<String> allEmailsKey = emailProperties->Get(i)->ToString();
		Local<Value> allEmailsValue = emailObject->Get(allEmailsKey);

		AttributeSubKind::Type subKind = AttributeSubKind::Other;

		QString allEmailsKeyString = titanium::V8ValueToQString(allEmailsKey).toLower();
		if(allEmailsKeyString == "work")
		{
			subKind = AttributeSubKind::Work;
		}
		else
		if(allEmailsKeyString == "personal")
		{
			subKind = AttributeSubKind::Personal;
		}
		else
		if(allEmailsKeyString == "home")
		{
			subKind = AttributeSubKind::Home;
		}

		if(!allEmailsValue->IsArray()) return;

		Local<Array> emails = Local<Array>::Cast(allEmailsValue);
		for(int i = 0, len = emails->Length(); i < len; i++)
		{
			Local<Value> emailValue = emails->Get(Number::New(i));
			if(emailValue->IsString() || emailValue->IsNumber())
			{
				 obj->setContactDetails(AttributeKind::Email, subKind, emailValue);
			}
			else
			{
				// Something goes here, throw an error?
			}
		}
	}
}
void ContactsPersonProxy::_setPhone(void* userContext, Handle<Value> value)
{
    ContactsPersonProxy *obj = (ContactsPersonProxy*) userContext;

    if(!value->IsObject()) return;

    Handle<Object> phoneObject = value->ToObject();
	Local<Array> phoneProperties = phoneObject->GetPropertyNames();

	for(int i = 0, len = phoneProperties->Length(); i < len; i++)
	{
		Local<String> phoneKey = phoneProperties->Get(i)->ToString();
		Local<Value> phoneValue = phoneObject->Get(phoneKey);

		AttributeSubKind::Type subKind = AttributeSubKind::Other;
		QString phoneStringValue = titanium::V8ValueToQString(phoneKey);

		if(phoneStringValue == "home") {
			subKind = AttributeSubKind::Home;
		} else
		if(phoneStringValue == "work") {
			subKind = AttributeSubKind::Work;
		} else
		if(phoneStringValue == "mobile") {
			subKind = AttributeSubKind::PhoneMobile;
		}

		if(!phoneValue->IsArray()) return;

		Local<Array> phones = Local<Array>::Cast(phoneValue);
		for(int i = 0, len = phones->Length(); i < len; i++)
		{
			Local<Value> currentMessage = phones->Get(Number::New(i));
			if(!currentMessage->IsString()) return;

			obj->setContactDetails(AttributeKind::Phone, subKind, currentMessage);
		}
	}
}
Exemplo n.º 6
0
   Handle<Value> MSAddSpawner(const Arguments& args)
   {
      HandleScope scope;
      dtEntity::MapSystem* ms = UnwrapMapSystem(args.This());

      if(args.Length() != 1)
      {
         return ThrowError("Usage: addSpawner({components, name, guicategory, mapname, addtospawnerstore, iconpath})");
      }

      Handle<Object> obj = Handle<Object>::Cast(args[0]);

      Handle<Value> vname = obj->Get(String::New("name"));
      Handle<Value> vcomponents = obj->Get(String::New("components"));

      if(vname.IsEmpty() || vcomponents.IsEmpty())
      {
         return ThrowError("Usage: addSpawner({components, name, guicategory, mapname, addtospawnerstore, iconpath, parentname})");
      }

      Handle<Value> vguicategory = obj->Get(String::New("guicategory"));
      Handle<Value> vmapname = obj->Get(String::New("mapname"));
      Handle<Value> vaddtospawnerstore = obj->Get(String::New("addtospawnerstore"));
      Handle<Value> viconpath = obj->Get(String::New("iconpath"));
      Handle<Value> vparentname = obj->Get(String::New("parentname"));

      std::string name = ToStdString(vname);
      std::string mapname = vmapname.IsEmpty() ? "" : ToStdString(vmapname);

      Handle<Object> components = Handle<Object>::Cast(vcomponents);

      dtEntity::Spawner* parent = NULL;

      if(!vparentname.IsEmpty() && !vparentname->IsUndefined())
      {
         ms->GetSpawner(ToStdString(vparentname), parent);
      }

      osg::ref_ptr<dtEntity::Spawner> spawner = new dtEntity::Spawner(name, mapname, parent);

      if(!vguicategory.IsEmpty() && !vguicategory->IsUndefined())
      {
         spawner->SetGUICategory(ToStdString(vguicategory));
      }

      if(!vaddtospawnerstore.IsEmpty() && !vaddtospawnerstore->IsUndefined())
      {
         spawner->SetAddToSpawnerStore(vaddtospawnerstore->BooleanValue());
      }

      if(!viconpath.IsEmpty() && !viconpath->IsUndefined())
      {
         spawner->SetIconPath(ToStdString(viconpath));
      }

      Handle<Array> keys = components->GetPropertyNames();
      
      for(unsigned int i = 0; i < keys->Length(); ++i)
      {
         Handle<Value> key = keys->Get(Integer::New(i));
         std::string keyname = ToStdString(key);

         dtEntity::StringId ctype = dtEntity::SIDHash(keyname);

         dtEntity::ComponentPluginManager::GetInstance().StartEntitySystem(ms->GetEntityManager(), ctype);

         if(ms->GetEntityManager().HasEntitySystem(ctype))
         {
            Handle<Value> val = components->Get(key);
            if(val->IsObject())
            {
               Handle<Object> compobj = Handle<Object>::Cast(val);
               Handle<Array> compkeys = compobj->GetPropertyNames();

               dtEntity::GroupProperty props;
               for(unsigned int j = 0; j < compkeys->Length(); ++j)
               {
                  Handle<Value> compkey = compkeys->Get(Integer::New(j));
                  std::string compkeystr = ToStdString(compkey);
                  Handle<Value> compval = compobj->Get(compkey);
                  dtEntity::Property* prop = ConvertValueToProperty(compval);
                  props.Add(dtEntity::SIDHash(compkeystr), prop);
               }
               spawner->AddComponent(ctype, props);
            }
         }
      }
      
      ms->AddSpawner(*spawner);
      return Undefined();
   }
void ContactsPersonProxy::_setInstantMessage(void* userContext, Handle<Value> value)
{
    ContactsPersonProxy *obj = (ContactsPersonProxy*) userContext;

    if(!value->IsObject()) return;

    Handle<Object> messageObject = value->ToObject();
	Local<Array> messageProperties = messageObject->GetPropertyNames();

	for(int i = 0, len = messageProperties->Length(); i < len; i++)
	{
		Local<String> messageKey = messageProperties->Get(i)->ToString();
		Local<Value> messageValue = messageObject->Get(messageKey);

		AttributeSubKind::Type subKind = AttributeSubKind::Other;
		QString messageKeyString = titanium::V8ValueToQString(messageKey).toLower();

		if(messageKeyString == "aim") {
			subKind = AttributeSubKind::InstantMessagingAim;
		} else
		if(messageKeyString == "aliwangwang") {
			subKind = AttributeSubKind::InstantMessagingAliwangwang;
		} else
		if(messageKeyString == "bbmpin") {
			subKind = AttributeSubKind::InstantMessagingBbmPin;
		} else
		if(messageKeyString == "googletalk") {
			subKind = AttributeSubKind::InstantMessagingGoogleTalk;
		} else
		if(messageKeyString == "icq") {
			subKind = AttributeSubKind::InstantMessagingIcq;
		} else
		if(messageKeyString == "irc") {
			subKind = AttributeSubKind::InstantMessagingIrc;
		} else
		if(messageKeyString == "jabber") {
			subKind = AttributeSubKind::InstantMessagingJabber;
		} else
		if(messageKeyString == "msLcs") {
			subKind = AttributeSubKind::InstantMessagingMsLcs;
		} else
		if(messageKeyString == "msn") {
			subKind = AttributeSubKind::InstantMessagingMsn;
		} else
		if(messageKeyString == "qq") {
			subKind = AttributeSubKind::InstantMessagingQq;
		} else
		if(messageKeyString == "sametime") {
			subKind = AttributeSubKind::InstantMessagingSametime;
		} else
		if(messageKeyString == "skype") {
			subKind = AttributeSubKind::InstantMessagingSkype;
		} else
		if(messageKeyString == "yahoomessenger") {
			subKind = AttributeSubKind::InstantMessagingYahooMessenger;
		} else
		if(messageKeyString == "yahoomessengerjapan") {
			subKind = AttributeSubKind::InstantMessagingYahooMessengerJapan;
		}

		if(!messageValue->IsArray()) return;

		Local<Array> messages = Local<Array>::Cast(messageValue);
		for(int i = 0, len = messages->Length(); i < len; i++)
		{
			Local<Value> currentMessage = messages->Get(Number::New(i));
			if(!currentMessage->IsString()) return;

			obj->setContactDetails(AttributeKind::InstantMessaging, subKind, currentMessage);
		}
	}
}
Exemplo n.º 8
0
   Handle<Object> WrapComponent(Handle<Object> wrappedes, ScriptSystem* scriptsys, dtEntity::EntityId eid, dtEntity::Component* v)
   {

      HandleScope handle_scope;

      Handle<Object> wrapped = scriptsys->GetFromComponentMap(v->GetType(), eid);
      if(!wrapped.IsEmpty())
      {
         return wrapped;
      }

      Handle<FunctionTemplate> templt = GetScriptSystem()->GetTemplateBySID(s_componentWrapper);
      if(templt.IsEmpty())
      {

         templt = FunctionTemplate::New();

         templt->SetClassName(String::New("Component"));
         templt->InstanceTemplate()->SetInternalFieldCount(1);

         Handle<ObjectTemplate> proto = templt->PrototypeTemplate();

         proto->Set("equals", FunctionTemplate::New(COEquals));
         proto->Set("getType", FunctionTemplate::New(COGetType));
         proto->Set("properties", FunctionTemplate::New(COProperties));
         proto->Set("toString", FunctionTemplate::New(COToString));
         proto->Set("finished", FunctionTemplate::New(COFinished));
         proto->Set("copyPropertyValues", FunctionTemplate::New(COCopyPropertyValues));

         GetScriptSystem()->SetTemplateBySID(s_componentWrapper, templt);
      }


      Local<Object> instance = templt->GetFunction()->NewInstance();
      instance->SetInternalField(0, External::New(v));
      instance->SetHiddenValue(scriptsys->GetEntityIdString(), Uint32::New(eid));

      // GetStringFromSID and conversion to v8::String is costly, create a 
      // hidden value in entity system wrapper that stores
      // strings and their string ids as name=>value pairs
      Handle<Value> propnamesval = wrappedes->GetHiddenValue(scriptsys->GetPropertyNamesString());
      if(propnamesval.IsEmpty())
      {
         Handle<Object> names = Object::New();
         dtEntity::PropertyGroup::const_iterator i;
         const dtEntity::PropertyGroup& props = v->Get();
         for(i = props.begin(); i != props.end(); ++i)
         {
            dtEntity::StringId sid = i->first;
            std::string propname = dtEntity::GetStringFromSID(sid);
            names->Set(String::New(propname.c_str()), WrapSID(sid));
         }
         wrappedes->SetHiddenValue(scriptsys->GetPropertyNamesString(), names);
         propnamesval = names;
      }

      Handle<Object> propnames = Handle<Object>::Cast(propnamesval);
      Handle<Array> keys = propnames->GetPropertyNames();
      for(unsigned int i = 0; i < keys->Length(); ++i)
      {
         Handle<String> str = keys->Get(i)->ToString();
         dtEntity::StringId sid = UnwrapSID(propnames->Get(str));
         dtEntity::Property* prop = v->Get(sid);
         if(prop == NULL)
         {
            LOG_ERROR("Could not find property in component: " << ToStdString(str));
            continue;
         }
         Handle<External> ext = v8::External::New(static_cast<void*>(prop));
         instance->SetAccessor(str, COPropertyGetter, COPropertySetter, ext);
      }
      
      // store wrapped component to script system
      scriptsys->AddToComponentMap(v->GetType(), eid, instance);
      return handle_scope.Close(instance);
   }
Exemplo n.º 9
0
Handle<Object> ProxyFactory::createV8Proxy(jclass javaClass, jobject javaProxy)
{
	LOGV(TAG, "create v8 proxy");
	JNIEnv* env = JNIScope::getEnv();
	if (!env) {
		LOG_JNIENV_ERROR("while creating Java proxy.");
		return Handle<Object>();
	}

	ENTER_V8(V8Runtime::globalContext);
	Local<Function> creator;

	LOGV(TAG, "get proxy info");

	ProxyInfo* info;
	GET_PROXY_INFO(javaClass, info);
	if (!info) {
		// No info has been registered for this class yet, fall back
		// to the binding lookup table
		jstring javaClassName = JNIUtil::getClassName(javaClass);
		Handle<Value> className = TypeConverter::javaStringToJsString(javaClassName);
		env->DeleteLocalRef(javaClassName);

		Handle<Object> exports = KrollBindings::getBinding(className->ToString());

		if (exports.IsEmpty()) {
			String::Utf8Value classStr(className);
			LOGE(TAG, "Failed to find class for %s", *classStr);
			LOG_JNIENV_ERROR("while creating V8 Proxy.");
			return Handle<Object>();
		}

		// TODO: The first value in exports should be the type that's exported
		// But there's probably a better way to do this
		Handle<Array> names = exports->GetPropertyNames();
		if (names->Length() >= 1) {
			creator = Local<Function>::Cast(exports->Get(names->Get(0)));
		}
	} else {
		creator = info->v8ProxyTemplate->GetFunction();
	}

	Local<Value> external = External::New(javaProxy);
	TryCatch tryCatch;
	Local<Object> v8Proxy = creator->NewInstance(1, &external);
	if (tryCatch.HasCaught()) {
		LOGE(TAG, "Exception thrown while creating V8 proxy.");
		V8Util::reportException(tryCatch);
		return Handle<Object>();
	}

	// set the pointer back on the java proxy
	Proxy* proxy = NativeObject::Unwrap<Proxy>(v8Proxy);
	jlong ptr = (jlong) *(proxy->handle_);

	jobject javaV8Object = env->NewObject(JNIUtil::v8ObjectClass,
		JNIUtil::v8ObjectInitMethod, ptr);

	env->SetObjectField(javaProxy,
		JNIUtil::krollProxyKrollObjectField, javaV8Object);
	env->DeleteLocalRef(javaV8Object);

	return scope.Close(v8Proxy);
}