update example and simplify a bit
This commit is contained in:
parent
7d297a686b
commit
3e4f5d2f6c
2
Makefile
Normal file
2
Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
main: main.c
|
||||||
|
gcc -o $@ $^ $$(pkg-config --cflags --libs ruby)
|
22
Rakefile.rb
22
Rakefile.rb
@ -1,22 +0,0 @@
|
|||||||
require "rscons"
|
|
||||||
|
|
||||||
WINDOWS_RUBY_PATH = "/cygdrive/c/Ruby193"
|
|
||||||
|
|
||||||
task :default do
|
|
||||||
Rscons::Environment.new do |env|
|
|
||||||
if RUBY_PLATFORM =~ /cygwin/
|
|
||||||
env["CC"] = "i686-pc-mingw32-gcc"
|
|
||||||
env["CPPPATH"] += [
|
|
||||||
"#{WINDOWS_RUBY_PATH}/include/ruby-1.9.1",
|
|
||||||
"#{WINDOWS_RUBY_PATH}/include/ruby-1.9.1/i386-mingw32",
|
|
||||||
]
|
|
||||||
env["LIBS"] += ["msvcrt-ruby191"]
|
|
||||||
env["LDFLAGS"] += ["-L#{WINDOWS_RUBY_PATH}/lib"]
|
|
||||||
else
|
|
||||||
env.parse_flags!("!pkg-config --cflags --libs ruby-1.9")
|
|
||||||
end
|
|
||||||
env["CFLAGS"] += ["-Wall"]
|
|
||||||
sources = Dir["*.c"]
|
|
||||||
env.Program("main", sources)
|
|
||||||
end
|
|
||||||
end
|
|
65
main.c
65
main.c
@ -1,39 +1,50 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "ruby.h"
|
#include <ruby.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
RUBY_GLOBAL_SETUP
|
VALUE ruby_protect_eval_string_rescue(VALUE exception, VALUE exception_object)
|
||||||
|
|
||||||
static VALUE eval(const char * string)
|
|
||||||
{
|
{
|
||||||
int state;
|
*(bool *)exception = true;
|
||||||
VALUE v = rb_eval_string_protect(string, &state);
|
fprintf(stderr, "exception: %s\n",
|
||||||
if (state != 0)
|
rb_obj_classname(exception_object));
|
||||||
{
|
return Qnil;
|
||||||
// fprintf(stderr, "rb_eval_string_protect() returned state %d!\n", state);
|
|
||||||
VALUE e = rb_errinfo();
|
|
||||||
VALUE s = rb_funcall(e, rb_intern("message"), 0);
|
|
||||||
fprintf(stderr, "%s\n", StringValueCStr(s));
|
|
||||||
rb_set_errinfo(Qnil);
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
VALUE ruby_protect_eval_string(const char * ruby_expression, bool * exception)
|
||||||
{
|
{
|
||||||
int _argc = 1;
|
*exception = false;
|
||||||
char *_argv[] = {"main"};
|
return rb_rescue(rb_eval_string, (VALUE)ruby_expression,
|
||||||
char **_argv_p = &_argv[0];
|
ruby_protect_eval_string_rescue, (VALUE)exception);
|
||||||
ruby_sysinit(&_argc, &_argv_p);
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s <ruby-expression>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
VALUE v = INT2FIX(0);
|
||||||
|
bool exception;
|
||||||
{
|
{
|
||||||
RUBY_INIT_STACK;
|
RUBY_INIT_STACK;
|
||||||
ruby_init();
|
ruby_init();
|
||||||
ruby_init_loadpath();
|
v = ruby_protect_eval_string(argv[1], &exception);
|
||||||
VALUE v = eval("3 + 8");
|
}
|
||||||
printf("v: %d\n", FIX2INT(v));
|
if (exception)
|
||||||
eval("yoda");
|
{
|
||||||
eval("File.open('out.txt', 'w') {|fh| fh.puts 'hello!'}");
|
fprintf(stderr, "Exception!\n");
|
||||||
eval("puts 'Hello, World!'");
|
}
|
||||||
ruby_finalize();
|
else
|
||||||
|
{
|
||||||
|
if (FIXNUM_P(v))
|
||||||
|
{
|
||||||
|
printf("Success! Result is %d\n", FIX2INT(v));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Success! Non-Fixnum result\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user