Skip to content

Commit 8f64f76

Browse files
committed
Fixes for msgpack to pass all specs of msgpack v1.2.6
PullRequest: truffleruby/538
2 parents 92ccc82 + f87d267 commit 8f64f76

File tree

10 files changed

+95
-21
lines changed

10 files changed

+95
-21
lines changed

lib/truffle/stringio.rb

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ def self.open(*args)
6969

7070
def initialize(string=nil, mode=nil)
7171
if string.nil?
72-
@__data__ = Data.new ''
73-
set_encoding(nil)
72+
@__data__ = Data.new ''.force_encoding(Encoding.default_external)
7473
mode = IO::RDWR
7574
else
7675
string = Truffle::Type.coerce_to string, String, :to_str
@@ -104,19 +103,15 @@ def initialize_copy(from)
104103
self
105104
end
106105

107-
def check_readable
106+
private def check_readable
108107
raise IOError, 'not opened for reading' unless @readable
109108
end
110109

111-
private :check_readable
112-
113-
def check_writable
110+
private def check_writable
114111
raise IOError, 'not opened for writing' unless @writable
115112
raise IOError, 'unable to modify data' if @__data__.string.frozen?
116113
end
117114

118-
private :check_writable
119-
120115
def set_encoding(external, internal=nil, options=nil)
121116
encoding = external || Encoding.default_external
122117
@__data__.encoding = encoding
@@ -148,7 +143,6 @@ def each_byte
148143

149144
self
150145
end
151-
152146
alias_method :bytes, :each_byte
153147

154148
def each_char
@@ -159,7 +153,6 @@ def each_char
159153

160154
self
161155
end
162-
163156
alias_method :chars, :each_char
164157

165158
def each_codepoint(&block)
@@ -182,7 +175,6 @@ def each_codepoint(&block)
182175

183176
self
184177
end
185-
186178
alias_method :codepoints, :each_codepoint
187179

188180
def each(sep=$/, limit=Undefined)
@@ -663,9 +655,7 @@ def yaml_initialize(type, val)
663655
@__data__ = Data.new('')
664656
end
665657

666-
protected
667-
668-
def mode_from_string(mode)
658+
private def mode_from_string(mode)
669659
@append = truncate = false
670660

671661
if mode[0] == ?r
@@ -688,7 +678,7 @@ def mode_from_string(mode)
688678
d.string.replace('') if truncate
689679
end
690680

691-
def mode_from_integer(mode)
681+
private def mode_from_integer(mode)
692682
@readable = @writable = @append = false
693683
d = @__data__
694684

@@ -705,7 +695,7 @@ def mode_from_integer(mode)
705695
d.string.replace('') if (mode & IO::TRUNC) != 0
706696
end
707697

708-
def getline(arg_error, sep, limit)
698+
private def getline(arg_error, sep, limit)
709699
if limit != Undefined
710700
limit = Truffle::Type.coerce_to_int limit if limit
711701
sep = Truffle::Type.coerce_to sep, String, :to_str if sep

lib/truffle/truffle/cext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def rb_type(value)
362362
T_DATA
363363
when Data
364364
T_DATA
365-
when Object
365+
when BasicObject
366366
if hidden_variable_get(value, :data_holder)
367367
T_DATA
368368
else

spec/ruby/library/stringio/initialize_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,27 @@
183183
@io.string.should == ""
184184
end
185185
end
186+
187+
describe "StringIO#initialize sets the encoding to" do
188+
before :each do
189+
@external = Encoding.default_external
190+
Encoding.default_external = Encoding::ISO_8859_2
191+
end
192+
193+
after :each do
194+
Encoding.default_external = @external
195+
end
196+
197+
it "Encoding.default_external when passed no arguments" do
198+
io = StringIO.new
199+
io.external_encoding.should == Encoding::ISO_8859_2
200+
io.string.encoding.should == Encoding::ISO_8859_2
201+
end
202+
203+
it "the same as the encoding of the String when passed a String" do
204+
s = ''.force_encoding(Encoding::EUC_JP)
205+
io = StringIO.new(s)
206+
io.external_encoding.should == Encoding::EUC_JP
207+
io.string.encoding.should == Encoding::EUC_JP
208+
end
209+
end

