Unexpected output ide #1412
Replies: 19 comments
-
Posted at 2020-06-18 by @allObjects @user114097, interesting... because if you have something else going on in the delayed function then just writing to the console, such as changing a value, you would see that showing in the console by the log statements. It for sure gives me something to think about. Btw, what is the output of your 'another example:'? ...because I know that a loop as you implemented holds on to the CPU in regard to JavaScript execution... I hope that is still true. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-18 by d3nd3-o0 output from the 2nd example is :
I expected that 'done' comes first. Because thats the behaviour of JS timeouts. But it output 'nope' which means the timeout callback is fired inbetween the while loop. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-18 by @fanoush
it is called after the while loop finishes. Each syntactically complete line is evaluated separately. And more so if you are just copy pasting it or sending it (which is done line by line too). Maybe if you would save it to flash and then restart, only then maybe it would fire after whole file from flash is done but maybe not even then. I am still a bit surprised why you would expect it to work like you suggest, that would be quite limiting. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-18 by d3nd3-o0 @fanoush ye i think you're right. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-18 by d3nd3-o0 Some more evidence of what i'm talking about.. without console.log..
timeout should be printed below final because ypos would be larger if the setTimeout callback ran 'after' the code was parsed and in idle state.. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by @fanoush You still repeat what it 'should' do. 'should' according to your assumptions which you found to be incorrect already in your first test? Maybe javascript works like you say when it is running as part of webpage/DOM rendering but there is no reason espruino on some device would work in same way. But anyway, unless your code is part of already defined function, you are entering it line by line so it is evaluated line by (syntactically complete) line. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by d3nd3-o0
If its not a javascript standard then we can forget about it. I thought it might had been a standard cos it affects the flow of code and thus the output of scripts if one were to copy paste from different environments. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by @fanoush
Did you try to save it to flash and reboot? Did you try to define it inside of function? Did you try it e.g. in node.js console? Can you point me to 'javascript standard' that would backup your expectations? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by d3nd3-o0 Tested on node.js. Outputs 'as' i expected:
https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf#page=169
|
I will try this later. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by d3nd3-o0 Im thinking the reasoning behind not having this compliance could be related to accuracy of scheduling? If the hardware execution is slow, it will make the timers less accurate and suspended too far into the future? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by Raik Hi, Here is your first example with added timestamps:
Output:
You can see that nope was written earlier than the yeps. Now let's wrap this in a function and call the function to execute it as a single block:
Output:
You see that the nope now is being executed last, even after printing the result of the function. Maybe you can run your other tests in a similar fashion and it will clear things up for you. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by d3nd3-o0 Some good news :) EDIT: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by d3nd3-o0 Dude, i think your solution valid actually. I think the right hand side of the ide should be automatically wrapped in a function just like you did, to preserve the scheduled tasks behaviour :D. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by @fanoush
Sorry but there is nothing about setTimeout with zero parameter or interactive console and how it handles copy pasted unstructured code or anything that would map it directly to the detailed ordering of evaluation you expect in your example. Your link and the text you pasted is completely irrelevant to our discussion. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by @fanoush
That would be pretty pointless and even could cause lot of memory and performance related issues if it would be too large. Just write it yourself in your code if you want it like that. BTW I am amazed you still use words like 'compliance', 'fixable' and 'limitation' for such non issue. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by d3nd3-o0 FYI: espruino/EspruinoTools#124
Well, it can be a big deal for some users who are not familiar or technical. And want 1:1 behaviour in the ide emulator without having to dig around. It depends on their initial expectation when using the ide. Mine was clearly wrong. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by @fanoush
I'd say that what you want is unreasonable and that it is a bad habit to think and write code like that - expecting 1:1 behavior on something that can be basically random. The sooner 'users who are not familiar or technical' hit issues like this the better so they can learn and fix their code so it is more reliable. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-19 by @allObjects @user114097, Works as expected... if you know what is going on... EDIT: missed somehow @raik 's post... so keeping goin is TL;DR, except checking out the linked conversation in the bottom of the post... All this excitement for just nothing other than that the (Chrome) browser console (and nodeJS console) behave a little bit different than the Espruino console. Btw, ECMAScfript / ECMA-262 / ES # makes no specification about console implementation. In other words, your statement If its not a javascript standard then we can forget about it. is a bit off-track. I want to tackle this difference first and then move on to a more essential thing... The Espruino console and related REPL work line by line - just as @fanoush pointed out - event though you may have pasted all seven (7) code lines in post #1 at once, where as the browser console - if you copy it all at once - behaves as expected. The (Chrome) browser console works like uploading of JavaScript in a Web page: Wait until all code that is between the <script> and </script> tag is received, then it is compiled into byte code, and then it is executed. For understandable memory reason and because Espruino interprets from source and does not compile and execute from byte code, it does this line wise - more precisely - statement wise execution. Now you are asking yourself what happens when I upload this code onto Espruino... This brings me to my almost most famous and over and over repeated statement in tis forum: Do not put active stuff - other than function / prototype / class definitions into code level 0 or root level. ...since it is just the same as pasting that very same code into the console. Why? The upload uses the very same REPL 'channel' to get the code onto the Espruino board where the Espruino JavaScript interpreter receives it and executes it line by line. And now my citation continues: *** Always put your code into functions and activate the execution in the While you develop and upload over and over this is a little bit a pain, so add as last line something that triggers As a side note why you do not see the uploading of the code in the console? ...because the echo is turned off before the upload and turned back on after it. The code then looks like this:
With that code you get what you expect...
Happy Espruino coding! - ao PS: @user114097, more? See this conversation: simple explanation how to save code that espruino run on start?. - I guess it is my most referenced one. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-06-22 by @gfwilliams This might help as well: http://www.espruino.com/Saving But basically Espruino is designed for devices with limited RAM. As such, by allowing it to execute code as it is uploaded it is able to fit in twice as much code (it's not having to store the text version of what it is running, as well as the 'interpreted' version). Your suggestion for the EspruinoTools change effectively not only negates the efficiency but makes it 50% worse: you now have to store the text version, interpreted giant function, and the result of executing that function. If you want the behaviour you expect, all you have to do is flip the IDE over to 'save to flash' and you're sorted. When saving to flash Espruino can execute direct from flash so it's not having to use the RAM - but this isn't an option for all devices. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2020-06-18 by d3nd3-o0
To my understanding of javascript, asynchronous-like behaviour from timeout is meant to run when execution stack is empty. I expect output with 'nope' at the bottom of the list. Why is this happening? Im running in the 'web ide' with emulator mode. Tested on my bangle watch connected to the ide too, same.
OUTPUT:
another example :
Beta Was this translation helpful? Give feedback.
All reactions