Example #1
0
	void AppBinding::GetStreamURL(const ValueList& args, SharedValue result)
	{
		const SharedApplication app = this->host->GetApplication();
		std::string stream = app->stream;
		
		// TODO: switch to HTTPS once the ti.Network XHR supports it
		std::string url = "http://api.appcelerator.net/";
		if (stream == "production")
		{
			url+="p/v1";
		}
		else if (stream == "dev")
		{
			url+="d/v1";
		}
		else if (stream == "test")
		{
			url+="t/v1";
		}
		else
		{
			url+=stream;
			url+="/v1";
		}
		for (size_t c = 0; c < args.size(); c++)
		{
			SharedValue arg = args.at(c);
			if (arg->IsString())
			{
				url+="/";
				url+=arg->ToString();
			}
		}
		result->SetString(url);
	}
Example #2
0
	void KPHPArrayObject::AddKrollValueToPHPArray(SharedValue value, zval *phpArray)
	{
		if (value->IsNull() || value->IsUndefined())
		{
			add_next_index_null(phpArray);
		}
		else if (value->IsBool())
		{
			if (value->ToBool())
				add_next_index_bool(phpArray, 1);
			else
				add_next_index_bool(phpArray, 0);
		}
		else if (value->IsNumber())
		{
			/* No way to check whether the number is an
			   integer or a double here. All Kroll numbers
			   are doubles, so return a double. This could
			   cause some PHP to function incorrectly if it's
			   doing strict type checking. */
			add_next_index_double(phpArray, value->ToNumber());
		}
		else if (value->IsString())
		{
			add_next_index_stringl(phpArray, (char *) value->ToString(), strlen(value->ToString()), 1);
		}
		else if (value->IsObject())
		{
			/*TODO: Implement*/
		}
		else if (value->IsMethod())
		{
			/*TODO: Implement*/
		}
		else if (value->IsList())
		{
			zval *phpValue;
			AutoPtr<KPHPArrayObject> pl = value->ToList().cast<KPHPArrayObject>();
			if (!pl.isNull())
				phpValue = pl->ToPHP();
			else
				phpValue = PHPUtils::ToPHPValue(value);

			add_next_index_zval(phpArray, phpValue);
		}
	}
	void WorkerBinding::CreateWorker(const ValueList& args, SharedValue result)
	{
		if (args.size()!=2)
		{
			throw ValueException::FromString("invalid argument specified");
		}
		
		bool is_function = args.at(1)->ToBool();
		
		std::string code;
		Logger *logger = Logger::Get("Worker");
		
		if (is_function)
		{
			// convert the function to a string block 
			code =  "(";
			code += args.at(0)->ToString();
			code += ")()";
		}
		else 
		{
			// this is a path -- probably should verify that this is relative and not an absolute URL to remote
			SharedKMethod appURLToPath = global->GetNS("App.appURLToPath")->ToMethod();
			ValueList a;
			a.push_back(args.at(0));
			SharedValue result = appURLToPath->Call(a);
			const char *path = result->ToString();
			
			logger->Debug("worker file path = %s",path);
			
			std::ios::openmode flags = std::ios::in;
			Poco::FileInputStream *fis = new Poco::FileInputStream(path,flags);
			std::stringstream ostr;
			char buf[8096];
			int count = 0;
			while(!fis->eof())
			{
				fis->read((char*)&buf,8095);
				std::streamsize len = fis->gcount();
				if (len>0)
				{
					buf[len]='\0';
					count+=len;
					ostr << buf;
				}
				else break;
			}
			fis->close();
			code = std::string(ostr.str());
		}
		
		logger->Debug("Worker script code = %s", code.c_str());
		
		SharedKObject worker = new Worker(host,global,code);
		result->SetObject(worker);
	}
Example #4
0
	void AppBinding::StdErr(const ValueList& args, SharedValue result)
	{
		for (size_t c=0;c<args.size();c++)
		{
			SharedValue arg = args.at(c);
			const char *s = arg->ToString();
			std::cerr << s;
		}
		std::cerr << std::endl;
	}
