-
Notifications
You must be signed in to change notification settings - Fork 72
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
Structures #382
Comments
Similar to #234 though this one is more about the data structure side of things. I like the use of tags to indicate type however I feel this could get confusing - differentiating between whether One aspect I think has a lot of merit in this suggestion is the proper support for arrays within structures. Enumerations that contain array-like syntax have confused people for years, me included! |
Some thoughts:
|
I've thought about this before, I don't think it's really necessary as you can replicate this with arrays an enums. I don't think it's a bad idea though. It could make stuff less confusing and more straight-forward. |
Methodmap syntax from Sourcepawn 1.7: https://wiki.alliedmods.net/SourcePawn_Transitional_Syntax#Methodmaps We can take some inspiration from this maybe? |
That's more calling functions than data storage, which is more what the issue @Southclaws linked to is about than this one. |
Nice to hear your thoughts. // Allocates in the heap the struct info
struct Vehicle
{
Id,
Name[32]
}
// Allocates in the heap sizeof(Vehicle) cells, keeps the pointer to this instance in a vector and assigns the index to the pawn cell
new Vehicle: veh;
// The value of the veh variable is actually the index of that table/vector whose value is the pointer to the memory allocated for this struct instance
printf("%d", veh); // 0
new Vehicle: veh2;
printf("%d", veh2); // 1
// But when the dot operator is used, the compiler will automatically know that what the programmer is asking for is the value pointed by the pointer variable
printf("%d - %s", veh.Id, veh.Name); // 400 - Landstalker
About the declaring syntax, I agree that it might be difficult to implement without breaking something in its existing usage. struct Color
{
R,
G,
B,
A
}
new Color* color; About the overall usefulness, I think that this is much more than just changing [ ] with a dot, this is a progress in the life of pawn. |
Pawn resets the heap after every call to amx_Exec, i.e. every public function. I've tried very very hard to find a way around this, even resorting to exploiting VM bugs present only in the exact version used in SA:MP (and thus not even possible in newer versions) - it didn't work. Basically, heap allocations are ephemeral. |
Check y_malloc - that's where I was playing with this. If you find a way to overcome this issue PLEASE do tell me - even compiler support isn't really going to add anything, since I was already using assembly. The only other solution I could think of was augmenting evey single public function call with code to save and restore the heap pointer if it wasn't in the right place. I didn't because that is silly overhead for something that might not even be used (determining the requirement for it is, however, one place where compiler support would be useful). But what you are suggesting essentially amounts to manual memory management, which is the one thing pawn has never had as it complicates code massively. |
I'm also wondering how your indexing system would even work, assuming the heap allocation/free issues could be solved. If there are two structs of different sizes, and 10 of each are allocated in random orders, do the indexes continue over both? Does each type get their own indexing? How are those indexes mapped to real memory? Why aren't you just using real memory locations in the first place (as with references and arrays)? |
My knowledge about how is the AMX abstract machine implemented is extremely limited, so I didn't know about this issue with the heap memory. Some years ago I tried to implement a Pawn wrapper for some WinAPI functions (https://forum.sa-mp.com/showthread.php?t=286543), mainly for fun because I was still young and learning, but I never went into understanding this abstract machine's source code. About the idea of the indexing system, I think that if each struct type has its own indexing, it is better than exposing the actual pointer, because the average Pawn scripter usually does not know about memory management, and it is more intuitive for him to have the elements as if they were in an array. I am thinking about a solution for allocation, but at the moment I still don't have one. |
My point with the indexes was even if they are addresses, that is just an implementation detail - it doesn't matter to any end-users what the value is. Reference parameters ( |
Yes that is true and I didn't think well about it. |
If enum arrays can already store some kind of emulated structures, then I don't think structure memory allocation would be that different. I think the problem was brought up with your table/vector and index proposal, which doesn't seem to be very convenient since real addresses and existing memory allocation methods could be used instead. |
Issue description:
My suggestion is about implementing compiler support for structs. I am aware that this way, any new code using structs will not be compilable with the default PAWN compiler included in the SA-MP Server package, but I believe it is worth it.
Suggested PAWN syntax:
I suggest to make its declaration syntax similar to the PAWN enum syntax:
This would be equivalent to this C code:
As for variable declaration, PAWN does actually not have types, but it has tags; hence this could syntactically work well with the already existing tag syntax:
A viable syntax for accessing a struct's inner variables could be the C-like syntax:
Finally, a very cool feature would be an array-initialization-like syntax for quicker struct initialization:
It could also work well with array-initialization:
Advantages
The usual PAWN way of storing a structure of data is using a 2D array with an enum.
This way:
The struct instead:
The text was updated successfully, but these errors were encountered: