void RubyScript::compile() { #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::compile()"); #endif int critical; ruby_nerrs = 0; ruby_errinfo = Qnil; VALUE src = RubyExtension::toVALUE( m_scriptcontainer->getCode() ); StringValue(src); critical = rb_thread_critical; rb_thread_critical = Qtrue; ruby_in_eval++; d->m_compile = rb_compile_string((char*) m_scriptcontainer->getName().latin1(), src, 0); ruby_in_eval--; rb_thread_critical = critical; if (ruby_nerrs != 0) { #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Compilation has failed"); #endif setException( new Kross::Api::Exception(QString("Failed to compile ruby code: %1").arg(STR2CSTR( rb_obj_as_string(ruby_errinfo) )), 0) ); // TODO: get the error d->m_compile = 0; } #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Compilation was successfull"); #endif }
VALUE RubyInterpreter::require (VALUE obj, VALUE name) { #ifdef KROSS_RUBY_INTERPRETER_DEBUG krossdebug("RubyInterpreter::require(obj,name)"); #endif QString modname = StringValuePtr(name); if(modname.startsWith("kross")) { krossdebug( QString("RubyInterpreter::require() module=%1").arg(modname) ); if( modname.find( QRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) { krosswarning( QString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) ); } else { Kross::Api::Module::Ptr module = Kross::Api::Manager::scriptManager()->loadModule(modname); if(module) { new RubyModule(module, modname); // VALUE rmodule = rb_define_module(modname.ascii()); // rb_define_module_function(); // VALUE rm = RubyExtension::toVALUE(module); // rb_define_variable( ("$" + modname).ascii(), & RubyInterpreter::d->m_modules.insert( mStrVALUE::value_type( modname, rm) ).first->second ); return Qtrue; } krosswarning( QString("Loading of Kross module '%1' failed.").arg(modname) ); } } else { return rb_f_require(obj, name); } return Qfalse; }
const QStringList& RubyScript::getFunctionNames() { #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::getFunctionNames()"); #endif if(d->m_compile == 0) { compile(); } return d->m_functions; }
/** * Exported and loadable function as entry point to use * the \a RubyInterpreter. * The krossruby library will be loaded dynamicly at runtime from e.g. * \a Kross::Api::Manager::getInterpreter and this exported * function will be used to return an instance of the * \a RubyInterpreter implementation. */ void* krossinterpreter(Kross::Api::InterpreterInfo* info) { #ifdef KROSS_RUBY_INTERPRETER_DEBUG krossdebug("krossinterpreter(info)"); #endif try { return new Kross::Ruby::RubyInterpreter(info); } catch(Kross::Api::Exception::Ptr e) { Kross::krosswarning("krossinterpreter(Kross::Api::InterpreterInfo* info): Unhandled exception."); } return 0; }
Kross::Api::Object::Ptr RubyScript::classInstance(const QString& name) { Q_UNUSED(name) #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::classInstance()"); #endif if(d->m_compile == 0) { compile(); } selectScript(); unselectScript(); return 0; }
bool initialize() { #ifdef KROSS_JVM_INTERPRETER_DEBUG krossdebug("JVMInterpreter initialize"); #endif jint res = JNI_CreateJavaVM( &jvm, (void **)&env, &vm_args ); if(res < 0) return false; //Create classloader jclass clclass = env->FindClass( "org/kde/kdebindings/java/krossjava/KrossClassLoader" ); if (clclass == 0) { krosswarning( "Class 'KrossClassLoader' not found! Is kross.jar accessible?" ); return false; } addclass = env->GetMethodID(clclass, "addClass", "(Ljava/lang/String;[B)Ljava/lang/String;"); newinst = env->GetMethodID(clclass, "newInstance", "(Ljava/lang/String;)Ljava/lang/Object;"); addurl = env->GetMethodID(clclass, "addURL", "(Ljava/net/URL;)V"); addextension = env->GetMethodID(clclass, "addExtension", "(Ljava/lang/String;J)Lorg/kde/kdebindings/java/krossjava/KrossQExtension;"); if (addclass == 0 || newinst == 0 || addurl == 0 || addextension == 0) { krosswarning("Classloader method not found!"); return false; } jmethodID ctor = env->GetMethodID(clclass, "<init>", "()V"); if (ctor == 0) { krosswarning("Classloader constructor not found!"); return false; } jobject loaderweak = env->NewObject(clclass, ctor); if (loaderweak == 0) { krosswarning("Could not create classloader!"); return false; } classloader = env->NewGlobalRef(loaderweak); jclass proxy = env->FindClass("org/kde/kdebindings/java/krossjava/KrossQExtension"); JNINativeMethod nativeMethod[2]; nativeMethod[0].name = "invokeNative"; nativeMethod[0].signature = "(JLjava/lang/String;ILjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;" "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;" "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"; nativeMethod[0].fnPtr = (void*) callQMethod; nativeMethod[1].name = "connect"; nativeMethod[1].signature = "(JLjava/lang/String;Ljava/lang/Object;Ljava/lang/reflect/Method;)Z"; nativeMethod[1].fnPtr = (void*) callConnect; env->RegisterNatives(proxy, nativeMethod, 2); handleException(); return true; }
Kross::Api::Object::Ptr RubyScript::callFunction(const QString& name, Kross::Api::List::Ptr args) { Q_UNUSED(name) Q_UNUSED(args) #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("RubyScript::callFunction()"); #endif if(d->m_compile == 0) { compile(); } selectScript(); unselectScript(); return 0; }
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(); if (result != 0) { #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Execution has failed"); #endif if( TYPE( ruby_errinfo ) == T_DATA && RubyExtension::isOfExceptionType( ruby_errinfo ) ) { #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Kross exception"); #endif setException( RubyExtension::convertToException( ruby_errinfo ) ); } else { setException( new Kross::Api::Exception(QString("Failed to execute ruby code: %1").arg(STR2CSTR( rb_obj_as_string(ruby_errinfo) )), 0) ); // TODO: get the error } } unselectScript(); #ifdef KROSS_RUBY_SCRIPT_DEBUG krossdebug("Execution is finished"); #endif return 0; }
RubyInterpreter::RubyInterpreter(Kross::Api::InterpreterInfo* info): Kross::Api::Interpreter(info) { #ifdef KROSS_RUBY_INTERPRETER_DEBUG krossdebug("RubyInterpreter::RubyInterpreter(info)"); #endif if(d == 0) { initRuby(); } if(info->hasOption("safelevel") ) { rb_set_safe_level( info->getOption("safelevel")->value.toInt() ); } else { rb_set_safe_level(4); // if the safelevel option is undefined, set it to maximum level } }