Example #5
0
	std::string KObject::GetString(const char* name, std::string defaultValue)
	{
		SharedValue prop = this->Get(name);
		if(prop->IsString())
		{
			return prop->ToString();
		}
		else
		{
			return defaultValue;
		}
	}
Example #6
0
	VALUE RubyUtils::ToRubyValue(SharedValue value)
	{
		if (value->IsNull() || value->IsUndefined())
		{
			return Qnil;
		}
		if (value->IsBool())
		{
			return value->ToBool() ? Qtrue : Qfalse;
		}
		else if (value->IsNumber())
		{
			return rb_float_new(value->ToNumber());
		}
		else if (value->IsString())
		{
			return rb_str_new2(value->ToString());
		}
		else if (value->IsObject())
		{
			AutoPtr<KRubyObject> ro = value->ToObject().cast<KRubyObject>();
			if (!ro.isNull())
				return ro->ToRuby();

			AutoPtr<KRubyHash> rh = value->ToObject().cast<KRubyHash>();
			if (!rh.isNull())
				return rh->ToRuby();

			return RubyUtils::KObjectToRubyValue(value);
		}
		else if (value->IsMethod())
		{
			AutoPtr<KRubyMethod> rm = value->ToMethod().cast<KRubyMethod>();
			if (!rm.isNull())
				return rm->ToRuby();
			else
				return RubyUtils::KMethodToRubyValue(value);
		}
		else if (value->IsList())
		{
			AutoPtr<KRubyList> rl = value->ToList().cast<KRubyList>();
			if (!rl.isNull())
				return rl->ToRuby();
			else
				return RubyUtils::KListToRubyValue(value);
		}
		return Qnil;
	}
	void AppBinding::GetStreamURL(const ValueList& args, SharedValue result)
	{
		SharedApplication app = this->host->GetApplication();
		std::string url(app->GetStreamURL("https"));

		for (size_t c = 0; c < args.size(); c++)
		{
			SharedValue arg = args.at(c);
			if (arg->IsString())
			{
				url.append("/");
				url.append(arg->ToString());
			}
		}
		result->SetString(url);
	}
	void AppBinding::StdErr(const ValueList& args, SharedValue result)
	{
		for (size_t c = 0; c < args.size(); c++)
		{
			SharedValue arg = args.at(c);
			if (arg->IsString())
			{
				const char *s = arg->ToString();
				std::cerr << s;
			}
			else
			{
				SharedString ss = arg->DisplayString();
				std::cerr << *ss;
			}
		}
		std::cerr << std::endl;
	}
Example #9
0
	void HTTPClientBinding::SendFile(const ValueList& args, SharedValue result)
	{
		if (this->Get("connected")->ToBool())
		{
			throw ValueException::FromString("already connected");
		}
		if (args.size()==1)
		{
			// can be a string of data or a file
			SharedValue v = args.at(0);
			if (v->IsObject())
			{
				// probably a file
				SharedKObject obj = v->ToObject();
				if (obj->Get("isFile")->IsMethod())
				{
					std::string file = obj->Get("toString")->ToMethod()->Call()->ToString();
					this->filestream = new Poco::FileInputStream(file);
					Poco::Path p(file);
					this->filename = p.getFileName();
				}
				else
				{
					this->datastream = obj->Get("toString")->ToMethod()->Call()->ToString();
				}
			}
			else if (v->IsString())
			{
				this->filestream = new Poco::FileInputStream(v->ToString());
			}
			else
			{
				throw ValueException::FromString("unknown data type");
			}
		}
		this->thread = new Poco::Thread();
		this->thread->start(&HTTPClientBinding::Run,(void*)this);
		if (!this->async)
		{
			PRINTD("Waiting on HTTP Client thread to finish");
			this->thread->join();
		}
	}
