lib

rubymodule.cpp

00001 /***************************************************************************
00002  * rubyinterpreter.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2005 by Cyrille Berger (cberger@cberger.net)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "rubymodule.h"
00021 
00022 #include <kdebug.h>
00023 
00024 #include <api/object.h>
00025 
00026 #include "rubyconfig.h"
00027 #include "rubyextension.h"
00028 
00029 namespace Kross {
00030 
00031 namespace Ruby {
00032 
00033 class RubyModulePrivate {
00034     friend class RubyModule;
00036     Kross::Api::Module* m_module;
00037 
00038 };
00039 
00040 RubyModule::RubyModule(Kross::Api::Module* mod, QString modname) : d(new RubyModulePrivate)
00041 {
00042     d->m_module = mod;
00043     modname = modname.left(1).upper() + modname.right(modname.length() - 1 );
00044     kdDebug() << modname << endl;
00045     VALUE rmodule = rb_define_module(modname.ascii());
00046     rb_define_module_function(rmodule,"method_missing",  (VALUE (*)(...))RubyModule::method_missing, -1);
00047     VALUE rm = RubyExtension::toVALUE((Kross::Api::Object*)mod);
00048     rb_define_const(rmodule, "MODULEOBJ", rm);
00049 }
00050 
00051 RubyModule::~RubyModule()
00052 {
00053 }
00054 
00055 VALUE RubyModule::method_missing(int argc, VALUE *argv, VALUE self)
00056 {
00057 #ifdef KROSS_RUBY_MODULE_DEBUG
00058     QString funcname = rb_id2name(SYM2ID(argv[0]));
00059     kdDebug() << "Function " << funcname << " \"missing\" in a module" << endl;
00060 #endif
00061 
00062     VALUE rubyObjectModule = rb_funcall( self, rb_intern("const_get"), 1, ID2SYM(rb_intern("MODULEOBJ")) );
00063     RubyModule* objectModule;
00064     Data_Get_Struct(rubyObjectModule, RubyModule, objectModule);
00065     Kross::Api::Object::Ptr object = (Kross::Api::Object*)objectModule->d->m_module;
00066     
00067     return RubyExtension::call_method(object, argc, argv);
00068 }
00069 
00070 }
00071 
00072 }
KDE Home | KDE Accessibility Home | Description of Access Keys