示例#1
0
KValueRef KObject::GetNS(const char *name)
{
    std::string s(name);
    std::string::size_type last = 0;
    std::string::size_type pos = s.find_first_of(".");
    KValueRef 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;
}
	KValueRef ProfiledBoundObject::Wrap(KValueRef value, std::string type)
	{
		if (AlreadyWrapped(value))
		{
			return value;
		}
		else if (value->IsMethod())
		{
			KMethodRef toWrap = value->ToMethod();
			KMethodRef wrapped = new ProfiledBoundMethod(toWrap, type);
			return Value::NewMethod(wrapped);
		}
		else if (value->IsList())
		{
			KListRef wrapped = new ProfiledBoundList(value->ToList());
			return Value::NewList(wrapped);
		}
		else if (value->IsObject())
		{
			KObjectRef wrapped = new ProfiledBoundObject(value->ToObject());
			return Value::NewObject(wrapped);
		}
		else
		{
			return value;
		}
	}
示例#3
0
static std::string GetPathFromValue(KValueRef value)
{
    if (value->IsObject())
    {
        return value->ToObject()->DisplayString()->c_str();
    }
    else
    {
        return value->ToString();
    }
}
示例#4
0
KObjectRef KObject::GetObject(const char* name, KObjectRef defaultValue)
{
    KValueRef prop = this->Get(name);
    if (prop->IsObject())
    {
        return prop->ToObject();
    }
    else
    {
        return defaultValue;
    }
}
示例#5
0
 static BytesRef ValueToBytes(KValueRef value)
 {
     if (value->IsObject())
     {
         BytesRef bytes = value->ToObject().cast<Bytes>();
         if (bytes.isNull())
             bytes = new Bytes("", 0);
         return bytes;
     }
     else if (value->IsString())
     {
         const char* data = value->ToString();
         return new Bytes(data, strlen(data));
     }
     else
     {
         throw ValueException::FromString("Need a Bytes or a String");
     }
 }
	bool ProfiledBoundObject::AlreadyWrapped(KValueRef value)
	{
		if (value->IsMethod()) {
			KMethodRef source = value->ToMethod();
			AutoPtr<ProfiledBoundMethod> po = source.cast<ProfiledBoundMethod>();
			return !po.isNull();

		} else if (value->IsList()) {
			KListRef source = value->ToList();
			AutoPtr<ProfiledBoundList> po = source.cast<ProfiledBoundList>();
			return !po.isNull();

		} else if (value->IsObject()) {
			KObjectRef source = value->ToObject();
			AutoPtr<ProfiledBoundObject> po = source.cast<ProfiledBoundObject>();
			return !po.isNull();

		} else {
			return true;
		}
	}
示例#7
0
 Logger::Level APIBinding::ValueToLevel(KValueRef v)
 {
     if (v->IsString())
     {
         string levelString = v->ToString();
         return Logger::GetLevel(levelString);
     }
     else if (v->IsObject())
     {
         SharedString ss = v->ToObject()->DisplayString();
         return Logger::GetLevel(*ss);
     }
     else if (v->IsNumber())
     {
         return (Logger::Level) v->ToInt();
     }
     else // return the appropriate default
     {
         string levelString = "";
         return Logger::GetLevel(levelString);
     }
 }
示例#8
0
 static void GetBytes(KValueRef value, std::vector<BytesRef>& blobs)
 {
     if (value->IsObject())
     {
         blobs.push_back(value->ToObject().cast<Bytes>());
     }
     else if (value->IsString())
     {
         blobs.push_back(new Bytes(value->ToString()));
     }
     else if (value->IsList())
     {
         KListRef list = value->ToList();
         for (size_t j = 0; j < list->Size(); j++)
         {
             GetBytes(list->At((int)j), blobs);
         }
     }
     else if (value->IsNumber())
     {
         blobs.push_back(new Bytes(value->ToInt()));
     }
 }
示例#9
0
文件: script.cpp 项目: mital/kroll
	AutoPtr<PreprocessData> Script::Preprocess(const char *url, KObjectRef scope)
	{
		KObjectRef evaluator = this->FindEvaluatorWithMethod("canPreprocess", url);
		if (!evaluator.isNull())
		{
			KMethodRef preprocess = evaluator->GetMethod("preprocess");
			if (!preprocess.isNull())
			{
				ValueList args;
				args.push_back(Value::NewString(url));
				args.push_back(Value::NewObject(scope));
				
				KValueRef result = preprocess->Call(args);
				
				if (result->IsObject())
				{
					KObjectRef object = result->ToObject();
					AutoPtr<PreprocessData> data = new PreprocessData();
					if (object->HasProperty("data"))
					{
						KValueRef objectData = object->Get("data");
						if (objectData->IsObject())
						{
							BlobRef blobData = objectData->ToObject().cast<Blob>();
							if (!blobData.isNull())
							{
								data->data = blobData;
							}
						}
						else if (objectData->IsString())
						{
							data->data = new Blob(objectData->ToString(), strlen(objectData->ToString()));
						}
					}
					else
					{
						throw ValueException::FromString("Preprocessor didn't return any data");
					}
					if (object->HasProperty("mimeType"))
					{
						data->mimeType = object->Get("mimeType")->ToString();
					}
					else
					{
						throw ValueException::FromString("Preprocessor didn't return a mimeType");
					}
					
					return data;
				}
			}
			else
			{
				throw ValueException::FromFormat(
					"Error preprocessing: No \"preprocess\" method found on evaluator for url: \"%s\"", url);
			}
		}
		else
		{
			throw ValueException::FromFormat("Error preprocessing: No evaluator found for url: \"%s\"", url);
		}
		return 0;
	}
	bool HTTPClientBinding::BeginRequest(KValueRef sendData)
	{
		if (this->Get("connected")->ToBool())
		{
			return false;
		}

		// Reset internal variables for new request
		if (this->dirty)
		{
			this->Reset();
		}

		// Determine what data type we have to send
		if (sendData->IsObject())
		{
			KObjectRef dataObject = sendData->ToObject();
			KMethodRef nativePathMethod(dataObject->GetMethod("nativePath"));
			KMethodRef sizeMethod(dataObject->GetMethod("size"));

			if (nativePathMethod.isNull() || sizeMethod.isNull())
			{
				std::string err("Unsupported File-like object: did not have"
					"nativePath and size methods");
				logger->Error(err);
				throw ValueException::FromString(err);
			}

			const char* filename = nativePathMethod->Call()->ToString();
			if (!filename)
			{
				std::string err("Unsupported File-like object: nativePath method"
					"did not return a String");
				logger->Error(err);
				throw ValueException::FromString(err);
			}

			this->datastream = new std::ifstream(filename,
				std::ios::in | std::ios::binary);
			if (this->datastream->fail())
			{
				std::string err("Failed to open file: ");
				err.append(filename);
				logger->Error(err);
				throw ValueException::FromString(err);
			}

			this->contentLength = sizeMethod->Call()->ToInt();
		}
		else if (sendData->IsString())
		{
			std::string dataString(sendData->ToString());
			if (!dataString.empty())
			{
				this->datastream = new std::istringstream(dataString,
						std::ios::in | std::ios::binary);
				this->contentLength = dataString.length();
			}
		}
		else
		{
			// Sending no data
			this->datastream = 0;
		}

		this->dirty = true;
		this->Set("connected", Value::NewBool(true));

		if (this->async)
		{
			this->thread = new Poco::Thread();
			this->thread->start(*this);
		}
		else
		{
			this->ExecuteRequest();
		}

		return true;
	}