Skip to content

Commit

Permalink
Add support for TruffleRuby in the gem (#35)
Browse files Browse the repository at this point in the history
* This is necessary since lib/strscan.rb was added (#25), as the
  TruffleRuby stdlib strscan.rb is no longer found first by `require "strscan"`.
* Write conditions in a way that any other Ruby implementation would
  simply use its stdlib until it is added explicit support in this gem.

Fixes oracle/truffleruby#2420

Co-authored-by: Sutou Kouhei <[email protected]>
  • Loading branch information
eregon and kou authored May 1, 2022
1 parent 38ce77f commit 4cdb3ee
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 23 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ jobs:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- ubuntu-18.04
- macos-11.0
- macos-10.15
- ubuntu-latest
- macos-latest
- windows-latest
ruby:
- '2.4'
Expand All @@ -26,11 +24,15 @@ jobs:
- '3.1'
- debug
- jruby-head
- truffleruby
- truffleruby-head
include:
- { os: windows-latest , ruby: mingw }
- { os: windows-latest , ruby: mswin }
exclude:
- { os: windows-latest , ruby: debug }
- { os: windows-latest , ruby: truffleruby }
- { os: windows-latest , ruby: truffleruby-head }

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -73,3 +75,7 @@ jobs:
- run: bundle exec rake test

- run: gem install --verbose --backtrace pkg/*.gem

- name: Run tests on the installed gem
run: ruby run-test.rb
if: matrix.ruby != '2.4' # strscan is a default gem from 2.5
7 changes: 5 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ if RUBY_ENGINE == "jruby"
ext.target_version = '1.8'
ext.ext_dir = 'ext/java'
end
else
elsif RUBY_ENGINE == "ruby"
require 'rake/extensiontask'
Rake::ExtensionTask.new("strscan")
else
task :compile
end

desc "Run test"
task :test do
ENV["RUBYOPT"] = "-Ilib -Itest/lib -rbundler/setup -rhelper"
require_path = RUBY_ENGINE == 'jruby' ? "lib/jruby" : "lib"
ENV["RUBYOPT"] = "-I#{require_path} -rbundler/setup"
ruby("run-test.rb")
end

Expand Down
10 changes: 7 additions & 3 deletions ext/strscan/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true
require 'mkmf'
$INCFLAGS << " -I$(top_srcdir)" if $extmk
have_func("onig_region_memsize", "ruby.h")
create_makefile 'strscan'
if RUBY_ENGINE == 'ruby'
$INCFLAGS << " -I$(top_srcdir)" if $extmk
have_func("onig_region_memsize", "ruby.h")
create_makefile 'strscan'
else
File.write('Makefile', dummy_makefile("").join)
end
2 changes: 2 additions & 0 deletions lib/jruby/strscan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'strscan.jar'
JRuby::Util.load_ext("org.jruby.ext.strscan.StringScannerLibrary")
6 changes: 0 additions & 6 deletions lib/strscan.rb

This file was deleted.

8 changes: 5 additions & 3 deletions run-test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env ruby

$LOAD_PATH.unshift("test")
$LOAD_PATH.unshift("test/lib")
$LOAD_PATH.unshift("lib")
require 'strscan'
puts "Loaded strscan from #{$".grep(/\/strscan\./).join(', ')}"
puts "Gem from #{Gem.loaded_specs["strscan"]&.full_gem_path}"

require_relative 'test/lib/helper'

Dir.glob("test/strscan/**/*test_*.rb") do |test_rb|
require File.expand_path(test_rb)
Expand Down
7 changes: 3 additions & 4 deletions strscan.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ Gem::Specification.new do |s|
s.summary = "Provides lexical scanning operations on a String."
s.description = "Provides lexical scanning operations on a String."

s.require_path = %w{lib}

jruby = true if Gem::Platform.new('java') =~ s.platform or RUBY_ENGINE == 'jruby'

if jruby
s.files = %w{lib/strscan.jar lib/strscan.rb}
s.require_paths = %w{lib/jruby lib}
s.files = %w{lib/strscan.jar lib/jruby/strscan.rb}
s.platform = "java"
else
s.require_paths = %w{lib}
s.files = %w{ext/strscan/extconf.rb ext/strscan/strscan.c}
s.extensions = %w{ext/strscan/extconf.rb}
end
Expand Down
2 changes: 1 addition & 1 deletion test/strscan/test_ractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class TestStringScannerRactor < Test::Unit::TestCase
def setup
pend unless defined? Ractor
omit "Ractor not defined" unless defined? Ractor
end

def test_ractor
Expand Down
6 changes: 6 additions & 0 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def test_pos_unicode
end

def test_charpos_not_use_string_methods
omit "not supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"

string = +'abcädeföghi'
scanner = create_string_scanner(string)

Expand Down Expand Up @@ -567,6 +569,8 @@ def test_encoding_string
end

def test_invalid_encoding_string
omit "no encoding check on TruffleRuby for scan(String)" if RUBY_ENGINE == "truffleruby"

str = "\xA1\xA2".dup.force_encoding("euc-jp")
ss = create_string_scanner(str)
assert_raise(Encoding::CompatibilityError) do
Expand Down Expand Up @@ -712,6 +716,8 @@ def test_inspect2
end

def test_aref_without_regex
omit "#[:missing] always raises on TruffleRuby if matched" if RUBY_ENGINE == "truffleruby"

s = create_string_scanner('abc')
s.get_byte
assert_nil(s[:c])
Expand Down

0 comments on commit 4cdb3ee

Please sign in to comment.