Wednesday, January 26, 2011

Problems with key-value store via JavaScript API

Problems with key-value store via JavaScript API

For security, concurrency and high scalability reasons we wanted to expose a key-value store, which would be implemented by us in Erlang and use Mnesia, as a convenient API for the game programmers in JavaScript.

I wanted for example have a "world" object, like for example the "document" object in browsers. You should be able to use this object like every vanilla JavaScript object, except that it would not store functions, only primitive objects {}, arrays [], strings "foo" and numbers 1.63.

You would set and get objects like this:

world.foo = "abc";
console.log(world.foo) // returns "abc"
world.bar = { "myArray": [99,33,1] };
console.log(world.bar.myArray[1]) // returns 33

and so on. Every time you save something in this object, we would need to call a setter method which would call a erlang function which would save the data in our erlang-database. The problem is, that this seems not possible and easy as I thought in todays JS-VMs.

At first I thought about getters and setters in JavaScript, but if you have a look at the code, then you see that you already have to know the name of your properties.

What we would like to have are something like "default getters and setters" which would be called even if the property isn't there yet. There is the non standard __noSuchMethod__(id, args) in Mozillas spidermonkey, but it only is called on methods world.undefinedFun(), not on properties like world.undefinedProperty123 = "test".

I talked to the guys in the irc://irc.epd-me.org/#selfhtml, they often know a lot about JavaScript, and got a link to something very promissing, but sadly not implemented yet, harmony proxies (bugtracker) in Googles V8 JavaScript engine. In the bugtracker comments you find a link to node-overload which is node.js addon that provides an Watchable class that has getters/setters without property names. And node.js is a server running in V8. I don't know if we'll be able to use it, first need to have a deeper look at it, but it looks like it would do what we want.

Time report

16 hours

2 comments:

  1. Don't worry! I'm from the internet and here to help!

    Two better links:
    http://brendaneich.com/2010/11/proxy-inception/
    http://prog.vub.ac.be/~tvcutsem/proxies/index.html

    ReplyDelete
  2. I tried your second link in Firefox 4 beta, ttepasse, and it seemed to work really well. This would have been a nice solution to the problem, but here the next problem arises, the erlang spidermonkey bindings do not allow callbacks from JS to erlang. We'd have to implement it on our own. But the good news is that the developer offered to help to point us in the right direction.

    ReplyDelete