QVariant KJSEmbed::convertToVariant(KJS::ExecState *exec, KJS::JSValue *value) { #ifdef KJSEMBED_VARIANT_DEBUG qDebug() << "KJSEmbed::convertToVariant"; #endif QVariant returnValue; switch (value->type()) { case KJS::UndefinedType: case KJS::NullType: break; case KJS::StringType: returnValue = toQString(value->toString(exec)); break; case KJS::NumberType: returnValue = value->toNumber(exec); break; case KJS::BooleanType: returnValue = value->toBoolean(exec); break; case KJS::ObjectType: { KJS::JSObject *obj = value->toObject(exec); //qDebug() << "Object type: " << toQString(obj->className()); if (toQString(obj->className()) == "Array") { if (checkArray(exec, value) == List) { returnValue = convertArrayToList(exec, value); } else { returnValue = convertArrayToMap(exec, value); } } else if (toQString(obj->className()) == "Date") { returnValue = convertDateToDateTime(exec, value); } else { returnValue = extractVariant(exec, value); } //if( returnValue.isNull() ) returnValue = toQString(value->toString(exec)); } break; default: returnValue = extractVariant(exec, value); //if( returnValue.isNull() ) returnValue = toQString(value->toString(exec)); break; } return returnValue; }
bool EventProxy::callHandler( QEvent *e ) { // Be careful enabling this as if there are a lot of events then the event loop times // out and the app crashes with 'Alarm Clock'. // qDebug("JSObjectEventProxy::callHandler() event type %d" , e->type() ); KJS::ExecState *exec = m_interpreter->globalExec(); KJS::Identifier id = JSEventMapper::mapper()->findEventHandler( e->type() ); KJS::JSObject *jsobj(m_watch); KJS::JSObject *fun = jsobj->get(exec, id )->toObject( exec ); KJS::JSValue *retValue; if ( !fun->implementsCall() ) { QString msg = i18n( "Bad event handler: Object %1 Identifier %2 Method %3 Type: %4.", jsobj->className().ascii(), id.ascii(), fun->className().ascii(), e->type()); retValue = throwError(exec, KJS::TypeError, msg); } else { // Process args KJS::List args; args.append( JSEventUtils::event(exec, e) ); // Call handler retValue = fun->call( exec, jsobj, args ); } if ( exec->hadException() ) { if (m_interpreter->shouldPrintExceptions()) { KJS::JSLock lock; KJS::JSObject* exceptObj = retValue->toObject(exec); QString message = toQString(exceptObj->toString(exec)); QString sourceURL = toQString(exceptObj->get(exec, "sourceURL")->toString(exec)); int sourceId = exceptObj->get(exec, "sourceId")->toUInt32(exec); int line = exceptObj->get(exec, "line")->toUInt32(exec); (*KJSEmbed::conerr()) << i18n("Exception calling '%1' function from %2:%3:%4", id.ascii(), !sourceURL.isEmpty() ? sourceURL : QString::number(sourceId), line, message) << endl; } // clear it so it doesn't stop other things exec->clearException(); return false; } return true; }
QDateTime convertDateToDateTime(KJS::ExecState *exec, KJS::JSValue *value) { KJS::List args; QDateTime returnDateTime; KJS::JSObject *obj = value->toObject(exec); if (toQString(obj->className()) == "Date") { int seconds = int(obj->get(exec, KJS::Identifier("getSeconds"))->toObject(exec)->call(exec, obj, args)->toInteger(exec)); int minutes = int(obj->get(exec, KJS::Identifier("getMinutes"))->toObject(exec)->call(exec, obj, args)->toInteger(exec)); int hours = int(obj->get(exec, KJS::Identifier("getHours"))->toObject(exec)->call(exec, obj, args)->toInteger(exec)); int month = int(obj->get(exec, KJS::Identifier("getMonth"))->toObject(exec)->call(exec, obj, args)->toInteger(exec)); int day = int(obj->get(exec, KJS::Identifier("getDate"))->toObject(exec)->call(exec, obj, args)->toInteger(exec)); int year = int(obj->get(exec, KJS::Identifier("getFullYear"))->toObject(exec)->call(exec, obj, args)->toInteger(exec)); returnDateTime.setDate(QDate(year, month + 1, day)); returnDateTime.setTime(QTime(hours, minutes, seconds)); } else { // Throw error } return returnDateTime; }
JavaScriptArrayType checkArray(KJS::ExecState *exec, KJS::JSValue *val) { KJS::JSObject *obj = val->toObject(exec); if (toQString(obj->className()) == "Array") { if (!obj->hasProperty(exec, KJS::Identifier("length"))) { return Map; } KJS::JSValue *jslen = obj->get(exec, KJS::Identifier("length")); const int len = jslen->toNumber(exec); if (len > 0) { QByteArray buff; buff.setNum(len - 1); if (!obj->hasProperty(exec, KJS::Identifier(buff.data()))) { return Map; } } return List; } else { return None; } }