[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    koffice/lib/kross
From:       Cyrille Berger <cyb () lepi ! org>
Date:       2005-12-16 15:20:52
Message-ID: 1134746452.655080.13169.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 488948 by berger:

- add a check in configure for ruby header and if a program embedding a ruby \
                interpreter can be build
- activate security in ruby
- basic error reporting



 M  +39 -13    configure.in.in  
 M  +3 -3      main/manager.cpp  
 M  +1 -1      ruby/rubyconfig.h  
 M  +6 -0      ruby/rubyinterpreter.cpp  
 M  +20 -5     ruby/rubyscript.cpp  
 M  +0 -1      ruby/rubyscript.h  


--- trunk/koffice/lib/kross/configure.in.in #488947:488948
@@ -16,19 +16,45 @@
 AC_CHECK_PROG(RUBY, ruby, ruby)
 
 if test "$RUBY"; then
-  AC_MSG_CHECKING(for ruby dirs)
-  RUBY_ARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"archdir"@:>@)'`
-  RUBY_SITEARCHDIR=`$RUBY -r rbconfig -e \
                'printf("%s",Config::CONFIG@<:@"sitearchdir"@:>@)'`
-  RUBY_SITEDIR=`$RUBY -r rbconfig -e \
                'printf("%s",Config::CONFIG@<:@"sitelibdir"@:>@)'`
-  RUBY_LIBDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"libdir"@:>@)'`
-  RUBY_LIBRUBYARG=`$RUBY -r rbconfig -e \
                'printf("%s",Config::CONFIG@<:@"LIBRUBYARG_SHARED"@:>@)'`
-  AC_MSG_RESULT([archdir $RUBY_ARCHDIR, sitearchdir $RUBY_SITEARCHDIR, sitedir \
                $RUBY_SITEDIR, libdir $RUBY_LIBDIR, librubyarg $RUBY_LIBRUBYARG])
-  AC_SUBST(RUBY_ARCHDIR)
-  AC_SUBST(RUBY_SITEARCHDIR)
-  AC_SUBST(RUBY_SITEDIR)
-  AC_SUBST(RUBY_LIBDIR)
-  AC_SUBST(RUBY_LIBRUBYARG)
+    AC_MSG_CHECKING(for ruby dirs)
+    RUBY_ARCHDIR=`$RUBY -r rbconfig -e \
'printf("%s",Config::CONFIG@<:@"archdir"@:>@)'` +    RUBY_SITEARCHDIR=`$RUBY -r \
rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitearchdir"@:>@)'` +    \
RUBY_SITEDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitelibdir"@:>@)'` \
+    RUBY_LIBDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"libdir"@:>@)'` \
+    RUBY_LIBRUBYARG=`$RUBY -r rbconfig -e \
'printf("%s",Config::CONFIG@<:@"LIBRUBYARG_SHARED"@:>@)'` +    AC_MSG_RESULT([archdir \
$RUBY_ARCHDIR, sitearchdir $RUBY_SITEARCHDIR, sitedir $RUBY_SITEDIR, libdir \
$RUBY_LIBDIR, librubyarg $RUBY_LIBRUBYARG]) +    AC_SUBST(RUBY_ARCHDIR)
+    AC_SUBST(RUBY_SITEARCHDIR)
+    AC_SUBST(RUBY_SITEDIR)
+    AC_SUBST(RUBY_LIBDIR)
+    AC_SUBST(RUBY_LIBRUBYARG)
+    AC_MSG_CHECKING(for ruby header)
+    if test ! -r $RUBY_ARCHDIR/ruby.h; then
+        RUBY_LIBDIR=""
+        AC_MSG_RESULT([not found])
+    else
+        AC_MSG_CHECKING([if C++ program with ruby can be compiled])
+        AC_LANG_SAVE
+        AC_LANG_CPLUSPLUS
+        ac_save_CXXFLAGS="$CXXFLAGS"
+        CXXFLAGS="$CXXFLAGS -I$RUBY_ARCHDIR"
+        AC_CACHE_VAL(ruby_build,
+        [
+            AC_TRY_COMPILE([
+                #include <ruby.h>
+            ],[
+                ruby_init();
+                return 0;
+            ], ruby_build=yes,
+            ruby_build=no)
+        ])
+        AC_MSG_RESULT($ruby_build)
+        if test "$ruby_build" = "no"; then
+            RUBY_LIBDIR=""
+        fi
+        CXXFLAGS="$ac_save_CXXFLAGS"
+        AC_LANG_RESTORE
+    fi
 fi
 
