Thursday, March 31, 2011

JavaScript notes: an Applet puzzle

This one has driven me a little crazy over the last few hours.

As part of my learning about JavaScript and HTML5 I thought I would try embedding an applet in an HTML5 page. Which I could easily do. Then I made the applet record a sound and then replay it. To do this the applet needs to be signed. So no problem, I used a self signed certificate, loaded my applet and everything just worked. Sweet.

The next thing I then tried to do was to get the JavaScript on the web page to call into the applet.
 <input type="button" value="record" onClick="document.getElementById('recorderApplet').doRecord();">

Hacky - In my defense, I was just trying to see if I could get the call to work...

When I pressed the HTML button in my browser:
POW!: access denied (javax.sound.sampled.AudioPermission record)

This was a little unexpected - if the applet button was pressed, I recorded. If the HTML button was pressed, then I got a security error.

I Googled the world and found Bug 4406607 and Mozilla bug 60120
But somehow the answers within didn't seem satisfactory.
Then my searches hit gold: Java Access Control Mechanisms (pdf)

A very nice explanation of what was happening, with a simple answer: change my Java method that was being called by the JavaScript to the following:

public String doRecord() {
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
            public String run() {
                return "recording started";

Now my applet records when I hit the HTML button. Sweet.

Friday, March 25, 2011

JavaScript notes: a Firebug puzzle

There it stood, written before me on the page "Globals created with var (those created in the program outside of any function) cannot be deleted."1

Yet in Firebug
var a = 10;
delete a;
console.log("this is a: " + a);
consistently returned:
ReferenceError: a is not defined (Fool!)2

How could this be happening? 

If I reworked the code and ran it as a script in a web page, it gave me "this is a: 10", as expected.

After a bit of research (thank you, Google),  I found the answer. Ironically in a blog entry about another book by the same author that was puzzling me now...

The solution is well worth reading, but I'll see if I can summarize it:
  • Variables are in fact properties with a "don't delete" flag set.
  • Variables declared in JavaScript code that would otherwise be global, when executed by an eval call are in fact deletable (the flag isn't set)!
  • Those deletable variables are added to the calling object's variable object, for want of a better place to put them.
Firebug appears to be executing code entered into the console via an eval call, hence my strange power to be able to delete global variables. No black magic here!

 Of course this answer poses another question: why are global variables that are in code executed in an eval context deletable?

I think that the following code might propose an answer:
(function test() {
   eval('var wtf = "wtf";');
   console.log(delete wtf);    // true

When you evaluate code you will possibly have variables added to your calling context that you might not want there. By marking them as deletable, you have a chance to clean them up. Now to work out how you can do this automatically...

1 Page 12, JavaScript Patterns by Stoyan Stefanov, September 2010: First Edition.
2 Fool! wasn't really part of the message: that is just how I felt when I saw the error.

Wednesday, March 23, 2011

What am I doing?

Good question! This entry is not going to be the answer to a deeply philosophical question. It's just a simple description of what's happening my life at the moment.

My current journey was started last year when my wife, Terry, saw an interesting advertisement in her physiotherapy journal. Due to very sad personal circumstances someone was being forced to sell a web site that they owned. We made enquiries and found that not only was the asking price too much for Terry and I to pay, it also seemed, for various reasons, that the data that underpinned the site was not as complete as we would have liked.

As Terry and I explored the opportunity, we talked about what we thought would make a profitable web site. In the midst of our conversations, Terry came up with a what seemed to be a great idea for a web site.

As the year ran out we found that we spent more and more of our time talking about her idea – and so started doing some basic research into the idea. The more research we did, the sounder the idea looked.

So at the end of the year I resigned and came home to work for my wife. On a very unsalaried basis, I must admit. Terry and I calculated that we had enough savings in reserve to keep me at home for just under a year, all going well.

Shortly before I finished up with my work Terry had received some quotes from landscapers to do some cleaning up and rearranging of the garden. The quotes ranged from between 30 to 70 thousand dollars!

We were so confident of Terry's site idea that we decided the first few weeks of my new work would be dedicated to cleaning up the garden.

So first came down the horrible old grey shed in the back garden.

Our grey monster!

There were several trips to the tip!

Was made of more 'stuff' than we thought...

Then in went some retaining walls and a lot of back fill. 

Any wonder I am loosing weight?

Then the builders came and put a new pair of windows into our house.

Our work is just about to start.

We lined and painted the room ourselves. 

Need some lining done?

To me the painting was truly horrible work. It just seemed to mindlessly go on and on without end. I have decided that painting is a special form of torture. But after what seemed like ages the room was finished.

At last!

We were now into the start of March. And although we were far from achieving all we aimed to do in the garden, I really wanted to return to programming. 

Yes. Typical software developer. Underestimated the time it would take.

So we agreed that we would work in the garden and on the house over weekends, as most people do, and I would start to construct Terry's site.

I created some rough paper prototypes, discussed them with my client (Terry), and then based on our discussions built some quick and dirty html mock-ups. We took the mock-ups, played with them, discarded one approach and then settled on a path forward.

I am now working on the technology stack to be used whilst Terry works on creating and building the content.

In creating the content, it would appear that Terry has found the first major wrinkle in our plans. That is that under Australian legislation it would appear we might well have to register the site as a medical device! If true this will throw a whole heap of unexpected expense and overhead our way, which may well render the original idea unviable.

It might well be time for our first course correction!

One that will possibly affect the technology stack I had chosen.

As it looked as though we were going to be creating a web application, I had settled on JavaScript/jQuery and HTML 5 (that degrades gracefully) on the client and Rails as the quick (and dirty) back end. Now if we are going to deliver a site full of content I think we might better be served with Drupal on the server.

As Terry is producing the content, and as we are trying to work out how best to navigate Australian legislation, I am bringing myself up to speed on JavaScript, jQuery and HTML 5. For sadly, as a regular employee working in the Java environment, I had never had the time to truly master JavaScript. I can now see that there is a huge amount that I had missed in the osmotic learning of a salaried environment!

Tuesday, March 22, 2011

Paulo's Postulate

Who cares? At this rate by 2020 the IP lawyers will have strangled the whole software scene. Any slow down on the hardware will struggle to match the slow down that the patent lawyers and music/movie industries will have brought to the table :-(