sequence of code inside function changed (?) #418
Replies: 11 comments
-
Posted at 2015-05-21 by @gfwilliams Thanks, but I'm not sure that would have ever worked - Espruino executes functions top-to-bottom, so the normal JavaScript 'function hoisting' wouldn't work. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-21 by DrAzzy Wait, what is that supposed to do? I think I may be missing something about JS here. How do function statements inside another function call work in js? Does it make a global, or does it do something else? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-21 by @gfwilliams The functions aren't global, but in normal JS it's as if they were defined right at the top of the function. You can use them before they are defined. Because Espruino doesn't precompile, that doesn't happen though :( |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-21 by DrAzzy That is some freaky shit. I did not know JS let you do that. Is there a reason for allowing such deviant syntax? That seems to break all sorts of normal conventions... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-22 by @allObjects ...js is first parsed and jit'ed, and only after it made it syntactically through, it starts to execute... at least what my gut had do accept... and yes, I had a bit a similar idea, because when you then look in (some) browser debugger walking step by step, you see the vars already defined even though you r programm pointer is not there yet / has not passed the var blaBla =... yet. I saw this pattern quite frequently in some code... and it did not feel right to me, becuase it assumes that walking through on load happens, which does not happen in Espruino. My other half gut - no a bit Espruino trained - also tells me when done this way, the function is created all over again... since Espruino runs/interprets of the source... @gorden is frank enough that I ran into the woods again... (would not be the first time) - but every time I learn something. Over time I hope to stay better in the middle of the road... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-22 by @gfwilliams
I often wonder that about a lot of JS :)
I think it was a side-effect of turning it into Bytecode first as @allObjects says, but then they thought it was handy so they left it in... At some point I will implement this properly, there are some nice side-effects when applied to Espruino:
The code above would have had the function b parsed beforehand, so execution would be faster, and if you called it twice it'd be able to use the same copy of |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-22 by @allObjects Assumed in the top / global level: A) What is the difference between line 1 and 2 below?
B) With A) answered, what is the difference between the two blocks below?
C):
I assume you mean: called it twice I expect that assigning a function to a variable
...is that not a contradiction to Espruino executes from source code? Because what is created on parsing? If it is more than some 'isParsed' flag that is stored within the 'variable/function' value reference store, then it goes towards JIT compiling and uses unpredictable amounts of memory. And if it is just a flag, what would be the use of it? ...so far I did not find the time - or guts(?) - yet to dive into the interpreter's gut (source code). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-22 by @gfwilliams There is no difference between the two types at the moment... But:
What happens now
Result:
What will happen in a later version of Espruino
Result:
Does that make more sense? If you used |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-23 by @allObjects ...@gordon, I gues I got your (Espruino) 'byte' code... I like it a lot: a hybrid between jit and running 'on' the source... I always wondered how running 'solely and purely' on the source still can perform, because, for example, in an if-then at the end, the else - if present - including all its 'content' would have to be parsed, in order to find the end... that would perform 'poorly'... so much for '...purely'. Ever since you posted the pseudo code above, my back burner (mind) explored the potential VM construct you are using. Now, I guess, I know also why code uses up variables (variable space) - not just variables for variables and function entry points (which is just another type of variable) - but also unconditioned code segments: the code is parsed and 'code segment variables' are created (in a special variable space in a three structure), on which the VM then 'executes'. That's the way I would be able to handle quickly control structures. Separation of the souce into control structure and linear source fragments. Because you mention '...later version of Espruino', I wonder about proper coding for now: less nesting (of function declarations) and all in global (root) level of code gives you faster exection? For example:
is more efficient than
This could/would(? - or at least partially) explain that the .bind() you implemented 'internally' is faster (AND variable space saving) than the 'self-built' construct with an anonymous function and - most of the time needed - _this or that variable holding on to the this (for proper resolution in the closure / deferred execution context, which has a different context / this...). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-26 by @gfwilliams Ahh - well, in the root scope:
is slightly less efficient than this:
As the first has to also define a named variable for the function. However,
Is definitely more efficient than:
as the function only exists in memory once. In that last example, it exists in Again, that's something that could potentially be changed in the future though - it could just re-use the same characters. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-26 by @allObjects Ic. thanks. ...the details create the excitement... ;-) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-05-21 by JumJum
Just updated to actual firmware and got problems (function undefined)
I'm pretty sure it worked fine with older version of firmware.
After changing sequence it worked fine as before
Beta Was this translation helpful? Give feedback.
All reactions