Example #1
0
	// 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;
	}
Example #2
0
	// A :method_missing method for finding KObject properties in Ruby
	static VALUE RubyKObjectMethodMissing(int argc, VALUE *argv, VALUE self)
	{
		SharedValue* dval = NULL;
		Data_Get_Struct(self, SharedValue, dval);
		SharedKObject object = (*dval)->ToObject();

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

		// This is the same error that ruby throws
		if (argc == 0 || !SYMBOL_P(argv[0]))
		{
			rb_raise(rb_eArgError, "no id given");
		}

		// We need to determine the method that was invoked:
		// store the method name and arguments in separate variables
		VALUE r_name, args;
		rb_scan_args(argc, argv, "1*", &r_name, &args);
		const char* name = rb_id2name(SYM2ID(r_name));

		// Check if this is an assignment
		SharedValue value = object->Get(name);
		if (name[strlen(name) - 1] == '=' && argc > 1)
		{
			char* mod_name = strdup(name);
			mod_name[strlen(mod_name) - 1] = '\0';
			value = RubyUtils::ToKrollValue(argv[1]);
			object->Set(mod_name, value);
			free(mod_name);
			return argv[1];
		}
		else if (value->IsUndefined()) // raise a method missing error
		{
			VALUE selfString = rb_obj_as_string(self);
			rb_raise(rb_eNoMethodError, "undefined method `%s' for %s",
				name, RubyUtils::ToString(selfString));
		}
		else if (value->IsMethod()) // actually call a method
		{
			return RubyUtils::GenericKMethodCall(value->ToMethod(), args);
		}
		else // Plain old access
		{
			return RubyUtils::ToRubyValue(value);
		}
	}
Example #3
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;
	}
Example #4
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);
		}
	}
SharedValue DelegateStaticBoundObject::Get(const char *name)
{
    ScopedLock lock(&mutex);

    SharedValue val = local->Get(name);
    if (!val->IsUndefined())
    {
        // We want properties of the local object to
        // override // properties set on the global object.
        return val;
    }
    else
    {
        // If the property isn't found on the local object, search
        // for it in the global object.
        return this->global->Get(name);
    }

}
Example #6
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)");
		}
	}
Example #7
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);
					}
				}
			}
		}
	}