summaryrefslogtreecommitdiffstats
path: root/lib/kross/api/interpreter.cpp
blob: 8d26693066324b08fda02f5cc2f6716702860858 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***************************************************************************
 * interpreter.cpp
 * This file is part of the KDE project
 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "interpreter.h"
#include "script.h"
#include "../main/manager.h"
#include "../main/scriptcontainer.h"

#include <klibloader.h>

extern "C"
{
    typedef int (*def_interpreter_func)(Kross::Api::InterpreterInfo*);
}

using namespace Kross::Api;

/*************************************************************************
 * InterpreterInfo
 */

InterpreterInfo::InterpreterInfo(const TQString& interpretername, const TQString& library, const TQString& wildcard, TQStringList mimetypes, Option::Map options)
    : m_interpretername(interpretername)
    , m_library(library)
    , m_wildcard(wildcard)
    , m_mimetypes(mimetypes)
    , m_options(options)
    , m_interpreter(0)
{
}

InterpreterInfo::~InterpreterInfo()
{
    for(Option::Map::Iterator it = m_options.begin(); it != m_options.end(); ++it)
        delete it.data();

    delete m_interpreter;
    m_interpreter = 0;
}

const TQString InterpreterInfo::getInterpretername()
{
    return m_interpretername;
}

const TQString InterpreterInfo::getWildcard()
{
    return m_wildcard;
}

const TQStringList InterpreterInfo::getMimeTypes()
{
    return m_mimetypes;
}

bool InterpreterInfo::hasOption(const TQString& key)
{
    return m_options.contains(key);
}

InterpreterInfo::Option* InterpreterInfo::getOption(const TQString name)
{
    return m_options[name];
}

const TQVariant InterpreterInfo::getOptionValue(const TQString name, TQVariant defaultvalue)
{
    Option* o = m_options[name];
    return o ? o->value : defaultvalue;
}

InterpreterInfo::Option::Map InterpreterInfo::getOptions()
{
    return m_options;
}

Interpreter* InterpreterInfo::getInterpreter()
{
    if(m_interpreter) // buffered
        return m_interpreter;

    krossdebug( TQString("Loading the interpreter library for %1").tqarg(m_interpretername) );

    // Load the krosspython library.
    KLibLoader *libloader = KLibLoader::self();

    KLibrary* library = libloader->globalLibrary( m_library.latin1() );
    if(! library) {
        /*
        setException(
            new Exception( TQString("Could not load library \"%1\" for the \"%2\" interpreter.").tqarg(m_library).tqarg(m_interpretername) )
        );
        */
        krosswarning( TQString("Could not load library \"%1\" for the \"%2\" interpreter.").tqarg(m_library).tqarg(m_interpretername) );
        return 0;
    }

    // Get the extern "C" krosspython_instance function.
    def_interpreter_func interpreter_func;
    interpreter_func = (def_interpreter_func) library->symbol("krossinterpreter");
    if(! interpreter_func) {
        //setException( new Exception("Failed to load symbol in krosspython library.") );
        krosswarning("Failed to load the 'krossinterpreter' symbol from the library.");
    }
    else {
        // and execute the extern krosspython_instance function.
        m_interpreter = (Interpreter*) (interpreter_func)(this);
        if(! m_interpreter) {
            krosswarning("Failed to load the Interpreter instance from library.");
        }
        else {
            // Job done. The library is loaded and our Interpreter* points
            // to the external Kross::Python::Interpreter* instance.
            krossdebug("Successfully loaded Interpreter instance from library.");
        }
    }

    // finally unload the library.
    library->unload();

    return m_interpreter;
}

/*************************************************************************
 * Interpreter
 */

Interpreter::Interpreter(InterpreterInfo* info)
    : m_interpreterinfo(info)
{
}

Interpreter::~Interpreter()
{
}