How does the Lua embed work?

Post Reply
Dracunos
Posts: 3
Joined: Fri Oct 19, 2012 5:23 am

How does the Lua embed work?

Post by Dracunos » Thu Oct 18, 2018 8:30 pm

Hello, tldr at the bottom. I just have some general questions about how Mudlet works. I read through the source but I'm not super familiar with the language. Basically, I'm looking into creating a really basic client on android. Emphasis on looking into it. I don't really expect to come out with a finished product, unless it turns out to be easier than I'm thinking it's going to be.

One thing I'm really curious about is how the the script embedding works. I've read through a couple guides on embedding c or lua for speed or whatever else, but I have not found much relating creating a scripting environment for the end user. I was thinking of making this Android client in python using kivy (I know I should probably use java, but I think kivy should be okay for something like this). In which case, the program will be running in a python environment, and it will have a separate python sub-process where all the user's scripts will live.

But how do you create an open and unrestricted(ish) script environment like mudlet has without using a function that is essentially like eval() (the evil function one should never use?)? Basically what eval does is take a string and run it as python code. Taking the users' scripts as strings and running them as code in a different python environment kinda feels similar to eval()-ing, but it's also very different as it's a completely separate environment. I'm probably being overcautious but I am just unfamiliar and don't want to get invested just to find out I'm going about it the completely wrong way.

TLDR: To make a user-scriptable environment (like the user making his own lua code in mudlet), is it pretty much as necessary as I think it is to take the user's code as a string and basically run that string as code in the isolated environment? Are there a lot of necessary safety precautions that mudlet takes in regards to this?

User avatar
Vadi
Posts: 4608
Joined: Sat Mar 14, 2009 3:13 pm

Re: How does the Lua embed work?

Post by Vadi » Fri Oct 19, 2018 2:42 am

Lua runs entirely on its own, and Mudlet injects its functions into it. So there is no eval() running ever, at most Mudlet invokes Lua for the parts that it needs (when a trigger/alias/etc matches) and Lua code runs.

User avatar
SlySven
Posts: 892
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: How does the Lua embed work?

Post by SlySven » Tue Nov 06, 2018 2:08 am

In fact there can be multiple Lua instances running independently of each other - each active profile has a main one that the user and their scripts and packages interacts/runs on and a separate one that is only used to format code in the editor (so that when it tries to compile/execute the code to test it it does not affect the main instance IIRC).

More generally, a complete Lua interpreter is a relative small amount of executable code which can be compiled from it's C source by pretty much any ANSI compatible C compiler.

This is one of the things that makes it an attractive proposition for an embedded system to allow user expansion of an application - what Mudlet does is provide a number of MUD/Mudlet specific extra functions that interface to the C++ core and which are the things documented in the Lua API part of the Wiki!

As for the user script-able environment the user's scripts are indeed taken as a (UTF-8) string and that is wrapped up inside a function call which is then run/interpreted via a protected function call - protected in that if it fails to execute properly, execution is returned to the parent (via a setjump/longjmp mechanism) rather than the parent being terminated in an untoward manner.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests