void UIBinding::Log(Logger::Level level, std::string& message) { if (level > Logger::LWARN) return; std::string methodName("warn"); if (level < Logger::LWARN) methodName = "error"; std::string origMethodName(methodName); origMethodName.append("_orig"); std::vector<AutoUserWindow>& openWindows = UIBinding::GetInstance()->GetOpenWindows(); for (size_t i = 0; i < openWindows.size(); i++) { KObjectRef domWindow = openWindows[i]->GetDOMWindow(); if (domWindow.isNull()) continue; KObjectRef console = domWindow->GetObject("console", 0); if (console.isNull()) continue; KMethodRef method = console->GetMethod(origMethodName.c_str(), 0); if (method.isNull()) method = console->GetMethod(methodName.c_str(), 0); RunOnMainThread(method, ValueList(Value::NewString(message)), false); } }
KValueRef Script::Evaluate(const char *mimeType, const char *name, const char *code, KObjectRef scope) { KObjectRef evaluator = this->FindEvaluatorWithMethod("canEvaluate", mimeType); if (!evaluator.isNull()) { KMethodRef evaluate = evaluator->GetMethod("evaluate"); if (!evaluate.isNull()) { ValueList args; args.push_back(Value::NewString(mimeType)); args.push_back(Value::NewString(name)); args.push_back(Value::NewString(code)); args.push_back(Value::NewObject(scope)); return evaluate->Call(args); } else { throw ValueException::FromFormat( "Error evaluating: No \"evaluate\" method found on evaluator for mimeType: \"%s\"", mimeType); } } else { throw ValueException::FromFormat("Error evaluating: No evaluator found for mimeType: \"%s\"", mimeType); } }
void UIBinding::_SetContextMenu(const ValueList& args, KValueRef result) { args.VerifyException("setContextMenu", "o|0"); KObjectRef argObj = args.GetObject(0, NULL); AutoMenu menu = NULL; if (!argObj.isNull()) { menu = argObj.cast<Menu>(); } this->SetContextMenu(menu); }
static VALUE m_missing(int argc, VALUE* argv, VALUE self) { bool assignment = false; if (global_object.isNull()) return Qnil; // store the method name and arguments in separate variables VALUE method_name, args; rb_scan_args(argc, argv, "1*", &method_name, &args); char* name = strdup(rb_id2name(SYM2ID(method_name))); // Check if this is an assignment if (name[strlen(name) - 1] == '=') { name[strlen(name) - 1] = '\0'; assignment = true; } // If we can't find this property perhaps we should return // the same property name except capitalized. KValueRef v = global_object->Get(name); if (v->IsUndefined()) { name[0] = toupper(name[0]); v = global_object->Get(name); } // Okay, maybe not if (v->IsUndefined()) name[0] = tolower(name[0]); VALUE rval; if (assignment) // Assignment { rval = rb_ary_entry(args, 0); KValueRef val = RubyUtils::ToKrollValue(rval); global_object->Set(name, val); } else if (v->IsMethod()) // Method call { rval = RubyUtils::GenericKMethodCall(v->ToMethod(), args); } else // Plain old access { rval = RubyUtils::ToRubyValue(v); } return rval; }
void UIBinding::_SetMenu(const ValueList& args, KValueRef result) { args.VerifyException("setMenu", "o|0"); KObjectRef argObj = args.GetObject(0, NULL); AutoMenu menu = NULL; if (!argObj.isNull()) { menu = argObj.cast<Menu>(); } this->SetMenu(menu); // platform-specific impl // Notify all windows that the app menu has changed. std::vector<AutoUserWindow>::iterator i = openWindows.begin(); while (i != openWindows.end()) { (*i++)->AppMenuChanged(); } }
void RubyEvaluator::ContextToGlobal(VALUE ctx, KObjectRef o) { if (global_object.isNull()) return; // Next copy all methods over -- they override variables VALUE methods = rb_funcall(ctx, rb_intern("singleton_methods"), 0); for (long i = 0; i < RARRAY_LEN(methods); i++) { VALUE meth_symbol = rb_ary_entry(methods, i); const char* meth_name = STR2CSTR(meth_symbol); // Skip our special method_missing method if (strcmp(meth_name, "method_missing") == 0) continue; volatile VALUE rmeth = rb_funcall(ctx, rb_intern("method"), 1, meth_symbol); KMethodRef method = new KRubyMethod(rmeth, meth_name); o->SetMethod(meth_name, method); } }
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; }