initial import
[vuplus_webkit] / Source / JavaScriptCore / qt / api / qscriptsyntaxcheckresult.cpp
1 /*
2     Copyright (C) 2010 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 #include "config.h"
21
22 #include "qscriptsyntaxcheckresult.h"
23 #include "qscriptsyntaxcheckresult_p.h"
24
25 /*!
26   \class QScriptSyntaxCheckResult
27
28   \brief The QScriptSyntaxCheckResult class provides the result of a script syntax check.
29
30   \ingroup script
31   \mainclass
32
33   QScriptSyntaxCheckResult is returned by QScriptEngine::checkSyntax() to
34   provide information about the syntactical (in)correctness of a script.
35 */
36
37 /*!
38     \enum QScriptSyntaxCheckResult::State
39
40     This enum specifies the state of a syntax check.
41
42     \value Error The program contains a syntax error.
43     \value Intermediate The program is incomplete.
44     \value Valid The program is a syntactically correct Qt Script program.
45 */
46
47 /*!
48   Constructs a new QScriptSyntaxCheckResult from the \a other result.
49 */
50 QScriptSyntaxCheckResult::QScriptSyntaxCheckResult(const QScriptSyntaxCheckResult& other)
51     : d_ptr(other.d_ptr)
52 {}
53
54 /*!
55   Constructs a new QScriptSyntaxCheckResult from an internal representation.
56   \internal
57 */
58 QScriptSyntaxCheckResult::QScriptSyntaxCheckResult(QScriptSyntaxCheckResultPrivate* d)
59     : d_ptr(d)
60 {}
61
62 /*!
63   Destroys this QScriptSyntaxCheckResult.
64 */
65 QScriptSyntaxCheckResult::~QScriptSyntaxCheckResult()
66 {}
67
68 /*!
69   Assigns the \a other result to this QScriptSyntaxCheckResult, and returns a
70   reference to this QScriptSyntaxCheckResult.
71 */
72 QScriptSyntaxCheckResult& QScriptSyntaxCheckResult::operator=(const QScriptSyntaxCheckResult& other)
73 {
74     d_ptr = other.d_ptr;
75     return *this;
76 }
77
78 /*!
79   Returns the state of this QScriptSyntaxCheckResult.
80 */
81 QScriptSyntaxCheckResult::State QScriptSyntaxCheckResult::state() const
82 {
83     return d_ptr->state();
84 }
85
86 /*!
87   Returns the error line number of this QScriptSyntaxCheckResult, or -1 if
88   there is no error.
89
90   \sa state(), errorMessage()
91 */
92 int QScriptSyntaxCheckResult::errorLineNumber() const
93 {
94     return d_ptr->errorLineNumber();
95 }
96
97 /*!
98   Returns the error column number of this QScriptSyntaxCheckResult, or -1 if
99   there is no error.
100
101   \sa state(), errorLineNumber()
102 */
103 int QScriptSyntaxCheckResult::errorColumnNumber() const
104 {
105     return d_ptr->errorColumnNumber();
106 }
107
108 /*!
109   Returns the error message of this QScriptSyntaxCheckResult, or an empty
110   string if there is no error.
111
112   \sa state(), errorLineNumber()
113 */
114 QString QScriptSyntaxCheckResult::errorMessage() const
115 {
116     return d_ptr->errorMessage();
117 }
118
119 QScriptSyntaxCheckResultPrivate::~QScriptSyntaxCheckResultPrivate()
120 {
121     if (m_exception)
122         JSValueUnprotect(*m_engine, m_exception);
123 }
124
125 QString QScriptSyntaxCheckResultPrivate::errorMessage() const
126 {
127     if (!m_exception)
128         return QString();
129
130     JSStringRef tmp = JSValueToStringCopy(*m_engine, m_exception, /* exception */ 0);
131     QString message = QScriptConverter::toString(tmp);
132     JSStringRelease(tmp);
133     return message;
134 }
135
136 int QScriptSyntaxCheckResultPrivate::errorLineNumber() const
137 {
138     if (!m_exception)
139         return -1;
140     // m_exception is an instance of the Exception so it has "line" attribute.
141     JSStringRef lineAttrName = QScriptConverter::toString("line");
142     JSValueRef line = JSObjectGetProperty(*m_engine,
143                                           m_exception,
144                                           lineAttrName,
145                                           /* exceptions */0);
146     JSStringRelease(lineAttrName);
147     return JSValueToNumber(*m_engine, line, /* exceptions */0);
148 }