Berry add reuse of methods for interface-like code reuse #21500
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This is a new experimental feature for Berry.
Reason behind it: Matter use classes and inheritance, unfortunately some methods need to be duplicated between several sub-classes. Berry does not have multiple-inheritance nor Java-like interfaces. This features aims at sharing the same methods between multiple classes that are not in the same inheritance line.
The syntax is simple. Let's say you have
class A
with methoddo_stuff(x)
. You can define aclass B
and define a method (with same or different name) that copies the implementation fromclass A
.Implementation notes:
Storing of parent class
(in #21490) each Berry method now stored its original parent class. This allows solidification to recognize that the method is borrowed from another class, and does not output it again. The
C
name is then matched to the correct class.The parent class is stored in
ptab
. Two cases are possible:nproto
is0
andptab
normally contains NULL. We stored the parent class inptab
which is free storage (no code increase)nproto
is not0
, we append the class to theptab
array (it costs 4 more bytes) but does not interfere with the runtimeSolidification is changed accordingly
Change in Berry VM
It was necessary to ass
SETMET
VM opcode to be able to store dynamically a new method to a class static variable. This is done only in code initialization of the class, but never happens in normal code.Checklist:
NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass