3 ms·
As smart as Mike Pall is, it isn't just that he's some crazy genius. Lua lacks a mutable prototype chain which can really spoil javascript performance and optim
by djrenren 11y ago
As smart as Mike Pall is, it isn't just that he's some crazy genius. Lua lacks a mutable prototype chain which can really spoil javascript performance and optimizability.
As far as I can tell you can't something like:
Array.prototype[0] = 5;
and make [] the same as [5].
There's a lot of edge cases in javascript that make its optimization much more complicated than lua's.
- Twisol 11y agoYou can create a prototype chain in Lua by associating an object with a metatable that has an __index metamethod. If the table that __index references has a metatable itself, the chain continues. http://www.lua.org/pil/13.4.1.html http://www.lua.org/pil/13.4.1.html Importantly, `tbl[key]` only consults `__index` if `key` isn't actually a key in `tbl`, so keys in derived objects override keys up the prototype chain.
- david-given 11y agoAlso in Lua you can change the prototype of constructed objects, by just calling setmetatable() on them again; while in Javascript you can't --- once the object's been constructed, that's it. http://stackoverflow.com/questions/7015693/how-to-set-the-prototype-of-a-javascript-object-that-has-already-been-instantiat http://stackoverflow.com/questions/7015693/how-to-set-the-pr... I ran into this a little while back while trying to port some Lua code to Javascript. It was very annoying.
- masklinn 11y ago> while in Javascript you can't --- once the object's been constructed, that's it. ES2015 standardised the read/write __proto__ attribute (which most browsers had implemented for a long time), and added a #setPrototypeOf call. The first answer of your link notes exactly that.
- david-given 11y agoIt's added as an optional feature, as the first sentence of that answer describes: > ...the answer below is no longer accurate. _proto_ is being added to ecmascript6 as "normative optional" which means it isn't required to be implemented... It can't always be polyfilled, either, which makes the feature effectively useless. I'm not even sure why they added it as such a half-hearted feature to ES6; I suspect politics (Brendon Eich appears to have been dead set against having mutable protos for years).
- masklinn 11y ago> It's added as an optional feature Fuck's sake mate, Object.setPrototypeOf is not optional (and __proto__ is only optional for non-browser environments). > It can't always be polyfilled, either, which makes the feature effectively useless. That's breathtaking dishonesty.
- mikemike 11y agoJust in case, anyone has somehow gotten to the conclusion that Lua's semantics are 'simple', they should closely inspect this example and try to figure out through which contortions the VM has to go to make this work: local t = setmetatable({}, { __index = pcall, __newindex = rawset, __call = function(t, i) t[i] = 42 end, }) for i=1,100 do assert(t[i] == true and rawget(t, i) == 42) end [LuaJIT has no problems with this code and turns it into 8 machine code instructions for the actual loop.] Anyway ... This permanent excuse of JavaScript proponents that it has more complex semantics, which somehow prevents it from being made fast, is getting old. There are no insurmountable obstacles to make JavaScript fast -- it just takes more effort! And they dug this hole themselves, by not cleaning up the language and allowing new complicated features into the language. Well ...
- daurnimator 11y agoArray.prototype[0] = 5; and make [] the same as [5]. Huh? I don't know what you mean by this. In lua you can easily replicate javascript's prototype inheritance.
- usrusr 11y agoBeing able to build it on top of the language and having it right inside basic language elements as an always-on possibility are quite different things. The former allows straight forward optimization of the simple case, simply accepting the performance hit of the stuff built on top (which still might benefit from some of those optimizations), while the latter forces the optimizer to invest a large effort into distinguishing between the simple case and the exceptional, and then there still needs to be an always-on fallback path from simple to exotic.
- groovy2shoes 11y ago"Prototype inheritance" is a fundamental building block in Lua, enabled via the metatable mechanism. It's by far the most common way to do OOP in Lua. I'd say it is, in fact, built right in to the basic language.