Inquiry Regarding an Issue Found in an Interpreter Implementation #178799
Replies: 1 comment
-
|
Hi nieuwmijnleven, It’s not a bug. In Crafting Interpreters, slot 0 is reserved in every call frame. When a method runs, slot 0 holds the receiver (this). When running top-level code or a normal function, slot 0 is just a placeholder (usually nil). This is done to keep the VM simple and consistent. Every frame starts at the same layout, so the bytecode and opcodes don’t need special cases to handle methods versus functions. The compiler ensures that this can only be accessed inside methods, so even though slot 0 always exists, it’s never visible from normal Lox code. The confusion comes from looking at the VM’s internal stack layout and assuming that what exists internally is also part of the language semantics. The presence of slot 0 in the global frame is just an implementation detail, not something that affects program behavior. So the interpreter behaves as intended: slot 0 is always reserved for uniformity, but this is only meaningful inside class methods. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
The reason I am writing post is to ask your opinion about an issue I have encountered.
While studying the interpreter language Lox, which is implemented in the book Crafting Interpreters, I discovered what I believe to be a problem.
I found that the interpreter stores the this pointer in slot 0 of every stack including the global stack even when there is no class context. In my view, this is clearly a bug, so I reported it to the author.
However, instead of focusing on the fact that the this pointer is stored even in the global stack’s slot 0, the author seemed more interested in proving that this behavior was acceptable by allowing access to the this pointer from the global stack to demonstrate the behavior, insisting that it was not a bug.
Out of respect for the author’s opinion, I refrained from replying for over a month and thought deeply about it, but I still cannot accept the conclusion that this is not a bug. I also asked about it in a community forum, but the responses I received were similar to the author’s.
may I ask, would you, too, focus on criticizing me for demonstrating the issue by allowing access to the this pointer, rather than on the fact that the this pointer is always being stored in slot 0 of the global stack?
Here is the link to the community discussion:
https://www.reddit.com/r/Compilers/comments/1onis4v/a_misunderstanding_about_a_bug_in_an_interpreter/
munificent/craftinginterpreters#1201
munificent/craftinginterpreters#1202
munificent/craftinginterpreters#1203
If you have the time, I would be deeply grateful for your valuable response.
Thank you in advanced.
Beta Was this translation helpful? Give feedback.
All reactions