File: executor_kjs.cpp

package info (click to toggle)
okular 4%3A4.8.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,040 kB
  • sloc: cpp: 62,030; ansic: 3,964; xml: 66; sh: 22; makefile: 7
file content (113 lines) | stat: -rw-r--r-- 3,272 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***************************************************************************
 *   Copyright (C) 2008 by Pino Toscano <pino@kde.org>                     *
 *   Copyright (C) 2008 by Harri Porten <porten@kde.org>                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "executor_kjs_p.h"

#include <kjs/kjsinterpreter.h>
#include <kjs/kjsobject.h>
#include <kjs/kjsprototype.h>
#include <kjs/kjsarguments.h>

#include <kdebug.h>

#include "../debug_p.h"
#include "../document_p.h"

#include "kjs_app_p.h"
#include "kjs_console_p.h"
#include "kjs_data_p.h"
#include "kjs_document_p.h"
#include "kjs_field_p.h"
#include "kjs_fullscreen_p.h"
#include "kjs_spell_p.h"
#include "kjs_util_p.h"

using namespace Okular;

class Okular::ExecutorKJSPrivate
{
    public:
        ExecutorKJSPrivate( DocumentPrivate *doc )
            : m_doc( doc )
        {
            initTypes();
        }
        ~ExecutorKJSPrivate()
        {
            JSField::clearCachedFields();

            delete m_interpreter;
        }

        void initTypes();

        DocumentPrivate *m_doc;
        KJSInterpreter *m_interpreter;
        KJSGlobalObject m_docObject;
};

void ExecutorKJSPrivate::initTypes()
{
    m_docObject = JSDocument::wrapDocument( m_doc );
    m_interpreter = new KJSInterpreter( m_docObject );

    KJSContext *ctx = m_interpreter->globalContext();

    JSApp::initType( ctx );
    JSFullscreen::initType( ctx );
    JSConsole::initType( ctx );
    JSData::initType( ctx );
    JSDocument::initType( ctx );
    JSField::initType( ctx );
    JSSpell::initType( ctx );
    JSUtil::initType( ctx );

    m_docObject.setProperty( ctx, "app", JSApp::object( ctx, m_doc ) );
    m_docObject.setProperty( ctx, "console", JSConsole::object( ctx ) );
    m_docObject.setProperty( ctx, "Doc", m_docObject );
    m_docObject.setProperty( ctx, "spell", JSSpell::object( ctx ) );
    m_docObject.setProperty( ctx, "util", JSUtil::object( ctx ) );
}

ExecutorKJS::ExecutorKJS( DocumentPrivate *doc )
    : d( new ExecutorKJSPrivate( doc ) )
{
}

ExecutorKJS::~ExecutorKJS()
{
    delete d;
}

void ExecutorKJS::execute( const QString &script )
{
#if 0
    QString script2;
    QString errMsg;
    int errLine;
    if ( !KJSInterpreter::normalizeCode( script, &script2, &errLine, &errMsg ) )
    {
        kWarning(OkularDebug) << "Parse error during normalization!";
        script2 = script;
    }
#endif

    KJSResult result = d->m_interpreter->evaluate( "okular.js", 1,
                                                   script, &d->m_docObject );
    KJSContext* ctx = d->m_interpreter->globalContext();
    if ( result.isException() || ctx->hasException() )
    {
        kDebug(OkularDebug) << "JS exception" << result.errorMessage();
    }
    else
    {
        kDebug(OkularDebug) << "result:" << result.value().toString( ctx );
    }
}