spec/ruby/optional/capi/bignum_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ def ensure_bignum(n)
9797
end
9898
end
9999

100+
describe "RBIGNUM_SIGN" do
101+
it "returns 1 for a positive Bignum" do
102+
@s.RBIGNUM_SIGN(bignum_value(1)).should == 1
103+
end
104+
105+
it "returns 0 for a negative Bignum" do
106+
@s.RBIGNUM_SIGN(-bignum_value(1)).should == 0
107+
end
108+
end
109+
100110
describe "rb_big_cmp" do
101111
it "compares a Bignum with a Bignum" do
102112
@s.rb_big_cmp(bignum_value, bignum_value(1)).should == -1

spec/ruby/optional/capi/ext/bignum_spec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ static VALUE bignum_spec_rb_big2ulong(VALUE self, VALUE num) {
3131
return ULONG2NUM(rb_big2ulong(num));
3232
}
3333

34+
static VALUE bignum_spec_RBIGNUM_SIGN(VALUE self, VALUE val) {
35+
return INT2FIX(RBIGNUM_SIGN(val));
36+
}
37+
3438
static VALUE bignum_spec_rb_big_cmp(VALUE self, VALUE x, VALUE y) {
3539
return rb_big_cmp(x, y);
3640
}
@@ -90,6 +94,7 @@ void Init_bignum_spec(void) {
9094
rb_define_method(cls, "rb_big2long", bignum_spec_rb_big2long, 1);
9195
rb_define_method(cls, "rb_big2str", bignum_spec_rb_big2str, 2);
9296
rb_define_method(cls, "rb_big2ulong", bignum_spec_rb_big2ulong, 1);
97+
rb_define_method(cls, "RBIGNUM_SIGN", bignum_spec_RBIGNUM_SIGN, 1);
9398
rb_define_method(cls, "rb_big_cmp", bignum_spec_rb_big_cmp, 2);
9499
rb_define_method(cls, "rb_big_pack", bignum_spec_rb_big_pack, 1);
95100
rb_define_method(cls, "rb_big_pack_array", bignum_spec_rb_big_pack_array, 2);

spec/ruby/optional/capi/ext/kernel_spec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ VALUE kernel_spec_rb_block_proc(VALUE self) {
2626
return rb_block_proc();
2727
}
2828

29+
VALUE kernel_spec_rb_block_lambda(VALUE self) {
30+
return rb_block_lambda();
31+
}
2932

3033
VALUE block_call_inject(VALUE yield_value, VALUE data2) {
3134
/* yield_value yields the first block argument */
@@ -286,6 +289,7 @@ void Init_kernel_spec(void) {
286289
rb_define_method(cls, "rb_block_call_multi_arg", kernel_spec_rb_block_call_multi_arg, 1);
287290
rb_define_method(cls, "rb_block_call_no_func", kernel_spec_rb_block_call_no_func, 1);
288291
rb_define_method(cls, "rb_block_proc", kernel_spec_rb_block_proc, 0);
292+
rb_define_method(cls, "rb_block_lambda", kernel_spec_rb_block_lambda, 0);
289293
rb_define_method(cls, "rb_frame_this_func_test", kernel_spec_rb_frame_this_func, 0);
290294
rb_define_method(cls, "rb_frame_this_func_test_again", kernel_spec_rb_frame_this_func, 0);
291295
rb_define_method(cls, "rb_ensure", kernel_spec_rb_ensure, 4);

spec/ruby/optional/capi/kernel_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,32 @@ def proc_caller
440440
proc = @s.rb_block_proc() { 1+1 }
441441
proc.should be_kind_of(Proc)
442442
proc.call.should == 2
443+
proc.lambda?.should == false
444+
end
445+
446+
it "passes through an existing lambda and does not convert to a proc" do
447+
b = -> { 1+1 }
448+
proc = @s.rb_block_proc(&b)
449+
proc.should equal(b)
450+
proc.call.should == 2
451+
proc.lambda?.should == true
452+
end
453+
end
454+
455+
describe "rb_block_lambda" do
456+
it "converts the implicit block into a Proc but does not convert it to a lambda" do
457+
proc = @s.rb_block_proc { 1+1 }
458+
proc.should be_kind_of(Proc)
459+
proc.call.should == 2
460+
proc.lambda?.should == false
461+
end
462+
463+
it "passes through an existing Proc and does not convert to a lambda" do
464+
b = proc { 1+1 }
465+
proc = @s.rb_block_proc(&b)
466+
proc.should equal(b)
467+
proc.call.should == 2
468+
proc.lambda?.should == false
443469
end
444470
end
445471

src/main/c/cext/ruby.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,10 @@ unsigned long rb_big2ulong(VALUE x) {
671671
return polyglot_as_i64(RUBY_CEXT_INVOKE_NO_WRAP("rb_num2ulong", x));
672672
}
673673

674+
int rb_big_sign(VALUE x) {
675+
return RTEST(RUBY_INVOKE(x, ">=", INT2FIX(0))) ? 1 : 0;
676+
}
677+
674678
VALUE rb_big_cmp(VALUE x, VALUE y) {
675679
return RUBY_INVOKE(x, "<=>", y);
676680
}
@@ -1759,7 +1763,7 @@ VALUE rb_block_proc(void) {
17591763
}
17601764

17611765
VALUE rb_block_lambda(void) {
1762-
return RUBY_CEXT_INVOKE("rb_block_lambda");
1766+
return rb_block_proc();
17631767
}
17641768

17651769
VALUE rb_yield(VALUE value) {

test/truffle/cexts/msgpack/msgpack.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@ source test/truffle/common.sh.inc
44

55
jt gem-test-pack
66

7-
jt ruby -S gem install --local "$(jt gem-test-pack)/gem-cache/msgpack-1.2.4.gem" -V -N --backtrace
7+
ruby_home="$PWD"
8+
export PATH="$ruby_home/bin:$PATH"
9+
10+
# gem install "$(jt gem-test-pack)/gem-cache/bundler-1.16.5.gem" --local
11+
12+
cd "$(jt gem-test-pack)/gem-testing/msgpack-ruby"
13+
14+
bundle install --local --no-cache
15+
bundle exec rake compile
16+
bundle exec rake spec

tool/jt.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
MRI_TEST_CEXT_LIB_DIR = "#{TRUFFLERUBY_DIR}/.ext/c"
2626
PROFILES_DIR = "#{TRUFFLERUBY_DIR}/profiles"
2727

28-
TRUFFLERUBY_GEM_TEST_PACK_VERSION = "f23314cbf560d8578b0c2cbd972b83575ba93cd5"
28+
TRUFFLERUBY_GEM_TEST_PACK_VERSION = "af03b9fbb684624095de928f2b6238db26544052"
2929

3030
JDEBUG_PORT = 51819
3131
JDEBUG = "-J-agentlib:jdwp=transport=dt_socket,server=y,address=#{JDEBUG_PORT},suspend=y"
@@ -1205,14 +1205,16 @@ def test_cexts(*args)
12051205
end
12061206

12071207
# Tests using gem install to compile the cexts
1208-
sh "test/truffle/cexts/msgpack/msgpack.sh"
12091208
sh "test/truffle/cexts/puma/puma.sh"
12101209
sh "test/truffle/cexts/sqlite3/sqlite3.sh"
12111210
sh "test/truffle/cexts/unf_ext/unf_ext.sh"
12121211
sh "test/truffle/cexts/json/json.sh"
12131212

12141213
# Test a gem dynamically compiling a C extension
12151214
sh "test/truffle/cexts/RubyInline/RubyInline.sh"
1215+
1216+
# Test cexts used by many projects
1217+
sh "test/truffle/cexts/msgpack/msgpack.sh"
12161218
else
12171219
raise "unknown test: #{test_name}"
12181220
end

0 commit comments

Comments
 (0)