2 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #ifndef qscriptengine_p_h
21 #define qscriptengine_p_h
23 #include "qscriptconverter_p.h"
24 #include "qscriptengine.h"
25 #include "qscriptstring_p.h"
26 #include "qscriptsyntaxcheckresult_p.h"
27 #include "qscriptvalue.h"
28 #include <JavaScriptCore/JavaScript.h>
29 #include <JSBasePrivate.h>
30 #include <QtCore/qshareddata.h>
31 #include <QtCore/qstring.h>
34 class QScriptSyntaxCheckResultPrivate
;
36 class QScriptEnginePrivate
: public QSharedData
{
38 static QScriptEnginePrivate
* get(const QScriptEngine
* q
) { Q_ASSERT(q
); return q
->d_ptr
.data(); }
39 static QScriptEngine
* get(const QScriptEnginePrivate
* d
) { Q_ASSERT(d
); return d
->q_ptr
; }
41 QScriptEnginePrivate(const QScriptEngine
*);
42 ~QScriptEnginePrivate();
44 QScriptSyntaxCheckResultPrivate
* checkSyntax(const QString
& program
);
45 QScriptValuePrivate
* evaluate(const QString
& program
, const QString
& fileName
, int lineNumber
);
46 QScriptValuePrivate
* evaluate(const QScriptProgramPrivate
* program
);
47 inline JSValueRef
evaluate(JSStringRef program
, JSStringRef fileName
, int lineNumber
);
49 inline void collectGarbage();
50 inline void reportAdditionalMemoryCost(int cost
);
52 inline JSValueRef
makeJSValue(double number
) const;
53 inline JSValueRef
makeJSValue(int number
) const;
54 inline JSValueRef
makeJSValue(uint number
) const;
55 inline JSValueRef
makeJSValue(const QString
& string
) const;
56 inline JSValueRef
makeJSValue(bool number
) const;
57 inline JSValueRef
makeJSValue(QScriptValue::SpecialValue value
) const;
59 QScriptValuePrivate
* globalObject() const;
61 inline QScriptStringPrivate
* toStringHandle(const QString
& str
) const;
63 inline JSGlobalContextRef
context() const;
66 JSGlobalContextRef m_context
;
71 Evaluates given JavaScript program and returns result of the evaluation.
72 \attention this function doesn't take ownership of the parameters.
75 JSValueRef
QScriptEnginePrivate::evaluate(JSStringRef program
, JSStringRef fileName
, int lineNumber
)
78 JSValueRef result
= JSEvaluateScript(m_context
, program
, /* Global Object */ 0, fileName
, lineNumber
, &exception
);
80 return exception
; // returns an exception
84 void QScriptEnginePrivate::collectGarbage()
86 JSGarbageCollect(m_context
);
89 void QScriptEnginePrivate::reportAdditionalMemoryCost(int cost
)
92 JSReportExtraMemoryCost(m_context
, cost
);
95 JSValueRef
QScriptEnginePrivate::makeJSValue(double number
) const
97 return JSValueMakeNumber(m_context
, number
);
100 JSValueRef
QScriptEnginePrivate::makeJSValue(int number
) const
102 return JSValueMakeNumber(m_context
, number
);
105 JSValueRef
QScriptEnginePrivate::makeJSValue(uint number
) const
107 return JSValueMakeNumber(m_context
, number
);
110 JSValueRef
QScriptEnginePrivate::makeJSValue(const QString
& string
) const
112 JSStringRef tmp
= QScriptConverter::toString(string
);
113 JSValueRef result
= JSValueMakeString(m_context
, tmp
);
114 JSStringRelease(tmp
);
118 JSValueRef
QScriptEnginePrivate::makeJSValue(bool value
) const
120 return JSValueMakeBoolean(m_context
, value
);
123 JSValueRef
QScriptEnginePrivate::makeJSValue(QScriptValue::SpecialValue value
) const
125 if (value
== QScriptValue::NullValue
)
126 return JSValueMakeNull(m_context
);
127 return JSValueMakeUndefined(m_context
);
130 QScriptStringPrivate
* QScriptEnginePrivate::toStringHandle(const QString
& str
) const
132 return new QScriptStringPrivate(str
);
135 JSGlobalContextRef
QScriptEnginePrivate::context() const