Skip to content

Commit 6081d43

Browse files
committed
Pg capi fixes
PullRequest: truffleruby/699
2 parents a8ae4d3 + 15d1949 commit 6081d43

File tree

7 files changed

+64
-8
lines changed

7 files changed

+64
-8
lines changed

lib/cext/include/ruby/intern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ VALUE rb_str_buf_append(VALUE, VALUE);
731731
VALUE rb_str_buf_cat(VALUE, const char*, long);
732732
VALUE rb_str_buf_cat2(VALUE, const char*);
733733
VALUE rb_str_buf_cat_ascii(VALUE, const char*);
734-
#define rb_obj_as_string(object) rb_any_to_s(object)
734+
VALUE rb_obj_as_string(VALUE object);
735735
VALUE rb_check_string_type(VALUE);
736736
void rb_must_asciicompat(VALUE);
737737
#define rb_str_dup(string) rb_obj_dup(string)

lib/cext/include/ruby/ruby.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ typedef char ruby_check_sizeof_voidp[SIZEOF_VOIDP == sizeof(void*) ? 1 : -1];
225225
# endif
226226
#endif
227227

228-
#define RUBY_FIXNUM_MAX (LONG_MAX>>1)
229-
#define RUBY_FIXNUM_MIN RSHIFT((long)LONG_MIN,1)
228+
#define RUBY_FIXNUM_MAX LONG_MAX
229+
#define RUBY_FIXNUM_MIN LONG_MIN
230230
#define FIXNUM_MAX RUBY_FIXNUM_MAX
231231
#define FIXNUM_MIN RUBY_FIXNUM_MIN
232232

@@ -346,7 +346,7 @@ rb_fix2ulong(VALUE x)
346346
return RB_FIX2ULONG(x);
347347
}
348348
int RB_FIXNUM_P(VALUE value);
349-
#define RB_POSFIXABLE(f) ((f) < RUBY_FIXNUM_MAX+1)
349+
#define RB_POSFIXABLE(f) ((f) <= RUBY_FIXNUM_MAX)
350350
#define RB_NEGFIXABLE(f) ((f) >= RUBY_FIXNUM_MIN)
351351
#define RB_FIXABLE(f) (RB_POSFIXABLE(f) && RB_NEGFIXABLE(f))
352352
#define FIX2LONG(x) RB_FIX2LONG(x)

lib/cext/patches/pg_patches.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ class PgPatches
1515
case Qfalse_int_const : mybool = 0; break;
1616
EOF
1717

18+
PG_TEXT_DECODER_FREE_OLD = <<-EOF
19+
static VALUE
20+
pg_text_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
21+
EOF
22+
23+
PG_TEXT_DECODER_FREE_NEW = <<-EOF
24+
static VALUE pg_tr_pq_freemem(VALUE mem) {
25+
PQfreemem((void *)mem);
26+
return Qfalse;
27+
}
28+
29+
static VALUE
30+
pg_text_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
31+
EOF
32+
1833
PATCHES = {
1934
gem: 'pg',
2035
patches: {
@@ -24,6 +39,34 @@ class PgPatches
2439
replacement: PG_BINARY_ENCODER_PATCH
2540
}
2641
],
42+
'pg_result.c' => [
43+
{
44+
match: 'xmalloc(',
45+
replacement: 'calloc(1,'
46+
},
47+
],
48+
'pg_tuple.c' => [
49+
{
50+
match: 'xmalloc(',
51+
replacement: 'calloc(1,'
52+
},
53+
],
54+
'pg_text_decoder.c' => [
55+
{
56+
match: PG_TEXT_DECODER_FREE_OLD,
57+
replacement: PG_TEXT_DECODER_FREE_NEW
58+
},
59+
{
60+
match: '(VALUE(*)())PQfreemem',
61+
replacement: 'pg_tr_pq_freemem'
62+
}
63+
],
64+
'pg_text_encoder.c' => [
65+
{
66+
match: 'if(TYPE(*intermediate) == T_FIXNUM)',
67+
replacement: 'if(TYPE(*intermediate) == T_FIXNUM && NUM2LL(*intermediate) != -(NUM2LL(*intermediate)))'
68+
},
69+
],
2770
'pg_type_map_by_class.c' => [
2871
{
2972
match: '#define CACHE_LOOKUP(this, klass) ( &this->cache_row[(klass >> 8) & 0xff] )',

lib/truffle/truffle/cext.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,10 @@ def rb_any_to_s(object)
16621662
Truffle::Type.rb_any_to_s(object)
16631663
end
16641664

1665+
def rb_obj_as_string(object)
1666+
Truffle::Type.rb_obj_as_string(object)
1667+
end
1668+
16651669
def rb_class_inherited_p(ruby_module, object)
16661670
if object.is_a?(Module)
16671671
ruby_module <= object

src/main/c/cext/ruby.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ VALUE rb_obj_dup(VALUE object) {
438438
return RUBY_INVOKE(object, "dup");
439439
}
440440

441+
VALUE rb_obj_as_string(VALUE object) {
442+
return RUBY_CEXT_INVOKE("rb_obj_as_string", object);
443+
}
444+
441445
VALUE rb_any_to_s(VALUE object) {
442446
return RUBY_CEXT_INVOKE("rb_any_to_s", object);
443447
}
@@ -475,7 +479,12 @@ void rb_obj_call_init(VALUE object, int argc, const VALUE *argv) {
475479
}
476480

477481
const char *rb_obj_classname(VALUE object) {
478-
return RSTRING_PTR(RUBY_CEXT_INVOKE("rb_obj_classname", object));
482+
VALUE str = RUBY_CEXT_INVOKE("rb_obj_classname", object);
483+
if (str != Qnil) {
484+
return RSTRING_PTR(str);
485+
} else {
486+
return NULL;
487+
}
479488
}
480489

481490
VALUE rb_obj_id(VALUE object) {

src/main/java/org/truffleruby/core/rope/NativeRope.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public NativeRope resize(FinalizationService finalizationService, int newByteLen
8080
pointer.writeBytes(0, this.pointer, 0, Math.min(byteLength(), newByteLength));
8181
pointer.writeByte(newByteLength, (byte) 0); // Like MRI
8282
pointer.enableAutorelease(finalizationService);
83-
return new NativeRope(pointer, newByteLength, ASCIIEncoding.INSTANCE, UNKNOWN_CHARACTER_LENGTH, CodeRange.CR_UNKNOWN);
83+
return new NativeRope(pointer, newByteLength, getEncoding(), UNKNOWN_CHARACTER_LENGTH, CodeRange.CR_UNKNOWN);
8484
}
8585

8686
@Override
@@ -178,7 +178,7 @@ public String toString() {
178178

179179
@Override
180180
public Rope withEncoding(Encoding newEncoding, CodeRange newCodeRange) {
181-
return RopeOperations.create(getBytes(), newEncoding, newCodeRange);
181+
return new NativeRope(pointer, byteLength(), newEncoding, UNKNOWN_CHARACTER_LENGTH, CodeRange.CR_UNKNOWN);
182182
}
183183

184184
public Pointer getNativePointer() {

src/main/java/org/truffleruby/core/rope/RopeNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ protected Rope withEncodingSameEncoding(Rope rope, Encoding encoding) {
976976

977977
@Specialization(guards = "rope.getEncoding() != encoding")
978978
protected Rope nativeRopeWithEncoding(NativeRope rope, Encoding encoding) {
979-
return rescanBytesForEncoding(rope, encoding);
979+
return rope.withEncoding(encoding, rope.getCodeRange());
980980
}
981981

982982
@Specialization(guards = {

0 commit comments

Comments
 (0)