void RubyEvaluator::Evaluate(const ValueList& args, KValueRef result) { args.VerifyException("evaluate", "s s s o"); //const char *mimeType = args.GetString(0).c_str(); std::string name = args.GetString(1); std::string code = args.GetString(2); global_object = args.GetObject(3); VALUE ctx = this->GetContext(global_object); VALUE rargs = rb_ary_new(); rb_ary_push(rargs, ctx); rb_ary_push(rargs, rb_str_new2(code.c_str())); int error; VALUE returnValue = rb_protect(reval_do_call, rargs, &error); RubyEvaluator::ContextToGlobal(ctx, global_object); if (error != 0) { std::string error("An error occured while parsing Ruby ("); error += name; error += "): "; // Display a stringified version of the exception. VALUE exception = rb_gv_get("$!"); KValueRef v = RubyUtils::ToKrollValue(exception); SharedString ss = v->DisplayString(); error.append(ss->c_str()); // Try to make a nice backtrace for the user. VALUE backtrace = rb_funcall(exception, rb_intern("backtrace"), 0); VALUE rBacktraceString = rb_funcall(backtrace, rb_intern("join"), 1, rb_str_new2("\n")); if (TYPE(rBacktraceString) == T_STRING) { error.append("\n"); error.append(StringValuePtr(rBacktraceString)); } Logger *logger = Logger::Get("Ruby"); logger->Error(error); result->SetUndefined(); return; } result->SetValue(RubyUtils::ToKrollValue(returnValue)); }
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; }
//---------------- 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); } }
SharedString KObject::DisplayString(int levels) { std::stringstream ss; SharedStringList props = this->GetPropertyNames(); ss << "(" << this->GetType() << ")" << " {"; for (size_t i = 0; i < props->size(); i++) { KValueRef prop = this->Get(props->at(i)); SharedString disp_string = prop->DisplayString(levels); ss << " " << *(props->at(i)) << " : " << *disp_string << ","; } if (props->size() > 0) // Erase last comma ss.seekp((int)ss.tellp() - 1); ss << "}"; return new std::string(ss.str()); }