diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-10-23 12:45:13 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-10-23 12:45:13 (GMT) |
commit | c21d3a0094b0692f2f888b04e258229234200e3c (patch) | |
tree | f34338ec27f2c4ef5d7294f04d4ab7dac6283f02 /src/script/api/qscriptprogram.cpp | |
parent | b824fc37d618e55bd128bacebb78dc3770c73264 (diff) | |
download | Qt-c21d3a0094b0692f2f888b04e258229234200e3c.zip Qt-c21d3a0094b0692f2f888b04e258229234200e3c.tar.gz Qt-c21d3a0094b0692f2f888b04e258229234200e3c.tar.bz2 |
Refactor QScriptProgram and related API
Get rid of QScriptEngine::compile(); you pass arguments to
the QScriptProgram constructor instead. evaluate() will lazily
compile the program the first time it is evaluated, then use the
cached, compiled form on subsequent calls.
Diffstat (limited to 'src/script/api/qscriptprogram.cpp')
-rw-r--r-- | src/script/api/qscriptprogram.cpp | 61 |
1 files changed, 42 insertions, 19 deletions
diff --git a/src/script/api/qscriptprogram.cpp b/src/script/api/qscriptprogram.cpp index 9f88363..3ea4fa7 100644 --- a/src/script/api/qscriptprogram.cpp +++ b/src/script/api/qscriptprogram.cpp @@ -58,17 +58,18 @@ QT_BEGIN_NAMESPACE */ -QScriptProgramPrivate::QScriptProgramPrivate(QScriptEnginePrivate *e, - JSC::EvalExecutable *x, - intptr_t id) - : engine(e), executable(x), sourceId(id) +QScriptProgramPrivate::QScriptProgramPrivate(const QString &src, + const QString &fn, + int ln) + : sourceCode(src), fileName(fn), lineNumber(ln), + engine(0), _executable(0), sourceId(-1), isCompiled(false) { ref = 0; } QScriptProgramPrivate::~QScriptProgramPrivate() { - delete executable; + delete _executable; } QScriptProgramPrivate *QScriptProgramPrivate::get(const QScriptProgram &q) @@ -76,18 +77,26 @@ QScriptProgramPrivate *QScriptProgramPrivate::get(const QScriptProgram &q) return const_cast<QScriptProgramPrivate*>(q.d_func()); } -QScriptProgram QScriptProgramPrivate::create(QScriptEnginePrivate *engine, - JSC::EvalExecutable *executable, - intptr_t sourceId) +JSC::EvalExecutable *QScriptProgramPrivate::executable(JSC::ExecState *exec, + QScriptEnginePrivate *eng) { - QScriptProgramPrivate *d = new QScriptProgramPrivate(engine, executable, sourceId); - QScriptProgram result; - result.d_ptr = d; - return result; + if (_executable) { + if (eng == engine) + return _executable; + delete _executable; + } + WTF::PassRefPtr<QScript::UStringSourceProviderWithFeedback> provider + = QScript::UStringSourceProviderWithFeedback::create(sourceCode, fileName, lineNumber, eng); + sourceId = provider->asID(); + JSC::SourceCode source(provider, lineNumber); //after construction of SourceCode provider variable will be null. + _executable = new JSC::EvalExecutable(exec, source); + engine = eng; + isCompiled = false; + return _executable; } /*! - Constructs an invalid QScriptProgram. + Constructs a null QScriptProgram. */ QScriptProgram::QScriptProgram() : d_ptr(0) @@ -95,6 +104,17 @@ QScriptProgram::QScriptProgram() } /*! + Constructs a new QScriptProgram with the given \a sourceCode, \a + fileName and \a lineNumber. +*/ +QScriptProgram::QScriptProgram(const QString &sourceCode, + const QString fileName, + int lineNumber) + : d_ptr(new QScriptProgramPrivate(sourceCode, fileName, lineNumber)) +{ +} + +/*! Constructs a new QScriptProgram that is a copy of \a other. */ QScriptProgram::QScriptProgram(const QScriptProgram &other) @@ -125,13 +145,13 @@ QScriptProgram &QScriptProgram::operator=(const QScriptProgram &other) } /*! - Returns true if this QScriptProgram is valid; otherwise + Returns true if this QScriptProgram is null; otherwise returns false. */ -bool QScriptProgram::isValid() const +bool QScriptProgram::isNull() const { Q_D(const QScriptProgram); - return (d && d->engine); + return (d == 0); } /*! @@ -142,7 +162,7 @@ QString QScriptProgram::sourceCode() const Q_D(const QScriptProgram); if (!d) return QString(); - return d->executable->source().toString(); + return d->sourceCode; } /*! @@ -153,7 +173,7 @@ QString QScriptProgram::fileName() const Q_D(const QScriptProgram); if (!d) return QString(); - return d->executable->sourceURL(); + return d->fileName; } /*! @@ -164,7 +184,7 @@ int QScriptProgram::lineNumber() const Q_D(const QScriptProgram); if (!d) return -1; - return d->executable->lineNo(); + return d->lineNumber; } /*! @@ -173,6 +193,9 @@ int QScriptProgram::lineNumber() const */ bool QScriptProgram::operator==(const QScriptProgram &other) const { + Q_D(const QScriptProgram); + if (d == other.d_func()) + return true; return (sourceCode() == other.sourceCode()) && (fileName() == other.fileName()) && (lineNumber() == other.lineNumber()); |