byte manipulation in Javascript = not fun
Mood: irritated
Posted on 2011-02-24 09:22:00
Tags: palmpre projects work programming
Words: 191

Google's two-factor authentication is a really good idea, so I'm trying to port the Google Authenticator to webOS. This involves calculating hash values, etc. in Javascript, which is a giant pain because Javascript knows nothing but signed 32-bit integers and 64-bit floating point datatypes. So every time I look at the sample Java code and it uses a byte[], that means I have to manually convert to the -128..127 range. And the sample code does some things like converting 4 bytes to an int, which is easy in Java but a pain in Javascript.

Anyway, finally I had the bright idea to actually compile the Java code so I could compare the results, because I generated at least twenty different codes trying various permutations of things, none of which were right.

Another thing I learned! If you return an error string from a constructor, this does essentially nothing. You still get a constructed object. So, if your library is doing this, there's a 99% chance that no one's going to notice without a lot of pain in suffering.

Adding to my troubles: I am involved in some heavy yak-shaving at work.


6 comments

Comment from spchampion:
2011-02-24T10:30:21+00:00

RE errors in constructors - isn't that the whole point behind exceptions? Because the only thing that a constructor can and should return is a pointer/object reference?

Comment from gregstoll:
2011-02-24T10:33:10+00:00

Yeah, after I thought about it it made sense. It just seems a little weird that you can return something from a constructor that has absolutely no effect. I'm pretty sure if you tried to do the same in a compiled language you'd get a compile-time error saying "whatever you're trying to do, it isn't going to do anything"...

Comment from spchampion:
2011-02-24T16:01:46+00:00

For what it's worth, here's PHP's take on the world:


[stantonc:~] stantonc% php -v
PHP 5.3.3 (cli) (built: Aug 22 2010 19:41:55)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[stantonc:~] stantonc% php
<?php
class Foo {
public function __construct() {
$this->variable1 = "foo";
return "bar";
}
}

print_r(new Foo);
^D

Foo Object
(
[variable1] => foo
)

Comment from gregstoll:
2011-02-24T16:16:34+00:00

Hmm, so the same thing...maybe I'm just wrong :-)

Comment from spchampion:
2011-02-25T14:20:55+00:00

Actually, if I do print instead of print_r, I do get an error.

Catchable fatal error: Object of class Foo could not be converted to string in - on line 8

print_r is for dumping any kind of data structures, whereas print should just print strings. But the bigger issue remains - the return statement in the constructor is totally ignored and unreported.

Comment from omega697:
2011-02-24T13:35:47+00:00

+1 for 2-factor!

This backup was done by LJBackup.