rbyte 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ Makefile
2
+ *.o
3
+ *.bundle
4
+ pkg/
data/README ADDED
@@ -0,0 +1,17 @@
1
+ Ruby byte code library.
2
+
3
+ There are a few major bugs with byte encoding in Ruby 1.9.1 so,
4
+ to use this library, you need to use Ruby's trunk.
5
+
6
+ Installation:
7
+ sudo gem install rbyte
8
+
9
+ Usage:
10
+ Rbyte.compile_file(path)
11
+ Rbyte.decompile_file(path)
12
+
13
+ # To make RBC files automatically be required
14
+ Kernel.send(:include, Rbyte::RequirePatch)
15
+ require "my_rbc_file"
16
+
17
+
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "rbyte"
5
+ gemspec.summary = "Ruby byte compile lib"
6
+ gemspec.email = "info@eribium.org"
7
+ gemspec.homepage = "http://github.com/maccman/rbyte"
8
+ gemspec.description = "Ruby byte compile lib"
9
+ gemspec.authors = ["Alex MacCaw"]
10
+ gemspec.extensions = ["ext/extconf.rb"]
11
+ end
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,6 @@
1
+ unless defined?(RubyVM)
2
+ raise "Current Ruby version is not supported"
3
+ end
4
+
5
+ require "mkmf"
6
+ create_makefile("iseq_ext")
@@ -0,0 +1,20 @@
1
+ #include "ruby.h"
2
+
3
+ RUBY_EXTERN VALUE rb_cISeq;
4
+ VALUE ruby_iseq_load(VALUE data, VALUE parent, VALUE opt);
5
+
6
+ static VALUE
7
+ iseq_s_load(int argc, VALUE *argv, VALUE self)
8
+ {
9
+ VALUE data, opt=Qnil;
10
+ rb_scan_args(argc, argv, "11", &data, &opt);
11
+
12
+ return ruby_iseq_load(data, 0, opt);
13
+ }
14
+
15
+ void
16
+ Init_iseq_ext()
17
+ {
18
+ // Load method is commented out in iseq.c
19
+ rb_define_singleton_method(rb_cISeq, "load_array", iseq_s_load, -1);
20
+ }
@@ -0,0 +1,102 @@
1
+ require File.join(File.dirname(__FILE__), *%w[iseq_ext])
2
+
3
+ module Rbyte
4
+ module RequirePatch
5
+ def self.included(base)
6
+ base.class_eval do
7
+ alias :require_without_rbyte :require
8
+ alias :require :require_with_rbyte
9
+ end
10
+ end
11
+
12
+ # Searches load path for rbc files.
13
+ # If a normal Ruby file exists with with a
14
+ # more recent mtime, then that will be loaded
15
+ # instead. Delegates to Ruby's normal require
16
+ # if a file can't be found.
17
+ def require_with_rbyte(name)
18
+ path = Rbyte.search_for_rbc_file(name)
19
+ unless path
20
+ return require_without_rbyte(name)
21
+ end
22
+
23
+ # File already loaded?
24
+ return false if $".include?(path)
25
+
26
+ # File is plain ruby
27
+ if path =~ /\.rb\Z/
28
+ return require_without_rbyte(path)
29
+ end
30
+
31
+ # Find out if rbc file is out of date
32
+ rb_path = path.gsub(/\.rbc\Z/, ".rb")
33
+ if File.file?(rb_path) &&
34
+ mtime = File.mtime(rb_path)
35
+ if File.mtime(path) < mtime
36
+ return require_without_rbyte(rb_path)
37
+ end
38
+ end
39
+
40
+ # Evaluate rbc file
41
+ Rbyte.decompile_file(path)
42
+
43
+ # Add to loaded files
44
+ $" << File.expand_path(path)
45
+
46
+ true
47
+ end
48
+ end
49
+
50
+ def search_for_rbc_file(path) #:nodoc:
51
+ return unless supported_file?(path)
52
+ path.gsub!(/\.rbc\Z/, "")
53
+ $LOAD_PATH.each do |root|
54
+ test_path = File.join(root, path)
55
+
56
+ # Test for rbc files
57
+ rbc_test_path = test_path + ".rbc"
58
+ return rbc_test_path if File.file?(rbc_test_path)
59
+
60
+ # Test for rb files
61
+ rb_test_path = test_path + ".rb"
62
+ return rb_test_path if File.file?(rb_test_path)
63
+ end
64
+ nil
65
+ end
66
+ module_function :search_for_rbc_file
67
+
68
+ # Read a rbc file and evaluate it
69
+ def decompile_file(path)
70
+ res = Marshal.load(File.read(path))
71
+ RubyVM::InstructionSequence.load_array(res).eval
72
+ end
73
+ module_function :decompile_file
74
+
75
+ # Compile a Ruby file to a Ruby byte code (rbc) file.
76
+ # The rbc file will be placed next to the Ruby file.
77
+ # If the method returns false, than compilation failed.
78
+ def compile_file(path)
79
+ path = "#{path}.rb" unless path =~ /\.rb\Z/
80
+ res = RubyVM::InstructionSequence.compile_file(path)
81
+ data = Marshal.dump(res.to_a)
82
+ rbc_path = path + "c"
83
+ File.open(rbc_path, "w+") {|f| f.write data }
84
+ rescue NotImplementedError
85
+ # Ruby bug with terminated objects
86
+ false
87
+ end
88
+ module_function :compile_file
89
+
90
+ private
91
+ # Only paths without a extension
92
+ # or with an extension of .rbc are
93
+ # are supported. If you require a file
94
+ # with a rb extension, it's assumed you
95
+ # only want plain ruby files.
96
+ def supported_file?(path)
97
+ ext = File.extname(path)
98
+ return true if ext.empty?
99
+ ext =~ /\.rbc\Z/
100
+ end
101
+ module_function :supported_file?
102
+ end
@@ -0,0 +1,45 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rbyte}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alex MacCaw"]
12
+ s.date = %q{2009-12-31}
13
+ s.description = %q{Ruby byte compile lib}
14
+ s.email = %q{info@eribium.org}
15
+ s.extensions = ["ext/extconf.rb"]
16
+ s.extra_rdoc_files = [
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "ext/extconf.rb",
25
+ "ext/iseq_ext.c",
26
+ "lib/rbyte.rb",
27
+ "rbyte.gemspec"
28
+ ]
29
+ s.homepage = %q{http://github.com/maccman/rbyte}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.5}
33
+ s.summary = %q{Ruby byte compile lib}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ else
41
+ end
42
+ else
43
+ end
44
+ end
45
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbyte
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex MacCaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-31 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby byte compile lib
17
+ email: info@eribium.org
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - .gitignore
26
+ - README
27
+ - Rakefile
28
+ - VERSION
29
+ - ext/extconf.rb
30
+ - ext/iseq_ext.c
31
+ - lib/rbyte.rb
32
+ - rbyte.gemspec
33
+ has_rdoc: true
34
+ homepage: http://github.com/maccman/rbyte
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Ruby byte compile lib
61
+ test_files: []
62
+