rubygo 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 95415fca20df4f0c95726957a081fe5e21f93e6d50d373535ed3b121b50cd85a
4
+ data.tar.gz: 9f62430580c44d32e6c858fa31cfdd6143c5f9ae09f7a7c0487f0fc4c274c2c0
5
+ SHA512:
6
+ metadata.gz: 97b92aab318d0cc3b4267d7dbe78ff5ef4c4e483e0bc0f4828e5460c59d2ec882bd2e783f5212d7f16acea437b410272ded9c707d313251c1c5e55fe3d044f76
7
+ data.tar.gz: 2cfaecc9fadaf90a79e05cb2b2f1232b6e926d0de4b7d76161540b7afe382df6033fbc033cfd94fe2eeebe3de52d9355b5bf65ef1abb3a4d523a985b999e03fd
data/bin/rubygo ADDED
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "hilighter"
4
+ require "io/wait"
5
+ require "optparse"
6
+ require "pathname"
7
+ require "rubygo"
8
+
9
+ class RubyGoExit
10
+ GOOD = 0
11
+ INVALID_OPTION = 1
12
+ INVALID_ARGUMENT = 2
13
+ MISSING_ARGUMENT = 3
14
+ EXTRA_ARGUMENTS = 4
15
+ EXCEPTION = 5
16
+ AMBIGUOUS_ARGUMENT = 6
17
+ UNSUPPORTED_ART = 7
18
+ end
19
+
20
+ def parse(args)
21
+ options = Hash.new
22
+ options["verbose"] = false
23
+
24
+ info = "Test go native extension."
25
+
26
+ parser = OptionParser.new do |opts|
27
+ opts.summary_width = 16
28
+
29
+ opts.banner = [
30
+ "Usage: #{File.basename($0)} [OPTIONS]",
31
+ "<add|sub> <a> <b>"
32
+ ].join(" ")
33
+
34
+ opts.on("", "DESCRIPTION")
35
+
36
+ info.scan(/\S.{0,76}\S(?=\s|$)|\S+/).each do |line|
37
+ opts.on(" #{line}")
38
+ end
39
+
40
+ opts.on("", "OPTIONS")
41
+
42
+ opts.on("-h", "--help", "Display this help message") do
43
+ puts opts
44
+ exit RubyGoExit::GOOD
45
+ end
46
+
47
+ opts.on(
48
+ "-v",
49
+ "--verbose",
50
+ "Show backtrace when error occurs"
51
+ ) do
52
+ options["verbose"] = true
53
+ end
54
+
55
+ opts.on("--version", "Show version") do
56
+ __FILE__.match(/rubygo-(\d+\.\d+\.\d+)/) do |m|
57
+ puts m[1]
58
+ end
59
+ exit RubyGoExit::GOOD
60
+ end
61
+ end
62
+
63
+ begin
64
+ parser.parse!
65
+ rescue OptionParser::InvalidOption => e
66
+ puts e.message
67
+ puts parser
68
+ exit RubyGoExit::INVALID_OPTION
69
+ rescue OptionParser::InvalidArgument => e
70
+ puts e.message
71
+ puts parser
72
+ exit RubyGoExit::INVALID_ARGUMENT
73
+ rescue OptionParser::MissingArgument => e
74
+ puts e.message
75
+ puts parser
76
+ exit RubyGoExit::MISSING_ARGUMENT
77
+ rescue OptionParser::AmbiguousOption => e
78
+ puts e.message
79
+ puts parser
80
+ exit RubyGoExit::AMBIGUOUS_ARGUMENT
81
+ end
82
+
83
+ if (args.length > 3)
84
+ puts parser
85
+ exit RubyGoExit::EXTRA_ARGUMENTS
86
+ elsif (args.length != 3)
87
+ puts parser
88
+ exit RubyGoExit::MISSING_ARGUMENT
89
+ end
90
+
91
+ options["operation"] = args[0]
92
+ options["a"] = args[1].to_i
93
+ options["b"] = args[2].to_i
94
+
95
+ return options
96
+ end
97
+
98
+ begin
99
+ options = parse(ARGV)
100
+ rescue Interrupt
101
+ # Exit gracefully on ^C
102
+ exit RubyGoExit::GOOD
103
+ end
104
+
105
+ begin
106
+ case options["operation"]
107
+ when "add"
108
+ puts RubyGo.Add(options["a"], options["b"])
109
+ when "sub"
110
+ puts RubyGo.Minus(options["a"], options["b"])
111
+ else
112
+ puts "Unsupported operation"
113
+ end
114
+ rescue Interrupt
115
+ # Exit gracefully on ^C
116
+ rescue Errno::EPIPE
117
+ # Do nothing. This can happen if piping to another program such as
118
+ # less. Usually if less is closed before we're done with STDOUT.
119
+ rescue Exception => e
120
+ $stderr.puts "Oops! Looks like an error has occured! If the " \
121
+ "error persists, file a bug at:".wrap
122
+ $stderr.puts
123
+ $stderr.puts " https://gitlab.com/mjwhitta/rubygo/issues"
124
+ $stderr.puts
125
+ $stderr.puts "Maybe the message below will help. If not, you " \
126
+ "can use the --verbose flag to get a backtrace.".wrap
127
+
128
+ $stderr.puts e.message.white.on_red
129
+ if (options["verbose"])
130
+ e.backtrace.each do |line|
131
+ $stderr.puts line.light_yellow
132
+ end
133
+ end
134
+ exit RubyGoExit::EXCEPTION
135
+ end
136
+ exit RubyGoExit::GOOD
@@ -0,0 +1,16 @@
1
+ package main
2
+
3
+ import "C"
4
+ import "../lib/test"
5
+
6
+ //export Add
7
+ func Add(a, b C.int) C.int {
8
+ return C.int(test.Add(int(a), int(b)))
9
+ }
10
+
11
+ //export Minus
12
+ func Minus(a, b C.int) C.int {
13
+ return C.int(test.Minus(int(a), int(b)))
14
+ }
15
+
16
+ func main() {} // Ignore
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ File.open("Makefile", "w") do |f|
4
+ f.write("BINS = $(shell find bin -type f)\n")
5
+ f.write("\n")
6
+ f.write("all: build\n")
7
+ f.write("\n")
8
+ f.write(".PHONY: $(BINS)\n")
9
+ f.write("$(BINS):\n")
10
+ f.write("\t@mkdir -p build\n")
11
+ f.write("\tgo build -buildmode=c-shared ")
12
+ f.write("-o build/$(shell basename $@ .go).so $@\n")
13
+ f.write("\n")
14
+ f.write("build: $(BINS)\n")
15
+ f.write("\n")
16
+ f.write("clean:\n")
17
+ f.write("\t@rm -rf build ../../lib/*.so\n")
18
+ f.write("\n")
19
+ f.write("install:\n")
20
+ f.write("\t@mkdir -p ../../lib\n")
21
+ f.write("\t@mv build/*.so ../../lib/\n")
22
+ end
@@ -0,0 +1,9 @@
1
+ package test
2
+
3
+ func Add(a, b int) int {
4
+ return (a + b)
5
+ }
6
+
7
+ func Minus(a, b int) int {
8
+ return (a - b)
9
+ }
data/lib/rubygo.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "ffi"
2
+
3
+ module RubyGo
4
+ extend FFI::Library
5
+ ffi_lib Pathname.new(
6
+ "#{__FILE__}/../rubygo.so"
7
+ ).expand_path.to_s
8
+ attach_function :Add, [:int, :int], :int
9
+ attach_function :Minus, [:int, :int], :int
10
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubygo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Miles Whittaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 12.3.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '12.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 12.3.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: ffi
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.9'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.9.23
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.9'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.9.23
53
+ description: Test building and running go native extension.
54
+ email: mjwhitta@gmail.com
55
+ executables:
56
+ - rubygo
57
+ extensions:
58
+ - ext/rubygo/extconf.rb
59
+ extra_rdoc_files: []
60
+ files:
61
+ - bin/rubygo
62
+ - ext/rubygo/bin/rubygo.go
63
+ - ext/rubygo/extconf.rb
64
+ - ext/rubygo/lib/test/test.go
65
+ - lib/rubygo.rb
66
+ homepage: https://mjwhitta.github.io/rubygo
67
+ licenses:
68
+ - GPL-3.0
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.7.6
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Test go native extension.
90
+ test_files: []