diff options
Diffstat (limited to 'lib/kross/ruby/rubyscript.cpp')
-rw-r--r-- | lib/kross/ruby/rubyscript.cpp | 141 |
1 files changed, 108 insertions, 33 deletions
diff --git a/lib/kross/ruby/rubyscript.cpp b/lib/kross/ruby/rubyscript.cpp index 370d4aa7..106164c4 100644 --- a/lib/kross/ruby/rubyscript.cpp +++ b/lib/kross/ruby/rubyscript.cpp @@ -18,11 +18,22 @@ ***************************************************************************/ #include "rubyscript.h" +#include "rubyvariant.h" + +#define HAVE_STRLCAT_PROTO 1 +#define HAVE_STRLCPY_PROTO 1 +#include "config.h" #include <ruby.h> + +#ifndef HAVE_RUBY_1_9 #include <env.h> #include <rubysig.h> #include <node.h> +#else // HAVE_RUBY_1_9 +#include <ruby/backward/rubysig.h> +#define STR2CSTR(x) StringValuePtr(x) +#endif // HAVE_RUBY_1_9 #include <main/scriptcontainer.h> @@ -30,23 +41,44 @@ #include "rubyextension.h" #include "rubyinterpreter.h" -extern NODE *ruby_eval_tree; - namespace Kross { namespace Ruby { +namespace Internals { + namespace Script { + static VALUE method_added(VALUE self, VALUE unit) + { + rb_funcall(self, rb_intern("module_function"), unit); + return self; + } + }; +}; + class RubyScriptPrivate { friend class RubyScript; - RubyScriptPrivate() : m_compile(0) { } - RNode* m_compile; + + RubyScriptPrivate() : m_script(0), m_hasBeenCompiled(false) + { + if(RubyScriptPrivate::s_krossScript == 0) + { + RubyScriptPrivate::s_krossScript = rb_define_class_under(RubyInterpreter::krossModule(), "Script", rb_cModule); + rb_define_method(RubyScriptPrivate::s_krossScript, "method_added", (VALUE (*)(...))Internals::Script::method_added, 1); + } + } + VALUE m_script; + static VALUE s_krossScript; + bool m_hasBeenCompiled; + /// A list of functionnames. TQStringList m_functions; /// A list of classnames. TQStringList m_classes; }; - + +VALUE RubyScriptPrivate::s_krossScript = 0; + RubyScript::RubyScript(Kross::Api::Interpreter* interpreter, Kross::Api::ScriptContainer* scriptcontainer) : Kross::Api::Script(interpreter, scriptcontainer), d(new RubyScriptPrivate()) { @@ -57,13 +89,6 @@ RubyScript::~RubyScript() { } -#define selectScript() \ - NODE* old_tree = ruby_eval_tree; \ - ruby_eval_tree = d->m_compile; -#define unselectScript() \ - d->m_compile = 0; \ - ruby_eval_tree = old_tree; - void RubyScript::compile() { #ifdef KROSS_RUBY_SCRIPT_DEBUG @@ -71,24 +96,41 @@ void RubyScript::compile() #endif int critical; +#ifdef HAVE_RUBY_1_9 + // FIXME + int ruby_nerrs = 0; + int ruby_errinfo = Qnil; +#else // HAVE_RUBY_1_9 ruby_nerrs = 0; ruby_errinfo = Qnil; +#endif // HAVE_RUBY_1_9 VALUE src = RubyExtension::toVALUE( m_scriptcontainer->getCode() ); StringValue(src); + +#ifdef HAVE_RUBY_1_9 + // FIXME +#else // HAVE_RUBY_1_9 critical = rb_thread_critical; rb_thread_critical = Qtrue; ruby_in_eval++; - d->m_compile = rb_compile_string((char*) m_scriptcontainer->getName().latin1(), src, 0); +#endif // HAVE_RUBY_1_9 + rb_funcall(d->m_script, rb_intern("module_eval"), 2, src, (char*) m_scriptcontainer->getName().latin1()); +#ifdef HAVE_RUBY_1_9 + // FIXME +#else // HAVE_RUBY_1_9 ruby_in_eval--; rb_thread_critical = critical; +#endif // HAVE_RUBY_1_9 if (ruby_nerrs != 0) { #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Compilation has failed"); #endif - setException( new Kross::Api::Exception(TQString("Failed to compile ruby code: %1").arg(STR2CSTR( rb_obj_as_string(ruby_errinfo) )), 0) ); // TODO: get the error - d->m_compile = 0; + VALUE errorstring = rb_obj_as_string(ruby_errinfo); + setException( new Kross::Api::Exception(TQString("Failed to compile ruby code: %1").arg(STR2CSTR( errorstring )), 0) ); // TODO: get the error + } else { + d->m_hasBeenCompiled = true; } #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Compilation was successfull"); @@ -100,7 +142,7 @@ const TQStringList& RubyScript::getFunctionNames() #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::getFunctionNames()"); #endif - if(d->m_compile == 0) + if(not d->m_hasBeenCompiled ) { compile(); } @@ -112,15 +154,9 @@ Kross::Api::Object::Ptr RubyScript::execute() #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::execute()"); #endif - if(d->m_compile == 0) - { - compile(); - } -#ifdef KROSS_RUBY_SCRIPT_DEBUG - krossdebug("Start execution"); -#endif - selectScript(); - int result = ruby_exec(); + // TODO: catch ruby exception + compile(); +#if 0 if (result != 0) { #ifdef KROSS_RUBY_SCRIPT_DEBUG @@ -136,8 +172,8 @@ Kross::Api::Object::Ptr RubyScript::execute() setException( new Kross::Api::Exception(TQString("Failed to execute ruby code: %1").arg(STR2CSTR( rb_obj_as_string(ruby_errinfo) )), 0) ); // TODO: get the error } } +#endif // 0 - unselectScript(); #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Execution is finished"); #endif @@ -151,12 +187,47 @@ Kross::Api::Object::Ptr RubyScript::callFunction(const TQString& name, Kross::Ap #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::callFunction()"); #endif - if(d->m_compile == 0) + if(not d->m_hasBeenCompiled ) { compile(); } - selectScript(); - unselectScript(); + + Q_ASSERT(d->m_hasBeenCompiled ); + +#ifdef HAVE_RUBY_1_9 + // FIXME + int ruby_errinfo = Qnil; +#endif // HAVE_RUBY_1_9 + +#if 0 + // FIXME + // The original code never really did anything as far as I can tell! + TQVariant result; + int r = ruby_exec(); + if (r != 0) { + #ifdef KROSS_RUBY_SCRIPT_DEBUG + krossdebug("RubyScript::callFunction failed"); + #endif + VALUE errorstring = rb_obj_as_string(ruby_errinfo); + setException( new Kross::Api::Exception(TQString("Failed to call function \"%1\": %2").arg(name).arg(STR2CSTR( errorstring )), 0) ); // TODO: get the error + } + else { + VALUE self = rb_eval_string("self"); + //krossdebug(QString("RubyScript::callFunction() ===> %1").arg(STR2CSTR(rb_inspect(self)))); + + const int rnargs = args.size(); + VALUE *rargs = new VALUE[rnargs]; + for(int i = 0; i < rnargs; ++i) { + rargs[i] = RubyType<TQVariant>::toVALUE( args[i] ); + } + + //VALUE r = rb_eval_string("myFunc()"); + VALUE v = rb_funcall2(self, rb_intern(name.toLatin1()), rnargs, rargs); + result = RubyType<TQVariant>::toVariant(v); + delete[] rargs; + } +#endif + return 0; } @@ -165,7 +236,7 @@ const TQStringList& RubyScript::getClassNames() #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::getClassNames()"); #endif - if(d->m_compile == 0) + if(not d->m_hasBeenCompiled ) { compile(); } @@ -178,12 +249,16 @@ Kross::Api::Object::Ptr RubyScript::classInstance(const TQString& name) #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::classInstance()"); #endif - if(d->m_compile == 0) + if(not d->m_hasBeenCompiled ) { compile(); } - selectScript(); - unselectScript(); + + Q_ASSERT(d->m_hasBeenCompiled ); + + // FIXME + // The original code never really did anything from what I can tell! + return 0; } |