Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct Type Introspection crashing #20

Closed
kevinend opened this issue Feb 10, 2017 · 2 comments
Closed

Struct Type Introspection crashing #20

kevinend opened this issue Feb 10, 2017 · 2 comments

Comments

@kevinend
Copy link

kevinend commented Feb 10, 2017

Hey Bill,

I was trying to use the type introspection features of the language on a struct, namely trying to access the field names composing the struct. When I went to print the values the program crashed. Subsequent runs fared similarly. I figured it might just be something with the name field of the Type_Info_Member struct so I tried looking up the offset and was receiving garbage values as well.

Not sure if the type introspection is still in a working state or not, but I figured I should let you know.

Below is my actual code in case I wasn't accessing the data appropriately.

#import "fmt.odin";
#import "mem.odin";

NewType :: struct {

	field1: int,
	field2: int,
	field3: int,
}

main :: proc() {

	newtype := NewType {
		field1 = 10,
		field2 = 11,
		field3 = 12,
	};

	newtype_info := cast(^Type_Info.Struct) type_info_of_val( newtype );
	newtype_members := newtype_info^.fields;

	fmt.println( "", newtype_members[0].name );   // crash
	fmt.println( "", newtype_members[0].offset ); // garbage value when i comment out the above line

}
@kevinend
Copy link
Author

I came back to the preload file again and noticed there are other types like map and dynamic_array which currently don't appear to be part of the language yet. Perhaps I just jumped the gun and the struct type info hadn't been properly implemented up to this point v0.0.6. If that is the case I apologize for opening an issue Bill.

@gingerBill
Copy link
Member

First, when casting a union to its subtype/tag, do:

newtype_info, ok := cast(^Type_Info.Struct) type_info_of_val(newtype);

This will return a boolean to check if it's valid. cast doesn't do any other checks (maybe I should make a raw_cast ).

Second, the kind of that type isn't a Type_Info.Struct but a Type_Info.Named. This means you want to get the base type of that Named type. This can be done manually or through the procedure type_info_base.

Third, check fmt.odin for a good exampe of what I mean.

Note: You don't need to do a manual dereference to get a struct's field as the language does an automatic one indirect dereference in those cases (as well as in the using cases). This is help with refactoring, and logic.

x: ^Foo;
// These two are identical in outputted code
x.^bar = 123;
x.bar = 123;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants