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
Functions for operating on Maps from C #725
Conversation
This patch is broken/insufficient. It might leak UNDEFINED_VAL and also
does not handle/provide enough error handling.
|
Added something to handle UNDEFINED_VAL. I suppose the major error case is trying to use an invalid type of object as a map key? Preventing that shouldn't be too difficult, I just need to establish which types are allowed. |
Although for what it's worth, I can't see anything in the Wren VM which does error checking like that... so does it even enforce what can be used as a key? |
Lines 50 to 59 in 6ab4abe
|
Ah, pardon me. I guess I can call |
Seem that function isn't exposed to wren_vm.h. is it okay to import wren_primitive.h just for this function call? I don't want to duplicate it, in case the criteria for valid keys changes in the future. |
Added in the key-type validation, which hopefully helps cover the potential error scenarios. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nitpicks.
I think it's a nice addition. |
The fact that all these functions doesn't return an error code at minimum
frighten me.
|
By "all these functions", do you mean (1) the new ones introduced by this PR, or (2) the whole Wren<->C api? For (1), the functions I've added here are based on the existing API, which is why they have limited fail-modes. If your concern is (2), there's a high amount of assumption built into the FFI that you know what you are doing, or are performing your own verification, which I believe is done for performance purposes. Trying to address those shortcoming would be out of scope for this PR. |
Yes you're correct @avivbeeri, this PR is consistent with the code base as is, and that's reasonable. |
@avivbeeri I've accidentally duplicated your effort in #760 🤦 /* wren.h */
// Returns the number of entries in the map stored in [slot].
int wrenGetMapCount(WrenVM* vm, int slot); /* wren_vm.c */
int wrenGetMapCount(WrenVM* vm, int slot)
{
validateApiSlot(vm, slot);
ASSERT(IS_MAP(vm->apiStack[slot]), "Slot must hold a map.");
ObjMap* map = AS_MAP(vm->apiStack[slot]);
return map->count;
} The reason I'm suggesting it because there is already Aside from that, kudos for great work 🏆 |
test/api/maps.h
Outdated
|
||
WrenForeignMethodFn mapsBindMethod(const char* signature); | ||
void mapBindClass( | ||
const char* className, WrenForeignClassMethods* methods); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are missing a new line here.
@avivbeeri I've rebased your work on top of current master and added It would be awesome if you could take a look and update this PR accordingly. |
I figured this would need some maintenance once 0.3.0 dropped. I'll get onto it when I have a moment, which will likely be this coming weekend. |
they got lost in the rebase by me
Am I missing something or is it not possible to iterate keys of a map with the implemented functionality? This would be important e.g. for serialization. |
At the time I wrote this, you were assumed to know which keys you wanted to extract. The side-step solution is to pass |
I had some more functions for iteration coming, I just merged the PR with the foundation :) |
@ruby0x1 I'm highly interested in this. Can you give me a hint of how the api will look like. My example from above won't work from what I've learned by now. |
I found myself wanting to pass structured data from Wren to C, but I didn't want to define a foreign class specifically to do that, so I thought having access to Maps from the C slot API made sense.