Linking LPeg

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Linking LPeg

Александр Машин
Good time of day,

I am trying to  link LPeg library to a very restrictive Lua environment
(which is luasandbox Lua engine for PHP written in C++,
https://github.com/wikimedia/mediawiki-php-luasandbox), which, in
particular, reloads require () function.

I managed to achieve this, more or less, by running this C++ code:

extern "C" int luaopen_lpeg(lua_State*);
...
lua_pushcfunction(L, luaopen_lpeg);
lua_call(L, 0, 0);

After that, I get lpeg table in my global namespace and can call
lpeg.P(1), for example.

Unfortunately, getmetatable (lpeg.P(1)) == nil, which makes the library
useless. The metatable seems to be lost or have benn overwritten by some
paranoid sanitation code in luasandbox.

Is there a way to restore or export to Lua global namespace it in C++
somewhere before or after lua_pushcfunction(L, luaopen_lpeg) call?

I will appreciate any help.

Alexander Mashin

Reply | Threaded
Open this post in threaded view
|

Re: Linking LPeg

Pierre-Yves Gérardy
On Tue, May 19, 2015 at 9:07 PM, Александр Машин <[hidden email]> wrote:
> Good time of day,
>
> I am trying to  link LPeg library to a very restrictive Lua environment
> (which is luasandbox Lua engine for PHP written in C++,
> https://github.com/wikimedia/mediawiki-php-luasandbox), which, in
> particular, reloads require () function.

I'm not familiar with the MediaWiki sandbox, but did you give LuLPeg a
try? It is a pure Lua implementation.

https://github.com/pygy/lulpeg/

If the sandbox is based on Lua 5.1 and disables `newproxy()`, you'll have to use

    LuLPeg.L(LuLPeg.P(...))

rather than

    #LuLPeg.P(...)

Otherwise, it is compatible, but slower.

—Pierre-Yves