Skip to content

Commit

Permalink
Implement fake pack 'p' behavior
Browse files Browse the repository at this point in the history
We can't provide an actual pointer, so this just encodes the
identity hashcode as a long.
  • Loading branch information
headius committed Nov 2, 2023
1 parent 931c4fc commit 956f8cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
38 changes: 38 additions & 0 deletions core/src/main/java/org/jruby/util/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,44 @@ public void encode(Ruby runtime, IRubyObject o, ByteList result){
};
converters['q' + BE] = tmp;
if (Platform.BIT_WIDTH == 64) converters['j' + BE] = tmp;

// pointer; we can't provide a real pointer, so we just use identity hashcode
tmp = new QuadConverter(8) {
@Override
public IRubyObject decode(Ruby runtime, ByteBuffer format) {
return runtime.getNil();
}

@Override
public void encode(Ruby runtime, IRubyObject from, ByteList result) {
if (from.isNil()) {
encodeLongBigEndian(result, 0);
} else {
encodeLongBigEndian(result, System.identityHashCode(from));
}
}
};

converters['p'] = tmp;

// pointer; we can't provide a real pointer, so we just use identity hashcode
tmp = new QuadConverter(8) {
@Override
public IRubyObject decode(Ruby runtime, ByteBuffer format) {
return runtime.getNil();
}

@Override
public void encode(Ruby runtime, IRubyObject from, ByteList result) {
if (from.isNil()) {
encodeLongBigEndian(result, 0);
} else {
encodeLongBigEndian(result, System.identityHashCode(from.convertToString()));
}
}
};

converters['P'] = tmp;
}

public static int unpackInt_i(ByteBuffer enc) {
Expand Down
4 changes: 0 additions & 4 deletions spec/tags/ruby/core/array/pack/p_tags.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
fails:Array#pack with format 'P' round-trips a string through pack and unpack
fails:Array#pack with format 'P' with nil gives a null pointer
fails:Array#pack with format 'p' round-trips a string through pack and unpack
fails:Array#pack with format 'p' with nil gives a null pointer
fails:Array#pack with format 'P' produces as many bytes as there are in a pointer
fails:Array#pack with format 'p' produces as many bytes as there are in a pointer

0 comments on commit 956f8cb

Please sign in to comment.