[foreign] RFR: Add a toString method to struct impl classes.

Jorn Vernee jbvernee at xs4all.nl
Fri Dec 14 15:07:32 UTC 2018


Maurizio Cimadamore schreef op 2018-12-14 15:44:
> Looks good - an alternative would be to do that reflectively, or to
> have our own toString bootstrap (we already have a few of them).

Reflectively could work, but we're spinning bytecode any ways :)

As far as the custom bootstrap goes; I just went with 
StringConcatFactory since it gets the job done. A custom bootstrap would 
have to be in the public package, since it has to be accessible from the 
callsite which is in user land, right?

> Curious - what happens if you also enable it for unions? Granted, some
> of the values might be garbage, but it should work?

I haven't tried this tbh. The current code doesn't just straight up 
print the value, it boxes it into a Java carrier first, so with more 
complex types like pointers or nested structs that might be a problem 
with violated invariants in the boxing code. Especially if we start 
having custom LayoutTypes which could do even more invariant checking.

Since accessing a union member that is not currently active in C/C++ is 
undefined behaviour it seemed better to stay away from that.

Jorn

> Maurizio
> 
> On 14/12/2018 14:27, Jorn Vernee wrote:
>> Hi,
>> 
>> During porting the binder to Windows I wrote a bunch of debugging 
>> code. I thought this might be useful to have in the future, and I'd 
>> like to handle these so I can remove them from my other local changes 
>> (less stuff to maintain), so I'm creating this RFR. There are 3 
>> subjects that I've split into 3 patches; This is the first one.
>> 
>> 1.) toString method generation for Structs. This adds generation of a 
>> toString method to StructImplGenerator, which uses the struct's 
>> getters to output the values of the struct e.g.
>> 
>> @NativeStruct("[i32(get=one$get)(set=one$set)i32(get=two$get)(set=two$set)]")
>>     public interface MyStruct extends Struct<MyStruct> {
>>         int one$get();
>>         void one$set(int i);
>> 
>>         int two$get();
>>         void two$set(int i);
>>     }
>> 
>>     public static void main(String[] args) throws 
>> IllegalAccessException {
>>         try(var scope = Scope.newNativeScope()) {
>>             MyStruct s = scope.allocateStruct(MyStruct.class);
>> 
>>             s.one$set(10);
>>             s.two$set(20);
>> 
>>             System.out.println(s);
>>         }
>>     }
>> 
>> Will output:
>> 
>>     Main$MyStruct{ one$get=10, two$get=20 }
>> 
>> Webrev: 
>> http://cr.openjdk.java.net/~jvernee/panama/webrevs/structtostring/webrev.01/
>> 
>> Please let me know what you think and if you want to have this in the 
>> repo, otherwise I will just throw it away.
>> 
>> Cheers,
>> Jorn


More information about the panama-dev mailing list