PDA

View Full Version : Lua



JosephE
25-08-2010, 06:41
I've been tirelessly working on a simple interpreter for one of my projects that I need to allow my users write code for. Then, I discovered Lua. Since thinBASIC has had a module for everything I could think of so far, I decided it wouldn't hurt to search the forums. And to my surprise, you guys have a lua module (2007): http://community.thinbasic.com/index.php?topic=1049.0

I really need to have Lua call my thinBASIC functions for built in operations for my product, but the thread says that isn't supported. I realise it's now 2010 and I'm wondering if thinBASIC has support for C callbacks and what you guys think about all this.

Any opinions, options, or ideas? Lua blows my little interpreter away so I'd really like to see what other options I have.

Thanks guys! :D

Michael Hartlef
25-08-2010, 07:29
Hi Joseph,

wow, very old stuff you digged up here. But I will see what I can do for you. I have a personal need for it too, so I will look into this again. Thanks for bringing it into my memory.

Michael

JosephE
25-08-2010, 08:18
Thanks Michael. I think lua would be really great to have with callback functions but I would think you would be limited by thinbasic's lack of function pointers. Or maybe thinbasic does have them and I just don't know about it yet. If callbacks could work, would lua execute code asynchronously so I could be doing something else in thinbasic? Of course I would imagine thinbasic would interrupt me with lua's callbacks but after that it should return to wherever I left off in thinbasic execution. I would hope.

This would be so very helpful if this could work this way. *hopeful anticipation*

Michael Hartlef
25-08-2010, 09:59
I am not sure about the callback functions. We have now the ability to store and call thinbasic function pointers from/inside modules. But I don't think they could run asyncron. They might be but I imagine this being not safe regarding the state of the underlying thinbasic app. I have to test this.

What app are you writing that you need plugins for? I ask because thinBasic can include scripts at runtime and it could be used as plugins.

JosephE
25-08-2010, 16:47
I'm not so much needing plugins. I'm creating a graphical environment framework (as sort of an experiment) and i need my users to be able to develop their own "programs" within it, if that makes sense.

Even if it doesn't work async, I think ill be able to make good use of it...although being able to run several scripts at once would be awesome, but I'm not even sure lua supports that.

Michael Hartlef
25-08-2010, 20:11
Your project sounds interesting. Let us know when it is show off able :)

About Lua, I am not 100% sure about it, but I think that a Lua thread does not support running several scripts at the same time. You could open more threads, but they wouldn't know of each other.

JosephE
25-08-2010, 20:42
Your project sounds interesting. Let us know when it is show off able :)

I sure will. It's a rather large undertaking, but thinBASIC has had everything I need so far. There is a lack of documentation on some of the modules, but the examples spot for it.


About Lua, I am not 100% sure about it, but I think that a Lua thread does not support running several scripts at the same time. You could open more threads, but they wouldn't know of each other.


That would be fine, as long as callbacks could work. The programs my users write don't really need direct communication with each other, and my thinBASIC program could act as a inter-process communication mediator (using callback functions that store global data) if I needed it.

Please keep me posted with any news you have. I appreciate your help so far.

If callbacks aren't possible yet, just let me know and I'll resume work on my interpreter engine. Meanwhile I'll be working on my front-end. :)

Michael Hartlef
25-08-2010, 22:50
Can you please explain, what you need the callbacks for. Just a simple example so I get the picture.

JosephE
25-08-2010, 22:55
Sure. My user might want to draw some text on the screen, so he would use a function in lua that calls my thinBasic code back to do that. I hope that clarifies it a little. Most of my user's lua code will be centered around drawing functions.

Michael Hartlef
25-08-2010, 23:08
So you basically write an API inside thinBasic which you then call inside the Lua script.
Thanks, I will let you know about the progress.

JosephE
25-08-2010, 23:10
Yep, that's right. I appreciate it, and I hope all goes well on it.

Michael Hartlef
28-08-2010, 09:12
Hi Joseph,

I have now a good idea how to implement calling back thinbasic functions. The problem right now is that I have difficulties to interface LUA
inside a thinbasic module. The newest Lua version depends on the VC Runtimes of MS, maybe that is way. The module seem to load with a return code of 12 (Eros??)
but I can't seem to be able to load the libraries. Means I can't see if the code is running or not. I will let it rest for a few days and than will work on it again, to go furtehr with a fresh mind.

Michael

ErosOlmi
28-08-2010, 11:43
Michael,

regarding module loading sequence
please have a look at thinBasic help for USES keyword: http://www.thinbasic.com/public/products/thinBasic/help/html/uses.htm

If number is positive, it indicates the directory where the module has been found. See table below for path sequence.
So looking at the mentioned table in help, 12 means that your module has been found in "thinBasic path + Lib" and that is correct
thinBasic will never fall into Windows DLL hell because thinBasic Core engine will always know where to load thinBasic module and will never spread dll over the operating system directories like many other applications do.


Now to Lua DLL.
thinBasic loads all modules with a precise sequence as described into the above USES help. So for your modules there will be no problem.
But when you use Lua DLL into your module, unless you manually load your DLL using API function LoadLibrary (http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx) you will fall into the classic Windows DLL hell that is: the search of your Lua DLL will be passed to the operating system. This is true when you use DECLARE statement in PowerBasic code. DECLARE will search external declared function using operating system rules.

