AutoPtr<StaticBoundObject> ScopeMethodDelegate::CreateDelegate(KObjectRef global, KObjectRef bo) { AutoPtr<StaticBoundObject> scope = new StaticBoundObject(); SharedStringList keys = bo->GetPropertyNames(); StringList::iterator iter = keys->begin(); while(iter!=keys->end()) { SharedString key_ptr = (*iter++); std::string key = *key_ptr; KValueRef value = bo->Get(key.c_str()); if (key == "set") { KMethodRef d = new ScopeMethodDelegate(SET, global, scope, value->ToMethod()); KValueRef v = Value::NewMethod(d); scope->Set(key.c_str(), v); } else if (key == "get") { KMethodRef d = new ScopeMethodDelegate(GET, global, scope, value->ToMethod()); KValueRef v = Value::NewMethod(d); scope->Set(key.c_str(), v); } else { scope->Set(key.c_str(), value); } } return scope; }
KValueRef ProfiledBoundObject::Wrap(KValueRef value, std::string type) { if (AlreadyWrapped(value)) { return value; } else if (value->IsMethod()) { KMethodRef toWrap = value->ToMethod(); KMethodRef wrapped = new ProfiledBoundMethod(toWrap, type); return Value::NewMethod(wrapped); } else if (value->IsList()) { KListRef wrapped = new ProfiledBoundList(value->ToList()); return Value::NewList(wrapped); } else if (value->IsObject()) { KObjectRef wrapped = new ProfiledBoundObject(value->ToObject()); return Value::NewObject(wrapped); } else { return value; } }
void WorkerContext::DeliverMessage(KValueRef message) { AutoPtr<Event> event(this->CreateEvent("worker.message")); event->Set("message", message); KValueRef callback = this->Get("onmessage"); if (callback->IsMethod()) callback->ToMethod()->Call(Value::NewObject(event)); }
KMethodRef KObject::GetMethod(const char* name, KMethodRef defaultValue) { KValueRef prop = this->Get(name); if (prop->IsMethod()) { return prop->ToMethod(); } else { return defaultValue; } }
void WorkerContext::DeliverMessage(KValueRef message) { AutoPtr<Event> event(this->CreateEvent("worker.message")); event->Set("message", message); KValueRef callback = this->Get("onmessage"); if (callback->IsMethod()) { Host::GetInstance()->RunOnMainThread( callback->ToMethod(), ValueList(Value::NewObject(event)), false); } }
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; }
bool ProfiledBoundObject::AlreadyWrapped(KValueRef value) { if (value->IsMethod()) { KMethodRef source = value->ToMethod(); AutoPtr<ProfiledBoundMethod> po = source.cast<ProfiledBoundMethod>(); return !po.isNull(); } else if (value->IsList()) { KListRef source = value->ToList(); AutoPtr<ProfiledBoundList> po = source.cast<ProfiledBoundList>(); return !po.isNull(); } else if (value->IsObject()) { KObjectRef source = value->ToObject(); AutoPtr<ProfiledBoundObject> po = source.cast<ProfiledBoundObject>(); return !po.isNull(); } else { return true; } }
void WorkerContext::SendQueuedMessages() { Logger *logger = Logger::Get("WorkerContext"); logger->Debug("SendQueuedMessages called"); if (messages.size() <= 0) return; KValueRef onMessageValue = worker->Get("onmessage"); if (!onMessageValue->IsMethod()) return; KMethodRef onMessage(onMessageValue->ToMethod()); Poco::ScopedLock<Poco::Mutex> lock(mutex); std::list<KValueRef>::iterator i = messages.begin(); while (i != messages.end()) { KValueRef message(*i++); this->CallOnMessageCallback(onMessage, message); } messages.clear(); }