Calling other languages from love2d?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Calling other languages from love2d?

Post by Skeletonxf »

Say I wanted to mix Lua with another language such as Haskell, I've found https://wiki.haskell.org/HsLua and to me it looks like using two languages each with a C FFI would probably not be quite so hard as mixing other sorts.

Can I / how would I go about setting something like this up from within a love2d program, and would such an implementation be easily cross compatible? Ideally I'd like to write my code once and be able to get it to easily it run on desktops and mobile.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Calling other languages from love2d?

Post by raidho36 »

And just why would you do that? Lua is already the single best language for this type of job. It's the fastest, most flexible, and easiest to integrate into parent program, which in context of the LOVE framework have already been done for you. Some functionality may be available as a dynamic link library (aka shared object) and LuaJIT had the FFI to load those.
dixita
Prole
Posts: 5
Joined: Fri Mar 10, 2017 9:10 am

Re: Calling other languages from love2d?

Post by dixita »

Hei, i also want to know that, why would you do that?
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Calling other languages from love2d?

Post by MasterLee »

raidho36 wrote: Fri Mar 24, 2017 4:45 am Lua is already the single best language for this type of job.
Source please
raidho36 wrote: Fri Mar 24, 2017 4:45 am It's the fastest, most flexible, and easiest to integrate into parent program, which in context of the LOVE framework have already been done for you. Some functionality may be available as a dynamic link library (aka shared object) and LuaJIT had the FFI to load those.
There are many shortcomings in Lua http://nekovm.org/lua

BY the way hsLua is an Lua Interpreter written in Haskell. So you can use Lua in Haskell as an scripting language.
For other language for example Red the is no sense in embedding Red in any other language or embedding any other languages in Red. Because you can do both sides in Red.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Calling other languages from love2d?

Post by Tjakka5 »

The link you provided only lists differences, not shortcomings.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Calling other languages from love2d?

Post by MasterLee »

Tjakka5 wrote: Fri Mar 24, 2017 12:56 pm The link you provided only lists differences, not shortcomings.
Ok lats make it short Lua has no native integer support.
And the Arrays starting with one are a little bit painful sometimes.
For example when you have an canvas of width 800 i goes from 0 to 799 in löve2d but arrays goes from 1 to 800 by default.
Next when you wrap arround you could write

Code: Select all

pos=(pos+delta)%size
in cpp
but in Lua it becomes

Code: Select all

pos=((pos+delta-1)%size)+1
when using 1 based Arrays
Last edited by MasterLee on Fri Mar 24, 2017 5:11 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Calling other languages from love2d?

Post by zorg »

MasterLee wrote: Fri Mar 24, 2017 2:19 pm Ok, let's make it short; lua has no native integer support.
The arrays starting with one are a little bit painful sometimes;
For example, when you have an canvas of width 800, i goes from 0 to 799 in löve2d, but arrays go from 1 to 800, by default.
Next, when you wrap around, you could write

Code: Select all

pos=(pos+delta)%size
in cpp, but in lua, it becomes

Code: Select all

pos=((pos+delta-1)%size)+1
when using 1-based arrays.
Löve comes with luaJIT; luaJIT comes with an FFI interface; The FFI interface allows you to define C types on the lua side; now you have integers, arrays, structs, unions, etc...

Nothing is forcing you to use 1-based indexing, just forget about ipairs and #; for loops will work with whatever limits you give it. (And yes, the 1-based modular arithmetic does tend to get on my nerves as well, but again, you can use 0-based indexing if you wish, it won't incur any performance penalties or anything.)

