mruby_tools 0.0.1.1 → 0.0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9aa01e1c7834a6b9ba351aa1e70df7399628dc8c
4
- data.tar.gz: 42b72b7ca714f805e00a0f73b12439d1611dbd65
3
+ metadata.gz: bde41419ba3eb29e2b6487d232c94b2cf630387f
4
+ data.tar.gz: 643bb284b8af86dbd22cd29a70d5badb5824fcfb
5
5
  SHA512:
6
- metadata.gz: 5aae2588bb3b849cba817a377f2995c55d7e0e8b27f770b9f5c991093f6c9f3032a7d2e9e64810bee44ffb8b616b2e6a1f55694297af1b8ca20bc22c1c4b5cde
7
- data.tar.gz: 2e225121b919de40e9fbd803fbf0c3aa2cf5ebb7b0433c1bfa30e58e52cce864361f4248ef00b90892669a239d5908c0cab64b1cbf3ea72f1ee868f63dd34a40
6
+ metadata.gz: 4dcbdcf5da887ffad6bb6ac220aa4e2525724e228af89329beb64e58131c7eea40bf66038d13cb95c6dd3a1fe2f321e6246a6bc1cac68cf7c7b564ec6d60dff5
7
+ data.tar.gz: 376214a2967c23a8c9fd27aa113337a6439b4ed18c48e4749b90b94f96ac9a9e465e9bf9a86a5ec8b20235155fca3cb0ef930328d6529da7dfe5fd8ed1e28376
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1.1
1
+ 0.0.2.1
data/bin/mrbt CHANGED
@@ -7,59 +7,24 @@ require 'mruby_tools'
7
7
  # possibly: file1.rb -o outfile file2.rb -c generated.c
8
8
 
9
9
  opts = MRubyTools.args(ARGV)
10
- outfile = opts.fetch(:outfile)
11
-
12
- c_code = <<'EOF'
13
- #include <stdlib.h>
14
- #include <mruby.h>
15
- #include <mruby/compile.h>
16
- #include <mruby/string.h>
17
-
18
- void check_exc(mrb_state *mrb, char *filename) {
19
- if (mrb->exc) {
20
- mrb_value exc = mrb_obj_value(mrb->exc);
21
- mrb_value exc_msg = mrb_funcall(mrb, exc, "to_s", 0);
22
- fprintf(stderr, "ERROR in %s - %s: %s\n",
23
- filename,
24
- mrb_obj_classname(mrb, exc),
25
- mrb_str_to_cstr(mrb, exc_msg));
26
- /* mrb_print_backtrace(mrb); # empty */
27
- exit(1);
28
- }
29
- }
30
-
31
- int
32
- main(void)
33
- {
34
- mrb_state *mrb = mrb_open();
35
- if (!mrb) {
36
- printf("mrb problem");
37
- exit(1);
38
- }
39
- EOF
40
-
41
- c_code += opts.fetch(:rb_files).map { |rbf|
42
- "\n" + MRubyTools.rb2c(rbf) + "\n\n"
43
- }.join
44
-
45
- c_code += <<EOF
46
- mrb_close(mrb);
47
- return 0;
48
- }
49
- EOF
10
+ MRubyTools.usage if opts[:help]
50
11
 
12
+ rb_files = opts.fetch(:rb_files)
13
+ MRubyTools.usage("no .rb files provided") if rb_files.empty?
14
+ c_code = MRubyTools.c_wrapper(rb_files)
51
15
  puts c_code + "\n" if opts[:verbose]
52
16
 
53
- file = opts[:cfile] || Tempfile.new(['generated', '.c'])
54
- file.write(c_code)
55
- file.close
17
+ out_file = opts.fetch(:out_file)
18
+ c_file = opts.fetch(:c_file)
19
+ c_file.write(c_code)
20
+ c_file.close
56
21
 
57
22
  msd = MRubyTools.mruby_src_dir
58
23
 
59
24
  gcc_args = [
60
- '-std=c99', "-I", File.join(msd, 'include'), file.path, "-o", outfile,
25
+ '-std=c99', "-I", File.join(msd, 'include'), c_file.path, "-o", out_file,
61
26
  File.join(msd, 'build', 'host', 'lib', 'libmruby.a'), '-lm',
62
27
  ]
63
28
 
64
29
  puts "compiling..."
65
- puts "created binary executable: #{outfile}" if system('gcc', *gcc_args)
30
+ puts "created binary executable: #{out_file}" if system('gcc', *gcc_args)
data/lib/mruby_tools.rb CHANGED
@@ -1,4 +1,47 @@
1
+ require 'tempfile'
2
+
1
3
  module MRubyTools
4
+ def self.c_wrapper(rb_files)
5
+ c_code = <<'EOF'
6
+ #include <stdlib.h>
7
+ #include <mruby.h>
8
+ #include <mruby/compile.h>
9
+ #include <mruby/string.h>
10
+
11
+ void check_exc(mrb_state *mrb, char *filename) {
12
+ if (mrb->exc) {
13
+ mrb_value exc = mrb_obj_value(mrb->exc);
14
+ mrb_value exc_msg = mrb_funcall(mrb, exc, "to_s", 0);
15
+ fprintf(stderr, "ERROR in %s - %s: %s\n",
16
+ filename,
17
+ mrb_obj_classname(mrb, exc),
18
+ mrb_str_to_cstr(mrb, exc_msg));
19
+ /* mrb_print_backtrace(mrb); # empty */
20
+ exit(1);
21
+ }
22
+ }
23
+
24
+ int
25
+ main(void)
26
+ {
27
+ mrb_state *mrb = mrb_open();
28
+ if (!mrb) {
29
+ printf("mrb problem");
30
+ exit(1);
31
+ }
32
+ EOF
33
+ c_code += rb_files.map { |rbf|
34
+ "\n" + self.rb2c(rbf) + "\n\n"
35
+ }.join
36
+
37
+ c_code += <<EOF
38
+ mrb_close(mrb);
39
+ return 0;
40
+ }
41
+ EOF
42
+ c_code
43
+ end
44
+
2
45
  def self.rb2c(rb_filename, indent: ' ')
