tag:blogger.com,1999:blog-79508403534840963782024-03-09T18:45:41.493-08:00Scott DowneScotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.comBlogger62125tag:blogger.com,1999:blog-7950840353484096378.post-73021955218116914292010-06-05T15:40:00.000-07:002010-06-05T15:50:16.209-07:00Moving to wordpress...I'm in the middle of moving my blog over to <a href="http://scottdowne.wordpress.com">http://scottdowne.wordpress.com/</a>. I will be spending the next few weeks fiddling with the smaller details of design, the sort of detail that can get someone like me wrapped up for days doing, but not accomplishing anything :).<br /><br />I was impressed that I was able to just export all my old posts over to wordpress, by clicking a "magic button", literally, that's what it was labeled as. It's all automated, and a really painless process.<br /><br />The reason for the move was for more robust visitor tracking.<br /><br />I have some pretty cool ideas in mind for my new blog, as far as design goes, so we'll see how far wordpress allows me to hack it. I would like to get some <a href="http://processingjs.org/">processing.js</a> sketches in my design, maybe in the background, or the banner, or both! if it's possible. If not, I might consider writing something myself, and hosting it myself, but I'm not there yet.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com3tag:blogger.com,1999:blog-7950840353484096378.post-56336548977038084282010-05-21T19:46:00.001-07:002010-05-21T19:46:54.301-07:00JavaScript is not JavaJust how deep does this rabbit hole go?Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-63810874230032320262010-05-15T05:05:00.000-07:002010-05-15T05:28:55.828-07:00Make one to throw it awayThe title of this blog becomes more and more true the more I work in open source, but if you don't know what the title means, let me describe to you something about open source before i tell you what it means.<br /><br />Open source development differs from school, and I can only assume, propriety jobs like a bank because in open source, if something has been written before, why are you going to write it again? At school, you spend so much time writing code that has been written hundreds of times before, by hundreds of students before you, these assignments are possible, have been proven, and if they were not possible, and they asked you to do it, you would be pretty ticket off spending days trying to figure it out, and you would probably think you accomplished nothing, and wasted time. You are wrong. When you are working on something that has never been done before, you have to try different things, there is no one to tell you what works, only ideas, because no one has done it. You have to think for yourself, make decisions for yourself, and be prepared to be wrong most of the time, until you get it right. You can be wrong hundreds of times, but only be right once... per problem that is.<br /><br />To "make one to throw it away" means, before you get code that is working, and doing what you want, you have to make one or more versions that fail. This isn't too bad when dealing with a couple hundred lines of code, but something like 8000+ well, that bring me to my next story.<br /><br />I've been spending the last week, trying to figure out a way to remove the final with() function call from <a href="http://processingjs.org/">Processing.js</a>. In order to remove the with() function call, I had to rewrite a lot of the code... 8000+ lines of code, which also meant I had to understand (more or less) 8000 lines of code. If you are curious, here is the <a href="http://processingjs.org/content/download/processing-js-0.9/processing-0.9.js">source</a> in all it's glory. When you "make one to throw it away", you have just written and thrown away 8000 lines of code... if you think that's a waste of time, you are again wrong. This was 8000 lines more educational for me than rewriting 500 lines. It's simple math, really.<br /><br /><pre><br />lines written * rewrites = productivity score :P<br /></pre>Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-41766612023416597472010-05-06T10:45:00.000-07:002010-05-06T11:09:29.652-07:00Processing.js casting bugI have been working on <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/625-parse-error-with-the-following-code">processing.js bug #625</a>.<br /><br />What's happening is there is some Java like code, that's being converted into JavaScript, so it can be run in a browser, and this bug deals with converting cast like syntax in java, into something usable in JavaScript.<br /><br />JavaScript is typeless, what is important about this is the types between variables don't matter (for the most part) so when I create a variable of an int, I just create a variable, and make it an int, and if I need to know the type of the variable, I can check the type of what's in the variable, which would be an int. Looking at <a href="http://processing.org/reference/append_.html">p5 append()</a>, is what the cast is being used for in this bug.<br /><br />"When using an array of objects, the data returned from the function must be cast to the object array's data type. For example: SomeClass[] items = (SomeClass[]) append(originalArray, element)."<br /><br />In pjs, I don't think the cast is even needed, as append just returns an array, this is the pjs append code:<br /><br /><pre><br /> p.append = function(array, element) {<br /> array[array.length] = element;<br /> return array;<br /> };<br /></pre><br /><br />Maybe p5 (the Java version of processing) has append returning an array list, which would require the cast.<br /><br />Either way, JavaScript does not need the cast.<br /><br />I removed the cast in the example, and it works, but the problem still remains with parsing the cast, as it's valid code. In p5 the cast still needs to be handled by the parser, cannot just remove it. There is a way to do this currently in the parser, but it will only parse out classes inside a list.<br /><br />One thing I could do is add "PVector" to the list of classes, which currently looks like this:<br /><br /><pre><br />var classes = ["int", "float", "boolean", "String", "byte", "double", "long", "ArrayList"];<br /></pre><br /><br />then in this code:<br /><br /><pre><br /> // Remove Casting<br /> aCode = aCode.replace(new RegExp("\\((" + classes.join("|") + ")(\\[\\])*\\)", "g"), "");<br /></pre><br /><br />It would find PVector, and remove the cast, without hurting anything else. Only problem I see in this is we'll have to add each and every pjs class to the list of classes, and if a new class is added to pjs, someone will have to remember to add the class to the list of classes. There must be a more dynamic way to accomplish this, which is what I want.<br /><br />I thought about possibly parsing any instance of \(/w+\) and check that the match in w+ is "typeof Object" or something along those lines. Then I could remove the list of classes all together. Yes, I will try that. Thanks for the help blog.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-76194956381654730322010-05-05T08:28:00.001-07:002010-05-05T08:43:56.491-07:00Building Firefox on FedoraToday I went to set up a working Firefox build environment ON Linux Fedora. It was actually easier than I first felt. When I would run into an error message, I would google it, and get an answer usually saying to add modifications in the config file to pass over, or to include, or change the version of various products. <br /><br />Some of the problems:<br /><ul><br /><li>Something called necko-wifi was not compatible, researching it I found I could just disable it in the mozconfig file by appending a line "ac_add_options --disable-necko-wifi". Solved, next error!</li><br /><li>Something called maemo was causing an error message, and researching that I found a similar solution that didn't work, to append "ac_add_options --with-maemo-version=5", fixed, but this caused other issues.</li><br /><li>I then found out, I could do a search for the missing files Firefox was complaining about using Fedora's "yum search 'name of file'" looking for something that looked like what was missing, and then "yum install 'file you found'" and crossed my fingers, would sometimes take a couple hits and misses before it would build, but after that each and every error was solved in that manner, including maemo.</li><br /></ul><br />I have to say I'm liking Fedora a lot. I also found a neat trick using vim, where you can do ctrl-p while typing a word to finish it, or scroll through words kinda like auto-complete. Linux can pretty much do anything I would need now.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-5414391900720607972010-04-25T17:29:00.001-07:002010-04-25T17:55:23.761-07:00Solution to my last post.In my last <a href="http://scottdowne.blogspot.com/2010/04/copy-multidimensional-array-in-js.html">post</a> I was trying to create a function that receives a multidimensional array inside parameters like (2, 4, 5) but with an infinite amount that could be passed in, and I was having problems with pass by reference, instead of pass by value.<br /><br />I fixed it, and came up with a very simple solution:<br /><pre><br /> p.ArrayList = function() {<br /> var createArrayList = function(args){<br /> var array = new Array();<br /> for (var i = 0; i < args[0]; i++){<br /> array[i] = (args.length !== 1 ? createArrayList(args.slice(1)) : 0 );<br /> }<br /> <br /> return array;<br /> };<br /> return createArrayList(Array.prototype.slice.call(arguments));<br /> };<br /></pre><br />I came across the problem of arguments only acting like an array, but not having access to many array like functions, like slice(). Solution.<br /><pre><br />Array.prototype.slice.call(arguments)<br /></pre><br />Array.prototype.slice gets the slice method from the Array object.<br />call() allows you to call an object's function from the context of another object.<br />and the slice method would be called, using the function definition attached to Array, like it was part of arguments, and just return a copy of the array's values, as an array. :DScotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-172699838288956732010-04-25T07:34:00.000-07:002010-04-25T08:59:21.524-07:00copy a multidimensional array in js.I'm working on ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/587-parraylist-should-work-for-more-than-3-dimensions">#587</a>, for <a href="http://processingjs.org/">processing.js</a>.<br /><br />What I have to do is convert a custom ArrayList function to create a multidimensional array based on parameters passed in. For example:<br /><pre><br />var array = new ArrayList(4, 5, 6);<br /></pre><br />would create a 3D array, similar to:<br /><pre><br />int[][][] array = new int[4][5][6]<br /></pre><br />This is what I've put together:<br /><pre><br /> p.ArrayList = function() {<br /> var array = [],<br /> tempArray = [];<br /><br /> for (var i = 0, aLength = arguments[arguments.length-1]; i < aLength; i++){<br /> array[i] = 0;<br /> }<br /> <br /> for (var i = arguments.length-2; i >= 0; i--){<br /> tempArray = array.slice();<br /> array = [];<br /> for (var j = 0, aLength = arguments[i]; j < aLength; j++){<br /> array[j] = tempArray.slice();<br /> }<br /> }<br /> return array;<br /> };<br /></pre><br />It starts at the back of the arguments, and copies the array recursively until it's done arguments[0], then returns the array.<br /><br />In JavaScript arrays are copied by reference, and not by value, but if you use array.slice() it creates a copy of the array's values, but not a copy of the array inside the array, those are still passed by reference. In other words, it only works for single, and 2D arrays, anything larger and you start getting arrays containing references to the same data, which is not good.<br /><br />I'm going to have to find a better way to do this, it must be recursive, and must be based on the number of arguments.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com25tag:blogger.com,1999:blog-7950840353484096378.post-25712121103151368522010-04-21T18:22:00.000-07:002010-04-21T18:34:19.967-07:00OSD600 is overAfter tomorrow, I'm done this semester, and the real test begins: will I continue blogging between OSD600 and OSD700. It's already been a week since my last post, but not because of being almost finished, but because of English assignments and exams, and right now I have a breather (I never study hard the night before the exam, I just make sure I didn't miss anything). I would like to say I'll continue blogging for as long as the internet lives, but time is a better test. I really do feel like I've found a great spot in open source, and I'll enjoy it while it lasts.<br /><br />Anyway, I've been trying to do at least one push to <a href="https://processing-js.lighthouseapp.com">processing.js</a> a day, while I study, big or small, at least one contribution.<br /><br />Over the last week though, I've really got into irc, and branched into a few of the other channels, which have a totally different atmosphere than the processing.js channel. I got involved in the open source vs proprietary argument, which despite being educational and funny, really is never solved. I've tried to get some to at least try it before they slag it, but I guess for some programming is for money *shrugs*. I've told them to at least take the osd600 class in the fall, because they are quite promising students, just bias and not willing to try certain things. I tried explaining that open source is a good way to get work experience to get those jobs that make all the money they keep talking about. For them to take OSD600, they wouldn't be programming for free, but for marks, and that's what they do in all their other classes.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-89086739230307184922010-04-14T04:35:00.000-07:002010-04-14T05:21:41.776-07:00JavaScript this keywordI know this this keyword's (in JavaScript) meaning changes depending on how it's called, considering this class:<br /><pre><br />function Obj() {<br /> this.item = "contents";<br />}<br /></pre><br />and using it like this:<br /><pre><br />var obj = new Obj();<br />obj.item += " of this.item"; // item now contains "contents of this.item"<br /></pre><br />the this. keyword means the object only when it's called with new keyword, which declares it as an object.<br /><br />Now, if I am to change the object slightly, like so:<br /><pre><br />function Obj() {<br /> this.item = "contents";<br /> var funct = this.funct = function(){<br /> return this.item;<br /> };<br />}<br /></pre><br />I have two functions that should return the item inside the object. var funct should be usable inside the object, and this.funct should be usable outside the object, and do the same thing. Cept, when a function is created with var funct, the meaning of this. changes again. I'm not sure what it changes into, but in the above example, when I call funct() from inside the class, it returns "undefined". Everything makes sense, I'm just not sure what happens to this. in that crazy example. There is a way to fix this. You need to put the function inside of a closure, sending this, and using it as another word. For example:<br /><pre><br />function Obj() {<br /> this.item = "contents";<br /> var funct = this.funct = (function(obj) { return function(){<br /> return this.item;<br /> };})(this);<br />}<br /></pre><br />What this does is, instead of sending a function using the this. keyword inside, it creates an anonymous function, sending the object this, RENAMING it to obj, and returning a new function where this is changed to obj, hiding the double meaning of the this keyword, making it refer to the same thing no matter when and how it's called.<br /><br />I've been learning the meaning of this, and I thought I had it figured out. This just shows you can never know you know everything, only that you don't know everything, by learning something you didn't know :PScotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-3233261949632529222010-04-12T01:47:00.001-07:002010-04-12T02:00:13.327-07:00with(this) { ... } // no more<a href="http://matrix.senecac.on.ca/~sdowne/processingjs/withNoMore/">Here</a> is the source, and an example for <a href="http://processingjs.org/">processing.js</a> with "with(this) { ... }" removed.<br /><br />I was having issues using the new getters and setters. What was happening was considering this JavaScrpt class.<br /><pre><br />function Foo() {<br /> this.value = 0;<br /> __defineGetter__('value', function() { return this.value; });<br /> __defineSetter__('value', function(x) { this.value = x; });<br /><br /> this.getValue = function(){ return value; };<br /> this.setValue = function(x) { value = x; };<br />}<br /></pre><br />The this. keywords scope wasn't as intended. It was actually referring to the getter or setters, and not the object that uses it. I solved this by putting it into a closure, and passing the object I wanted the getters and setters to be added to, like so:<br /><pre><br />function Foo() {<br /> this.value = 0;<br /> __defineGetter__('value', (function(obj) { return function() { return obj.value; };})(this));<br /> __defineSetter__('value', (function(obj) { return function(x) { obj.value = x; };})(this));<br /><br /> this.getValue = function(){ return value; };<br /> this.setValue = function(x) { value = x; };<br />}<br /></pre>Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com1tag:blogger.com,1999:blog-7950840353484096378.post-46138367903033379022010-04-12T01:18:00.000-07:002010-04-12T09:11:34.135-07:00OSD600 release 0.3I guess technically I'm late on this release, but these things happen when you're doing stuff with this ;)<br /><br />I have been back and forth on this private <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword#ticket-133-9">ticket</a> for while, trying different things, doing research, and then finally worked with <a href="http://vocamus.net/dave/">Dave</a> to get it perfect. I did get lots done on it, even thought it's not all going to be used, I consider the research and work I did to be part of my 0.3. <a href="http://scottdowne.blogspot.com/2010/04/withthis-no-more.html">Here</a> is a post on what me and Dave worked on, and this is a better foundation for future work in the class parser.<br /><br />I also worked with <a href="http://asalga.wordpress.com/">Andor Salga</a>. One of his 3D examples had a silly parser error, so I filed a <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/549-function-parameter-newlines">ticket</a> and fixed it. It was up for review, but I had to back up because it was relying on code from a previous fix that's not going in. It was a simple parser fix, so it won't be hard for me to re implement it when the time comes. Also, Andor has a way around it, so his 3D example does work now. <a href="http://processing.org/learning/3d/birds.html">This</a> is the example he was working with, if anyone is interested.<br /><br />I was able to fix, and not have to back up ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/536-returning-array-of-floats-breaks-parser">#536</a> and it's awaiting review.<br /><br />I have learned one very important lesson; if two, or more bugs reside in the same spot in the code, I should consider keeping them in the same ticket, then I wouldn't of had the problems I'm having with ticket #133, mainly, having to revert a bunch of my "peer review requested". Really though, with the getters and setter, these are all going to be easy to refix (also, because I've already fixed them).Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-74572079978877115072010-04-09T21:44:00.000-07:002010-04-09T21:53:26.491-07:00processing.js private and public finally working, I thinkI think I have finally finished <a href="http://processingjs.org/">processing.js</a> ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword">#133</a>.<br /><br />A simple <a href="http://matrix.senecac.on.ca/~sdowne/processingjs/shiffman/example2.html">test</a> to see it.<br /><br />And a cool <a href="http://matrix.senecac.on.ca/~sdowne/processingjs/shiffman/example.html">example</a> that uses public variables. This particular test program was giving me the most difficulties while I was coding. It seemed whenever I would run the automated tests, this one would always have a new problem, and it also happens to be a long test, so it's harder to debug, so to be able to edit it and change the example is satisfying.<br /><br />I just need to write some clear, simple, and thorough test cases for private and public use, and I'll be done.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-48724668629296006662010-04-08T20:48:00.000-07:002010-04-08T21:08:29.685-07:00I love you word boundary.A word boundary is, other than my savior, a regular expression match that matches the tiny, invisible space between a word character (a-zA-Z0-9_) and a non-word character.<br /><br />You use it with \b and \B (\B matching a non-word boundary, same as [^\b])<br /><br />Example: "this is some text" with the regular expression \bs. This would only match the "s" in "some" because there is a non word character before the s. Also, if you did s\b it would match the "s" in "this" because it's before a non word character. Finally, \Bs would match "this" and "is" and s\B would match "some". I hope that all made sense.<br /><br />I needed this in a particular problem where I had to parse some code, and catch any specific words, globally. The problem was, when I checked for a match (previously) I was checking for (\W) which is a non word character, then check for my matching word, so if I was looking for all instances of "scott" I would not want to catch "scotty" as in this case, would be a different variable. So, /(\W)(scott){\W)/g, problem with this is, it would "eat" the leading and trailing characters, making any other matches in the string not check the leading and trailing characters again.<br /><br />By using \b, I was able to check what I needed, without "eating" the leading and trailing characters.<br /><br />If anyone is curious, the final product looks like so:<br /><pre><br /> // add this. to public variables used inside member functions, and constructors<br /> if (publicVars) {<br /> // Search functions for public variables<br /> for (var i = 0; i < methodsArray.length; i++) {<br /> methods += methodsArray[i].replace(/(addMethod.*?\{)([\s|\S]*\}\);)/g, function(all, header, body) {<br /> return header + body.replace(new RegExp("(\\.)?\\b(" + publicVars.substr(0, publicVars.length-1) + ")\\b", "g"), function (all, first, variable) {<br /> if (first === ".") {<br /> return all;<br /> } else {<br /> return "this." + variable;<br /> }<br /> });<br /> });<br /> }<br /> // Search constructors for public variables<br /> constructors = constructors.replace(new RegExp("(var\\s*?|\\.)?\\b(" + publicVars.substr(0, publicVars.length-1) + ")\\b", "g"), function (all, first, variable) {<br /> if (/var\s*?$/.test(first) || first === ".") {<br /> return all;<br /> } else {<br /> return "this." + variable;<br /> }<br /> });<br /> }<br /></pre><br />Obviously, there is code before and after, with declared variables, but just use your imagination ;) and I'm still looking for bug, but feel free to scrutinize my code.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com1tag:blogger.com,1999:blog-7950840353484096378.post-63382241456585931802010-04-07T11:45:00.000-07:002010-04-07T12:32:00.168-07:00Stuck on Processing.js private and public variablesIt's been awhile since I posted, but I needed to create some time, which meant moving things around.<br /><br />Warning, this may seem more like a rant, or crazy ramblings than a normal blog post. I'm just going to write it all down, in hopes that it helps me better understand all that I'm dealing with.<br /><br />Alright, to start off I'm working on <a href="http://processingjs.org/">processing,js</a>, specifically <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword">ticket #133</a> (still). It didn't seem hard at first, and I had a lot of initial success, but as I went deeper, I kept finding more and more. This has really taken a lot to get right.<br /><br />What I'm trying to accomplish, is to grab a class, so I can work with, and parse just the class, as to not parse non class related code. Once I'm in the class, I want to grab and remove the class functions, and constructors for later use, I don't want to be parsing "var i" that is contained in a function, into a private variable. Outside the function, anything that's private, is private, and public is public, but by default, it should be public. This is why I cannot be parsing functions and constructors, but I also have to know what's public, and what's private by the time I work with the functions and constructors, because when a public variable is used in a function or constructor, I need to add a "this." in front of it.<br /><br />I think some day, hopefully soon, all this class parsing code should have a major overhaul, as I keep coming to code that I don't see any use for, but I'm not comfortable removing it yet. I'm starting to fix things, and as I do, other areas should be removed. I think that would be better suited for a separate bug once I got private and public working.<br /><br />I did take an attempt at overhauling the class parsing, but I'm conflicted if I should just redo it, and go from there, or if I should use what's already there. I believe most would say not to redo it, but it's still tempting.<br /><br />Right now, I'm dealing with some code that's being "left behind" as I move parts of the class up, and other parts down. I know that classes with a constructor is parsed separately than a class without a constructor, but I don't think we should be doing this. What we should do is, open the class up, from top brace to bottom brace, and deal with any constructors contained within, as we get to them. This also has the advantage of not needing constructors to be declared first thing, and any member variables can also be declared anywhere. I've made a work around for this, but now I think it's causing my "code left behind" problem, as the code left behind is either the class declaration, or a constructor declaration. For example, imagine a class called Place, when I parse it, I'm left with a line of code like this "processing.Place = function" I think it's a case of a regex somewhere just working with what's between the {} and not what's also in front of it.<br /><br />Another issue that was solved about a day ago, is private and public functions, being declared with a custom "addMethod()" function, which for the longest time made no sense to me, and I hated it. I have since understood it, and although I still don't like it much, I see the need and don't want to remove it. This function wants an object, a String, and a function as it's parameters, and it will then create a function, with that name, and add it to the object you send, also allowing for overloading (the reason why it's there, is for overloading) I was stuck figuring out how to add a function to this. without making it public. I would send "this" as the object, into addMethod, I thought about intercepting it, and making private functions separately, but then we would be left with separate code for public functions, and private functions, not very ideal. I decided on adding a private object called private to the scope of the class, then when I want to add a function to the private scope, instead of sending "this" I would send my "private" object. The caveat with this is I now have to parse the functions and constructors in the class for any use of a private function, and add "private." in front of it, much like how I add "this." to public variables.<br /><br />The other option would be to add a "with(this){with(private){ .. }}" around all the code. This would check all variable and function calls if it's inside private (which I would create as "var private = {}"), if not, it would then check if it exists inside this, the problem is, if it doesn't exist inside private, or this, it would be created as a global variable, I think, possibly overwritting any global instances of that variable already defined. Processing.js already uses "with(this) { .. }", and I'm trying to remove it, with moderate success, but it's extremely helpful in this case, and is the difference between 2 lines of code, vs 50 lines of code (I may be exaggerating, as I have not counted which lines are new, and which are being left, or modified, but it's a lot). I plan on talking to <a href="http://vocamus.net/dave/">David Humphrey</a> in person about possibly, continuing the use of "with()" even with the dangers involved.<br /><br />I have to admit, this has been quite challenging, and I'm getting kinda frustrated with it, but I guess that's a good thing? *shrugs*Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-63172274920707096342010-03-30T20:34:00.000-07:002010-03-30T20:49:14.170-07:00public and private in JavaScriptIn order to enjoy programming in JavaScript you have to be at least a little crazy. Take this for example:<br /><pre><br />function Obj() {<br /> var private = {};<br /><br /> this.publicVar = "inside";<br /> private.privateVar = "inside";<br /> this.getPublic = function() {with(this){with(private){<br /> return publicVar;<br /> }}};<br /> this.getPrivate = function() {with(this){with(private){<br /> return privateVar;<br /> }}};<br />}<br /></pre><br />This is, I think, the complete way to emulate Java objects with private and public usage. All other ways are close, but always lack some little oddity that makes it less than pure. Another good thing about this method is it's easily created after parsing Java-like code.<br /><br />Take this usage for the above object:<br /><pre><br />var obj = new Obj();<br />alert(obj.getPublic()); // should alert inside<br />alert(obj.publicVar); // should alert inside<br />obj.publicVar = "outside";<br />alert(obj.publicVar); // should alert outside<br />alert(obj.getPublic()); // should alert outside<br /><br />alert(obj.getPrivate()); // should alert inside<br />alert(obj.privateVar); // should alert undefined<br />obj.privateVar = "outside";<br />alert(obj.privateVar); // should alert outside<br />alert(obj.getPrivate()); // should alert inside<br /></pre><br />Now, I think I've thought of everything, and everything else, but if anyone out there can break this, or find a shortcut, or challenge it in another means, I would be very grateful. Note, if you want to try breaking it, you don't have to understand the class itself, just how to use it, like I did in the second example.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-46399346814250538662010-03-29T17:28:00.000-07:002010-03-29T18:15:49.152-07:00blog post patchOk, I wasn't 100% correct in my last <a href="http://scottdowne.blogspot.com/2010/03/processingjs-public-members-public-to.html">post</a>. There was something I didn't consider.<br /><br />Where I said:<br /><pre><br />Considering this class:<br /><br /><br />class Obj {<br /> private String item;<br /> public void setItem(String str) {<br /> item = str;<br /> }<br /> public String getItem() {<br /> return item;<br /> }<br />}<br /><br /><br />and consider this usage:<br /><br /><br />Obj thing = new Obj();<br />thing.setItem("this has been set");<br />println(thing.getItem()); // prints "this has been set"<br />println(thing.item); // prints undefined, as it's not been used yet<br /><br /><br />Everything working as intended, until I try this:<br /><br /><br />thing.item = "if you see this, it's broken";<br />println(thing.getItem()) // prints "if you see this, it's broken" :(<br /></pre><br />I was wrong, that should work as I originally had hoped, but what was happening was <a href="http://processingjs.org/">processing.js</a> would wrap it inside a with(this){...} function, which I'm still looking into, but it was doing things that I figured was just normal JavaScript, so I over looked it.<br /><br />Here are some comparing classes, to illustrate what I mean:<br /><pre><br />// What should, and does work as intended.<br />function Obj() {<br /> var privateMember = "hidden";<br /> this.getMember = function() {<br /> return privateMember;<br /> }<br />}<br />var obj = new Obj();<br />alert(obj.getMember()); // alerts "hidden"<br />obj.privateMember = "not hidden";<br />alert(obj.getMember()); // alerts "hidden" :D<br /></pre><br />Now, what processing.js does:<br /><pre><br />// Does not work as intended.<br />function Obj() { with (this) { // This is the only change<br /> var privateMember = "hidden";<br /> this.getMember = function() {<br /> return privateMember;<br /> }<br />}}<br />var obj = new Obj();<br />alert(obj.getMember()); // alerts "hidden"<br />obj.privateMember = "not hidden";<br />alert(obj.getMember()); // alerts "not hidden" :(<br /></pre><br />Now, why not yank "with(this){...}" right out of there? I don't know what it's doing, yet. More on that later. But I'm hopeful to say I have a pretty solid direction.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-69697539358572754802010-03-29T12:27:00.000-07:002010-03-29T13:47:39.563-07:00processing.js public members public to everyone but itselfOk, here's what I'm doing. I'm trying to implement the <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword">private/public</a> keywords for <a href="http://processingjs.org/">processing.js</a><br /><br />And here are the problems. First attempt was ages ago, and that was to, inside a class, use this.variable; for public, and var variable; for private. This sort of worked, as it allowed me to use public members freely inside and outside the object, and I could not access private members, until they were set from outside the object. <br /><br />Considering this class:<br /><pre><br />class Obj {<br /> private String item;<br /> public void setItem(String str) {<br /> item = str;<br /> }<br /> public String getItem() {<br /> return item;<br /> }<br />}<br /></pre><br />and consider this usage:<br /><pre><br />Obj thing = new Obj();<br />thing.setItem("this has been set");<br />println(thing.getItem()); // prints "this has been set"<br />println(thing.item); // prints undefined, as it's not been used yet<br /></pre><br />Everything working as intended, until I try this:<br /><pre><br />thing.item = "if you see this, it's broken";<br />println(thing.getItem()) // prints "if you see this, it's broken" :(<br /></pre><br />It's not very useful if private is restrictive but not protective. I thought the whole purpose was to protect you, the person using the object, from themselves, and not just restrict them from, themselves.<br /><br />Anyway, I wasn't done there. I spent this weekend rewriting the way processing.js translates a class. I was changing it so when you created an object, instead of returning an object, I would call a function that would set all the private members and functions, then return an object literal that would contain all the public variables and functions, which had access to the members in the preceding function, but those members no longer existed, and it worked! Here is an example of what I mean:<br /><pre><br />function Obj() {<br /> var privateVariable = "It's a secret to everybody";<br /> function privateFunction() {<br /> ...<br /> }<br /> return {<br /> publicVariable: "...",<br /> publicFunction: function() { return privateVariable; }<br /> };<br />}<br /></pre><br />Usage of this is like so:<br /><pre><br />var obj = new Obj();<br />alert(obj.publicVariable); // alertss "..."<br />alert(obj.publicFunction()); // alerts "It's a secret to everybody"<br /></pre><br />Everything as expected until I try this... Consider this modified class:<br /><pre><br />function Obj() {<br /> return {<br /> publicVariable: "...",<br /> publicFunction: function() { return publicVariable; }<br /> };<br />}<br /></pre><br />and this usage:<br /><pre><br />var obj = new Obj();<br />alert(obj.publicFunction()); // crashes<br /></pre><br />So now, a private member is private to everyone but itself, which is good, and a public member is public to everyone... but itself. Two solutions, with two completely opposite caveats.<br /><br />Final though: while writing this, I was thinking, and organizing my thoughts (kinda the point of blogging, for me) and I have two options I need to explore.<br /><ul><br /><li>Change the public functions to use public variables with a "this." before it. Example:<br /><pre><br /> return {<br /> publicVariable: "...",<br /> publicFunction: function() { return this.publicVariable; }<br /> };<br /></pre><br />This works, but isn't very practical as I'm translating Java-like class syntax into the javascript you see now, and I don't want to go through all the code, just the structure; this method would require looking for the usage of all public variables, once I have a list of public variables, and add "this.". Not ideal.</li><br /><li>Another option, which I'll try, is to add a list of public variables at the top of the code inside a function, like this:<br /><pre><br /> return {<br /> publicVariable: "...",<br /> publicFunction: function() { var publicVariable = this.publicVariable; return publicVariable; }<br /> };<br /></pre><br />Passes the public variable inside this, into a new variable with said name, so I don't have to touch the code inside the function, just add a header of sorts. I would have to do this with functions too.</li><br /></ul>Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com1tag:blogger.com,1999:blog-7950840353484096378.post-50957396019890889412010-03-28T11:42:00.000-07:002010-03-28T12:22:21.289-07:00JavaScript and multiline regular expressionsJust something I would like to share, because it was causing me grief :P.<br /><br />While working on parsing a Java-like class into a JavaScript class, for <a href="http://processingjs.org/">processing.js</a>. I needed to grab all member variables that are marked as public, using a regular expression, and return them as an object literal. Given this as an example:<br /><pre><br />public String thingOne;<br />public String thingTwo;<br /></pre><br />Into this:<br /><pre><br />{<br /> thingOne: null,<br /> thingTwo: null<br />}<br /></pre><br />This is what I ended up doing:<br /><pre><br /> middle = middle.replace(/(?:(private|public)\s+)?(var[\s|\S]*?;)/g, function(all, access, variable) {<br /> if (access === "private") {<br /> return variable;<br /> } else {<br /> variable = variable.replace(/,\s*/g, ";\nvar ");<br /> publicVars += variable.replace(/(?:var\s+)?(\w+)\s*=?\s*([^;,]*)(?:;)?/g, function(all, name, value) {<br /> if (value) {<br /> return "\n" + name + ": " + value + ",";<br /> } else {<br /> return "\n" + name + ": null,";<br /> }<br /> });<br /> return "";<br /> }<br /> });<br /></pre><br />Now don't worry too much about that, just a short background of what I was doing to bring me to the problem. Multiline regular expressions in Javascript.<br /><br />From my favourite Javascript regex <a href="http://www.evolt.org/regexp_in_javascript">resource</a> "Multiline Input 'm' This flag makes the beginning of input (^) and end of input ($) codes also catch beginning and end of line respectively.". It does NOT do what you would expect, it simply allows code like this:<br /><pre><br />String one;<br />String two;<br /></pre><br />to match both lines with this /^String/gm. Without m, String two; would not match as it does not start at the beginning of the line, but after a \n. Another way to look at it is, m changes the meaning of ^ and $ to also match \n and \r (\r being carriage return). JavaScript regular expression is already multiline, in a way, as all lines are actually one long line with newline characters.<br /><br />What I wanted to do was match any instance of "var (bunch of stuff) ;" spanning multiple lines. To grab examples like this:<br /><pre><br />public String thingOne,<br /> thingTwo;<br /></pre><br />What I was using was something like this /var.*?;/g grabbing anything between var and ;, including whitespace and newlines? right? Nope, apparently not.<br /><br />There was a trick I found to solve this. /var[\s|\S]*?;/ This uses \s which is any whitespace, including newline, OR \S which is anything NOT a white space, this is the TRUE anything, and a neat little trick.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com132tag:blogger.com,1999:blog-7950840353484096378.post-23838147383829398912010-03-24T05:22:00.000-07:002010-03-24T06:11:26.956-07:00morning train ride tangentThis morning, on the train, I wanted to open up <a href="http://processingjs.org/">processing.js</a> and get back to working on <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/237-custom-classes-not-handled-properly">ticket #237</a>. I got into it, earlier and reproduced all the problems that the ticket entails. We have empty classes with no methods or constructors not working, we have constructors following methods working, but the methods above the constructor not working, and we have the hardest problem in overloading only working in terms of number of arguments, and not types of arguments. What I mean is:<br /><pre><br />void addItem(int item){...}<br />void addItem(String item){...}<br /></pre><br />This is valid processing, because the two methods are different, but, in JavaScript, variable types are not important, and the above code will be translated into:<br /><pre><br />var addItem = function(var item){...};<br />var addItem = function(var item){...};<br /></pre><br />The methods are translated into the same method, this also does not work with constructors either, which is a different problem because the way processing.js handles consturctors. What processing.js does is, because you can send any number of arguments into a method or constructor, and access the variable by using arguments[n], in a for loop less than arguments.length. This is how constructor overloading is handled in processing.js. There is only one real constructor, it just has if statements checking how many arguments there are. Here is an example for how processing.js translates a class:<br /><pre><br />class Obj{<br /> Obj(String item){<br /> item = "in constructor one";<br /> }<br /> Obj(int item, String item2){<br /> item = "in constructor two";<br /> }<br />}<br /></pre><br />into this:<br /><pre><br />function Obj() {with(this){<br /> if ( arguments.length === 1 ) {<br /> var item = arguments[0];<br /><br /><br /> item = "in constructor one";<br /><br /> }<br /><br /> if ( arguments.length === 2 ) {<br /> var item = arguments[0];<br /> var item2 = arguments[1];<br /><br /><br /> item = "in constructor two";<br /><br /> }<br /><br /><br />}}<br /></pre><br />If you notice, it's just one method that filters out the contents of the constructor into various if statements, but it's only the length of the arguments, and not the types. What I would need to do is grab the types before the types are lost, and then add a type check for all variables at the same time I check the argument length, but methods are not like this.<br /><br />Funny thing is, while researching this, I came across <a href="http://ejohn.org/blog/javascript-method-overloading/">this</a> blog post, and as soon as I saw the code, I recognized it. This is John Resig's <a href="http://ejohn.org/blog/">blog</a>, and he is the creator of processing.js, and his post helped me a lot with how to understand the addMethod, method, and the caveats involved.<br /><br />Ah shoot, I got off on another tangent. Ok, While I was planning on doing this ticket I have been talking about up to this point on the train, I ended up seeing something in code that made me go back to my last <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/440-syntax-error-with-declared-typed-array-init">ticke</a> on array initialization. I noticed, later in the code, the use of ArrayList() for more than just creating an array, but initializing. I still cannot get it to initialize the array, but I've spend an unexpected morning on this. The idea of ArrayList is to send a size for the array, to create it, but all the values will be empty. Like so:<br /><pre><br />new ArrayList(2); // creates an arraylist with 2 empty items<br />new ArrayList(2, 2); // creates a 2D arraylist with an array of two, with two empty items<br /></pre><br />I saw later in the code something like this<br /><pre><br />new ArrayList(["Thing 1", "Thing 2"]); // apparently, this is creating and initializing an arraylist<br /></pre><br />Like I said, I didn't (yet) get this to work, but I think it would improve the way it's already being done, with just the array like this: <br /><pre><br />var things = ["Thing 1", "Thing 2"];<br /></pre><br />I should get this wrapped up into:<br /><pre><br />var things = new ArrayList(["Thing 1", "Thing 2"]); // this would be ideal<br /></pre><br />My train ride tests did not work, but I'm going to reduce it more and see why. First I should get arraylist initialization to work. period. Then I will attempt to add it into processing.js. Anyway, that's all for today.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-48326862165859681652010-03-23T19:08:00.000-07:002010-03-24T05:22:25.450-07:00Processing.js array initializationJust the other day I fixed a bug for <a href="http://processingjs.org/">processing.js</a>, where creating an array and initializing it at the same time would crash the parser. What was happening was, code like this:<br /><pre><br />float[] baz;<br />baz = new float[] {1.0, 2.5};<br /></pre><br />when parsed, "new float[]" would be converted to "new ArrayList[]" then "{1.0, 2.5};" would get tacked onto the end, and it shouldn't. "new ArrayList[]" is only for creating an empty array, without initializing it. If you want to initialize, you want the JavaScript to look like this:<br /><pre><br />baz = [1.0, 2.5];<br /></pre><br />notice how I no longer use new ArrayList. ArrayList just isn't setup for initialization that I could see, but the above method works.<br /><br />Anyway, I got all this working fine and dandy <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/440-syntax-error-with-declared-typed-array-init">here</a>.<br /><br />When the parser came across something with the word "new\s(\w)" which translates to "new" + whitespace + any word, it would package it all up, what I did was added a check for the array contents, ie. everything inside {} it even worked for 2D arrays and beyond. I would then simply replace the whole thing with the array initialization, and later in the parser it would be changed from {} to []. So, whatever the array was, {{1.2, 2.1}, {7.6, 8.8}} for a 2D array, would just replace all {}'s with []. The problem was, and I didn't notice this until after the patch was submitted, noticed it on the train ride home, was that two arrays can be declared on one line. Sometimes things like this just slip everyone's minds, there are so many ins and outs of a language, and to translate one language to another you have to have good attention to detail. So, something like this:<br /><pre><br />baz = new float[][] {{1.0, 2.0}, {3.0, 4.0}}; baz2 = new float[][] {{1.0, 2.0}, {3.0, 4.5}};<br /></pre><br />would break my fix. It would gobble up the whooole thing into one, and the second "new" would get lost, and cause a syntax error. the fix was pretty easy, and I fixed it one the same train I thought of it on. All I had to do was eat all {}'s and everything inside of them until I hit the semi-colon, then stop! the stop is important, and the part I was lacking before. Before I would gobble it all up until I hit the last semi-colon, so, it was greedy.<br /><br />Here is the <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/471-two-array-initializations-on-the-same-line#ticket-471-2">fix</a>. Also, I'll be fitting this work into my 0.2 release as it's so very close.<br /><br />Something else I noticed today (hijacking my own blog post here) working in Processing.js has a pretty big advantage over working in other open source projects. We are a community of developers, so when someone finds a bug, half the time, most of the work in finding the bug is done for us, because the person that found the bug can read the code, and reduce it to a acceptable level of code. But, in a project like Firefox, when a user finds a bug, it might just be a description of what they were doing at the time, and that it crashed. Which is an interesting aspect maybe some of the other members of the processing.js team might not of considered, yet.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-52005786395533248012010-03-19T04:17:00.000-07:002010-03-19T12:06:21.272-07:000.2<h2>Intro</h2><br />This release has been weird. My main, original tickets <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/230-mouse-scroll-wheel-support">230</a> and <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword">133</a> for this release were not as expected.<br /><br /><h2>Problems and Challenges</h2><br /><a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/230-mouse-scroll-wheel-support">230</a> already existed, but just didn't have any life, I just needed to breathe some life into it by writing some code to show it works, it was more of a documentation issue. The ticket is still awaiting review. I felt out of place with this one, and still do. I had a lot of fun writing it, and even more fun creating this <a href="http://matrix.senecac.on.ca/~sdowne/processingjs/scrollWheelFun/example.html">example</a>, but the fact that my code isn't really going to be added, just, feels weird. What I think would be best, and I'll push for this when the time comes. In the original <a href="http://processing.org/">processing</a> there is a place on their website for <a href="http://processing.org/hacks/">hacks</a>, basically, lists features, and shows examples, of things that can be done with processing through the use of java, that was not intended. What could happen in <a href="http://processingjs.org/">processing.js</a> is sort of the same thing, a place on the website, that documents all the hacks that can be used, through the use of JavaScript inside the processing.js code, and this would be a big job, but something that should probably be done much later, maybe even after 1.0 of processing.js has been released. Until then, I feel like my <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/230-mouse-scroll-wheel-support">230</a> creation is a lost soul.<br /><br />My other main ticket, ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword">#133</a> also had issues. In the past I struggled with getting the private keyword to work on member functions, but the way processing.js created member functions, through using an addMember function, caused me issues. I would have to intercept the the addMember function, and create it my way. Although, <a href="http://vocamus.net/dave/">Dave</a>, was showing a <a href="http://matrix.senecac.on.ca/~sdowne/processingjs/JavaScript/counter.html">trick</a> in JavaScript, on how to make classes, with members, and it seemed to make a lot of sense. This is the code.<br /><pre><br /><br /> var counter = (function (){<br /> var c = arguments[0];<br /> var i = arguments[1];<br /><br /> return {<br /> increase: function(){c+=i;},<br /> getValue: function(){return c;}<br /> };<br /> })(1, 2);<br /><br /> counter.increase();<br /> alert(counter.getValue());<br /><br /> counter.increase();<br /> alert(counter.getValue());<br /><br /></pre><br />Also, I have mentioned before, that <a href="http://weare.buildingsky.net/">Corban Brook</a> is working on ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/237-custom-classes-not-handled-properly">#237</a> and talked about restructuring the way classes are parsed. I've been holding off working on ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword">#133 prviate keyword</a> because of possible changes.<br /><br /><h2>Specifications</h2><br />What I did do for 0.2:<br />The main one, was the <a href="http://matrix.senecac.on.ca/~sdowne/processingjs/scrollWheelFun/example.html">example</a> used for <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/230-mouse-scroll-wheel-support">230</a>, and the ticket itself.<br /><br />I also did three little ones to to supplement this release.<br />Tickets:<br /><ul><br /><li><a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/382-date-and-month">#382</a></li><br /><li><a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/411-parser-breaks-on-arrays-declarations-that-have-a-space-between-and">#411</a></li><br /><li><a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/351-tabs-break-code">#351</a></li><br /></ul><br />I've done some review of other tickets, and found a <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/183-splittokens-returning-undefined">ticket</a> that was fixed from my 0.1 that I didn't see. All easy stuff, but it adds up, and still must be done right.<br /><br /><h2>Conclusion</h2><br />This release has been very different than my <a href="http://scottdowne.blogspot.com/2010/02/processingjs-01-release.html">last</a> in terms of the work done, and I spent a lot less time trouble shooting, and more time creating tests and examples. I am still finding my groove. Finally, <a href="http://weare.buildingsky.net/">Corban</a> said I could take a stab at ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/237-custom-classes-not-handled-properly">#237</a>, I will probably do that and my private keyword at the same time, for my 0.3, and still try to get my regex parser fit in somehow, it shouldn't be a problem as it's really only two tickets, and all related to parsing, stuff I've been doing since the beginning of my stay here :)Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com1tag:blogger.com,1999:blog-7950840353484096378.post-42442485190842609612010-03-16T17:42:00.000-07:002010-03-16T21:05:40.034-07:00Quick little fixYesterday, while cruising the <a href="http://processingjs.org/">processing.js</a> bug tracking website <a href="https://processing-js.lighthouseapp.com/projects/41284-processingjs/overview">lightouse</a> looking at the open tickets, and seeing what was happening I can across ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/382-date-and-month#ticket-382-7">#382</a>.<br /><br />It was small so I figured I might as well do it. Throw it in with my 0.2.<br /><br />I've also started thinking about doing what I did here for <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/226-parsing-indifferent-to-string-content">string parsing</a> to regular expressions. The first thing that comes to mind is the order in which I must do this. I can mask all strings first, then mask all regular expressions, or vice versa. I would have to replace them in the opposite order in which I mask them, that's a given. I also have to now modify my <STRING n> as I now have to check if a string contains <STRING n> OR <REGEX n>, as an example. I think it would be easiest to mask regex first, as I already have the code to check that it's not in a string. So, use that code, find all regex's not in a string, mask them, and any string like code inside the regex will already be gone, so I wouldn't have to check for regex when I mask the strings, and replace again, but in reverse.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-75020295327688715372010-03-14T20:42:00.000-07:002010-03-14T21:01:40.832-07:00Tabs break nothing!I mentioned in my last <a href="http://scottdowne.blogspot.com/2010/03/back.html">post</a> that I would be taking something on to add to my 0.2, so I did processing.js ticket <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/351-tabs-break-code">#351</a> and wrapped it up the same day. Not bad, but it was easy.<br /><br />In my last post I talked about how I was able to reproduce the bug, and the next step was to find, where in the code, variables are changed to var. This is what the code in question looked like before I got to it.<br /><pre><br /> // float foo = 5;<br /> aCode = aCode.replace(/(?:static\s+)?(?:final\s+)?(\w+)((?:\[\])+| ) *(\w+)\[?\]?(\s*[=,;])/g, function (all, type, arr, name, sep) {<br /> if (type === "return") {<br /> return all;<br /> } else {<br /> return "var " + name + sep;<br /> }<br /> });<br /></pre><br />What is happening, is it's matching a regular expression to the code to be parsed, sending the matched groups into a function in the order of (all, type, arr, name, sep) the first parameter is always the whole matching string, and each one that follows is the next match group and so on. The function will do something with the sent values, in this case, swapping the variable type for "var " and removing any array brackets. this new string is then replaced with the original matching string.<br /><br />The problem was, it was only doing this if there was a space between "int i" this is a closer look at the culprit "(\w+)((?:\[\])+| ) *(\w+)" first it matches a word, something like int, char, anything like that, then it look for any number of [] OR a space, then 0 or more spaces, then another word. I simply changed the spaces to \s, which included all whitespace, including tab, and not just a space, resulting in this.<br /><pre><br /> // float foo = 5;<br /> aCode = aCode.replace(/(?:static\s+)?(?:final\s+)?(\w+)((?:\[\])+|\s)\s*(\w+)\[?\]?(\s*[=,;])/g, function (all, type, arr, name, sep) {<br /> if (type === "return") {<br /> return all;<br /> } else {<br /> return "var " + name + sep;<br /> }<br /> });<br /></pre><br />Now when a tab is found between int i; it's not ignored, the parsing of int i; is not ignored.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-30426933981199508282010-03-13T11:44:00.000-08:002010-03-13T12:34:20.184-08:00Back!It's been a week since I blogged, but I've been all over the place doing small stuff and getting organized, and there wasn't much to say :P<br /><br />My 0.2 is due next week and I've got my <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/230-mouse-scroll-wheel-support">mouse scroll</a> awaiting review, and here is my <a href="http://scottdowne.blogspot.com/2010/02/todays-challenge.html">post</a> covering that, but I would like to get one more thing pushed out before next week is over.<br /><br />I have the <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/133-private-keyword">private keyword</a> that I've been working on in the past, and kinda pushed it to the side in favour of <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/226-parsing-indifferent-to-string-content">string parsing</a>. I figured I would go back to the private keyword but I noticed <a href="http://weare.buildingsky.net/">Corban Brook</a> was working on this <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/237-custom-classes-not-handled-properly">ticket</a>, which may include an overhauling for the way <a href="http://processingjs.org/">processing.js</a> handles the parsing of classes, which would make the way it handles the private keyword too, hopefully making it easier for me later. This excites me and I'm going to be following it, and pushing the private key word back to 0.3 along with another planned ticket to parse regex content much like I parsed string content.<br /><br />Anyway, for now, I've decided to take on this one <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/351-tabs-break-code">tabs break code</a>. This is a bug that causes the code to crash if a tab is used in a certain place instead of a space. I have reproduced the bug and it seems to only work (the bug works, not the code:P) when there is a tab after a variable type, but before the variable name, like so:<br /><pre><br />int anInt = 0;<br /></pre><br />This does not work, but oddly enough, these example DO work:<br /><pre><br /> int anInt = 0;<br /> println (anInt);<br /></pre><br />The only place this causes the error (that I have found) is after the variable type and before the variable name. I printed off the translated code, after it's been turned into javascript to see what's going on, and this is the result of the above error:<br /><pre><br />int anInt = 0;<br /></pre><br />and if I take out the tab, I get:<br /><pre><br />var anInt = 0;<br /></pre><br />Which works, so obviously, the tab is stopping the conversion of variable names in java like int, to var in javascript. The solution is probably pretty easy, just need to look through the parser, look for the regex that converts variables to vars, and add in a condition for tabsScotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0tag:blogger.com,1999:blog-7950840353484096378.post-89362456473248013682010-03-04T17:49:00.000-08:002010-03-04T18:29:10.566-08:00Study week day 4: something to showMy five day <a href="http://scottdowne.blogspot.com/2010/02/study-week-project-day-05.html">mission</a> is coming to an end, and I have something to show for it.<br /><br />In order to view this experiment, you must have a capable browser. I use <a href="http://www.mozilla.org/projects/minefield/">Minefield</a>. Click this <a href="http://learningwebgl.com/blog/?p=11">link</a> for more information.<br /><br />Once inside, you can use the arrow keys to rotate the cubes up and down. Not much else to tell, so here are the examples: <a href="http://matrix.senecac.on.ca/~sdowne/webGL/lesson4%20-%20Copy.html">example 1</a> and <a href="http://matrix.senecac.on.ca/~sdowne/webGL/lesson4.html">example 2</a><br /><br />A screen shot of example 2 for those currently not with a capable browser.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijdS0-zTgq5L9VjdmvIqdQu2F0yl2o3W2ryq0W_1AE2z1PGasbf8SoGDucjt3jdvTkcJopyni1g5ftxkTQS1S638w2cY1P6EZPymw_T7MXnr8f5tsiYA_HDhJ5JGIKM_Fh2ZZCAvLHMiiF/s1600-h/New+Bitmap+Image+(9).bmp"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 250px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijdS0-zTgq5L9VjdmvIqdQu2F0yl2o3W2ryq0W_1AE2z1PGasbf8SoGDucjt3jdvTkcJopyni1g5ftxkTQS1S638w2cY1P6EZPymw_T7MXnr8f5tsiYA_HDhJ5JGIKM_Fh2ZZCAvLHMiiF/s400/New+Bitmap+Image+(9).bmp" border="0" alt=""id="BLOGGER_PHOTO_ID_5444969613599163346" /></a><br />This gives me an idea though. Have a player that can be moved with the arrow keys, and he moves along this giant cube. You can rotate the cube with the mouse, and there is a 3D randomly generated maze on the cube. As the cube moves gravity would change, so the player (and the maze) wrap around the cube. Finally, add multiplayer.Scotthttp://www.blogger.com/profile/08136798146273920164noreply@blogger.com0