lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 2017-07-26 10:09 PM, Daurnimator wrote:
On 27 July 2017 at 09:07, Hisham <h@hisham.hm> wrote:
The linked article mentions that the approach of compiling Lua C code
into the browser via Emscripten makes you end up with two garbage
collectors. For this reason, simply building both a C library and its
Lua/C binding with Emscripten and expecting them to work with Fengari
doesn't sound feasible. But the approach I described above, in which
the C library is compiled into JS and a Lua/JS binding is
custom-written for it, does (unless I'm missing something—the Fengari
authors will surely correct me if that's the case).
Actually it's feasible.

The core issue is that a garbage collector needs to be able to sweep
*all* references: when you e.g. compile lua via emscripten the JS GC
has no idea what lua objects reference what. However the lua C api
doesn't create out-of-band references: you create+manage lua
references yourself.

That doesn't mean that it's currently turn-key to get an
emscripten-compiled lua C library to work with fengari. Someone will
need to write a table of emscripten <-> fengari bridge functions (one
for each C-api function).

However, note that fengari doesn't support __gc (which most C bindings
use), and can't until JS supports finalisers.


What stops Lua/Fengari from having its own GC? Just make it so pushing a JS object/lightuserdata finds or creates a lookup table entry for mapping between JS and the GC? And don't expose Lua GC objects? This gives you the exact Lua/C behaviour without screwing up the JS GC. (Heavy userdata may be trickier, tho. Maybe require use of typed arrays or something?)

I don't see why you'd need synced GCs. (I do see why one would want them, because it simplifies implementation. But it's not portable - e.g. for weak tables. Even if you could do a synced GC with JS, would you still be able to do Lua 5.2+ ephemeral tables?)

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.