Friday, September 4, 2009

Javascript Inheritance Gotcha


This is a short warning about inheritance with Javascript :

A = function() {
  this.value=5;
  this.arr = new Array();
}
A.prototype.changeValue = function() { this.value=6; this.arr.push(4);}
A.prototype.getValue = function() { return this.value;}
A.prototype.getArray = function() { return this.arr; }

B = function() {}
B.prototype = new A;

function doit() {

  var b = new B();
  alert('#1 : '+b.getValue()); // Should be 5
  alert('#2 : '+b.getArray()); // Should be empty
  b.changeValue();
  alert('#3 : '+b.getValue()); // Should be 6
  alert('#4 : '+b.getArray()); // Should be 4

  var b2 = new B();
  alert('#5 : '+b2.getValue()); // Should be 5,
    // because value didn't change for this instance yet
  alert('#6 : '+b2.getArray()); // Should be empty,
    // because this.arr didn't change for this instance,
    // and yet it is not empty, it contains the value of the previous instance
}

So you've be warned ! Javascript inheritance doesn't work well with objects !

Or maybe I missed something...

This happened on firefox3.0 and firefox 3.5 on ubuntu 9.04

This can be dealt with by adding
B.prototype=new A();
also in the constructor of B. Then the constructor of A get called every time B's get called, and the new object addresses are recomputed.
But then the class specific methods are lost. So they have to be declared again.

This lead to the following solution :
B = function() {
  B.buildPrototype();
  this.that=something;
}
B.buildPrototype = function() {
  B.prototype = new A();
  B.prototype.aMethod = function() {...}
  B.prototype.anotherMethod = function() {...}
}
B.buildPrototype();
This way, the new object is built with the correct dynamic inherited properties, and it's prototype methods are not lost.

Tuesday, September 1, 2009

Some tips for pasting output from the console into TeXmacs

I like TeXmacs. It's a cool editor. But it's also a really weird editor.

So here is how I managed to get output from MySQL into TeXmacs, using vim along the way.

If you paste directly the data into TeXmacs, you don't get much. This is because the | gets in the way, and the spaces are reduced to two. Also, you need to separate each line with a blank line to make it clear to TeXmacs that these should be formatted as single lines and not be joined.

Why does it do that ?? Why do you ask ??

So here's the process :

Copy your data from mysql into vi, which would be something like that :
+--------+-----------+
| %      | name      |
+--------+-----------+
|  51.00 | number    | 
|  84.00 | volume    | 
+--------+-----------+

Then replace each | with \|, remove any trailing spaces, replaces every spaces by \{space}, and finally insert a line in between every lines.

The sequence of operation to perform is vi is the following :
:%s/ \+$//
:%s/|/\\|/g
:%s/ /\\ /g
:%s/$/^M/g

To get the ^M, you should hit Ctrl+V Enter, for any properly configured terminal.

To make this into a function, embed that into
function! MySQL2TeXmacs()
%s/|/\\|/g
%s/ /\\ /g
%s/$/^M/g
endfunction
and save it into ~/.vimrc.

You can use the insert command to insert ^M, since in insert mode, Ctrl+V switch to Visual Mode and back.

Then you can perform the transformation with
:call MySQL2TeXmacs()

You can use Tab here.

Copy the result with the mouse, or use gg"*yG if that works, then in TeXmacs, hit the following keys sequence :
\center F7 Ctrl+Y

This should paste your data properly into TeXmacs.

(cr)Happy TeXmacs-ing ! :))

Saturday, August 29, 2009

EasyPHP VirtualHost for Eclipse PDT on Windows

I use eclipse PDT for my PHP development, and EasyPHP as the server on Windows.

And each time I want to set up a VirtualHost for it, I get misled by a forbidden message that I interpret to come from the Windows file permissions.

Here are the instructions to set up a VirtualHost on eclipse.localhost, allowed only from localhost, with directory listing enabled, that I grabbed from a few places.

  1. Add the following line to C:\windows\system32\drivers\etc\hosts :
    127.0.0.1 eclipse.localhost
  2. Add the following to C:\Program Files\EasyPHP 3.0\apache\conf\httpd.conf :
    <VirtualHost 127.0.0.1:80>
      DocumentRoot "C:/Users/xavier/workspace"
      ServerName eclipse.localhost
      <Directory "C:/Users/xavier/workspace">
        Options Indexes
        Order Allow,Deny
        Allow from all
      </Directory>
      ErrorLog "C:\Program Files\EasyPHP 3.0\apache\logs\eclipse.localhost.log"
    </VirtualHost>

The permissions problem, in my case, came from the listing of the directory being disabled by default.

Hope this helps some.

Thursday, August 27, 2009

Flex AIR on Ubuntu 64 bits

In this article, I describe the necessary steps for developing Flash/Flex AIR applications on Ubuntu 9.04 64 bits for free.

Flex SDK
You first need the flex SDK, which is free, and 120 Mb. Unzip it somewhere.
mkdir -p ~/addons/flex/3.4
unzip flex_sdk_3.4.zip -d  ~/addons/flex/3.4
Air SDK
The AIR SDK is included in the Flex SDK, but the binaries are for mac. The good ones can be found in the Air SDK, as well as the air runtime. Copy that into the Flex SDK.
mkdir -p ~/tmp/AIRSDK
tar xfv AdobeAIRSDK.tbz2 -C ~/tmp/AIRSDK/
cp ~/tmp/AIRSDK/bin/* ~/addons/flex/3.4/bin
cp ~/tmp/AIRSDK/runtimes/air/linux ~/addons/flex/3.4/runtimes/air/ -Rfv
Make a sample app
test.mxml :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    horizontalAlign="center" verticalAlign="center">
    <mx:Script>
    <![CDATA[
      import mx.controls.Alert;
      import flash.display.MovieClip;
     
      private function Test(e:Event) : void {
        Alert.show('Blah!');
      }
    ]]>
    </mx:Script>
    <mx:Button id="myButton" label="I'm a button!" click="Test(event);"/>
</mx:Application>
application.xml :
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://ns.adobe.com/air/application/1.5">
    <id>test</id>
    <version>1</version>
    <filename>test</filename>
    <initialWindow>
        <content>test.swf</content>
        <visible>true</visible>
        <width>400</width>
        <height>200</height>
    </initialWindow>
</application>
Make it run
Because the binaries are for 32-bits platforms, you need the ia32-libs package :
sudo apt-get install ia32-libs
Now you can compile your application :
~/addons/flex/3.4/bin/mxmlc test.mxml
And run it :
GTK_PATH=/usr/lib32/gtk-2.0 ~/addons/flex_sdk/3.4/bin/adl application.xml
Information about running 32 bits apps on 64 bits has been found on a debian related article, and the GTK_PATH in a bug report thread
And make a movie clip :)
Simply add the required import and the following code to the event handler.
import mx.core.UIComponent;
import flash.display.MovieClip;

var ui:UIComponent = new UIComponent();
var mc:MovieClip = new MovieClip();
mc.graphics.lineStyle(1,0);
mc.graphics.moveTo(0,0);
mc.graphics.lineTo(50,50);
addChild(ui);
ui.addChild(mc);

Enjoy !