private working this time, for real

Still working on Processing.js‘s bug number #133.

In my last post I said I had private working for private variables, but I was wrong. It worked, as it was able to handle the word private, as in not crash, but the variable still was not private. There were a few reasons why this was happening, but mostly I was doing my conversion in the wrong location. I decided to start printing off my html page’s processing code, in various stages of conversion, and noticed my old method for converting private, was being converted back again by another part of the code, so I added my conversion to that block of code, this is what it looked like originally.

// Replace var foo = 0; with this.foo = 0;
// and force var foo; to become this.foo = null;
vars
.replace( /\s*,\s*/g, ";\n this." ).replace( /\b(var |final |public )+\s*/g, "this." )
.replace( /\b(var |final |public )+\s*/g, "this." )
.replace( /this.(\w+);/g, "this.$1 = null;" ) +
( extend ? "extendClass(this, " + extend + ");\n" : "" ) +
"" + ( typeof last == "string" ? last : name + "(" );

I added one line to the end of the replaces

.replace(/\b(private this.)/g, "var ") +

It now looks like this.

// Replace var foo = 0; with this.foo = 0;
// and force var foo; to become this.foo = null;
vars
.replace( /\s*,\s*/g, ";\n this." ).replace( /\b(var |final |public )+\s*/g, "this." )
.replace( /\b(var |final |public )+\s*/g, "this." )
.replace( /this.(\w+);/g, "this.$1 = null;" )
.replace(/\b(private this.)/g, "var ") +
( extend ? "extendClass(this, " + extend + ");\n" : "" ) +
"" + ( typeof last == "string" ? last : name + "(" );

What this now does, is after it’s done parsing through the variables, if it’s going to be private, it’ll look like this

private this.i;

So I’m replacing this with.

var i;

Instead of.

this.i;

An apparent problem is if later in the constructor, I’m using something like this

this.num = num;

It will cause problems. It will be trying to use a private member, by declaring it later with this., which in JavaScript, will make it a public method, ignoring the previously set private one. Also, I don’t have private methods working, but I have located the location in the code that I have to work with it. It appears it’s adding the methods to the object through the use of a created method called addMethod, which I don’t understand yet. Here is the implementation and calling of said code.

p.addMethod = function addMethod( object, name, fn ){
if( object[ name ] ){
var args = fn.length,
oldfn = object[ name ];

object[ name ] = function(){
if( arguments.length == args ){
return fn.apply( this, arguments );
}else{
return oldfn.apply( this, arguments );
}
};
}else{
object[ name ] = fn;
}
};

and calling

// Fix class method names
// this.collide = function() { ... }
// and add closing } for with(this) ...
rest = rest.replace( /(?:public )?Processing.\w+ = function (\w+)\((.*?)\)/g, function( all, name, args ){
return "ADDMETHOD(this, '" + name + "', function(" + args + ")";
});

I’m not sure how this is all working, but the second part of code here will change the html processing code, to call the addMethod, I’m pretty sure about this, just not sure how it’s doing it.

Finally, I noticed comments are removed only if a space follows the //. This is easy to fix, I’m just trying to find a good reason as to why this is here, or if it’s simply been over looked. Like this.

// Remove end-of-line comments
aCode = aCode.replace( /\/\/ .*\n/g, "\n" );

As you can see, someone has added a space after the //, is this on purpose? why?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: