Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial pass at getting default pure-Ruby versions working.

  • Loading branch information...
commit fa03dede5a1a1cea1e0ede57d626737fb3f0ac5f 1 parent 8308f57
Charles Oliver Nutter authored July 21, 2011
2  Rakefile
@@ -24,7 +24,7 @@ if defined?(JRUBY_VERSION)
24 24
   end
25 25
  
26 26
   task :package => :jar
27  
-elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
  27
+else
28 28
   task :package do
29 29
     # nop
30 30
   end
26  lib/rubinius/compat.rb
... ...
@@ -0,0 +1,26 @@
  1
+# This leads to the fallback versions of the core API classes implemented
  2
+# entirely in Ruby, so they'll work where we don't have native versions
  3
+# just yet.
  4
+
  5
+# Rubinius::Channel
  6
+require 'rubinius/kernel/bootstrap/channel'
  7
+require 'rubinius/compat/channel'
  8
+require 'rubinius/kernel/common/channel'
  9
+
  10
+# Rubinius::Tuple
  11
+require 'rubinius/compat/tuple'
  12
+require 'rubinius/kernel/common/tuple'
  13
+
  14
+# Kernel.StringValue
  15
+require 'rubinius/compat/kernel'
  16
+
  17
+# Rubinius::Type
  18
+require 'rubinius/compat/type'
  19
+require 'rubinius/kernel/common/type'
  20
+
  21
+# Rubinius::ByteArray
  22
+require 'rubinius/compat/bytearray'
  23
+require 'rubinius/kernel/common/bytearray'
  24
+
  25
+# Rubinius::EnvironmentAccess
  26
+require 'rubinius/compat/environmentaccess'
87  lib/rubinius/compat/bytearray.rb
... ...
@@ -0,0 +1,87 @@
  1
+module Rubinius  
  2
+  # TODO: error cases, testing
  3
+  class ByteArray
  4
+    def intialize(size_or_ary)
  5
+      if Array === size_or_ary
  6
+        @array = size_or_ary
  7
+      else
  8
+        @array = Array.new(size)
  9
+      end
  10
+    end
  11
+    
  12
+    class << self
  13
+      alias allocate_sized new
  14
+    end
  15
+    
  16
+    def fetch_bytes(start, count)
  17
+      ByteArray.new(@array[start, count])
  18
+    end
  19
+      
  20
+    def move_bytes(start, count, dest)
  21
+      @array[dest, count] = @array[start, count]
  22
+      
  23
+      count
  24
+    end
  25
+    
  26
+    def get_byte(index)
  27
+      @array[index]
  28
+    end
  29
+    
  30
+    def set_byte(index, value)
  31
+      @array[index] = value
  32
+    end
  33
+    
  34
+    def compare_bytes(other, self_limit, other_limit)
  35
+      self_limit = size if self_limit > size
  36
+      other_limit = other.size if other_limit > other.size
  37
+      limit = self_limit < other_limit ? self_limit : other_limit
  38
+      
  39
+      cmp = @array[0, limit] <=> other._array[0, limit]
  40
+      
  41
+      return cmp != 0 ? cmp : self_limit <=> other_limit
  42
+    end
  43
+    
  44
+    def size
  45
+      @array.size
  46
+    end
  47
+    
  48
+    def locate(pattern, start, max)
  49
+      len = pattern.length
  50
+      return start if len == 0
  51
+      
  52
+      max = size if max > size || max == 0
  53
+      max -= len - 1
  54
+      
  55
+      i = start
  56
+      while i < max
  57
+        if @array[i] == pattern[0]
  58
+          j = 1
  59
+          while j < len
  60
+            break if @array[i + j] != pattern[j]
  61
+          end
  62
+          
  63
+          return i + len if j == len
  64
+        end
  65
+      end
  66
+      
  67
+      nil
  68
+    end
  69
+    
  70
+    def prepend(other)
  71
+      ba = ByteArray.new(size + other.size)
  72
+      ba._array[0, str.size] = other._array
  73
+      ba._array[str.size, size] = @array
  74
+      ba
  75
+    end
  76
+    
  77
+    def reverse
  78
+      ba = ByteArray.new(size)
  79
+      ba._array[0, size] = @array.reverse
  80
+    end
  81
+    
  82
+    def _array
  83
+      @array
  84
+    end
  85
+    private :_array
  86
+  end
  87
+end
34  lib/rubinius/compat/channel.rb
... ...
@@ -0,0 +1,34 @@
  1
+require 'thread'
  2
+require 'timeout'
  3
+
  4
+module Rubinius
  5
+  # Channel implemented as a thin layer around Queue
  6
+  class Channel
  7
+    def initialize
  8
+      @queue = Queue.new
  9
+    end
  10
+    
  11
+    def send(object)
  12
+      @queue.push(object)
  13
+    end
  14
+    alias << send
  15
+    
  16
+    def receive
  17
+      @queue.pop
  18
+    end
  19
+    
  20
+    def receive_timeout(t)
  21
+      return receive if t == -1
  22
+      
  23
+      Timeout.timeout(t) do
  24
+        receive
  25
+      end
  26
+    end
  27
+    
  28
+    def try_receive
  29
+      @queue.pop(false)
  30
+    rescue ThreadError
  31
+      nil
  32
+    end
  33
+  end
  34
+end
15  lib/rubinius/compat/environmentaccess.rb
... ...
@@ -0,0 +1,15 @@
  1
+module Rubinius
  2
+  module EnvironmentAccess
  3
+    def getenv(key)
  4
+      ENV[key]
  5
+    end
  6
+    
  7
+    def setenv(key, value)
  8
+      ENV[key] = value
  9
+    end
  10
+    
  11
+    def environ_as_hash
  12
+      ENV.to_hash
  13
+    end
  14
+  end
  15
+end
9  lib/rubinius/compat/fail.rb
... ...
@@ -0,0 +1,9 @@
  1
+module Rubinius
  2
+  # Others are errors for now
  3
+  def const_missing(name)
  4
+    if %w[ByteArray EnvironmentAccess, Type].include? name.to_s
  5
+      raise "Rubinius::#{name} not yet supported under this Ruby impl"
  6
+    end
  7
+    super
  8
+  end
  9
+end
6  lib/rubinius/compat/kernel.rb
... ...
@@ -0,0 +1,6 @@
  1
+module Kernel
  2
+  def StringValue(obj)
  3
+    obj.to_str
  4
+  end
  5
+end
  6
+    
4  lib/rubinius/compat/tuple.rb
... ...
@@ -0,0 +1,4 @@
  1
+module Rubinius
  2
+  # Array duck-types fine for Tuple
  3
+  Tuple = ::Array.clone
  4
+end
7  lib/rubinius/compat/type.rb
... ...
@@ -0,0 +1,7 @@
  1
+module Rubinius
  2
+  module Type
  3
+    def object_kind_of?(obj, cls)
  4
+      obj.class <= cls
  5
+    end
  6
+  end
  7
+end
2  lib/rubinius/core-api.rb
@@ -12,5 +12,5 @@
12 12
 elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
13 13
   # do nothing for Rubinius
14 14
 else
15  
-  raise "Only supported on JRuby and Rubinius"
  15
+  require 'rubinius/compat'
16 16
 end
8  rubinius-core-api.gemspec
@@ -2,12 +2,12 @@
2 2
 
3 3
 base_specification = proc do |s|
4 4
   s.name = %q{rubinius-core-api}
5  
-  s.version = "0.0.1"
  5
+  s.version = "0.0.2"
6 6
   s.authors = ["Charles Oliver Nutter", "Evan Phoenix"]
7 7
   s.date = Time.now
8 8
   s.description = "Cross-impl versions of interesting Rubinius core classes"
9 9
   s.email = ["headius@headius.com", "evan@fallingsnow.net"]
10  
-  s.files = Dir['{lib,src}/**/*'] + Dir['{*.txt,*.gemspec,Rakefile}']
  10
+  s.files = Dir['{lib}/**/*'] + Dir['{*.txt,*.gemspec,Rakefile}']
11 11
   s.homepage = "http://github.com/rubinius/rubinius-core-api"
12 12
   s.require_paths = ["lib"]
13 13
   s.summary = "Cross-impl versions of interesting Rubinius core classes"
@@ -19,10 +19,8 @@ if defined?(JRUBY_VERSION)
19 19
     s.tap(&base_specification)
20 20
     s.platform = "java"
21 21
   end
22  
-elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
  22
+else
23 23
   Gem::Specification.new do |s|
24 24
     s.tap(&base_specification)
25 25
   end
26  
-else
27  
-  raise "Only supported on JRuby right now."
28 26
 end

0 notes on commit fa03ded

Please sign in to comment.
Something went wrong with that request. Please try again.