The Windows operating system follows a very precise sequence that is described here: http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx

So if you are using DECLARE for Lua functions, you have to put Lua DLL in same directory of thinBasic.exe otherwise your module will not be able to find Lua dll.
Otherwise you have to manually load Lua functions at runtime using LoadLibrary and GetProcAddress APIs. In this way you will be free to get Lua DLL from whereever you prefer. And that's why thinBasic Core engine is passing to LoadLocalSymbols module function the Optional ByVal sPath As String parameters that is the path where thinBasic has found your module.

I hope this can be of some help.

Ciao
Eros

Michael Hartlef
28-08-2010, 11:55
Thanks Eros,

that explains it. I have the LUA.DLL inside the scripts own folder. And I use declares. :oops:

Michael

ErosOlmi
28-08-2010, 15:29
Michael,

in this case maybe it can be worth to check TBASS module source code present in SVN server where I've used a technique to load an external DLL (BASS.DLL) from your module resource (or from a string buffer) remapping the DLL functions you need.
It seems complex at first but later you can have a lot of advantages because your module will not have any dependancies and you will be sure about external DLL because will be included inside your module.

Ciao
Eros

JosephE
28-08-2010, 18:03
Hi Joseph,

I have now a good idea how to implement calling back thinbasic functions. The problem right now is that I have difficulties to interface LUA
inside a thinbasic module. The newest Lua version depends on the VC Runtimes of MS, maybe that is way. The module seem to load with a return code of 12 (Eros??)
but I can't seem to be able to load the libraries. Means I can't see if the code is running or not. I will let it rest for a few days and than will work on it again, to go further with a fresh mind.

Thanks for all the hard work. Please keep me posted with your progress or other information. I'm really hoping this works out well.

ErosOlmi
28-08-2010, 18:43
Michael,

just in case you missed, José did some experiment with Lua and PowerBasic.

Lua headers:
http://www.jose.it-berater.org/smfforum/index.php?topic=1998.0

Lua examples:
http://www.jose.it-berater.org/smfforum/index.php?board=231.0

Michael Hartlef
28-08-2010, 23:49
Thanks Eros, I saw this already!

Michael Hartlef
05-09-2010, 00:51
Hi Joseph,

tonight I finally made some progress. I am using FreeBasic now and was able to create a module which can run lua scripts. You don't need the LUA dll for this as everything is included inside the module i guess.

Next will be the implementation of callbacks for thinbasic functions. Maybe tommorow or next week. But I think it will work. At first I was pulling my hair out as I could not see the PRINT output from LUA in the console window. When I used my include files from 2007, you can see them. Not with the module. Then I tried using a thinbasic console script and there it works. Go figure.

Anyway, just wanted to let you know that I am still working on it.

Cheers
Michael

JosephE
05-09-2010, 20:40
Wow Michael, that's great! I'm super excited, and thank you for all the hard work.

I'm wondering, will it be possible to have multiple instances of lua scripts executing at the same time (or perhaps toggle between each one for a set amount of time [multitasking]?)

Michael Hartlef
05-09-2010, 22:00
Nope, Lua does not support multitasking.

Michael Hartlef
09-09-2010, 10:16
Current status: :cry:

I thought you could retrieve the calling LUA function name from the LUA stack somehow but I was wrong. At leats everything I tried os far does not give me the result I need.

My solution for registering TB functions for LUA is/was that I store the TB function name with the LUA function name inside a list. Registered inside LUA is a function of the module, which then tries to determine the calling LUA function, search through that list and then calls the TB function.

Anyone here who knows how to retrieve the damn LUA function name? :(

Michael Hartlef
09-09-2010, 10:44
Maybe I can use Upvalues.... will try that tonight.

Edit: Joseph, which function from the LUA headers would you need atm?

Michael Hartlef
09-09-2010, 20:31
Ok,upvalues did the trick :)

JosephE
10-09-2010, 03:13
I'm not very experienced with lua, some I'm not sure *what* i need. I know I need Lua programs to call my thinbasic functions, but that's all I know.

I also know I need to be able to run multiple programs at once (something you said Lua doesn't support). I'm starting to wonder if Lua isn't a solution for my situation. Is there a way to be able to save the state of a lua program, load another one, execute it for a short amount of time, and then go back to the other one? In this way perhaps I could achieve what I need. If not, I'll come up with something that will work. But I would hate not to be able to use what you've worked so hard on.

Michael Hartlef
24-09-2010, 22:48
Hi Joseph,

here is the first version of the module. Only a few commands are implemented atm as it is in an experimental stage.

Play with it and see if it can help you. I included all the freebasic sources too so you can make your own additions. It would nice if you can give some feedback. Just copy the DLL to the libs folder of thinbasic.

Cheers
Michael

JosephE
25-09-2010, 08:04
Wow, Michael that is absolutely amazing! Thanks so much for working on that! I'm impressed you've got it working so far. :)

Michael Hartlef
25-09-2010, 08:27
Thank you for your kind words. :)

JosephE
28-02-2011, 23:34
Michael, I've been playing with this Lua thing lately and i'm still quite impressed.

Is there a way to get the arguments that are passed to the thin basic functions from Lua?