lib

rubyinterpreter.cpp
1/***************************************************************************
2 * rubyinterpreter.cpp
3 * This file is part of the KDE project
4 * copyright (C)2005 by Cyrille Berger (cberger@cberger.net)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#define HAVE_STRLCAT_PROTO 1
21#define HAVE_STRLCPY_PROTO 1
22#include "config.h"
23
24#include "rubyinterpreter.h"
25
26#include <map>
27
28#include <tqregexp.h>
29#include <ksharedptr.h>
30
31#include <api/exception.h>
32#include <api/module.h>
33#include <main/manager.h>
34
35#include "rubyconfig.h"
36#include "rubyextension.h"
37#include "rubymodule.h"
38#include "rubyscript.h"
39#include "rubywrapper.h"
40
41extern "C"
42{
51 void* krossinterpreter(Kross::Api::InterpreterInfo* info)
52 {
53#ifdef KROSS_RUBY_INTERPRETER_DEBUG
54 krossdebug("krossinterpreter(info)");
55#endif
56 try {
57 return new Kross::Ruby::RubyInterpreter(info);
58 }
60 Kross::krosswarning("krossinterpreter(Kross::Api::InterpreterInfo* info): Unhandled exception.");
61 }
62 return 0;
63 }
64};
65
66
67namespace Kross {
68
69namespace Ruby {
70typedef std::map<TQString, VALUE> mStrVALUE;
71typedef mStrVALUE::iterator mStrVALUE_it;
72typedef mStrVALUE::const_iterator mStrVALUE_cit;
73class RubyInterpreterPrivate {
74 friend class RubyInterpreter;
75 static VALUE s_krossModule;
76};
77
78VALUE RubyInterpreterPrivate::s_krossModule = 0;
79
80RubyInterpreterPrivate* RubyInterpreter::d = 0;
81
83{
84#ifdef KROSS_RUBY_INTERPRETER_DEBUG
85 krossdebug("RubyInterpreter::RubyInterpreter(info)");
86#endif
87 if(d == 0)
88 {
89 initRuby();
90 }
91#ifndef HAVE_RUBY_3
92 if(info->hasOption("safelevel") )
93 {
94 kross_rb_set_safe_level( info->getOption("safelevel")->value.toInt() );
95 } else {
96 kross_rb_set_safe_level(3); // if the safelevel option is undefined, set it to maximum level
97 }
98#endif
99}
100
101
103{
104 finalizeRuby();
105}
106
107
109{
110 return new RubyScript(this, scriptcontainer);
111}
112
113void RubyInterpreter::initRuby()
114{
115 d = new RubyInterpreterPrivate();
116 ruby_init();
117 ruby_init_loadpath();
118 rb_define_global_function("require", (VALUE (*)(...))RubyInterpreter::require, 1);
119}
120
122{
123 if(RubyInterpreterPrivate::s_krossModule == 0)
124 {
125 RubyInterpreterPrivate::s_krossModule = rb_define_module("Kross");
126 }
127 return RubyInterpreterPrivate::s_krossModule;
128}
129
130void RubyInterpreter::finalizeRuby()
131{
132 delete d;
133 d = 0;
134 ruby_finalize();
135}
136
137VALUE RubyInterpreter::require (VALUE obj, VALUE name)
138{
139#ifdef KROSS_RUBY_INTERPRETER_DEBUG
140 krossdebug("RubyInterpreter::require(obj,name)");
141#endif
142 TQString modname = StringValuePtr(name);
143 if(modname.startsWith("kross")) {
144 krossdebug( TQString("RubyInterpreter::require() module=%1").arg(modname) );
145 if( modname.find( TQRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
146 krosswarning( TQString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) );
147 }
148 else {
150 if(module)
151 {
152 new RubyModule(module, modname);
153// VALUE rmodule = rb_define_module(modname.ascii());
154// rb_define_module_function();
155// VALUE rm = RubyExtension::toVALUE(module);
156// rb_define_variable( ("$" + modname).ascii(), & RubyInterpreter::d->m_modules.insert( mStrVALUE::value_type( modname, rm) ).first->second );
157 return Qtrue;
158 }
159 krosswarning( TQString("Loading of Kross module '%1' failed.").arg(modname) );
160 }
161 } else {
162 return rb_f_require(obj, name);
163 }
164 return Qfalse;
165}
166
167}
168
169}
TDESharedPtr< Exception > Ptr
Shared pointer to implement reference-counting.
Definition exception.h:45
TQVariant value
The value the option has.
Definition interpreter.h:76
While the Interpreter is the implemented interpreter this class is used to provide some abstract info...
Definition interpreter.h:42
Option * getOption(const TQString name)
bool hasOption(const TQString &key)
Interpreter(InterpreterInfo *info)
Constructor.
Module::Ptr loadModule(const TQString &modulename)
Load an external module and return it.
Definition manager.cpp:200
static Manager * scriptManager()
Return the Manager instance.
Definition manager.cpp:67
TDESharedPtr< Module > Ptr
Shared pointer to implement reference-counting.
Definition module.h:43
The ScriptContainer class is something like a single standalone scriptfile.
Base class for interpreter dependend functionality each script provides.
Definition script.h:46
This class is the bridget between Kross and Ruby.
virtual Kross::Api::Script * createScript(Kross::Api::ScriptContainer *scriptcontainer)
Factory method to create and return a new RubyScript instance.
virtual ~RubyInterpreter()
Destructor.
RubyInterpreter(Kross::Api::InterpreterInfo *info)
Constructor.
Handle ruby scripts.
Definition rubyscript.h:37
The common Kross API used as common codebase.
Definition callable.h:33
The Ruby plugin for the Kross scripting framework.
Definition rubyconfig.h:33
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:33
KOFFICE_EXPORT void krossdebug(const TQString &s)
Debugging function.
KOFFICE_EXPORT void krosswarning(const TQString &s)
Warning function.