1 /***************************************************************************
2 * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
3 * Copyright (C) 2008 by Harri Porten <porten@kde.org> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "executor_kjs_p.h"
13 #include <kjs/kjsinterpreter.h>
14 #include <kjs/kjsobject.h>
15 #include <kjs/kjsprototype.h>
16 #include <kjs/kjsarguments.h>
20 #include "../debug_p.h"
21 #include "../document_p.h"
23 #include "kjs_app_p.h"
24 #include "kjs_console_p.h"
25 #include "kjs_data_p.h"
26 #include "kjs_document_p.h"
27 #include "kjs_field_p.h"
28 #include "kjs_fullscreen_p.h"
29 #include "kjs_spell_p.h"
30 #include "kjs_util_p.h"
32 using namespace Okular
;
34 class Okular::ExecutorKJSPrivate
37 ExecutorKJSPrivate( DocumentPrivate
*doc
)
44 JSField::clearCachedFields();
51 DocumentPrivate
*m_doc
;
52 KJSInterpreter
*m_interpreter
;
53 KJSGlobalObject m_docObject
;
56 void ExecutorKJSPrivate::initTypes()
58 m_docObject
= JSDocument::wrapDocument( m_doc
);
59 m_interpreter
= new KJSInterpreter( m_docObject
);
61 KJSContext
*ctx
= m_interpreter
->globalContext();
63 JSApp::initType( ctx
);
64 JSFullscreen::initType( ctx
);
65 JSConsole::initType( ctx
);
66 JSData::initType( ctx
);
67 JSDocument::initType( ctx
);
68 JSField::initType( ctx
);
69 JSSpell::initType( ctx
);
70 JSUtil::initType( ctx
);
72 m_docObject
.setProperty( ctx
, "app", JSApp::object( ctx
, m_doc
) );
73 m_docObject
.setProperty( ctx
, "console", JSConsole::object( ctx
) );
74 m_docObject
.setProperty( ctx
, "Doc", m_docObject
);
75 m_docObject
.setProperty( ctx
, "spell", JSSpell::object( ctx
) );
76 m_docObject
.setProperty( ctx
, "util", JSUtil::object( ctx
) );
79 ExecutorKJS::ExecutorKJS( DocumentPrivate
*doc
)
80 : d( new ExecutorKJSPrivate( doc
) )
84 ExecutorKJS::~ExecutorKJS()
89 void ExecutorKJS::execute( const QString
&script
)
95 if ( !KJSInterpreter::normalizeCode( script
, &script2
, &errLine
, &errMsg
) )
97 kWarning(OkularDebug
) << "Parse error during normalization!";
102 KJSResult result
= d
->m_interpreter
->evaluate( "okular.js", 1,
103 script
, &d
->m_docObject
);
104 KJSContext
* ctx
= d
->m_interpreter
->globalContext();
105 if ( result
.isException() || ctx
->hasException() )
107 kDebug(OkularDebug
) << "JS exception" << result
.errorMessage();
111 kDebug(OkularDebug
) << "result:" << result
.value().toString( ctx
);