3
46
  c_str = File.read(rb_filename)
4
47
  size = c_str.size
@@ -19,30 +62,47 @@ module MRubyTools
19
62
  mruby_src_dir
20
63
  end
21
64
 
65
+ def self.usage(msg = nil)
66
+ puts <<EOF
67
+ USAGE: mrbt file1.rb file2.rb ...
68
+ OPTIONS: -o outfile (provide a name for the standalone executable)
69
+ -c generated.c (leave the specified C file on the filesystem)
70
+ -v (verbose)
71
+ EOF
72
+ warn " ERROR: #{msg}" if msg
73
+ exit
74
+ end
75
+
22
76
  def self.args(argv = ARGV)
23
77
  rb_files = []
24
- outfile = nil
25
- cfile = nil
78
+ out_file = nil
79
+ c_file = nil
26
80
  verbose = false
81
+ help = false
27
82
 
28
83
  while !argv.empty?
29
84
  arg = argv.shift
30
85
  if arg == '-o'
31
- outfile = argv.shift
32
- raise "no outfile provided with -o" unless outfile
33
- raise "#{outfile} is misnamed" if File.extname(outfile) == '.rb'
86
+ out_file = argv.shift
87
+ raise "no out_file provided with -o" unless out_file
88
+ raise "#{out_file} is misnamed" if File.extname(out_file) == '.rb'
34
89
  elsif arg == '-c'
35
- cfile = File.open(argv.shift || 'generated.c', "w")
90
+ c_file = File.open(argv.shift || 'generated.c', "w")
36
91
  elsif arg == '-v'
37
92
  verbose = true
93
+ elsif arg == '-h'
94
+ help = true
38
95
  else
39
96
  rb_files << arg
40
97
  end
41
98
  end
42
99
 
100
+ c_file ||= Tempfile.new(['generated', '.c'])
101
+
43
102
  { verbose: verbose,
44
- cfile: cfile,
45
- outfile: outfile || 'outfile',
103
+ help: help,
104
+ c_file: c_file,
105
+ out_file: out_file || 'outfile',
46
106
  rb_files: rb_files }
47
107
  end
48
108
  end
@@ -0,0 +1,68 @@
1
+ require 'mruby_tools'
2
+ require 'minitest/autorun'
3
+
4
+ describe MRubyTools do
5
+ describe "c_wrapper" do
6
+ it "must return a string of mruby C code" do
7
+ [[], [__FILE__]].each { |rb_files|
8
+ str = MRubyTools.c_wrapper(rb_files)
9
+ str.must_be_kind_of String
10
+ str.wont_be_empty
11
+ str.must_match(/main\(void\)/)
12
+ str.must_match(/return/)
13
+ str.must_match(/exit/)
14
+ str.must_match(/mruby/)
15
+ str.must_match(/mrb/)
16
+ }
17
+ end
18
+ end
19
+
20
+ describe "rb2c" do
21
+ it "must inject the contents of a ruby file into mrb_load_string()" do
22
+ str = MRubyTools.rb2c(__FILE__)
23
+ str.must_be_kind_of String
24
+ str.wont_be_empty
25
+ str.must_match(/mrb_load_n?string\(/)
26
+ str.must_match(/exc/)
27
+ end
28
+ end
29
+
30
+ describe "mruby_src_dir" do
31
+ it "must confirm the specified mruby source on the filesystem" do
32
+ candidates = []
33
+ %w{src git}.each { |subdir|
34
+ candidates +=
35
+ Dir[File.join(ENV['HOME'], subdir, 'mruby-1.*', 'include')]
36
+ }
37
+ unless candidates.empty?
38
+ latest = File.expand_path('..', candidates.last)
39
+ key = 'TESTING_MRUBY_SRC_DIR'
40
+ ENV[key] = latest
41
+ dir = MRubyTools.mruby_src_dir(key)
42
+ File.directory?(dir).must_equal true
43
+ File.directory?(File.join(dir, 'include')).must_equal true
44
+ end
45
+ end
46
+
47
+ it "must accept the dir specification from the environment" do
48
+ proc { MRubyTools.mruby_src_dir('bad key') }.must_raise Exception
49
+ end
50
+ end
51
+
52
+ describe "args" do
53
+ it "must provide a hash with expected keys" do
54
+ h = MRubyTools.args([])
55
+ h.must_be_kind_of Hash
56
+ [:verbose, :help, :c_file, :out_file, :rb_files].each { |key|
57
+ h.key?(key).must_equal true
58
+ }
59
+ h[:verbose].must_equal false
60
+ h[:help].must_equal false
61
+ [Tempfile, File].must_include h[:c_file].class
62
+ h[:out_file].must_be_kind_of String
63
+ h[:out_file].wont_be_empty
64
+ h[:rb_files].must_be_kind_of Array
65
+ h[:rb_files].must_be_empty
66
+ end
67
+ end
68
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mruby_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.1
4
+ version: 0.0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Hull
@@ -57,6 +57,7 @@ files:
57
57
  - examples/timer.rb
58
58
  - lib/mruby_tools.rb
59
59
  - mruby_tools.gemspec
60
+ - test/mruby_tools.rb
60
61
  homepage: https://github.com/rickhull/mruby-tools
61
62
  licenses:
62
63
  - LGPL-3.0