void Bytes::_Split(const ValueList& args, KValueRef result) { // This method now follows the spec located at: // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/split // Except support for regular expressions args.VerifyException("Bytes.split", "?s,i"); KListRef list = new StaticBoundList(); result->SetList(list); std::string target = ""; if (this->size > 0) { target = this->buffer; } else { list->Append(Value::NewString(target)); return; } if (args.size() <= 0) { list->Append(Value::NewString(target)); return; } std::string separator = args.GetString(0); int limit = args.GetInt(1, INT_MAX); // We could use Poco's tokenizer here, but it doesn't split strings // like "abc,def,," -> ['abc', 'def', '', ''] correctly. It produces // ['abc', 'def', ''] which is a different behavior than the JS split. // So we roll our own for now -- it's not very efficient right now, but // it should be correct. size_t next = target.find(separator); while (target.size() > 0 && next != std::string::npos) { std::string token; if (separator.size() == 0) { token = target.substr(0, 1); } else { token = target.substr(0, next); } target = target.substr(next + 1); next = target.find(separator); if ((int) list->Size() >= limit) return; list->Append(Value::NewString(token)); } if ((int) list->Size() < limit && separator.size() != 0) list->Append(Value::NewString(target)); }
void Menu::_RemoveItemAt(const ValueList& args, SharedValue result) { args.VerifyException("removeItemAt", "i"); size_t index = static_cast<size_t>(args.GetInt(0)); this->RemoveItemAt(index); }
void Network::_CreateTCPSocket(const ValueList& args, KValueRef result) { args.VerifyException("createTCPSocket", "sn"); std::string host(args.GetString(0)); int port = args.GetInt(1); result->SetObject(new TCPSocket(host, port)); }
void HttpServerRequest::Read(const ValueList& args, KValueRef result) { args.VerifyException("read", "?i"); std::istream &in = request.stream(); if (in.eof() || in.fail()) { result->SetNull(); return; } int maxSize = args.GetInt(0, 8096); char *buf = new char[maxSize]; in.read(buf, maxSize); int count = static_cast<int>(in.gcount()); if (count == 0) { result->SetNull(); } else { result->SetObject(new Bytes(buf,count)); } delete [] buf; }
void APIBinding::_CreateDependency(const ValueList& args, KValueRef result) { args.VerifyException("createDepenendency", "i,s,s,?i"); int type = args.GetInt(0, UNKNOWN); string name = args.GetString(1); string version = args.GetString(2); int requirement = (int) args.GetNumber(3, Dependency::EQ); if (type != MODULE && type != RUNTIME && type != SDK && type != MOBILESDK && type != APP_UPDATE) { throw ValueException::FromString( "Tried to create a dependency with an unknown dependency type"); } else if (requirement != Dependency::EQ && requirement != Dependency::GT && requirement != Dependency::LT && requirement != Dependency::GTE && requirement != Dependency::LTE) { throw ValueException::FromString( "Tried to create a dependency with an unknown requirement type"); } else { SharedDependency d = Dependency::NewDependencyFromValues( static_cast<KComponentType>(type), name, version); KObjectRef dBinding = new DependencyBinding(d); result->SetObject(dBinding); } }
void Bytes::_Substring(const ValueList& args, KValueRef result) { // This method now follows the spec located at: // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/substring args.VerifyException("Bytes.substring", "i,?i"); std::string target = ""; if (this->size > 0) { target = this->buffer; } long indexA = args.GetInt(0); if (indexA < 0) indexA = 0; if (indexA > (long) target.size()) indexA = target.size(); if (args.size() < 2) { std::string r = target.substr(indexA); result->SetString(r); } else { long indexB = args.GetInt(1); if (indexB < 0) indexB = 0; if (indexB > (long) target.size()) indexB = target.size(); if (indexA == indexB) { result->SetString(""); return; } if (indexA > indexB) { long temp = indexA; indexA = indexB; indexB = temp; } std::string r = target.substr(indexA, indexB - indexA); result->SetString(r); } }
void Bytes::_Slice(const ValueList& args, KValueRef result) { args.VerifyException("splice", "i,i"); size_t offset = args.GetInt(0); size_t length = args.GetInt(1); BytesRef slice = new Bytes(BytesRef(this, true), offset, length); result->SetObject(slice); }
void Bytes::_ByteAt(const ValueList& args, KValueRef result) { args.VerifyException("Bytes.byteAt", "n"); size_t position = args.GetInt(0); if (position >= 0 && position < this->size) { result->SetInt(static_cast<unsigned char>(this->buffer[position])); } }
void Blob::Substr(const ValueList& args, SharedValue result) { // This method now follows the spec located at: // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/substr args.VerifyException("Blob.substr", "i,?i"); std::string target = ""; if (this->length > 0) { target = this->buffer; } int start = args.GetInt(0); if (start > 0 && start >= this->length) { result->SetString(""); return; } if (start < 0 && (-1*start) > this->length) { start = 0; } else if (start < 0) { start = this->length + start; } int length = this->length - start; if (args.size() > 1) { length = args.GetInt(1); } if (length <= 0) { result->SetString(""); return; } std::string r = target.substr(start, length); result->SetString(r); }
void HTTPCookie::SetVersion(const ValueList& args, KValueRef result) { args.VerifyException("setVersion", "i"); int version = args.GetInt(0); if (version > 1 || version < 0) { // Version is out of range, can only be 0 or 1 throw ValueException::FromString("HTTPCookie version invalid, must be 0 or 1"); } this->cookie.setVersion(version); }
void Bytes::_CharAt(const ValueList& args, KValueRef result) { // https://developer.mozilla.org/en/core_javascript_1.5_reference/global_objects/string/charat args.VerifyException("Bytes.charAt", "n"); size_t position = args.GetInt(0); char buf[2] = {'\0', '\0'}; if (position >= 0 && position < this->size) { buf[0] = this->buffer[position]; } result->SetString(buf); }
void Bytes::_Substr(const ValueList& args, KValueRef result) { // This method now follows the spec located at: // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/substr args.VerifyException("Bytes.substr", "i,?i"); std::string target(this->buffer, this->size); int start = args.GetInt(0); if (start > 0 && start >= (int)target.length()) { result->SetString(""); return; } if (start < 0 && (-1*start) > (int)target.length()) { start = 0; } else if (start < 0) { start = target.length() + start; } long length = target.length() - start; if (args.size() > 1) { length = args.GetInt(1); } if (length <= 0) { result->SetString(""); return; } std::string r = target.substr(start, length); result->SetString(r); }
void KEventObject::_RemoveEventListener(const ValueList& args, KValueRef result) { args.VerifyException("removeEventListener", "s n|m"); std::string eventName(args.GetString(0)); if (args.at(1)->IsMethod()) { this->RemoveEventListener(eventName, args.GetMethod(1)); } else { this->RemoveEventListener(eventName, args.GetInt(1)); } }
void Menu::_InsertItemAt(const ValueList& args, SharedValue result) { args.VerifyException("insertItemAt", "o,i"); SharedKObject o = args.at(0)->ToObject(); AutoMenuItem item = o.cast<MenuItem>(); if (!item.isNull()) { if (item->ContainsSubmenu(this)) throw ValueException::FromString("Tried to construct a recursive menu"); size_t index = static_cast<size_t>(args.GetInt(1)); this->InsertItemAt(item, index); } }
void WorkerContext::_Sleep(const ValueList &args, KValueRef result) { args.VerifyException("sleep", "i"); long time = args.GetInt(0); GetLogger()->Debug("Worker will sleep for up to %ld milliseconds", time); terminateEvent.tryWait(time); if (!this->running) { // The main thread has requested that the worker be terminated, // so toss an exception here to try to finish up quickly. throw ValueException::FromString("Worker sleep was interrupted."); } }
void Bytes::_LastIndexOf(const ValueList& args, KValueRef result) { // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/lastIndexOf args.VerifyException("Bytes.lastIndexOf", "s,?i"); std::string target(this->AsString()); int start = args.GetInt(1, target.length() + 1); if (start < 0) start = 0; size_t pos = target.rfind(args.GetString(0), start); if (pos == std::string::npos) { // No matches found result->SetInt(-1); } else { result->SetInt(pos); } }
void APIBinding::_CreateBytes(const ValueList& args, KValueRef result) { args.VerifyException("createBytes", "?s|n"); BytesRef bytes; if (args.size() == 0) { bytes = new Bytes(); } else if (args.at(0)->IsString()) { std::string str(args.GetString(0)); bytes = new Bytes(str); } else { bytes = new Bytes(args.GetInt(0)); } result->SetObject(bytes); }
void Bytes::_Write(const ValueList& args, KValueRef result) { args.VerifyException("write", "s|o ?n"); int offset = args.GetInt(1, 0); int bytesWritten; if (args.at(0)->IsString()) { const char* str = args.at(0)->ToString(); size_t length = strlen(str); bytesWritten = Write(str, length, offset); } else { BytesRef data(args.GetObject(0).cast<Bytes>()); if (data.isNull()) { throw ValueException::FromString("May only write strings or Bytes object"); } bytesWritten = Write(data, offset); } result->SetInt(bytesWritten); }
void Notification::_SetTimeout(const ValueList& args, KValueRef result) { args.VerifyException("setTimeout", "i"); this->timeout = args.GetInt(0); }
void AppBinding::Exit(const ValueList& args, KValueRef result) { host->Exit(args.GetInt(0, 0)); }
void HTTPCookie::SetMaxAge(const ValueList& args, KValueRef result) { args.VerifyException("setMaxAge", "i"); this->cookie.setMaxAge(args.GetInt(0)); }
void Menu::_GetItemAt(const ValueList& args, SharedValue result) { args.VerifyException("getItemAt", "i"); AutoMenuItem item = this->GetItemAt(args.GetInt(0)); result->SetObject(item); }
void HTTPClientBinding::SetTimeout(const ValueList& args, KValueRef result) { args.VerifyException("setTimeout", "i"); this->timeout = args.GetInt(0); }