QSObject QSAEditor::vTypeToQSType( const QString &type ) const { QSObject qstype = env()->globalObject().get( type.mid( 1 ) ); if( qstype.objectType() && qstype.objectType()->valueType() == TypeClass ) { return qstype; } QVariant::Type t = QVariant::nameToType( type.toLatin1().constData() ); switch ( t ) { case QVariant::Time: case QVariant::DateTime: return env()->globalObject().get( QString::fromLatin1("Date") ); case QVariant::StringList: case QVariant::List: case QVariant::Map: return env()->globalObject().get( QString::fromLatin1("Array") ); case QVariant::ByteArray: return env()->globalObject().get( QString::fromLatin1("String") ); case QVariant::Int: case QVariant::UInt: case QVariant::Double: return env()->globalObject().get( QString::fromLatin1("Number") ); case QVariant::Bool: return env()->globalObject().get( QString::fromLatin1("Boolean") ); default: break; } return env()->createUndefined(); }
QSClass *QSCheckData::typeInfo( const QString &name ) const { QSMember member; QSScopeResolutionList::const_iterator it = scopeStack.begin(); QSObject tmp; tmp.setVal( (int) 0 ); int offset; while( it!=scopeStack.end() ) { offset = 0; tmp.setType( (*it).cl ); const QSClass *cl; if( (*it).cl->name() == QString::fromLatin1("QObject") ) { cl = env()->thisValue().resolveMember( name, &member, tmp.objectType(), &offset ); } else { cl = tmp.resolveMember( name, &member, tmp.objectType(), &offset ); tmp.invalidate(); } if( cl ) { /* Avoid calls to deref for QSWrapperClass, when tmp is destroyed */ tmp.invalidate(); QSObject o( cl->fetchValue( 0, member ) ); if ( o.objectType() == env()->typeClass() ) return QSTypeClass::classValue(&o); else return 0; } it++; } return 0; }
QSEqualsResult QSFuncRefClass::isEqual( const QSObject &a, const QSObject &b ) const { if( a.objectType() == b.objectType() ) return ( QSEqualsResult ) ( refBase( a ).equals( refBase( b ) ) && refMember( a ) == refMember( b ) ); return EqualsNotEqual; }
void QSPixmapClass::save( QSEnv *env ) { if ( env->numArgs() < 1 || env->numArgs() > 2 ) { env->throwError( QString::fromLatin1( "Pixmap.save() called with %1 arguments. 1 or 2 argument expected." ). arg( env->numArgs() ) ); return; } QSObject t = env->thisValue(); QSPixmapClass *pac = (QSPixmapClass*)t.objectType(); QPixmap *pix = pac->pixmap( &t ); if ( !env->arg( 0 ).isString() ) { env->throwError( QString::fromLatin1( "Pixmap.save() called with an argument of type %1. " "Type String is expeced" ). arg( env->arg( 0 ).typeName() ) ); return; } QString format = QFileInfo( env->arg( 0 ).toString() ).extension().upper(); QStrList l = QImageIO::outputFormats(); if ( l.find( format.latin1() ) == -1 ) format = QString::fromLatin1("PNG"); if ( env->numArgs() == 2 ) { if ( !env->arg( 1 ).isString() ) { env->throwError( QString::fromLatin1( "Pixmap.save() called with an argument of type %1. " "as second argument. Type String is expeced" ). arg( env->arg( 1 ).typeName() ) ); return; } format = env->arg( 1 ).toString(); } pix->save( env->arg( 0 ).toString(), format.latin1() ); }
QSObject QSObject::getQualified(const QString &name) const { QSObject obj = *this; QString s = name; // break up dotted notation ns like P1.P2.P3 while (!s.isEmpty()) { int pos = s.find('.'); if (pos < 0) pos = s.length(); // don't overwrite any existing objects, reuse them QString cname = s.left(pos); QSObject tmpobj = obj.get(cname); // Search the class hieriarchy for member if undefined if (!tmpobj.isDefined()) { QSClass *base = obj.objectType()->base(); while (base && !tmpobj.isDefined()) { tmpobj = base->get(&obj, cname); base = base->base(); } if (!tmpobj.isDefined()) return env()->createUndefined(); } obj = tmpobj; s = s.mid(pos + 1); } return obj; }
void QSPixmapClass::resize( QSEnv *env ) { if ( env->numArgs() < 1 || env->numArgs() > 2 ) { env->throwError( QString::fromLatin1( "Pixmap.resize() called with %1 arguments. 1 or 2 arguments expected." ). arg( env->numArgs() ) ); return; } QSObject t = env->thisValue(); QSPixmapClass *pac = (QSPixmapClass*)t.objectType(); QPixmap *pix = pac->pixmap( &t ); if ( env->numArgs() == 1 ) { QSObject a0 = env->arg( 0 ); if ( !a0.isA( "Size" ) ) { env->throwError( QString::fromLatin1( "Pixmap.resize() called with an argument of type %1. " "Type Size is expeced" ). arg( a0.typeName() ) ); return; } pix->resize( *( (QSSizeClass*)a0.objectType() )->size( &a0 ) ); } else { if ( !env->arg( 0 ).isA( "Number" ) || !env->arg( 1 ).isA( "Number" ) ) { env->throwError( QString::fromLatin1( "Pixmap.resize() called with arguments of type %1 and %2. " "Type Number and Number is expeced" ). arg( env->arg( 0 ).typeName() ).arg( env->arg( 1 ).typeName() ) ); return; } pix->resize( env->arg( 0 ).toInteger(), env->arg( 1 ).toInteger() ); } return; }
void QSPixmapClass::save( QSEnv *env ) { if ( env->numArgs() < 1 || env->numArgs() > 2 ) { env->throwError( QString::fromLatin1( "Pixmap.save() called with %1 arguments. 1 or 2 argument expected." ). arg( env->numArgs() ) ); return; } QSObject t = env->thisValue(); QSPixmapClass *pac = (QSPixmapClass*)t.objectType(); QPixmap *pix = pac->pixmap( &t ); if ( !env->arg( 0 ).isString() ) { env->throwError( QString::fromLatin1( "Pixmap.save() called with an argument of type %1. " "Type String is expeced" ). arg( env->arg( 0 ).typeName() ) ); return; } QString format = QFileInfo( env->arg( 0 ).toString() ).completeSuffix().toUpper(); QList<QByteArray> l = QImageWriter::supportedImageFormats(); if ( l.indexOf( format.toLatin1() ) == -1 ) format = QString::fromLatin1("PNG"); if ( env->numArgs() == 2 ) { if ( !env->arg( 1 ).isString() ) { env->throwError( QString::fromLatin1( "Pixmap.save() called with an argument of type %1. " "as second argument. Type String is expeced" ). arg( env->arg( 1 ).typeName() ) ); return; } format = env->arg( 1 ).toString(); } pix->save( env->arg( 0 ).toString(), format.toLatin1().constData() ); }
QSObject QSRectClass::normalize(QSEnv *env) { QSObject t = env->thisValue(); QSRectClass *cl = (QSRectClass *)t.objectType(); QRect *r = cl->rect(&t); return cl->construct(r->normalize()); }
QSObject QSPixmapClass::isNull( QSEnv *env ) { QSObject t = env->thisValue(); QSPixmapClass *pac = (QSPixmapClass*)t.objectType(); QSObject result; QPixmap *pix = pac->pixmap( &t ); return env->createBoolean( pix->isNull() ); }
QStringList QuickInterpreter::functionsOf(QSObject &obj, bool includeSignature, bool includeNative, bool includeMemberFunctions) const { const QSClass *objType = obj.objectType(); QSMemberMap mmap = objType == env()->typeClass() && includeMemberFunctions ? ((QSTypeClass*) objType)->allMembers(&obj) : objType->members(&obj); QMap<int, QString> functions; for (QSMemberMap::Iterator it = mmap.begin(); it != mmap.end(); ++it) { if (((*it).type() == QSMember::ScriptFunction || includeNative && ((*it).type() == QSMember::NativeFunction || (*it).type() == QSMember::NativeMemberFunction)) && !(*it).isPrivate()) { QString func = (*it).name(); // ### will break with mix of script and C++ functions int idx = (*it).type() == QSMember::ScriptFunction ? (*it).scriptFunction->index() : functions.size(); if (includeSignature) { if ((*it).type() == QSMember::NativeFunction || (*it).type() == QSMember::NativeMemberFunction) { func += QString::fromLatin1("()"); } else { QSFunctionScopeClass *fsc = (*it).scriptFunction->scopeDefinition(); int args = fsc->numArguments(); func += QString::fromLatin1("("); if (args > 0) { func += QString::fromLatin1(" "); QSMemberMap *members = fsc->definedMembers(); QVector<QString> vec(args); for (QSMemberMap::ConstIterator ait = members->begin(); ait != members->end(); ++ait) { if ((*ait).index() < args) vec[ (*ait).index() ] = (*ait).name(); } for (int i = 0; i < args; ++i) { if (i > 0) func += QString::fromLatin1(", "); func += vec[i]; } func += QString::fromLatin1(" "); } func += QString::fromLatin1(")"); } } functions[idx] = func; } } // assemble to list sorted by original index QStringList lst; QMap<int, QString>::const_iterator cit = functions.begin(); for (; cit != functions.end(); ++cit) lst << *cit; return lst; }
QSObject QSRegExpClass::toStringScript( QSEnv *env ) { QSObject that = env->thisValue(); Q_ASSERT(that.objectType() == env->regexpClass()); QString pattern = QString::fromLatin1("/") + source(&that) + QString::fromLatin1("/"); if (isIgnoreCase(&that)) pattern += 'i'; if (isGlobal(&that)) pattern += 'g'; return env->createString(pattern); }
static bool hasMember(QSEnv *env, const QString &function, QSMember::Type type) { QSObject o = env->globalObject(); QSMember member; QStringList names = function.split(QString::fromLatin1(".")); int nameCount = names.count(); for (QStringList::ConstIterator it = names.begin(); it != names.end(); ++it, --nameCount) { if (nameCount==1) { if (o.objectType() == env->typeClass()) return QSTypeClass::classValue(&o)->member(0, *it, &member) && member.type() == type; else if (o.objectType()->member(&o, *it, &member)) return o.objectType()->member(0, *it, &member) && member.type() == type; } else { o = o.get(*it); if (!o.isValid()) return false; } } return false; }
void QSEnv::printScopeChain(const ScopeChain *chain) { QStringList lst; ScopeChain::const_iterator it = chain->begin(); while (it != chain->end()) { QSObject obj = *it; QString tmp = !obj.isValid() ? QString::fromLatin1("#") : obj.objectType()->identifier(); lst.append(tmp); it++; } printf("Current scope is:: %s\n", lst.join(QString::fromLatin1(", ")).latin1()); }
void QSPixmapClass::resize( QSEnv *env ) { if ( env->numArgs() < 1 || env->numArgs() > 2 ) { env->throwError( QString::fromLatin1( "Pixmap.resize() called with %1 arguments. 1 or 2 arguments expected." ). arg( env->numArgs() ) ); return; } QSObject t = env->thisValue(); QSPixmapClass *pac = (QSPixmapClass*)t.objectType(); QPixmap *pix = pac->pixmap( &t ); int w=-1; int h=-1; if ( env->numArgs() == 1 ) { QSObject a0 = env->arg( 0 ); if ( !a0.isA( "Size" ) ) { env->throwError( QString::fromLatin1( "Pixmap.resize() called with an argument of type %1. " "Type Size is expected" ). arg( a0.typeName() ) ); return; } QSize size = *((QSSizeClass*)a0.objectType())->size(&a0); w = size.width(); h = size.height(); } else { if ( !env->arg( 0 ).isA( "Number" ) || !env->arg( 1 ).isA( "Number" ) ) { env->throwError( QString::fromLatin1( "Pixmap.resize() called with arguments of type %1 and %2. " "Type Number and Number is expected" ). arg( env->arg( 0 ).typeName() ).arg( env->arg( 1 ).typeName() ) ); return; } w = env->arg( 0 ).toInteger(); h = env->arg( 1 ).toInteger(); } if (w >= 0 && h >= 0) { QPixmap tmppm(w, h); tmppm.fill(Qt::black); QPainter p(&tmppm); p.drawPixmap(0, 0, *pix); *pix = tmppm; } return; }
QSArgument QuickInterpreter::convertToArgument( const QSObject &o ) { if( !o.isValid() ) return QSArgument(); const QSClass *cl = o.objectType(); if( cl->name() == QString::fromLatin1("QObject") ) { QSWrapperShared *shared = (QSWrapperShared*) o.shVal(); if (shared->objects.isEmpty()) return QSArgument(); return QSArgument( shared->objects[0] ); } else if( cl == ptrClass ) { Q_ASSERT( ptrClass->pointer( &o ) ); return QSArgument( ptrClass->pointer( &o ) ); } else { return QSArgument( o.toVariant( QVariant::Invalid ) ); } }
QStringList QuickInterpreter::variablesOf( QSObject &obj, bool includeStatic, bool includeCustom, bool includeMemberVariables ) const { const QSClass *objType = obj.objectType(); QSMemberMap mmap = objType == env()->typeClass() && includeMemberVariables ? ( (QSTypeClass*) objType )->allMembers( &obj ) : objType->members( &obj ); QStringList lst; for ( QSMemberMap::Iterator it = mmap.begin(); it != mmap.end(); ++it ) { if ( ( (*it).type() == QSMember::Variable || includeCustom && (*it).type() == QSMember::Custom ) && (!(*it).isStatic() || includeStatic) && !(*it).isExecutable() ) lst << (*it).name(); } return lst; }
void QuickInterpreter::addTopLevelObject(QObject *o) { QSEngine::init(); if (!toplevel) toplevel = new QObjectList; if (toplevel->indexOf(o) != -1) return; Q_ASSERT(!hasTopLevelParent(o)); for(int i=0; i<toplevel->size(); ++i) { QObject *cur = toplevel->at(i); if(cur == o) { return; } else if (cur && o && cur->objectName() == o->objectName()) { return; } } toplevel->append(o); kids.clear(); if (!toplevel) return; for (int i=0; i<toplevel->size(); ++i) { QObject *obj = toplevel->at(i); kids.append(obj->objectName()); } connect(o, SIGNAL(destroyed(QObject *)), this, SLOT(topLevelDestroyed(QObject *))); QSObject global = env()->globalObject(); const_cast<QSClass *>(global.objectType())->deleteMember(o->objectName()); QString context_name = o->objectName(); if (context_name.isEmpty()) { context_name = "unnamed"; } else { staticGlobals << context_name; } env()->globalObject().put(context_name, wrap(o)); }
QVariant QuickInterpreter::convertToArgument(const QSObject &o) { if(!o.isValid()) return QVariant(); const QSClass *cl = o.objectType(); if(cl->name() == QString::fromLatin1("QObject")) { QSWrapperShared *shared = (QSWrapperShared*) o.shVal(); if (shared->objects.isEmpty()) return QVariant(); QVariant v(QMetaType::QObjectStar, static_cast<QObject *>(0)); qVariantSetValue<QObject *>(v, shared->objects[0]); return v; } else if(cl == ptrClass) { Q_ASSERT(ptrClass->pointer(&o)); QVariant v(QMetaType::VoidStar, static_cast<void *>(0)); qVariantSetValue<void *>(v, ptrClass->pointer(&o)); return v; } else { return QVariant(o.toVariant(QVariant::Invalid)); } }
QSObject QSRectClass::intersects(QSEnv *env) { QSObject t = env->thisValue(); QSRectClass *cl = (QSRectClass *)t.objectType(); QRect *r = cl->rect(&t); if (env->numArgs() != 1) { env->throwError(QString::fromLatin1("Rect.intersects() called with %1 arguments. 1 argument expected."). arg(env->numArgs())); return env->createUndefined(); } QSObject a0 = env->arg(0); if (!a0.isA(cl)) { env->throwError(QString::fromLatin1("Rect.intersects() called with an argument of type %1. " "Type Rect is expeced"). arg(a0.typeName())); return env->createUndefined(); } return env->createBoolean(r->intersects(*cl->rect(&a0))); }
void QSPixmapClass::load( QSEnv *env ) { if ( env->numArgs() != 1 ) { env->throwError( QString::fromLatin1( "Pixmap.load() called with %1 arguments. 1 argument expected." ). arg( env->numArgs() ) ); return; } if ( !env->arg( 0 ).isString() ) { env->throwError( QString::fromLatin1( "Pixmap.load() called with an argument of type %1. " "Type String is expeced" ). arg( env->arg( 0 ).typeName() ) ); return; } QSObject t = env->thisValue(); QSPixmapClass *pac = (QSPixmapClass*)t.objectType(); QPixmap *pix = pac->pixmap( &t ); pix->load( env->arg( 0 ).toString() ); return; }
void QSPixmapClass::fill( QSEnv *env ) { if ( env->numArgs() != 1 ) { env->throwError( QString::fromLatin1( "Pixmap.fill() called with %1 arguments. 1 arguments expected." ). arg( env->numArgs() ) ); return; } QSObject a0 = env->arg( 0 ); if ( !a0.isA( "Color" ) ) { env->throwError( QString::fromLatin1( "Pixmap.fill() called with an argument of type %1. " "Type Color is expeced" ). arg( a0.typeName() ) ); return; } QSObject t = env->thisValue(); QSPixmapClass *pac = (QSPixmapClass*)t.objectType(); QPixmap *pix = pac->pixmap( &t ); pix->fill( *( (QSColorClass*)a0.objectType() )->color( &a0 ) ); return; }
void QuickInterpreter::addTopLevelObject( QObject *o ) { QSEngine::init(); if ( !toplevel ) toplevel = new QObjectList; if ( toplevel->findRef( o ) != -1 ) return; if ( hasTopLevelParent( o ) ) return; for( QObject *cur = toplevel->first(); cur; cur = toplevel->next() ) { if( cur == o ) { return; } else if ( cur && o && QString::fromLatin1( cur->name() ) == QString::fromLatin1( o->name() ) ) { return; } } toplevel->append( o ); kids.clear(); if ( !toplevel ) return; QObject *obj = toplevel->first(); while ( obj ) { kids.append( QString::fromLatin1(obj->name()) ); obj = toplevel->next(); } connect( o, SIGNAL( destroyed( QObject * ) ), this, SLOT( topLevelDestroyed( QObject * ) ) ); QSObject global = env()->globalObject(); const_cast<QSClass *>(global.objectType())->deleteMember(o->name()); env()->globalObject().put( QString::fromLatin1(o->name()), wrap( o ) ); staticGlobals << QString::fromLatin1(o->name()); }
void QSAEditor::doObjectCompletion() { QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); QString objectName = cursor.selectedText(); if (objectName.endsWith(QLatin1Char('-'))) objectName.chop(1); objectName = objectName.simplified(); QString object = resolveFullyQualifiedValue(objectName, parseAssignments(functionCode())); bool assumedStatic = false; QSCompletionObject o; if(objectName == object) { QSObject stobj = env()->globalObject().get(object); if(stobj.isValid() && stobj.objectType()->valueType() == TypeClass) { #if defined ( QSA_COMPLETION_DEBUG ) printf(" -> assuming static\n"); #endif o = stobj; assumedStatic = true; } } if(o.type == QSCompletionObject::TNull) { o = queryObject(object); } #if defined ( QSA_COMPLETION_DEBUG ) printf(" -> type is: %d\n", o.type); #endif o.resolve(); if (o.isNull()) return; QVector<CompletionEntry> res; QSObject nullObject; switch (o.type) { case QSCompletionObject::TQSObject: #if defined ( QSA_COMPLETION_DEBUG ) printf(" -> objectType is: %s\n", o.qsobj.objectType()->name().latin1()); #endif if(o.qsobj.objectType()->name() == QString::fromLatin1("FactoryObject")){ QSObject sinst = ( (QSFactoryObjectProxy*) o.qsobj.objectType() )->staticInstance(); if(!sinst.isValid()) return; QSWrapperShared *shared = (QSWrapperShared*) sinst.shVal(); completeQObject( shared->objects, object, res ); break; } completeQSObject(o.qsobj, res, !assumedStatic); break; case QSCompletionObject::TQMetaObject: completeQMetaObject(o.meta, object, res, IncludeSuperClass, nullObject); break; case QSCompletionObject::TQObject: completeQObject(o.qobj, object, res); break; case QSCompletionObject::TNull: break; } if (!res.isEmpty()) { QFrame *f = new QFrame(0, Qt::Popup); f->setAttribute(Qt::WA_DeleteOnClose); QWidget *box = new CompletionBox(this, res); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(box); layout->setMargin(0); f->setLayout(layout); f->move(mapToGlobal(cursorRect().bottomLeft())); f->show(); box->setFocus(); } }
bool QuickInterpreter::hasClass(const QString &className) const { QSObject foo = object(className); return foo.objectType() == env()->typeClass(); }
void QSFuncDeclNode::check( QSCheckData *c ) { // qDebug( "Function noticed: " + c->globalName(ident) ); if ( attrs ) attrs->check( c ); else c->setLastAttributes( AttributeNone ); int as = c->lastAttributes(); QSClass * cl = c->currentScope(); Q_ASSERT( cl ); if ( (as&AttributeStatic) && cl->name() != QString::fromLatin1("Class") ) { c->addError( this, QSErrAttrStaticContext, QString::fromLatin1( "Function '%1' cannot be declared static " "outside a class" ).arg( ident ) ); return; } // A bit of magic fail early when trying to overwrite a context. if (c->inGlobal()) { QSObject object = c->env()->globalObject().get(ident); if (object.isValid()) { if (object.objectType()->name() == QString::fromLatin1("QObject")) { c->addError(this, QString("Cannot declare function '%1', already a global object " "present with same name").arg(ident)); return; } } } QSMember m; m.setPrivate( as&AttributePrivate ); if ( cl->member( 0, ident, &m ) ) { QSMember mem( body, as ); cl->replaceMember( ident, &mem ); } else { cl->addFunctionMember( ident, body, as ); } int tmpVarBlockCount = c->varBlockCount(); c->setVarBlockCount( 0 ); QSFunctionScopeClass * fscope = new QSFunctionScopeClass( c->env()->objectClass(), this ); fscope->setEnclosingClass( cl ); body->setScopeDefinition( fscope ); fscope->setFunctionBodyNode(body); c->enterFunction( fscope ); if( param ) param->check( c ); body->check( c ); c->leaveFunction(); if( c->varBlockCount()>fscope->numVariables() ) fscope->setNumVariables( c->varBlockCount() ); c->setVarBlockCount( tmpVarBlockCount ); // Calculate the number of arguments int count = 0; QSParameterNode * node = param; while( node ) { count++; node = node->nextParam(); } fscope->setNumArguments( count ); // unset attributes c->setLastAttributes( AttributeNone ); }
// eval() static QSObject qsEval( QSEnv *env ) { QSObject x = env->arg( 0 ); if ( !x.isString() ) { return x; } else { QSEngineImp *eimp = env->engine()->imp(); QString s = x.toString(); QSNode *progNode = 0; { // begin mutext locker QMutexLocker locker(qsa_lexer_mutex()); QSLexer::lexer()->setCode( s, #ifdef QSDEBUGGER eimp->sourceId() #else 0 #endif ); int yp = qsyyparse(); progNode = QSProgramNode::last(); if ( yp || QSLexer::lexer()->lexerState() == QSLexer::Bad ) { if (!progNode && progNode->deref()) delete progNode; return env->throwError( SyntaxError ); } } // end mutext locker QSCheckData data( env ); QSObject scopeTop = env->currentScope(); postfixAdd( scopeTop.objectType(), &data ); QSEvalScopeClass *cl = new QSEvalScopeClass( env->objectClass() ); data.enterEval( cl ); progNode->check( &data ); if ( data.hasError() ) { if (!progNode->deref()) delete progNode; return env->throwError( EvalError, data.errorMessages().first(), data.errorLines().first() ); } QSList empty; env->pushScope( cl->construct( empty ) ); // ### delete tmp scope class QSObject res = ((QSProgramNode*)progNode)->execute( env ); if (!progNode->deref()) delete progNode; env->popScope(); if ( env->isReturnValueMode() ) return res; else if ( env->isNormalMode() ) { if ( res.isValid() ) return res; else return env->createUndefined(); } else return res; } }
static QSObject transpose(QSEnv *env) { QSObject t = env->thisValue(); ((QSSizeClass *)t.objectType())->size(&t)->transpose(); return env->createUndefined(); }