Archive for April, 2010

Solution to my last post.

Posted in open source on April 26, 2010 by scottdowne

In my last post 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.

I fixed it, and came up with a very simple solution:


p.ArrayList = function() {
var createArrayList = function(args){
var array = new Array();
for (var i = 0; i < args[0]; i++){
array[i] = (args.length !== 1 ? createArrayList(args.slice(1)) : 0 );
}

return array;
};
return createArrayList(Array.prototype.slice.call(arguments));
};

I came across the problem of arguments only acting like an array, but not having access to many array like functions, like slice(). Solution.


Array.prototype.slice.call(arguments)

Array.prototype.slice gets the slice method from the Array object.
call() allows you to call an object’s function from the context of another object.
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. :D

copy a multidimensional array in js.

Posted in open source on April 25, 2010 by scottdowne

I’m working on ticket #587, for processing.js.

What I have to do is convert a custom ArrayList function to create a multidimensional array based on parameters passed in. For example:


var array = new ArrayList(4, 5, 6);

would create a 3D array, similar to:


int[][][] array = new int[4][5][6]

This is what I’ve put together:


p.ArrayList = function() {
var array = [],
tempArray = [];

for (var i = 0, aLength = arguments[arguments.length-1]; i < aLength; i++){
array[i] = 0;
}

for (var i = arguments.length-2; i >= 0; i--){
tempArray = array.slice();
array = [];
for (var j = 0, aLength = arguments[i]; j < aLength; j++){
array[j] = tempArray.slice();
}
}
return array;
};

It starts at the back of the arguments, and copies the array recursively until it’s done arguments[0], then returns the array.

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.

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.

OSD600 is over

Posted in open source on April 22, 2010 by scottdowne

After 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.

Anyway, I’ve been trying to do at least one push to processing.js a day, while I study, big or small, at least one contribution.

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.

JavaScript this keyword

Posted in open source on April 14, 2010 by scottdowne

I know this this keyword’s (in JavaScript) meaning changes depending on how it’s called, considering this class:


function Obj() {
this.item = "contents";
}

and using it like this:


var obj = new Obj();
obj.item += " of this.item"; // item now contains "contents of this.item"

the this. keyword means the object only when it’s called with new keyword, which declares it as an object.

Now, if I am to change the object slightly, like so:


function Obj() {
this.item = "contents";
var funct = this.funct = function(){
return this.item;
};
}

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:


function Obj() {
this.item = "contents";
var funct = this.funct = (function(obj) { return function(){
return this.item;
};})(this);
}

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.

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 :P

with(this) { … } // no more

Posted in open source on April 12, 2010 by scottdowne

Here is the source, and an example for processing.js with “with(this) { … }” removed.

I was having issues using the new getters and setters. What was happening was considering this JavaScrpt class.


function Foo() {
this.value = 0;
__defineGetter__('value', function() { return this.value; });
__defineSetter__('value', function(x) { this.value = x; });

this.getValue = function(){ return value; };
this.setValue = function(x) { value = x; };
}

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:


function Foo() {
this.value = 0;
__defineGetter__('value', (function(obj) { return function() { return obj.value; };})(this));
__defineSetter__('value', (function(obj) { return function(x) { obj.value = x; };})(this));

this.getValue = function(){ return value; };
this.setValue = function(x) { value = x; };
}

OSD600 release 0.3

Posted in open source on April 12, 2010 by scottdowne

I guess technically I’m late on this release, but these things happen when you’re doing stuff with this ;)

I have been back and forth on this private ticket for while, trying different things, doing research, and then finally worked with Dave 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. Here is a post on what me and Dave worked on, and this is a better foundation for future work in the class parser.

I also worked with Andor Salga. One of his 3D examples had a silly parser error, so I filed a ticket 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. This is the example he was working with, if anyone is interested.

I was able to fix, and not have to back up ticket #536 and it’s awaiting review.

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).

processing.js private and public finally working, I think

Posted in open source on April 10, 2010 by scottdowne

I think I have finally finished processing.js ticket #133.

A simple test to see it.

And a cool example 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.

I just need to write some clear, simple, and thorough test cases for private and public use, and I’ll be done.

I love you word boundary.

Posted in open source on April 9, 2010 by scottdowne

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.

You use it with \b and \B (\B matching a non-word boundary, same as [^\b])

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.

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.

By using \b, I was able to check what I needed, without “eating” the leading and trailing characters.

If anyone is curious, the final product looks like so:


// add this. to public variables used inside member functions, and constructors
if (publicVars) {
// Search functions for public variables
for (var i = 0; i < methodsArray.length; i++) {
methods += methodsArray[i].replace(/(addMethod.*?\{)([\s|\S]*\}\);)/g, function(all, header, body) {
return header + body.replace(new RegExp("(\\.)?\\b(" + publicVars.substr(0, publicVars.length-1) + ")\\b", "g"), function (all, first, variable) {
if (first === ".") {
return all;
} else {
return "this." + variable;
}
});
});
}
// Search constructors for public variables
constructors = constructors.replace(new RegExp("(var\\s*?|\\.)?\\b(" + publicVars.substr(0, publicVars.length-1) + ")\\b", "g"), function (all, first, variable) {
if (/var\s*?$/.test(first) || first === ".") {
return all;
} else {
return "this." + variable;
}
});
}

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.

Stuck on Processing.js private and public variables

Posted in open source on April 7, 2010 by scottdowne

It’s been awhile since I posted, but I needed to create some time, which meant moving things around.

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.

Alright, to start off I’m working on processing,js, specifically ticket #133 (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.

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.

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.

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.

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.

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.

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 David Humphrey in person about possibly, continuing the use of “with()” even with the dangers involved.

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*

Follow

Get every new post delivered to your Inbox.