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#include "rubyinterpreter.h"
21
22#include <map>
23
24#include <tqregexp.h>
25#include <ksharedptr.h>
26
27#include <api/exception.h>
28#include <api/module.h>
29#include <main/manager.h>
30
31#include "rubyconfig.h"
32#include "rubyextension.h"
33#include "rubymodule.h"
34#include "rubyscript.h"
35#include "rubywrapper.h"
36
37extern "C"
38{
47 void* krossinterpreter(Kross::Api::InterpreterInfo* info)
48 {
49#ifdef KROSS_RUBY_INTERPRETER_DEBUG
50 krossdebug("krossinterpreter(info)");
51#endif
52 try {
53 return new Kross::Ruby::RubyInterpreter(info);
54 }
56 Kross::krosswarning("krossinterpreter(Kross::Api::InterpreterInfo* info): Unhandled exception.");
57 }
58 return 0;
59 }
60};
61
62
63namespace Kross {
64
65namespace Ruby {
66typedef std::map<TQString, VALUE> mStrVALUE;
67typedef mStrVALUE::iterator mStrVALUE_it;
68typedef mStrVALUE::const_iterator mStrVALUE_cit;
69class RubyInterpreterPrivate {
70 friend class RubyInterpreter;
71 static VALUE s_krossModule;
72};
73
74VALUE RubyInterpreterPrivate::s_krossModule = 0;
75
76RubyInterpreterPrivate* RubyInterpreter::d = 0;
77
79{
80#ifdef KROSS_RUBY_INTERPRETER_DEBUG
81 krossdebug("RubyInterpreter::RubyInterpreter(info)");
82#endif
83 if(d == 0)
84 {
85 initRuby();
86 }
87#ifndef HAVE_RUBY_3
88 if(info->hasOption("safelevel") )
89 {
90 kross_rb_set_safe_level( info->getOption("safelevel")->value.toInt() );
91 } else {
92 kross_rb_set_safe_level(3); // if the safelevel option is undefined, set it to maximum level
93 }
94#endif
95}
96
97
99{
100 finalizeRuby();
101}
102
103
105{
106 return new RubyScript(this, scriptcontainer);
107}
108
109void RubyInterpreter::initRuby()
110{
111 d = new RubyInterpreterPrivate();
112 ruby_init();
113 ruby_init_loadpath();
114 rb_define_global_function("require", (VALUE (*)(...))RubyInterpreter::require, 1);
115}
116
118{
119 if(RubyInterpreterPrivate::s_krossModule == 0)
120 {
121 RubyInterpreterPrivate::s_krossModule = rb_define_module("Kross");
122 }
123 return RubyInterpreterPrivate::s_krossModule;
124}
125
126void RubyInterpreter::finalizeRuby()
127{
128 delete d;
129 d = 0;
130 ruby_finalize();
131}
132
133VALUE RubyInterpreter::require (VALUE obj, VALUE name)
134{
135#ifdef KROSS_RUBY_INTERPRETER_DEBUG
136 krossdebug("RubyInterpreter::require(obj,name)");
137#endif
138 TQString modname = StringValuePtr(name);
139 if(modname.startsWith("kross")) {
140 krossdebug( TQString("RubyInterpreter::require() module=%1").arg(modname) );
141 if( modname.find( TQRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
142 krosswarning( TQString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) );
143 }
144 else {
146 if(module)
147 {
148 new RubyModule(module, modname);
149// VALUE rmodule = rb_define_module(modname.ascii());
150// rb_define_module_function();
151// VALUE rm = RubyExtension::toVALUE(module);
152// rb_define_variable( ("$" + modname).ascii(), & RubyInterpreter::d->m_modules.insert( mStrVALUE::value_type( modname, rm) ).first->second );
153 return Qtrue;
154 }
155 krosswarning( TQString("Loading of Kross module '%1' failed.").arg(modname) );
156 }
157 } else {
158 return rb_f_require(obj, name);
159 }
160 return Qfalse;
161}
162
163}
164
165}
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:41
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:31
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:31
void krossdebug(const TQString &s)
Debugging function.
void krosswarning(const TQString &s)
Warning function.