void JavascriptModuleInstance::Run() { std::string code(FileUtils::ReadFile(this->path)); // Insert the global object into this script's context. KValueRef globalValue = Value::NewObject(host->GetGlobalObject()); JSValueRef jsAPI = KJSUtil::ToJSValue(globalValue, context); JSStringRef propertyName = JSStringCreateWithUTF8CString(PRODUCT_NAME); JSObjectSetProperty(context, global, propertyName, jsAPI, kJSPropertyAttributeNone, NULL); JSStringRelease(propertyName); // Check the script's syntax. JSValueRef exception; JSStringRef jsCode = JSStringCreateWithUTF8CString(code.c_str()); bool syntax = JSCheckScriptSyntax(context, jsCode, NULL, 0, &exception); if (!syntax) { KValueRef e = KJSUtil::ToKrollValue(exception, context, NULL); JSStringRelease(jsCode); throw ValueException(e); } // Run the script. JSValueRef ret = JSEvaluateScript(context, jsCode, NULL, NULL, 1, &exception); JSStringRelease(jsCode); if (ret == NULL) { KValueRef e = KJSUtil::ToKrollValue(exception, context, NULL); throw ValueException(e); } }
SharedValue KKJSMethod::Call(const ValueList& args) { JSValueRef* js_args = new JSValueRef[args.size()]; for (int i = 0; i < (int) args.size(); i++) { SharedValue arg = args.at(i); js_args[i] = KJSUtil::ToJSValue(arg, this->context); } JSValueRef exception = NULL; JSValueRef js_value = JSObjectCallAsFunction( this->context, this->object, this->this_obj, args.size(), js_args, &exception); delete [] js_args; // clean up args if (js_value == NULL && exception != NULL) //exception thrown { SharedValue tv_exp = KJSUtil::ToKrollValue(exception, this->context, NULL); throw ValueException(tv_exp); } return KJSUtil::ToKrollValue(js_value, this->context, NULL); }
static inline int64 ParseEnumString(const string& ValueString, const ESFixedTypeBase* Type) { if (Type->As<ESException>() == nullptr) { throw ValueException((string)"Value \"" + ValueString + "\" is not a valid " + " constructor for type with ID: " + to_string(Type->GetID())); } // Try querying with just the string first string QueryString = ValueString; if(!Type->As<ESEnumType>()->IsValidEnumConstructor(QueryString)) { throw ValueException((string)"Value \"" + ValueString + "\" is not a valid " + " constructor for enumerated type " + Type->As<ESEnumType>()->GetName()); } return Type->As<ESEnumType>()->GetEnumValueIDForName(QueryString); }
static inline int64 ParseBoolString(const string& ValueString) { if(ValueString != "true" && ValueString != "false") { throw ValueException((string)"Value \"" + ValueString + "\" is a malformed boolean value"); } return (ValueString == "true" ? 1 : 0); }
const std::string& ValueWithSystematicsInternal<T>::Metadata(const std::string& key) const { if(metadata.count(key) == 0) { if(isLocked) throw ValueException("Unable to add metadata \""+key+"\" after locking."); metadata[key] = ""; } return metadata[key]; }
ValueException ValueException::FromFormat(const char* format, ...) { va_list args; va_start(args, format); std::string text = Logger::Format(format, args); va_end(args); return ValueException(Value::NewString(text)); }
const T& ValueWithSystematicsInternal<T>::Systematic(const std::string& name) const { if(systematics.count(name) == 0) { if(isLocked) throw ValueException("Unable to add systematic \""+name+"\" after locking."); systematics[name] = value; } return systematics[name]; }
bool ValueWithSystematicsInternal<T>::AddMetadata(const std::string& key, const std::string& value) { std::cout << "Deprecated AddMetadata used" << std::endl; if(metadata.count(key) != 0) std::cout << "Metadata already exists with that key, it will be overwritten. Old value: \"" << metadata[key] << "\"" << std::endl; else if(isLocked) throw ValueException("Unable to add metadata \""+key+":"+value+"\" after locking."); metadata[key] = value; return true; }
MainThreadJob::MainThreadJob(KMethodRef method, KObjectRef thisObject, const ValueList& args, bool waitForCompletion) : method(method), thisObject(thisObject), args(args), waitForCompletion(waitForCompletion), returnValue(NULL), exception(ValueException(NULL)), semaphore(0, 1) { // The semaphore starts at 0, meaning that the calling // thread can wait for the value to become >0 using wait() // and the main thread can call set() after job execution // which meets this condition. }
int Value::ToInt() const { if (IsNull()) return 0; else { if (IsNumber()) { std::stringstream sstr(valueStr); int val; sstr >> val; return val; } else throw ValueException(); }
void JavaScriptModuleInstance::Run() { std::string code(FileUtils::ReadFile(this->path)); // Check the script's syntax. JSValueRef exception; JSStringRef jsCode = JSStringCreateWithUTF8CString(code.c_str()); bool syntax = JSCheckScriptSyntax(context, jsCode, NULL, 0, &exception); if (!syntax) { KValueRef e = KJSUtil::ToKrollValue(exception, context, NULL); JSStringRelease(jsCode); throw ValueException(e); } KJSUtil::Evaluate(context, code.c_str()); }
SharedValue KRubyObject::Get(const char *name) { std::string iv_name = std::string("@") + name; ID iv_ID = rb_intern(iv_name.c_str()); ID get_ID = rb_intern(name); ID mm_ID = rb_intern("method_missing"); VALUE ruby_value = Qnil; if (rb_obj_respond_to(object, get_ID, Qtrue) == Qtrue) { ruby_value = rb_funcall(object, rb_intern("method"), 1, ID2SYM(get_ID)); } else if (rb_ivar_defined(object, iv_ID)) { ruby_value = rb_ivar_get(object, iv_ID); } else if (rb_obj_respond_to(object, mm_ID, Qtrue) == Qtrue) { // If this object has a method_missing, call that and return the result, int error; VALUE rargs = rb_ary_new(); rb_ary_push(rargs, object); rb_ary_push(rargs, ID2SYM(get_ID)); ruby_value = rb_protect(kobj_do_method_missing_call, rargs, &error); // protect against NoMethodErrors which we don't want to propogate // back through Kroll, but other exceptions should be thrown. VALUE exception = rb_gv_get("$!"); if (rb_obj_is_kind_of(exception, rb_eNoMethodError) == Qtrue || rb_obj_is_kind_of(exception,rb_eNameError) == Qtrue) { return Value::Undefined; } else { SharedValue exceptionValue = RubyUtils::ToKrollValue(exception); ValueException e = ValueException(exceptionValue); throw e; } } return RubyUtils::ToKrollValue(ruby_value); }
void KRubyObject::Set(const char *name, SharedValue value) { VALUE ruby_value = RubyUtils::ToRubyValue(value); std::string setter_name = std::string(name) + "="; ID set_ID = rb_intern(setter_name.c_str()); int error = 0; if (rb_obj_respond_to(object, set_ID, Qtrue) == Qtrue) { rb_funcall(object, set_ID, 1, ruby_value); } else { // First try calling method missing VALUE rargs = rb_ary_new(); rb_ary_push(rargs, object); rb_ary_push(rargs, rb_str_new2(name)); rb_ary_push(rargs, ruby_value); rb_protect(kobj_do_method_missing_call, rargs, &error); // If the exception wasn't a normal NoMethod exception actually // throw an exception here, because something went wrong. VALUE exception = rb_gv_get("$!"); if (rb_obj_is_kind_of(exception, rb_eNoMethodError) != Qtrue && rb_obj_is_kind_of(exception,rb_eNameError) == Qtrue) { SharedValue exceptionValue = RubyUtils::ToKrollValue(exception); ValueException e = ValueException(exceptionValue); throw e; } } // Last resort: set an instance variable if (error != 0) { std::string iv_name = std::string("@") + name; rb_iv_set(object, iv_name.c_str(), ruby_value); } }
KValueRef KKJSMethod::Call(JSObjectRef thisObject, const ValueList& args) { JSValueRef* jsArgs = new JSValueRef[args.size()]; for (int i = 0; i < (int) args.size(); i++) { KValueRef arg = args.at(i); jsArgs[i] = KJSUtil::ToJSValue(arg, this->context); } JSValueRef exception = NULL; JSValueRef jsValue = JSObjectCallAsFunction(this->context, thisObject, this->thisObject, args.size(), jsArgs, &exception); delete [] jsArgs; // clean up args if (jsValue == NULL && exception != NULL) //exception thrown { KValueRef exceptionValue = KJSUtil::ToKrollValue(exception, this->context, NULL); throw ValueException(exceptionValue); } return KJSUtil::ToKrollValue(jsValue, this->context, NULL); }
static inline int64 ParseBVString(const string& ValueString, uint32 NumBits) { string ValString = ValueString; int64 Retval; boost::algorithm::trim(ValString); if(boost::algorithm::istarts_with(ValString, "0x") || boost::algorithm::istarts_with(ValString, "#x")) { if (boost::algorithm::istarts_with(ValString, "#x")) { ValString[0] = '0'; } if (ValString.length() != (NumBits / 4) + 2) { throw ValueException((string)"Value \"" + ValString + "\" is not a " + to_string(NumBits) + " bit bitvector value"); } istringstream istr(ValString); istr >> hex >> Retval; } else if(boost::algorithm::istarts_with(ValString, "0b") ||
ValueException ValueException::FromString(std::string s) { return ValueException(Value::NewString(s)); }
ValueException ValueException::FromObject(KObjectRef o) { return ValueException(Value::NewObject(o)); }
void do_test_pool(const TestPoolOpts& opts) { std::cout << "Testing with parameters: " << opts.to_string() << std::endl; thread_pool<Value> pool{opts.n_workers}; std::cout << "Testing exception resilency ... " << std::endl; { Barrier min_threads{opts.n_workers}; std::atomic_size_t count{0}; std::vector<std::future<Value>> futures; for (size_t i = 0; i < opts.n_workers; ++i) { futures.push_back(pool.submit([i, &min_threads, &count, &opts]() -> Value { min_threads.wait(); count_guard guard{count, opts.n_workers}; throw ValueException(Value(i, std::to_string(i))); })); } check_exceptions(futures); test_assert(count == 0, "an invariant broken"); } std::cout << "Done" << std::endl; std::cout << "Testing minimal and maximal concurrency ... " << std::endl; { Barrier min_threads{std::min(opts.n_workers, opts.n_items)}; std::atomic_size_t count{0}; std::vector<std::future<Value>> futures; for (size_t i = 0; i < opts.n_items; ++i) { futures.push_back(pool.submit([i, &min_threads, &count, &opts]() { if (i < opts.n_workers) { min_threads.wait(); } count_guard guard{count, opts.n_workers}; return Value(i, std::to_string(i)); })); } check_results(futures); test_assert(count == 0, "an invariant broken"); } std::cout << "Done" << std::endl; std::cout << "Testing shutdown ... " << std::endl; { Barrier min_threads{std::min(opts.n_workers, opts.n_items)}; std::atomic_size_t count{0}; std::vector<std::future<Value>> futures; for (size_t i = 0; i < opts.n_items; ++i) { futures.push_back(pool.submit([i, &min_threads, &count, &opts]() { if (i < opts.n_workers) { min_threads.wait(); } count_guard guard{count, opts.n_workers}; return Value(i, std::to_string(i)); })); } pool.shutdown(); check_results(futures); bool thrown = false; try { pool.submit([]{return Value();}); } catch (const std::exception&) { thrown = true; } test_assert(thrown, "expected exception"); test_assert(count == 0, "an invariant broken"); } std::cout << "Done" << std::endl; std::cout << "OK" << std::endl; }
const char * ValueImpl::to_utf8() const { throw ValueException("No conversion to UTF8"); }
ValueIterator ValueImpl::end() const { throw ValueException("No end() available"); }
Document & ValueImpl::to_document() { throw ValueException("No conversion to document available"); }
Value ValueImpl::operator[](int i) const { throw ValueException("No conversion to array available"); }
Value ValueImpl::operator[](const char * str) const { throw ValueException("No conversion to document available"); }
std::tuple<const uint8_t *, size_t> ValueImpl::to_bson() const { throw ValueException("No conversion to bson"); }
int32_t ValueImpl::to_int32() const { throw ValueException("No conversion to int32"); }
ValueException ValueException::FromObject(SharedKObject o) { return ValueException(Value::NewObject(o)); }
ValueException ValueException::FromString(const char* s) { return ValueException(Value::NewString(s)); }
ValueException RubyUtils::GetException() { VALUE e = rb_gv_get("$!"); SharedValue v = RubyUtils::ToKrollValue(e); return ValueException(v); }
ValueIterator ValueImpl::begin() const { throw ValueException("No begin() available"); }