kuroko-lang / kuroko

Dialect of Python with explicit variable declaration and block scoping, with a lightweight and easy-to-embed bytecode compiler and interpreter.
https://kuroko-lang.github.io/
MIT License
427 stars 25 forks source link

Globals should be bound to `function`s, not `codeobject`s #26

Closed klange closed 2 years ago

klange commented 2 years ago

Currently, we are attaching globals tables to codeobjects (KrkCodeObject), as a pointer to an instance object (KrkInstance*). Thus far, this has been fine, as functions (KrkClosure) are only ever built from codeobjects in places where the difference is not visible, but if we want to be able to instantiate functions from codeobjects as one can do in Python, then the ownership of the global context needs to move to the function.

Further, the use of a pointer to an instance is not the best approach here, as it does not allow us to meaningfully use a dict as the globals of a function - all of the current global reference code assumes we are using an instance's fields, but the value table in a dict is not its fields - it is a separate table. We could use a pointer to the relevant table, but this would pose challenges for garbage collection. More investigation should be done here.

klange commented 2 years ago

Though:

This allows garbage collection to still scan the table owner, while allowing different offsets for the table within the object.