void KPHPArrayObject::AddKrollValueToPHPArray(KValueRef value, zval *phpArray, unsigned int index) { if (value->IsNull() || value->IsUndefined()) { add_index_null(phpArray, (unsigned long) index); } else if (value->IsBool()) { if (value->ToBool()) add_index_bool(phpArray, (unsigned long) index, 1); else add_index_bool(phpArray, (unsigned long) index, 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_index_double(phpArray, (unsigned long) index, value->ToNumber()); } else if (value->IsString()) { add_index_stringl(phpArray, (unsigned long) index, (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_index_zval(phpArray, (unsigned long) index, phpValue); } }
void APIBinding::_Print(const ValueList& args, KValueRef result) { for (size_t c=0; c < args.size(); c++) { KValueRef arg = args.at(c); if (arg->IsString()) { const char *s = arg->ToString(); std::cout << s; } else { SharedString ss = arg->DisplayString(); std::cout << *ss; } } std::cout.flush(); }
void AppBinding::StdErr(const ValueList& args, KValueRef result) { for (size_t c = 0; c < args.size(); c++) { KValueRef arg = args.at(c); if (arg->IsString()) { const char *s = arg->ToString(); std::cerr << s; } else { SharedString ss = arg->DisplayString(); std::cerr << *ss; } } std::cerr << std::endl; }
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"); } }
//---------------- IMPLEMENTATION METHODS void APIBinding::Log(int severity, KValueRef value) { // optimize these calls since they're called a lot if (false == logger->IsEnabled((Logger::Level)severity)) { return; } if (value->IsString()) { string message = value->ToString(); logger->Log((Logger::Level) severity, message); } else { SharedString message = value->DisplayString(); logger->Log((Logger::Level) severity, *message); } }
void KObject::GetStringList(const char *name, std::vector<std::string> &list) { KValueRef prop = this->Get(name); if(!prop->IsUndefined() && prop->IsList()) { KListRef values = prop->ToList(); if (values->Size() > 0) { for (unsigned int c = 0; c < values->Size(); c++) { KValueRef v = values->At(c); if (v->IsString()) { const char *s = v->ToString(); list.push_back(s); } } } } }
void PropertiesBinding::GetList(const ValueList& args, KValueRef result) { KValueRef stringValue = Value::Null; GetString(args, stringValue); if (!stringValue->IsNull()) { KListRef list = new StaticBoundList(); std::string string = stringValue->ToString(); Poco::StringTokenizer t(string, ",", Poco::StringTokenizer::TOK_TRIM); for (size_t i = 0; i < t.count(); i++) { KValueRef token = Value::NewString(t[i].c_str()); list->Append(token); } KListRef list2 = list; result->SetList(list2); } }
void convert (Statement &select, KValueRef arg) { if (arg->IsString()) { std::string *s = new std::string(arg->ToString()); select , use(*s); strings.push_back(s); } else if (arg->IsInt()) { int *i = new int(arg->ToInt()); select , use(*i); ints.push_back(i); } else if (arg->IsDouble()) { double *d = new double(arg->ToDouble()); select , use(*d); doubles.push_back(d); } else if (arg->IsBool()) { bool *b = new bool(arg->ToBool()); select , use(*b); bools.push_back(b); } else if (arg->IsNull() || arg->IsUndefined()) { // in this case, we bind a null string (dequoted) std::string *s = new std::string("null"); select , use(s); strings.push_back(s); } else { throw ValueException::FromFormat("Unsupport type for argument: %s", arg->GetType().c_str()); } }
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); } }
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())); } }
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; }