8000 py/obj: Add comments explaining the slot index scheme. by jimmo · Pull Request #9373 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

py/obj: Add comments explaining the slot index scheme. #9373

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

Closed
wants to merge 1 commit into from

Conversation

jimmo
Copy link
Member
@jimmo jimmo commented Sep 20, 2022

Based on suggestions from @stinos in #8813 (comment)

@jimmo
Copy link
Member Author
jimmo commented Sep 21, 2022

@stinos I have also updated the wiki with some notes on using these macros. https://github.com/micropython/micropython/wiki/Build-Troubleshooting

I'm curious, what was the scenario where you needed to use MP_OBJ_TYPE_SET_SLOT (or was that just something you noticed). I was wondering whether I needed to add a MP_OBJ_TYPE_UPDATE_SLOT that doesn't require you to pass the index, i.e. it re-uses the existing index.

@codecov-commenter
Copy link

Codecov Report

Merging #9373 (ffafb1f) into master (13c4470) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #9373   +/-   ##
=======================================
  Coverage   98.35%   98.35%           
=======================================
  Files         156      156           
  Lines       20506    20506           
=======================================
  Hits        20169    20169           
  Misses        337      337           
Impacted Files Coverage Δ
py/obj.h 100.00% <ø> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@stinos
Copy link
Contributor
stinos commented Sep 21, 2022

updated the wiki

looks good!

what was the scenario where you needed to use MP_OBJ_TYPE_SET_SLOT

I'm building types dynamically, sort of like in mp_obj_new_type, see here.

I was wondering whether I needed to add a MP_OBJ_TYPE_UPDATE_SLOT

Yeah I almost added it myself but then reasoned it might not see much usage. On the other hand, having to repeat the index is a pretty good recipe for bugs..

py/obj.h Outdated
#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f)
#define MP_OBJ_TYPE_GET_SLOT(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1])
#define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(MP_OBJ_TYPE_HAS_SLOT(t, f) ? MP_OBJ_TYPE_GET_SLOT(t, f) : NULL))
#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[(t)->slot_index_##f - 1] = (void *)v)
#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[n] = (void *)v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it matters functionally but for consistency use slots[(n)] ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good to always wrap macro args in parenthesis.

Also, does this make any functional change? The macro arg n is now evaluated twice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< 8000 /div>

It is always invoked with a literal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. But still worth wrapping n in parenthesis, if only for consistency with other macros.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (and rebased)

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Oct 25, 2022
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
@dpgeorge
Copy link
Member

Thanks, merged in d75c7e8

@dpgeorge dpgeorge closed this Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0