Module: MRubyTools
- Defined in:
- lib/mruby_tools.rb
Class Method Summary collapse
- .args(argv = ARGV) ⇒ Object
- .c_wrapper(rb_files) ⇒ Object
- .mruby_src_dir(env_var = 'MRUBY_SRC') ⇒ Object
- .rb2c(rb_filename, indent: ' ') ⇒ Object
- .usage(msg = nil) ⇒ Object
Class Method Details
.args(argv = ARGV) ⇒ Object
| 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | # File 'lib/mruby_tools.rb', line 76 def self.args(argv = ARGV) rb_files = [] out_file = nil c_file = nil verbose = false help = false while !argv.empty? arg = argv.shift if arg == '-o' out_file = argv.shift raise "no out_file provided with -o" unless out_file raise "#{out_file} is misnamed" if File.extname(out_file) == '.rb' elsif arg == '-c' c_file = File.open(argv.shift || 'generated.c', "w") elsif arg == '-v' verbose = true elsif arg == '-h' help = true else rb_files << arg end end c_file ||= Tempfile.new(['generated', '.c']) { verbose: verbose, help: help, c_file: c_file, out_file: out_file || 'outfile', rb_files: rb_files } end | 
.c_wrapper(rb_files) ⇒ Object
| 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # File 'lib/mruby_tools.rb', line 4 def self.c_wrapper(rb_files) c_code = <<'EOF' #include <stdlib.h> #include <mruby.h> #include <mruby/compile.h> #include <mruby/string.h> void check_exc(mrb_state *mrb, char *filename) { if (mrb->exc) { mrb_value exc = mrb_obj_value(mrb->exc); mrb_value exc_msg = mrb_funcall(mrb, exc, "to_s", 0); fprintf(stderr, "ERROR in %s - %s: %s\n", filename, mrb_obj_classname(mrb, exc), mrb_str_to_cstr(mrb, exc_msg)); /* mrb_print_backtrace(mrb); # empty */ exit(1); } } int main(void) { mrb_state *mrb = mrb_open(); if (!mrb) { printf("mrb problem"); exit(1); } EOF c_code += rb_files.map { |rbf| "\n" + self.rb2c(rbf) + "\n\n" }.join c_code += <<EOF mrb_close(mrb); return 0; } EOF c_code end | 
.mruby_src_dir(env_var = 'MRUBY_SRC') ⇒ Object
| 56 57 58 59 60 61 62 63 | # File 'lib/mruby_tools.rb', line 56 def self.mruby_src_dir(env_var = 'MRUBY_SRC') mruby_src_dir = ENV[env_var] raise "env: MRUBY_SRC is required" unless mruby_src_dir raise "bad MRUBY_SRC #{mruby_src_dir}" unless File.directory? mruby_src_dir mruby_inc_dir = File.join(mruby_src_dir, 'include') raise "bad MRUBY_SRC #{mruby_inc_dir}" unless File.directory? mruby_inc_dir mruby_src_dir end | 
.rb2c(rb_filename, indent: ' ') ⇒ Object
| 45 46 47 48 49 50 51 52 53 54 | # File 'lib/mruby_tools.rb', line 45 def self.rb2c(rb_filename, indent: ' ') c_str = File.read(rb_filename) size = c_str.size c_str = c_str.gsub("\n", '\n').gsub('"', '\"') c_str = File.read(rb_filename).gsub("\n", '\n').gsub('"', '\"') [ "/* #{rb_filename} */", 'mrb_load_nstring(mrb, "' + c_str + '", ' + "#{size});", "check_exc(mrb, \"#{rb_filename}\");", ].map { |s| indent + s }.join("\n") end | 
.usage(msg = nil) ⇒ Object
| 65 66 67 68 69 70 71 72 73 74 | # File 'lib/mruby_tools.rb', line 65 def self.usage(msg = nil) puts <<EOF USAGE: mrbt file1.rb file2.rb ... OPTIONS: -o outfile (provide a name for the standalone executable) -c generated.c (leave the specified C file on the filesystem) -v (verbose) EOF warn " ERROR: #{msg}" if msg exit end |