What languages could be used to implement the functions to be invoked by Lua?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

What languages could be used to implement the functions to be invoked by Lua?

孙世龙 sunshilong
Hi, list

As per the documentation(https://www.lua.org/pil/24.html), which says
that[emphasise mine]:
At the same time, a program that uses Lua can register new functions
in the Lua environment;
such functions are implemented in C (or **another language**) and can
add facilities that
cannot be written directly in Lua.

My question is what other languages?
Can these functions be implemented in C++? If not, is there any
workaround to achieve this goal?
If you could give some simple examples, that would be great.

I would be grateful to have some help with this question.

Best regards
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: What languages could be used to implement the functions to be invoked by Lua?

Francisco Olarte
On Fri, Sep 18, 2020 at 10:59 AM 孙世龙 sunshilong <[hidden email]> wrote:

> As per the documentation(https://www.lua.org/pil/24.html), which says
> that[emphasise mine]:
> At the same time, a program that uses Lua can register new functions
> in the Lua environment;
> such functions are implemented in C (or **another language**) and can
> add facilities that
> cannot be written directly in Lua.
> My question is what other languages?
> Can these functions be implemented in C++? If not, is there any
> workaround to achieve this goal?
> If you could give some simple examples, that would be great.

Once compiled, everything is machine code. You only have to worry
about the calling convention ( where are parameters passed and how are
values returned ). Also, Lua uses some C-oriented data ( like null
terminated strings ), so your language must be able to manage them,
but nearly every language can.

Regarding C++, I do all my modules using it. I embed the modules and
do not use dynamic loading, so I do not care about naming ( but using
extern "C" you can easily do it. I just use static functions in my
clases ( I register a static function in lua, this function gets the
object pointer, if needed, and calls a normal method, so I can keep
everyting private if I like to ).

Any language which can call C would do. If your language as bindings
for any of the popular libraries, be it xml, graphics, sound, whatever
chances are it can call ( to use the api functions ) and be called by
( to register extensions )  lua without problem.

Francisco Olarte.
Reply | Threaded
Open this post in threaded view
|

Re: What languages could be used to implement the functions to be invoked by Lua?

孙世龙 sunshilong
Hi, Francisco Olarte

Thank you for your clarification.

>Regarding C++, I do all my modules using it. I embed the modules and
>do not use dynamic loading.
Could you please explain that in more detail for me?

Thank you for your attention to my question.
Sunshilong

On Fri, Sep 18, 2020 at 5:35 PM Francisco Olarte <[hidden email]> wrote:

>
> On Fri, Sep 18, 2020 at 10:59 AM 孙世龙 sunshilong <[hidden email]> wrote:
> > As per the documentation(https://www.lua.org/pil/24.html), which says
> > that[emphasise mine]:
> > At the same time, a program that uses Lua can register new functions
> > in the Lua environment;
> > such functions are implemented in C (or **another language**) and can
> > add facilities that
> > cannot be written directly in Lua.
> > My question is what other languages?
> > Can these functions be implemented in C++? If not, is there any
> > workaround to achieve this goal?
> > If you could give some simple examples, that would be great.
>
> Once compiled, everything is machine code. You only have to worry
> about the calling convention ( where are parameters passed and how are
> values returned ). Also, Lua uses some C-oriented data ( like null
> terminated strings ), so your language must be able to manage them,
> but nearly every language can.
>
> Regarding C++, I do all my modules using it. I embed the modules and
> do not use dynamic loading, so I do not care about naming ( but using
> extern "C" you can easily do it. I just use static functions in my
> clases ( I register a static function in lua, this function gets the
> object pointer, if needed, and calls a normal method, so I can keep
> everyting private if I like to ).
>
> Any language which can call C would do. If your language as bindings
> for any of the popular libraries, be it xml, graphics, sound, whatever
> chances are it can call ( to use the api functions ) and be called by
> ( to register extensions )  lua without problem.
>
> Francisco Olarte.
Reply | Threaded
Open this post in threaded view
|

Re: What languages could be used to implement the functions to be invoked by Lua?

Albert Krewinkel
In reply to this post by Francisco Olarte

Francisco Olarte writes:

> On Fri, Sep 18, 2020 at 10:59 AM 孙世龙 sunshilong <[hidden email]> wrote:
>> As per the documentation(https://www.lua.org/pil/24.html), which says
>> that[emphasise mine]:
>> At the same time, a program that uses Lua can register new functions
>> in the Lua environment;
>> such functions are implemented in C (or **another language**) and can
>> add facilities that
>> cannot be written directly in Lua.
>> My question is what other languages?
>> Can these functions be implemented in C++? If not, is there any
>> workaround to achieve this goal?
>> If you could give some simple examples, that would be great.
>
> Once compiled, everything is machine code. You only have to worry
> about the calling convention ( where are parameters passed and how are
> values returned ).

I think this omits error handling and language run times, both of which
can be a challenge. E.g., HsLua, a Haskell wrapper around Lua, must be
very careful to handle errors at the language boundary. Assume a Lua
function `pcall`s a Haskell function, which then calls another Lua
function. If that last Lua function errors, then the Haskell runtime
system will be corrupted if we `longjmp` across language boundaries.

See <https://hslua.github.io> for a bit more info.

I believe languages like Rust and Go face similar issues.

--
Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124
Reply | Threaded
Open this post in threaded view
|

Re: What languages could be used to implement the functions to be invoked by Lua?

John Logsdon
In reply to this post by 孙世龙 sunshilong
What is the current state of Pallene?

This is meant as a statically typed compiled companion to Lua, written by
the same team that is compatible with Lua, can be called by or call Lua
more efficiently and cleaner than calling C API or FFI.  It also shares
garbage collection.

I am keeping an eye on this - I currently am using luajit 2.1.0-beta3 and
luaLanes for the heavy lifing in a large simulation suite.

Compiling core routines that don't need dynamic typing or lazy arguments
etc may be a route to take.

> Hi, Francisco Olarte
>
> Thank you for your clarification.
>
>>Regarding C++, I do all my modules using it. I embed the modules and
>>do not use dynamic loading.
> Could you please explain that in more detail for me?
>
> Thank you for your attention to my question.
> Sunshilong
>
> On Fri, Sep 18, 2020 at 5:35 PM Francisco Olarte <[hidden email]>
> wrote:
>>
>> On Fri, Sep 18, 2020 at 10:59 AM 孙世龙 sunshilong
>> <[hidden email]> wrote:
>> > As per the documentation(https://www.lua.org/pil/24.html), which says
>> > that[emphasise mine]:
>> > At the same time, a program that uses Lua can register new functions
>> > in the Lua environment;
>> > such functions are implemented in C (or **another language**) and can
>> > add facilities that
>> > cannot be written directly in Lua.
>> > My question is what other languages?
>> > Can these functions be implemented in C++? If not, is there any
>> > workaround to achieve this goal?
>> > If you could give some simple examples, that would be great.
>>
>> Once compiled, everything is machine code. You only have to worry
>> about the calling convention ( where are parameters passed and how are
>> values returned ). Also, Lua uses some C-oriented data ( like null
>> terminated strings ), so your language must be able to manage them,
>> but nearly every language can.
>>
>> Regarding C++, I do all my modules using it. I embed the modules and
>> do not use dynamic loading, so I do not care about naming ( but using
>> extern "C" you can easily do it. I just use static functions in my
>> clases ( I register a static function in lua, this function gets the
>> object pointer, if needed, and calls a normal method, so I can keep
>> everyting private if I like to ).
>>
>> Any language which can call C would do. If your language as bindings
>> for any of the popular libraries, be it xml, graphics, sound, whatever
>> chances are it can call ( to use the api functions ) and be called by
>> ( to register extensions )  lua without problem.
>>
>> Francisco Olarte.
>

Reply | Threaded
Open this post in threaded view
|

Re: What languages could be used to implement the functions to be invoked by Lua?

Viacheslav Usov
In reply to this post by Albert Krewinkel
On Fri, Sep 18, 2020 at 7:40 PM Albert Krewinkel
<[hidden email]> wrote:

> I believe languages like Rust and Go face similar issues.

Every language, including C, faces issues when Lua errors propagate
through code that does not expect that.

Paradoxically, C++ fares better, because Lua can be compiled in the
C++ mode, which will then use the standard C++ facility - exceptions -
for Lua errors, which works well with properly written C++ code. The
drawback is that exceptions may misbehave when they need to propagate
through a layer of non-C++ code, say a library written in C. This is
usually not a problem when libraries are statically linked. Note that
Francisco said he uses C++ and he uses static libraries, which is
probably why he did not mention this.

Cheers,
V.
Reply | Threaded
Open this post in threaded view
|

Re: What languages could be used to implement the functions to be invoked by Lua?

Philippe Verdy-2
you can still capture uncaught C++ exception in C, there are existing mechanism, like SOH and even if your C compiler does not have it, you can get it from OS-level services/API.
Your C code may be part of a C-library having other mechanisms (e.g. in a framework like JNI, or some plugin mechanisms for web browsers, or media players, or SQL clients, or other language integration libraries (PHP, Python, .Net/Mono, PostScript engines, BPF for kernel integration, and so on) which almost always include such C library for their integration You could even have integration of Lua within another Lua acting as an isolation layer...
And if it's not enough, there are also architecture solutions (e.g. interrupt handlers, BIOS interfaces) that you can use in native (or virtualized) assembly.
you need a small generic handler in your C++ library that will forward the exception possibly as an error without necessary crashing the app with some forced exit() and loosing all current state. it's up to you to kwow how you'll recover, or what you'll still want to try saving to recover differently (such as by restarting the process or rebooting the device/OS, and let it perform cleanup at startup according to the state that could correctly be saved).

Le ven. 18 sept. 2020 à 20:34, Viacheslav Usov <[hidden email]> a écrit :
On Fri, Sep 18, 2020 at 7:40 PM Albert Krewinkel
<[hidden email]> wrote:

> I believe languages like Rust and Go face similar issues.

Every language, including C, faces issues when Lua errors propagate
through code that does not expect that.

Paradoxically, C++ fares better, because Lua can be compiled in the
C++ mode, which will then use the standard C++ facility - exceptions -
for Lua errors, which works well with properly written C++ code. The
drawback is that exceptions may misbehave when they need to propagate
through a layer of non-C++ code, say a library written in C. This is
usually not a problem when libraries are statically linked. Note that
Francisco said he uses C++ and he uses static libraries, which is
probably why he did not mention this.

Cheers,
V.