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

Berry add reuse of methods for interface-like code reuse #21500

Merged

Conversation

s-hadinger
Copy link
Collaborator

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 method do_stuff(x). You can define a class B and define a method (with same or different name) that copies the implementation from class A.

class A
    def do_stuff(x)
        # do something
    end

    static def do_static_stuff(x)
        # do something static
    end
end

class B
    def do_stuff = A.do_stuff
    static def do_static_stuff = A.do_static_stuff
end

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:

  • the function has no sub-function (most common case), nproto is 0 and ptab normally contains NULL. We stored the parent class in ptab which is free storage (no code increase)
  • the function has sub-functions, nproto is not 0, we append the class to the ptab array (it costs 4 more bytes) but does not interfere with the runtime

Solidification 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:

  • The pull request is done against the latest development branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR and the code change compiles without warnings
  • The code change is tested and works with Tasmota core ESP8266 V.2.7.6
  • The code change is tested and works with Tasmota core ESP32 V.3.0.0
  • I accept the CLA.

NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass

@s-hadinger s-hadinger merged commit e56f6a1 into arendst:development May 26, 2024
59 checks passed
s-hadinger added a commit to s-hadinger/Tasmota that referenced this pull request Aug 29, 2024
hawa-lc4 pushed a commit to hawa-lc4/Tasmota-dev that referenced this pull request Sep 16, 2024
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

Successfully merging this pull request may close these issues.

1 participant