Example #10
0
	void HTTPClientBinding::Send(const ValueList& args, SharedValue result)
	{
		if (this->Get("connected")->ToBool())
		{
			throw ValueException::FromString("already connected");
		}
		if (args.size()==1)
		{
			// can be a string of data or a file
			SharedValue v = args.at(0);
			if (v->IsObject())
			{
				// probably a file
				SharedKObject obj = v->ToObject();
				this->datastream = obj->Get("toString")->ToMethod()->Call()->ToString();
			}
			else if (v->IsString())
			{
				this->datastream = v->ToString();
			}
			else if (v->IsNull() || v->IsUndefined())
			{
				this->datastream = "";
			}
			else
			{
				throw ValueException::FromString("unknown data type");
			}
		}
		this->thread = new Poco::Thread();
		this->thread->start(&HTTPClientBinding::Run,(void*)this);
		if (!this->async)
		{
			PRINTD("Waiting on HTTP Client thread to finish (sync)");
			Poco::Stopwatch sw;
			sw.start();
			while (!this->shutdown && sw.elapsedSeconds() * 1000 < this->timeout)
			{
				this->thread->tryJoin(100);
			}
			PRINTD("HTTP Client thread finished (sync)");
		}
	}
	void WorkerContext::ImportScripts(const ValueList &args, SharedValue result)
	{
		Logger *logger = Logger::Get("WorkerContext");
		
		SharedKMethod appURLToPath = host->GetGlobalObject()->GetNS("App.appURLToPath")->ToMethod();
		AutoPtr<Worker> _worker = worker.cast<Worker>();
		JSGlobalContextRef context = KJSUtil::GetGlobalContext(_worker->GetGlobalObject());
		
		for (size_t c = 0; c < args.size(); c++)
		{
			// first convert the path to a full URL file path
			ValueList a;
			a.push_back(args.at(c));
			SharedValue result = appURLToPath->Call(a);
			const char *path = result->ToString();

			logger->Debug("attempting to import worker script = %s",path);
			KJSUtil::EvaluateFile(context, (char*)path);
		}
	}
Example #12
0
	void KObject::GetStringList(const char *name, std::vector<std::string> &list)
	{
		SharedValue prop = this->Get(name);
		if(!prop->IsUndefined() && prop->IsList())
		{
			SharedKList values = prop->ToList();
			if (values->Size() > 0)
			{
				for (unsigned int c = 0; c < values->Size(); c++)
				{
					SharedValue v = values->At(c);
					if (v->IsString())
					{
						const char *s = v->ToString();
						list.push_back(s);
					}
				}
			}
		}
	}
Example #13
0
	void NetworkBinding::GetHostByAddress(const ValueList& args, SharedValue result)
	{
		if (args.at(0)->IsObject())
		{
			SharedKObject obj = args.at(0)->ToObject();
			SharedPtr<IPAddressBinding> b = obj.cast<IPAddressBinding>();
			if (!b.isNull())
			{
				// in this case, they've passed us an IPAddressBinding
				// object, which we can just retrieve the ipaddress
				// instance and resolving using it
				IPAddress addr(b->GetAddress()->toString());
				SharedPtr<HostBinding> binding = new HostBinding(addr);
				if (binding->IsInvalid())
				{
					throw ValueException::FromString("Could not resolve address");
				}
				result->SetObject(binding);
				return;
			}
			else
			{
				SharedValue bo = obj->Get("toString");
				if (bo->IsMethod())
				{
					SharedKMethod m = bo->ToMethod();
					ValueList args;
					SharedValue tostr = m->Call(args);
					this->_GetByHost(tostr->ToString(),result);
					return;
				}
				throw ValueException::FromString("Unknown object passed");
			}
		}
		else if (args.at(0)->IsString())
		{
			// in this case, they just passed in a string so resolve as
			// normal
			this->_GetByHost(args.at(0)->ToString(),result);
		}
	}