public string GetCurrentBackTrace() { #if MRUBY System.Text.StringBuilder builder = new System.Text.StringBuilder(); R_VAL backtrace = RubyDLL.r_get_backtrace(rb_state); builder.AppendLine("trace:"); for (var i = 0; i < RubyDLL.r_funcall(rb_state, backtrace, "size", 0); ++i) { R_VAL v = RubyDLL.r_ary_ref(rb_state, backtrace, i); builder.AppendLine($" [{i}] {v.ToString ( rb_state )}"); } return(builder.ToString()); #else System.Text.StringBuilder builder = new System.Text.StringBuilder(); R_VAL exc = DoString("Exception.new('*interactive*')"); R_VAL backtrace = RubyDLL.r_get_backtrace(exc); builder.AppendLine("trace:"); for (var i = 0; i < RubyDLL.r_funcall(backtrace, R_VAL.Create("size"), 0); ++i) { R_VAL v = RubyDLL.r_ary_ref(backtrace, R_VAL.Create(i)); builder.AppendLine($" [{i}] {v.ToString ()}"); } return(builder.ToString()); #endif // return RubyDLL.mrb_inspect ( mrb_state, RubyDLL.mrb_get_backtrace ( mrb_state ) ).ToString ( mrb_state ); // return RubyDLL.mrb_funcall ( mrb_state, RubyDLL.mrb_exc_backtrace ( mrb_state, RubyDLL.mrb_get_exc_value ( mrb_state ) ), "to_s", 0 ).ToString ( mrb_state ); // return RubyDLL.mrb_inspect ( mrb_state, RubyDLL.mrb_get_exc_value ( mrb_state ) ).ToString ( mrb_state ); // return RubyDLL.mrb_inspect ( mrb_state, RubyDLL.mrb_exc_backtrace ( mrb_state, RubyDLL.mrb_get_exc_value ( mrb_state ) ) ).ToString ( mrb_state ); }
public static R_VAL DoFile(IntPtr mrb, string path) { if (!File.Exists(path)) { return(R_VAL.NIL); } #if MRUBY string filename = Path.GetFileName(path); int arena = RubyDLL.mrb_gc_arena_save(mrb); IntPtr mrbc_context = RubyDLL.mrbc_context_new(mrb); RubyDLL.mrbc_filename(mrb, mrbc_context, filename); var ret = RubyDLL.mrb_load_string_cxt(mrb, RubyDLL.ToCBytes(File.ReadAllText(path)), mrbc_context); RubyDLL.mrbc_context_free(mrb, mrbc_context); if (RubyDLL.mrb_has_exc(mrb)) { Console.WriteLine(GetExceptionBackTrace(mrb)); RubyDLL.mrb_exc_clear(mrb); } RubyDLL.mrb_gc_arena_restore(mrb, arena); return(ret); #else int status; RubyDLL.rb_load_protect(R_VAL.Create(path), 0, out status); return(status == 0 ? R_VAL.TRUE : R_VAL.FALSE); #endif }
public R_VAL Call(string funcName, R_VAL arg) { #if MRUBY return(RubyDLL.r_funcall_1(rb_state, RubyDLL.mrb_top_self(rb_state), funcName, 1, arg)); #else return(RubyDLL.r_funcall_1(RubyDLL.rb_vm_top_self(), R_VAL.Create(funcName), 1, arg)); #endif }
public static string GetExceptionBackTrace() { #endif System.Text.StringBuilder builder = new System.Text.StringBuilder(); #if MRUBY R_VAL exc = RubyDLL.mrb_get_exc_value(mrb); R_VAL backtrace = RubyDLL.r_exc_backtrace(mrb, exc); builder.AppendLine(RubyDLL.r_funcall(mrb, exc, "inspect", 0).ToString(mrb)); builder.AppendLine("trace:"); for (var i = 0; i < RubyDLL.r_funcall(mrb, backtrace, "size", 0); ++i) { R_VAL v = RubyDLL.r_ary_ref(mrb, backtrace, i); builder.AppendLine($" [{i}] {v.ToString ( mrb )}"); } return(builder.ToString()); #else R_VAL exc = RubyDLL.rb_errinfo(); R_VAL backtrace = RubyDLL.r_exc_backtrace(exc); builder.AppendLine(RubyDLL.rb_obj_inspect(exc).ToString()); builder.AppendLine("trace:"); for (var i = 0; i < RubyDLL.r_funcall(backtrace, R_VAL.Create("size"), 0); ++i) { R_VAL v = RubyDLL.r_ary_ref(backtrace, R_VAL.Create(i)); builder.AppendLine($" [{i}] {v.ToString ()}"); } return(builder.ToString()); #endif }
static void Main() { // https://github.com/FNA-XNA/FNA/wiki/4:-FNA-and-Windows-API#64-bit-support if (Environment.OSVersion.Platform == PlatformID.Unix) { string path = Environment.GetEnvironmentVariable("PATH"); Environment.SetEnvironmentVariable("PATH", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "lib", "osx") + ";" + path); // string libPath = Environment.GetEnvironmentVariable ( "LD_LIBRARY_PATH" ); // Environment.SetEnvironmentVariable ( "LD_LIBRARY_PATH", Path.Combine ( AppDomain.CurrentDomain.BaseDirectory, "lib", "osx" ) + ";" + libPath ); } // Console.WriteLine ( (GC.GetTotalMemory(false) / 1048576f).ToString("F") + " MB" ); state = new RubyState(); state.DoString("puts \"ruby #{RUBY_VERSION}\""); // Console.WriteLine ( (GC.GetTotalMemory(false) / 1048576f).ToString("F") + " MB" ); #if MRUBY R_VAL v1 = R_VAL.Create(state, 2333); R_VAL v2 = R_VAL.Create(state, 3.1415926535897932f); #else R_VAL v1 = R_VAL.Create(2333); R_VAL v2 = R_VAL.Create(3.1415926535897932d); #endif R_VAL v3 = R_VAL.FALSE; R_VAL v4 = R_VAL.TRUE; R_VAL v5 = R_VAL.NIL; #if MRUBY Console.WriteLine(v1.ToString(state)); Console.WriteLine(v2.ToString(state)); Console.WriteLine(v3.ToString(state)); Console.WriteLine(v4.ToString(state)); Console.WriteLine(v5.ToString(state)); state.DefineMethod("WriteLine", WriteLine, rb_args.ANY()); state.DefineMethod("show_backtrace", ShowBackTrace, rb_args.NONE()); state.DefineMethod("WriteLineNormal", new System.Action <string, int, float, bool> (TestDelegate), rb_args.ANY()); mRubyClass klass = new mRubyClass(state, "CSharpClass"); klass.DefineMethod("write", WriteLine, rb_args.ANY()); // WrapperUtility.GenCSharpClass ( typeof ( System.Object ) ); // WrapperUtility.GenCSharpClass ( typeof ( System.Array ) ); // WrapperUtility.GenCSharpClass ( typeof ( System.TimeSpan ) ); // WrapperUtility.GenByAssembly ( typeof ( Microsoft.Xna.Framework.Game ).Assembly ); // WrapperUtility.GenUnityEngineCommon (); // WrapperUtility.GenCSharpClass ( typeof ( CustomClass ) ); // WrapperUtility.GenCSharpClass ( typeof ( CustomEnum ) ); UserDataUtility.RegisterType <CustomClass> (state); UserDataUtility.RegisterType <CustomEnum> (state); // CustomClass_Wrapper.__Register__ ( state ); // CustomEnum_Wrapper.__Register__ ( state ); // state.DoFile ( "main.rb" ); #else Console.WriteLine(v1.ToString()); Console.WriteLine(v2.ToString()); Console.WriteLine(v3.ToString()); Console.WriteLine(v4.ToString()); Console.WriteLine(v5.ToString()); state.DefineMethod("WriteLine", WriteLine, rb_args.ANY()); state.DefineMethod("show_backtrace", ShowBackTrace, rb_args.NONE()); #endif state.DoString("WriteLine(\"System::Object.new\")"); state.DoString("show_backtrace"); state.DoString("WriteLineNormal( 'mruby ok!', 1, 9.9, true )"); state.DoString("puts RubySharp::CustomEnum::A"); state.DoString("puts RubySharp::CustomEnum::B"); state.DoString("puts RubySharp::CustomEnum::C"); state.DoString("puts RubySharp::CustomClass.new.FuncB( 999 )"); state.DoString("puts RubySharp::CustomClass.new.FuncC( 'AABB' )"); state.DoString("puts RubySharp::CustomClass.new.FuncD( 1, 2.0, true, 'HelloString', RubySharp::CustomClass.new )"); state.DoString("puts RubySharp::CustomClass.+( RubySharp::CustomClass.new, 100 )"); state.DoString("puts RubySharp::CustomClass::FuncE( nil )"); state.DoString("puts RubySharp::CustomClass.FuncF( RubySharp::CustomClass.new, 900.0 )"); state.DoString("puts RubySharp::CustomClass.new.FuncG( RubySharp::CustomClass.new )"); #if MRUBY if (RubyDLL.mrb_has_exc(state)) { Console.WriteLine(state.GetExceptionBackTrace()); RubyDLL.mrb_exc_clear(state); } #else R_VAL exc = RubyDLL.rb_errinfo(); if (RubyDLL.r_type(exc) != rb_vtype.RUBY_T_NIL) { Console.WriteLine(RubyState.GetExceptionBackTrace()); } #endif (state as IDisposable).Dispose(); // Console.ReadKey (); }