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); }
void Blob::LastIndexOf(const ValueList& args, SharedValue result) { // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/lastIndexOf args.VerifyException("Blob.lastIndexOf", "s,?i"); if (this->length <= 0) { result->SetInt(-1); } else { std::string target = this->buffer; std::string needle = args.at(0)->ToString(); int start = target.size() + 1; if (args.size() > 1) { start = args.GetNumber(1); if (start < 0) { start = 0; } } result->SetInt(target.rfind(needle, start)); } }
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); }
void WorkerContext::SendQueuedMessages() { Logger *logger = Logger::Get("WorkerContext"); logger->Debug("SendQueuedMessages called"); SharedValue onmessage = worker->Get("onmessage"); Poco::ScopedLock<Poco::Mutex> lock(mutex); if (onmessage->IsMethod()) { if (messages.size()>0) { std::list<SharedValue>::iterator i = messages.begin(); while(i!=messages.end()) { SharedValue v = (*i++); ValueList _args; string name = "worker.message"; AutoPtr<KEventObject> target = this; this->duplicate(); AutoPtr<Event> event = new Event(target, name); event->Set("message", v); _args.push_back(Value::NewObject(event)); host->InvokeMethodOnMainThread(onmessage->ToMethod(),_args,false); } messages.clear(); } } }
SharedString KObject::DisplayString(int levels) { std::stringstream ss; if (levels == 0) { ss << "<KObject at " << this << ">"; } else { SharedStringList props = this->GetPropertyNames(); ss << "{"; for (size_t i = 0; i < props->size(); i++) { SharedValue prop = this->Get(props->at(i)); SharedString disp_string = prop->DisplayString(levels); ss << " " << *(props->at(i)) << " : " << *disp_string << ","; } if (props->size() > 0) // Erase last comma ss.seekp((int)ss.tellp() - 1); ss << "}"; } return new std::string(ss.str()); }
SharedValue KObject::GetNS(const char *name) { std::string s(name); std::string::size_type last = 0; std::string::size_type pos = s.find_first_of("."); SharedValue current; KObject* scope = this; while (pos != std::string::npos) { std::string token = s.substr(last,pos-last); current = scope->Get(token.c_str()); if (current->IsObject()) { scope = current->ToObject().get(); } else { return Value::Undefined; } last = pos + 1; pos = s.find_first_of(".", last); } if (pos!=s.length()) { std::string token = s.substr(last); current = scope->Get(token.c_str()); } return current; }
void NetworkBinding::EncodeURIComponent(const ValueList &args, SharedValue result) { if (args.at(0)->IsNull() || args.at(0)->IsUndefined()) { result->SetString(""); } else if (args.at(0)->IsString()) { std::string src = args.at(0)->ToString(); std::string sResult = DataUtils::EncodeURIComponent(src); result->SetString(sResult); } else if (args.at(0)->IsDouble()) { std::stringstream str; str << args.at(0)->ToDouble(); result->SetString(str.str().c_str()); } else if (args.at(0)->IsBool()) { std::stringstream str; str << args.at(0)->ToBool(); result->SetString(str.str().c_str()); } else if (args.at(0)->IsInt()) { std::stringstream str; str << args.at(0)->ToInt(); result->SetString(str.str().c_str()); } else { throw ValueException::FromString("Could not encodeURIComponent with type passed"); } }
void NetworkBinding::SetProxy(const ValueList& args, SharedValue result) { std::string hostname = args.at(0)->ToString(); std::string port = args.at(1)->ToString(); std::string username = args.at(2)->ToString(); std::string password = args.at(3)->ToString(); if(proxy) { delete proxy; proxy = NULL; } #if defined(OS_WIN32) std::string http_proxy = "http://"; http_proxy += username + ":" + password + "@"; http_proxy += hostname + ":" + port; int i = ::_putenv_s("HTTP_PROXY", http_proxy.c_str()); if(i != 0) { result->SetBool(false); } #endif proxy = new ti::Proxy(hostname, port, username,password); result->SetBool(true); }
void HttpServerRequest::Read(const ValueList& args, SharedValue result) { std::istream &in = request.stream(); if (in.eof() || in.fail()) { result->SetNull(); return; } int max_size = 8096; if (args.size()==1) { max_size = args.at(0)->ToInt(); } char *buf = new char[max_size]; in.read(buf,max_size); std::streamsize count = in.gcount(); if (count == 0) { result->SetNull(); } else { result->SetObject(new Blob(buf,count)); } delete [] buf; }
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; }
void AppBinding::GetIcon(const ValueList& args, SharedValue result) { SharedApplication app = this->host->GetApplication(); result->SetNull(); if (app && !app->image.empty()) { result->SetString(app->image); } }
void Blob::ToString(const ValueList& args, SharedValue result) { if (this->length == 0) { result->SetString(""); } else { result->SetString(buffer); } }
void ResultSetBinding::FieldCount(const ValueList& args, SharedValue result) { if (rs.isNull()) { result->SetInt(0); } else { result->SetInt(rs->columnCount()); } }
void MenuItem::_GetSubmenu(const ValueList& args, SharedValue result) { if (this->submenu.isNull()) { result->SetNull(); } else { result->SetObject(this->submenu); } }
void ResultSetBinding::IsValidRow(const ValueList& args, SharedValue result) { if (rs.isNull()) { result->SetBool(false); } else { result->SetBool(!eof); } }
SharedKMethod KObject::GetMethod(const char* name, SharedKMethod defaultValue) { SharedValue prop = this->Get(name); if (prop->IsMethod()) { return prop->ToMethod(); } else { return defaultValue; } }
std::string KObject::GetString(const char* name, std::string defaultValue) { SharedValue prop = this->Get(name); if(prop->IsString()) { return prop->ToString(); } else { return defaultValue; } }
SharedKList KObject::GetList(const char* name, SharedKList defaultValue) { SharedValue prop = this->Get(name); if (prop->IsList()) { return prop->ToList(); } else { return defaultValue; } }
int KObject::GetInt(const char* name, int defaultValue) { SharedValue prop = this->Get(name); if (prop->IsInt()) { return prop->ToInt(); } else { return defaultValue; } }
double KObject::GetNumber(const char* name, double defaultValue) { SharedValue prop = this->Get(name); if (prop->IsNumber()) { return prop->ToNumber(); } else { return defaultValue; } }
bool KObject::GetBool(const char* name, bool defaultValue) { SharedValue prop = this->Get(name); if (prop->IsBool()) { return prop->ToBool(); } else { return defaultValue; } }
bool KKJSList::Remove(unsigned int index) { if (index >= 0 && index < this->Size()) { SharedValue spliceMethod = this->kjs_bound_object->Get("splice"); spliceMethod->ToMethod()->Call( Value::NewInt(index), Value::NewInt(1)); return true; } return false; }
// A :responds_to? method for finding KObject properties in Ruby static VALUE RubyKObjectRespondTo(int argc, VALUE *argv, VALUE self) { SharedValue* dval = NULL; Data_Get_Struct(self, SharedValue, dval); SharedKObject object = (*dval)->ToObject(); VALUE mid, priv; // We ignore the priv argument rb_scan_args(argc, argv, "11", &mid, &priv); const char* name = rb_id2name(rb_to_id(mid)); SharedValue value = object->Get(name); return value->IsUndefined() ? Qfalse : Qtrue; }
void FileStream::Ready(const ValueList& args, SharedValue result) { Poco::FileInputStream* fis = dynamic_cast<Poco::FileInputStream*>(this->stream); if(!fis) { result->SetBool(false); } else { result->SetBool(fis->eof()==false); } }
void UIBinding::_GetMenu(const ValueList& args, SharedValue result) { AutoMenu menu = this->GetMenu(); if (menu.isNull()) { result->SetNull(); } else { result->SetObject(menu); } }
void Win32Process::InvokeOnExit() { SharedValue sv = this->Get("onexit"); if (!sv->IsMethod()) { return; } ValueList args(Value::NewInt(this->exitCode)); SharedKMethod callback = sv->ToMethod(); this->parent->GetHost()->InvokeMethodOnMainThread( callback, args, false); }
void AppBinding::GetIcon(const ValueList& args, SharedValue result) { const SharedApplication app = this->host->GetApplication(); if (app->image.empty()) { result->SetNull(); } else { std::string iconPath = app->image; result->SetString(iconPath); } }
void HttpServerRequest::GetHeader(const ValueList& args, SharedValue result) { std::string name = args.at(0)->ToString(); if (request.has(name)) { std::string value = request.get(name); result->SetString(value); } else { result->SetNull(); } }
void Blob::ToUpperCase(const ValueList& args, SharedValue result) { if (this->length > 0) { std::string target = this->buffer; std::string r = Poco::toUpper(target); result->SetString(r); } else { result->SetNull(); } }
void ResultSetBinding::FieldName(const ValueList& args, SharedValue result) { if (rs.isNull()) { result->SetNull(); } else { args.VerifyException("fieldName", "i"); const std::string &str = rs->columnName(args.at(0)->ToInt()); result->SetString(str.c_str()); } }