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 #2
0
	void UIModule::Start()
	{
		SharedKMethod api = this->host->GetGlobalObject()->GetNS("API.fire")->ToMethod();
		api->Call("ti.UI.start", Value::Undefined);

#ifdef OS_WIN32
		UIBinding* binding = new Win32UIBinding(host);
#elif OS_OSX
		UIBinding* binding = new OSXUIBinding(host);
#elif OS_LINUX
		UIBinding* binding = new GtkUIBinding(host);
#endif

		AppConfig *config = AppConfig::Instance();
		if (config == NULL)
		{
			std::string msg = "Error loading tiapp.xml. Your application "
			                  "is not properly configured or packaged.";
			binding->ErrorDialog(msg);
			throw ValueException::FromString(msg.c_str());
			return;
		}
		WindowConfig *main_window_config = config->GetMainWindow();
		if (main_window_config == NULL)
		{
			std::string msg ="Error loading tiapp.xml. Your application "
			                 "window is not properly configured or packaged.";
			binding->ErrorDialog(msg);
			throw ValueException::FromString(msg.c_str());
			return;
		}

		binding->CreateMainWindow(main_window_config);
	}
Example #3
0
	void UIModule::Exiting(int exitcode)
	{
		// send a stop notification - we need to do this before 
		// stop is called given that the API module is registered (and unregistered)
		// before our module and it will then be too late
		SharedKMethod api = this->host->GetGlobalObject()->GetNS("API.fire")->ToMethod();
		api->Call("ti.UI.stop", Value::Undefined);
	}
Example #4
0
	static VALUE RubyKMethodCall(VALUE self, VALUE args)
	{
		SharedValue* dval = NULL;
		Data_Get_Struct(self, SharedValue, dval);
		SharedKMethod method = (*dval)->ToMethod();

		// TODO: We should raise an exception instead
		if (method.isNull())
			return Qnil;

		return RubyUtils::GenericKMethodCall(method, args);
	}
	AutoMenuItem UIBinding::__CreateCheckMenuItem(const ValueList& args)
	{
		args.VerifyException("createCheckMenuItem", "?s m|0");
		std::string label = args.GetString(0, "");
		SharedKMethod eventListener = args.GetMethod(1, NULL);

		AutoMenuItem item = this->CreateCheckMenuItem();
		if (!label.empty())
			item->SetLabel(label);
		if (!eventListener.isNull())
			item->AddEventListener(Event::CLICKED, eventListener);

		return item;
	}
Example #6
0
	void MonkeyBinding::Callback(const ValueList &args, SharedValue result)
	{
		SharedKObject event = args.at(1)->ToObject();
		std::string url_value = event->Get("url")->ToString();
		
		std::vector< std::pair< std::pair< VectorOfPatterns,VectorOfPatterns >,std::string> >::iterator iter = scripts.begin();
		while(iter!=scripts.end())
		{
			std::pair< std::pair< VectorOfPatterns,VectorOfPatterns >, std::string> e = (*iter++);
			VectorOfPatterns include = e.first.first;
			VectorOfPatterns exclude = e.first.second;

			if (Matches(exclude,url_value))
			{
				continue;
			}
			if (Matches(include,url_value))
			{
				// I got a castle in brooklyn, that's where i dwell 
				try
				{
					SharedKMethod eval = event->Get("scope")->ToObject()->Get("window")->ToObject()->Get("eval")->ToMethod();
#ifdef DEBUG
					std::cout << ">>> loading user script for " << url_value << std::endl;
#endif
					eval->Call(Value::NewString(e.second));
				}
				catch (ValueException &ex)
				{
					Logger* logger = Logger::Get("Monkey");
					SharedString ss = ex.DisplayString();
					int line = -1;

					if (ex.GetValue()->IsObject() &&
						ex.GetValue()->ToObject()->Get("line")->IsNumber())
					{
						line = ex.GetValue()->ToObject()->Get("line")->ToInt();
					}
					logger->Error(
						"Exception generated evaluating user script for %s "
						"(line %i): %s", url_value.c_str(), line, ss->c_str());
				}
				catch (std::exception &ex)
				{
					Logger* logger = Logger::Get("Monkey");
					logger->Error("Exception generated evaluating user script for %s, Exception: %s",url_value.c_str(),ex.what());
				}
			}
		}
	}
Example #7
0
	SharedValue AccessorBoundList::Get(const char *name)
	{
		std::string getter_name = "get" + Capitalize(name);
		SharedValue v = this->RawGet(getter_name.c_str());
		if (v->IsMethod())
		{
			SharedKMethod m = v->ToMethod();
			return m->Call(ValueList());
		}
		else
		{
			return this->RawGet(name);
		}
	}
Example #8
0
	void AccessorBoundList::Set(const char *name, SharedValue value)
	{
		std::string setter_name = "set" + Capitalize(name);
		SharedValue v = this->RawGet(setter_name.c_str());
		if (v->IsMethod())
		{
			SharedKMethod m = v->ToMethod();
			ValueList args;
			args.push_back(value);
			m->Call(args);
		}
		else
		{
			this->RawSet(name, value);
		}
	}
Example #9
0
	VALUE RubyUtils::GenericKMethodCall(SharedKMethod method, VALUE args)
	{
		ValueList kargs;
		for (int i = 0; i < RARRAY(args)->len; i++)
		{
			VALUE rarg = rb_ary_entry(args, i);
			SharedValue arg = RubyUtils::ToKrollValue(rarg);
			Value::Unwrap(arg);
			kargs.push_back(arg);
		}

		try
		{
			SharedValue result = method->Call(kargs);
			return RubyUtils::ToRubyValue(result);
		}
		catch (ValueException& e)
		{
			// TODO: Eventually wrap these up in a special exception
			// class so that we can unwrap them into ValueExceptions again
			SharedString ss = e.DisplayString();
			rb_raise(rb_eStandardError, ss->c_str());
			return Qnil;
		}
	}
	void MonkeyBinding::EvaluateUserScript(
		SharedKObject event, std::string& url,
		SharedKObject windowObject, std::string& scriptSource)
	{
		static Logger *logger = Logger::Get("Monkey");
		// I got a castle in brooklyn, that's where i dwell
		// Word, brother.

		if (!windowObject->Get("eval")->IsMethod())
		{
			logger->Error("Found a window object without an "
				"eval function (%s) -- aborting", url.c_str());
			return;
		}

		SharedKObject target = event->GetObject("target");
		if (!windowObject->Get(GLOBAL_NS_VARNAME)->IsObject() &&
			!target.isNull() && target->Get("insertAPI")->IsMethod())
		{
			logger->Info("Forcing Titanium API into: %s\n", url.c_str());
			target->CallNS("insertAPI", Value::NewObject(windowObject));
		}

		SharedKMethod evalFunction = windowObject->GetMethod("eval");
		logger->Info("Loading userscript for %s\n", url.c_str());
		try
		{
			evalFunction->Call(Value::NewString(scriptSource));
		}
		catch (ValueException &ex)
		{
			SharedString ss = ex.DisplayString();
			int line = -1;
			if (ex.GetValue()->IsObject() &&
				ex.GetValue()->ToObject()->Get("line")->IsNumber())
			{
				line = ex.GetValue()->ToObject()->Get("line")->ToInt();
			}
			logger->Error(
				"Exception generated evaluating user script for %s "
				"(line %i): %s", url.c_str(), line, ss->c_str());
		}
	}
	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 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);
		}
	}