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#ifndef qscriptprogram_p_h
21#define qscriptprogram_p_h
22
23#include "qscriptconverter_p.h"
24#include "qscriptprogram.h"
25#include <JavaScriptCore/JavaScript.h>
26#include <QtCore/qshareddata.h>
27#include <QtCore/qstring.h>
28
29/*
30   FIXME The QScriptProgramPrivate potentially could be much faster. In current implementation we
31   gain CPU time only by avoiding QString -> JSStringRef conversion. In the ideal world we should
32   have a function inside the JSC C API that could provide us "parse once, execute multiple times"
33   functionality.
34*/
35
36class QScriptProgramPrivate : public QSharedData {
37public:
38    inline static QScriptProgramPrivate* get(const QScriptProgram& program);
39    inline QScriptProgramPrivate();
40    inline QScriptProgramPrivate(const QString& sourceCode,
41                   const QString fileName,
42                   int firstLineNumber);
43
44    inline ~QScriptProgramPrivate();
45
46    inline bool isNull() const;
47
48    inline QString sourceCode() const;
49    inline QString fileName() const;
50    inline int firstLineNumber() const;
51
52    inline bool operator==(const QScriptProgramPrivate& other) const;
53    inline bool operator!=(const QScriptProgramPrivate& other) const;
54
55    inline operator JSStringRef() const;
56    inline JSStringRef file() const;
57    inline int line() const;
58private:
59    JSStringRef m_program;
60    JSStringRef m_fileName;
61    int m_line;
62};
63
64QScriptProgramPrivate* QScriptProgramPrivate::get(const QScriptProgram& program)
65{
66    return const_cast<QScriptProgramPrivate*>(program.d_ptr.constData());
67}
68
69QScriptProgramPrivate::QScriptProgramPrivate()
70    : m_program(0)
71    , m_fileName(0)
72    , m_line(-1)
73{}
74
75QScriptProgramPrivate::QScriptProgramPrivate(const QString& sourceCode,
76               const QString fileName,
77               int firstLineNumber)
78                   : m_program(QScriptConverter::toString(sourceCode))
79                   , m_fileName(QScriptConverter::toString(fileName))
80                   , m_line(firstLineNumber)
81{}
82
83QScriptProgramPrivate::~QScriptProgramPrivate()
84{
85    if (!isNull()) {
86        JSStringRelease(m_program);
87        JSStringRelease(m_fileName);
88    }
89}
90
91bool QScriptProgramPrivate::isNull() const
92{
93    return !m_program;
94}
95
96QString QScriptProgramPrivate::sourceCode() const
97{
98    return QScriptConverter::toString(m_program);
99}
100
101QString QScriptProgramPrivate::fileName() const
102{
103    return QScriptConverter::toString(m_fileName);
104}
105
106int QScriptProgramPrivate::firstLineNumber() const
107{
108    return m_line;
109}
110
111bool QScriptProgramPrivate::operator==(const QScriptProgramPrivate& other) const
112{
113    return m_line == other.m_line
114            && JSStringIsEqual(m_fileName, other.m_fileName)
115            && JSStringIsEqual(m_program, other.m_program);
116}
117
118bool QScriptProgramPrivate::operator!=(const QScriptProgramPrivate& other) const
119{
120    return m_line != other.m_line
121            || !JSStringIsEqual(m_fileName, other.m_fileName)
122            || !JSStringIsEqual(m_program, other.m_program);
123}
124
125QScriptProgramPrivate::operator JSStringRef() const
126{
127    return m_program;
128}
129
130JSStringRef QScriptProgramPrivate::file() const {return m_fileName; }
131int QScriptProgramPrivate::line() const { return m_line; }
132
133#endif // qscriptprogram_p_h
134