-
 AM_CONDITIONAL(compile_kross_ruby, test -n "$RUBY_LIBDIR")
--- trunk/koffice/lib/kross/main/manager.cpp #488947:488948
@@ -107,9 +107,9 @@
     QString rubylib = QFile::encodeName( \
                KLibLoader::self()->findLibrary(KROSS_RUBY_LIBRARY) );
     if(! rubylib.isEmpty()) { // If the Kross Ruby plugin exists we offer it as \
supported scripting language.  InterpreterInfo::Option::Map rubyoptions;
-//       rubyoptions.replace("restricted",
-//                             new InterpreterInfo::Option("Restricted", "Restricted \
                Ruby interpreter", QVariant(false))
-//                            );
+      rubyoptions.replace("safelevel",
+                          new InterpreterInfo::Option("safelevel", "Level of safety \
of the Ruby interpreter", QVariant(0)) // 0 -> unsafe, 4 -> very safe +               \
);  d->interpreterinfos.replace("ruby",
                                   new InterpreterInfo("ruby",
                                       rubylib, // library
--- trunk/koffice/lib/kross/ruby/rubyconfig.h #488947:488948
@@ -23,7 +23,7 @@
 namespace Kross { namespace Ruby {
 // #define KROSS_RUBY_SCRIPT_DEBUG
 // #define KROSS_RUBY_INTERPRETER_DEBUG
-#define KROSS_RUBY_EXTENSION_DEBUG
+// #define KROSS_RUBY_EXTENSION_DEBUG
 }
 }
 
--- trunk/koffice/lib/kross/ruby/rubyinterpreter.cpp #488947:488948
@@ -82,6 +82,12 @@
     {
         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 +    }
 }
 
 
--- trunk/koffice/lib/kross/ruby/rubyscript.cpp #488947:488948
@@ -56,10 +56,11 @@
 {
 }
 
-void RubyScript::selectScript()
-{
+#define selectScript() \
+    NODE* old_tree = ruby_eval_tree; \
     ruby_eval_tree = d->m_compile;
-}
+#define unselectScript() \
+    ruby_eval_tree = old_tree;
 
 void RubyScript::compile()
 {
@@ -81,7 +82,8 @@
 #ifdef KROSS_RUBY_SCRIPT_DEBUG
         kdDebug() << "Compilation has failed" << endl;
 #endif
-        setException( new Kross::Api::Exception(QString("Failed to compile ruby \
code: %1").arg("unknow"), 0) ); // TODO: get the error +//         kdDebug() << \
TYPE(ruby_errinfo) << endl; +        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
@@ -101,6 +103,16 @@
     return d->m_functions;
 }
 
+VALUE ruby_runtime_error(VALUE data, VALUE errinfo)
+{
+    throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Error when \
running script.") ) ); +}
+
+VALUE ruby_exec2()
+{
+    return INT2FIX(ruby_exec());
+}
+
 Kross::Api::Object::Ptr RubyScript::execute()
 {
 #ifdef KROSS_RUBY_SCRIPT_DEBUG
@@ -116,8 +128,9 @@
     selectScript();
     RubyInterpreter* interpreterRuby = (RubyInterpreter*)m_interpreter;
     interpreterRuby->incCountScript();
-    ruby_exec();
+    rb_rescue2((VALUE (*)(...))ruby_exec2,0,0,0,rb_eStandardError,(VALUE)0);
     interpreterRuby->decCountScript();
+    unselectScript();
 #ifdef KROSS_RUBY_SCRIPT_DEBUG
     kdDebug() << "Execution is finished" << endl;
 #endif
@@ -134,6 +147,7 @@
         compile();
     }
     selectScript();
+    unselectScript();
     return 0;
 }
 
@@ -159,6 +173,7 @@
         compile();
     }
     selectScript();
+    unselectScript();
     return 0;
 }
 
--- trunk/koffice/lib/kross/ruby/rubyscript.h #488947:488948
@@ -63,7 +63,6 @@
         virtual Kross::Api::Object::Ptr classInstance(const QString& name);
     private:
         void compile();
-        inline void selectScript();
     private:
         RubyScriptPrivate* d;
 };


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic