News for hackers. Stuff that matters.
Re:Boot a linux kernel right inside your browser. by dkersten on Tuesday May 17, 2011 @03:53PM (#2557604)
I've always wished that, instead of a "browser language", browsers exposed a LOW LEVEL bytecode environment (which browsers are then free to interpret, JIT compile or on-the-fly AOT compile at page load time). It should be reasonably low level, in that it should provide an abstraction from the hardware, but still allow for very efficient execution of lower level code. Then high level languages and frameworks could be layered on top. This way, browser languages would be 1) more efficient if they need to be and 2) more diverse.

Its a pity that javascript is so in-grained in client-side web now, as its really a terrible bytecode... Hell, failing the above, I would have liked to see Lua as the default client-side language, rather than Javascript (for reasons I will mention shortly), but of course that will never happen now that Javascript has been the default for fifteen years.

The reasons I would like Lua as the default language, besides the fact that LuaJIT is very fast (certainly for number crunching code, especially if you use the FFI to operate on low level structs (I mean, without calling into C) - the FFI is also one of, if not the, simplest FFI I've seen in any language[1]), it is, at least under my impression, a much more consistent language, while being just as flexible, easy to learn and dynamic. The thing I dislike most about Javascript is that it has lots of little gotchas and inconsistencies. Having said that, though, perhaps Lua does too and I just haven't used it enough yet to get bitten by them. Then again, it didn't take long for this to happen in Javascript, so Lua does at least seem to be a little friendlier.

[1] Actually, I think Factor's FFI is similarly simple, if my memory serves me correctly

Re:Boot a linux kernel right inside your browser. by copper on Wednesday May 18, 2011 @12:20AM (#2559042)
Factor, Racket, and Lua all have ffi's that are very easy to use. I've never quite liked Lua's metaprogramming, though (macros are very useful when you're writing bindings.) and my least favourite Lua quirk would be the lack of integer types - both other languages have fairly sane (and performant!) numeric towers.

That said, I'd give a higher priority for the language in my browser to be safe: some kind of correctness guarantee would be nice. Or even, while we're at it, some kind of way to analyze the script and say "this script GETS from urls x,y, and posts data d to z"

Re:Boot a linux kernel right inside your browser. by dkersten on Wednesday May 18, 2011 @06:32AM (#2559669)
Standard Lua may not have integer types, but as far as I know, LuaJIT does actually support integer types under the hood. At the very least, you can force it by declaring an integer using the FFI, eg:

    local ffi = require("ffi")
    int = ffi.new("int")
    int = 10
    print(int)
I also don't find the lack of macros in Lua as limiting as I do in other languages. For example, if I wanted to create an EDSL in Lua, I would have it parse a string at startup. For example, imagine a state machine DSL:

    fsm = my_dsl[[
        start: foo
        stop:  quux
        foo -> bar
        foo -> baz
        bar -> foo
        baz -> quux
    ]]{ foo = function()
            code for foo here
        end,
        bar = function()
            code for bar here
        end,
        baz = function()
            code for baz here
        end,
        quux = function()
            code for quux here
        end
    }
    fsm.run()
As a real-world example, here is some Lua code I'm using in a hobby game project:

    DEFINE(Traits.Position)[[
        float x;
        float y;
        float z;
    ]]
    DEFINE(Traits.Movement)[[
        float x;
        float y;
        float z;
    ]]
    DEFINE(Behaviors.do_movement){
        input={Traits.Position, Traits.Movement},
        output=Traits.Position,
        frequency=0.01,
	
        -- Update entities position
        fn = function (pos, mov)
            local delta = dark.timedelta
            new_pos = Traits.Position.new()
            new_pos.x = pos.x + (mov.x * delta)
            new_pos.y = pos.y + (mov.y * delta)
            new_pos.z = pos.z + (mov.z * delta)
            return new_pos
        end}

    -- Create a sample entity
    local entity = Entities.new()
    entity.add(Traits.Position, {1.0, 0.0, 0.0})
    entity.add(Traits.Movement, {0.0, -1.0, 0.0})

"That said, I'd give a higher priority for the language in my browser to be safe: some kind of correctness guarantee would be nice. Or even, while we're at it, some kind of way to analyze the script and say "this script GETS from urls x,y, and posts data d to z""

I definitely agree with the above.

Re:Boot a linux kernel right inside your browser. by leoc on Wednesday May 18, 2011 @01:12AM (#2559128)
> Its a pity that javascript is so in-grained in client-side web now, as its really a terrible bytecode...

Gilad Bracha complains about this at http://gbracha.blogspot.com/2011/03/truthiness-is-out-there....