]> git.saurik.com Git - apple/javascriptcore.git/blob - qt/api/qscriptengine_p.h
c6c64dd9fed483b723313c618c094a3ce1c5e9ca
[apple/javascriptcore.git] / qt / api / qscriptengine_p.h
1 /*
2 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3
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.
8
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.
13
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.
18 */
19
20 #ifndef qscriptengine_p_h
21 #define qscriptengine_p_h
22
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>
32
33 class QScriptEngine;
34 class QScriptSyntaxCheckResultPrivate;
35
36 class QScriptEnginePrivate : public QSharedData {
37 public:
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; }
40
41 QScriptEnginePrivate(const QScriptEngine*);
42 ~QScriptEnginePrivate();
43
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);
48
49 inline void collectGarbage();
50 inline void reportAdditionalMemoryCost(int cost);
51
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;
58
59 QScriptValuePrivate* globalObject() const;
60
61 inline QScriptStringPrivate* toStringHandle(const QString& str) const;
62
63 inline JSGlobalContextRef context() const;
64 private:
65 QScriptEngine* q_ptr;
66 JSGlobalContextRef m_context;
67 };
68
69
70 /*!
71 Evaluates given JavaScript program and returns result of the evaluation.
72 \attention this function doesn't take ownership of the parameters.
73 \internal
74 */
75 JSValueRef QScriptEnginePrivate::evaluate(JSStringRef program, JSStringRef fileName, int lineNumber)
76 {
77 JSValueRef exception;
78 JSValueRef result = JSEvaluateScript(m_context, program, /* Global Object */ 0, fileName, lineNumber, &exception);
79 if (!result)
80 return exception; // returns an exception
81 return result;
82 }
83
84 void QScriptEnginePrivate::collectGarbage()
85 {
86 JSGarbageCollect(m_context);
87 }
88
89 void QScriptEnginePrivate::reportAdditionalMemoryCost(int cost)
90 {
91 if (cost > 0)
92 JSReportExtraMemoryCost(m_context, cost);
93 }
94
95 JSValueRef QScriptEnginePrivate::makeJSValue(double number) const
96 {
97 return JSValueMakeNumber(m_context, number);
98 }
99
100 JSValueRef QScriptEnginePrivate::makeJSValue(int number) const
101 {
102 return JSValueMakeNumber(m_context, number);
103 }
104
105 JSValueRef QScriptEnginePrivate::makeJSValue(uint number) const
106 {
107 return JSValueMakeNumber(m_context, number);
108 }
109
110 JSValueRef QScriptEnginePrivate::makeJSValue(const QString& string) const
111 {
112 JSStringRef tmp = QScriptConverter::toString(string);
113 JSValueRef result = JSValueMakeString(m_context, tmp);
114 JSStringRelease(tmp);
115 return result;
116 }
117
118 JSValueRef QScriptEnginePrivate::makeJSValue(bool value) const
119 {
120 return JSValueMakeBoolean(m_context, value);
121 }
122
123 JSValueRef QScriptEnginePrivate::makeJSValue(QScriptValue::SpecialValue value) const
124 {
125 if (value == QScriptValue::NullValue)
126 return JSValueMakeNull(m_context);
127 return JSValueMakeUndefined(m_context);
128 }
129
130 QScriptStringPrivate* QScriptEnginePrivate::toStringHandle(const QString& str) const
131 {
132 return new QScriptStringPrivate(str);
133 }
134
135 JSGlobalContextRef QScriptEnginePrivate::context() const
136 {
137 return m_context;
138 }
139
140 #endif