void AppBinding::CreateProperties(const ValueList& args, SharedValue result)
	{
		AutoPtr<PropertiesBinding> properties = new PropertiesBinding();
		result->SetObject(properties);
		
		if (args.size() > 0 && args.at(0)->IsObject())
		{
			SharedKObject p = args.at(0)->ToObject();
			SharedStringList names = p->GetPropertyNames();
			for (size_t i = 0; i < names->size(); i++)
			{
				SharedValue value = p->Get(names->at(i));
				ValueList setterArgs;
				setterArgs.push_back(Value::NewString(names->at(i)));
				setterArgs.push_back(value);
				PropertiesBinding::Type type;
				
				if (value->IsList()) type = PropertiesBinding::List;
				else if (value->IsInt()) type = PropertiesBinding::Int;
				else if (value->IsDouble()) type = PropertiesBinding::Double;
				else if (value->IsBool()) type = PropertiesBinding::Bool;
				else type = PropertiesBinding::String;
				
				properties->Setter(setterArgs, type);
			}
		}
	}
    static PyObject* ContextToPyGlobals(KObjectRef context)
    {
		PyObject* mainModule = PyImport_AddModule("__main__");
		PyObject* globals = PyDict_Copy(PyModule_GetDict(mainModule));
		PyObject* builtins = PyDict_GetItemString(globals, "__builtins__");

		SharedStringList props = context->GetPropertyNames();
		for (size_t i = 0; i < props->size(); i++) {
			const char* k = props->at(i)->c_str();

			// Don't override builtin Python properties like open, etc. We *do* want
			// to override the PRODUCT_NAME object though, as each window contains a
			// version of that object with special delegated properties.
			if ((!PyDict_GetItemString(globals, k) && !PyObject_HasAttrString(builtins, k))
					|| !strcmp(k, PRODUCT_NAME))
			{
				KValueRef v = context->Get(k);
				PyObject* pv = PythonUtils::ToPyObject(v);
				PyDict_SetItemString(globals, k, pv);
				Py_DECREF(pv);
			}
		}

        return globals;
    }
SharedValue LuaEvaluator::Call(const ValueList& args)
{
	debug( "LuaEvaluator::Call: %u\r\n", (unsigned int) args.size() );

	if( 	args.size() != 3
		|| !args.at( 1 )->IsString()
		|| !args.at( 2 )->IsObject() )
	{
		return Value::Undefined;
	}
	
	SharedStringList s = args.at( 2 )->ToObject()->GetPropertyNames();
	for( size_t i = 0; i < s->size(); i++ )
	{
		debug( "toplevel: %s\r\n", s->at(i)->c_str() );
	}
	
	lua_State *L = LuaModule::L();
	if( KLuaUtil::toLua( L, args.at( 2 ) ) == 1 )
	{
		lua_setfield( L, LUA_GLOBALSINDEX, "current" );  
		if( luaL_dostring( L, args.at( 1 )->ToString() ) == 1 )
		{
			debug( "LuaEvaluator::Call: error\r\n%s\r\n", lua_tostring( L, -1 ) );

		}
		else
		{
			debug( "LuaEvaluator::Call: success\r\n" );
			
		} // if	
	} // if	
	return Value::Undefined;
}
Beispiel #4
0
	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());
	}