(And yes, i'm assuming that the discussion was about Löve (or at the very least, luaJIT), not just plain lua. :3)

Also, that nekoVM site's claims about lua are interesting, and in some places either a bit counterintuitive, or (at least to me, seemingly) wrong.
nekovm.org/lua wrote:bool in Lua (true and false) is similar to the Neko bool except that in Neko only one instance of true and one instance of false are allocated and shared by all the code.
Not sure what is being implied here; i'm pretty sure lua doesn't "instance" booleans either. Maybe it's just talking about how neko's runtime representation mentioned below is somehow more optimal with them? (It would still need to refer to them, so i believe a pointer may have a larger (or equal) footprint than just storing a representation of the boolean true or false... i could be wrong though.)
nekovm.org/lua wrote:One large difference is that Neko's strings are mutable, that is, they can be modified at runtime. This makes an operation like reading a file to a string byte by byte a fast, efficient, linear operation. By contrast, if you were to try and do this in Lua (which has immutable strings), the operation would be quadratic and become prohibitively slow for even files of a few kilobytes. Lua offers a facility, table.concat, to alleviate this problem; but it still bites programmers on occasion.
I mean, sure, if you're reading a file in one byte at a time, even C tends to choke on it... reading n bytes at once with io or Löve's own filesystem stuff is fine as it is; also, immutable strings are a problem with concatenation, reading in n bytes from a file at once won't concatenate the input byte-by-byte. No need to use tables either, if you want to preserve memory, and are fine with one string.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Calling other languages from love2d?

Post by airstruck »

Skeletonxf wrote:Can I / how would I go about setting something like this up from within a love2d program, and would such an implementation be easily cross compatible? Ideally I'd like to write my code once and be able to get it to easily it run on desktops and mobile.
I think the most straightforward way would be to write some "foreign export" declarations, compile your Haskell code as a shared library (.dll/.so) and then use it though LuaJIT's FFI. Cross-compilation might be a bit of a pain to get set up, but I don't see any way around it (unless you actually want to compile on every architecture you target).

Lua and Haskell are so fundamentally different that it almost doesn't make any sense to compare them. You don't need to find flaws in Lua for Haskell to make sense. If you already use Haskell or just want some experience with an FP language, I see nothing wrong with using it for some of the heavy lifting.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Calling other languages from love2d?

Post by raidho36 »

Not that you cannot do heavy lifting with Lua, all thanks to LuaJIT's FFI. In good case scenarios, which is almost always, it provides comparable performance to C.
MasterLee wrote: Fri Mar 24, 2017 9:11 am Source please
You mean other than it's the language of choice of many major games and studios and vast multitude of smaller projects*, who are all citing speed, flexibility and ease of use as the reasons? Please.

Also yes the arguments in the links are not shortcomings, they're just the list of things some people personally don't like but that don't really make any difference.

*if you don't count the special snowflake ones that go out of their way to be different
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Calling other languages from love2d?

Post by airstruck »

raidho36 wrote:Not that you cannot do heavy lifting with Lua, all thanks to LuaJIT's FFI. In good case scenarios, which is almost always, it provides comparable performance to C.
I assume you mean LuaJIT's JIT? Outside of simple things like one-off C structs, the whole point of LuaJIT's FFI is calling native code that was written in compiled languages like C or Haskell.

Not everything is about performance. Sure, LuaJIT performs quite nicely thanks to JIT compilation (usually, if you know which things will compile and which won't). The point of using something like Haskell isn't just performance characteristics. It gives you type safety though implicit static typing, lazy evaluation, immutable everything, everything is a function, every function is pure, etc. It's basically impossible to get lost in state transformations or side effects in a purely FP language the way you can in imperative languages. You'll find simple and elegant solutions to many problems that really have no analogy in imperative languages. Consider something like `factorial n = product [1..n]`, for example.

Yes, you can do something similar in Python, but I don't consider it to be "strictly" imperative like Lua; you have things like list comprehensions, operators-as-functions, and things like "reduce" built in. At any rate, you can't do it in Lua.

@Skeletonxf, go on, learn you a Haskell, you won't regret it.
Last edited by airstruck on Fri Mar 24, 2017 9:11 pm, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 43 guests