rbartlensky / Lua-interpreter

A Lua interpreter in Rust
5 stars 2 forks source link

Luatable indexing #10

Closed rbartlensky closed 5 years ago

rbartlensky commented 5 years ago

LuaTables can now be indexed by any LuaVal. This is necessary because global variables are all kept in an _ENV variable which is a global table. For instance, x = 2 translates to _ENV["x"] = 2. This is also why this PR introduces the LuaString type.

More information about why I forked the gc crate can be found here.

ltratt commented 5 years ago

Just because Lua has the global _ENV var doesn't mean that we have to store things in a table, which is likely to be very slow (register lookups are a lot faster than hash-based table lookups!). We can have a special table type that dynamically looks up the value of variables, for example, amongst other tricks for dealing with this.

rbartlensky commented 5 years ago

This suggests that _ENV has to work just like a table. I am going to think about this in more detail before raising a PR for the _ENV implementation.

ltratt commented 5 years ago

"Has to work like a table" doesn't mean "has to be a table". So long as the Lua programmer can't tell the difference, you can optimise things under the bonnet in whatever way you want.

rbartlensky commented 5 years ago

Ready for another review.

ltratt commented 5 years ago

Please squash.

rbartlensky commented 5 years ago

Squashed!