AutoPtr<StaticBoundObject> ScopeMethodDelegate::CreateDelegate(KObjectRef global, KObjectRef bo)
{
	AutoPtr<StaticBoundObject> scope = new StaticBoundObject();
	SharedStringList keys = bo->GetPropertyNames();
	StringList::iterator iter = keys->begin();

	while(iter!=keys->end())
	{
		SharedString key_ptr = (*iter++);
		std::string key = *key_ptr;
		KValueRef value = bo->Get(key.c_str());

		if (key == "set")
		{
			KMethodRef d = new ScopeMethodDelegate(SET, global, scope, value->ToMethod());
			KValueRef v = Value::NewMethod(d);
			scope->Set(key.c_str(), v);
		}
		else if (key == "get")
		{
			KMethodRef d = new ScopeMethodDelegate(GET, global, scope, value->ToMethod());
			KValueRef v = Value::NewMethod(d);
			scope->Set(key.c_str(), v);
		}
		else
		{
			scope->Set(key.c_str(), value);
		}

	}
	return scope;
}
Beispiel #6
0
bool KObject::HasProperty(const char* name)
{
    SharedStringList names = this->GetPropertyNames();
    for (size_t i = 0; i < names->size(); i++)
    {
        if (!strcmp(name, names->at(i)->c_str()))
            return true;
    }
    return false;
}
	void PosixProcess::ForkAndExec()
	{
		nativeIn->CreateHandles();
		nativeOut->CreateHandles();
		nativeErr->CreateHandles();

		int pid = fork();
		if (pid < 0)
		{
			throw ValueException::FromFormat("Cannot fork process for %s", 
				args->At(0)->ToString());
		}
		else if (pid == 0)
		{
			// outPipe and errPipe may be the same, so we dup first and close later
			dup2(nativeIn->GetReadHandle(), STDIN_FILENO);
			dup2(nativeOut->GetWriteHandle(), STDOUT_FILENO);
			dup2(nativeErr->GetWriteHandle(), STDERR_FILENO);
			nativeIn->CloseNative();
			nativeOut->CloseNative();
			nativeErr->CloseNative();

			// close all open file descriptors other than stdin, stdout, stderr
			for (int i = 3; i < getdtablesize(); ++i)
				close(i);

			size_t i = 0;
			char** argv = new char*[args->Size() + 1];
			//argv[i++] = const_cast<char*>(command.c_str());
			for (;i < args->Size(); i++)
			{
				argv[i] = const_cast<char*>(args->At(i)->ToString());
			}
			argv[i] = NULL;

			SharedStringList envNames = environment->GetPropertyNames();
			for (i = 0; i < envNames->size(); i++)
			{
				const char* key = envNames->at(i)->c_str();
				std::string value(environment->Get(key)->ToString());
				setenv(key, value.c_str(), 1);
			}

			const char *command = args->At(0)->ToString();
			execvp(command, argv);
			_exit(72);
		}

		SetPID(pid);
		nativeIn->CloseNativeRead();
		nativeOut->CloseNativeWrite();
		nativeErr->CloseNativeWrite();
	}
Beispiel #8
0
	KObjectRef Process::CloneEnvironment()
	{
		SharedStringList properties = environment->GetPropertyNames();
		KObjectRef clonedEnvironment = new StaticBoundObject();
		for (size_t i = 0; i < properties->size(); i++)
		{
			std::string property = *properties->at(i);
			std::string value = environment->Get(property.c_str())->ToString();
			clonedEnvironment->Set(property.c_str(), Value::NewString(value.c_str()));
		}
		return clonedEnvironment;
	}
 SharedStringList EnvironmentBinding::GetPropertyNames()
 {
     std::map<std::string, std::string> env = EnvironmentUtils::GetEnvironment();
     SharedStringList keys = new StringList();
     
     std::map<std::string, std::string>::iterator iter = env.begin();
     for (; iter != env.end(); iter++)
     {
         keys->push_back(new std::string(iter->first));
     }
     return keys;
 }
SharedStringList DelegateStaticBoundObject::GetPropertyNames()
{
    ScopedLock lock(&mutex);

    SharedStringList globalList = global->GetPropertyNames();
    SharedStringList localList = local->GetPropertyNames();

    for (size_t i = 0; i < globalList->size(); i++)
    {
        bool found = false;
        for (size_t j = 0; j < localList->size(); j++)
        {
            if (globalList->at(i).get() == localList->at(j).get())
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            localList->push_back(globalList->at(i));
        }
    }

    return localList;
}
Beispiel #11
0
	static VALUE RubyKObjectMethods(VALUE self)
	{
		SharedValue* value = NULL;
		Data_Get_Struct(self, SharedValue, value);
		SharedKObject object = (*value)->ToObject();

		VALUE* args = NULL;
		VALUE methods = rb_call_super(0, args);

		SharedStringList props = object->GetPropertyNames();
		for (unsigned int i = 0; i < props->size(); i++)
		{
			SharedString prop_name = props->at(i);
			rb_ary_push(methods, rb_str_new2(prop_name->c_str()));
		}
		return methods;
	}