Ask HN: Architecture for embedding a language in my program

11 points by maxpth 2 months ago

I've been wondering about embedding a programming language in my application (Lua, Lisp or something else), what are some good resources related to this topic? I'm more interested in the overall architecture e.g. providing an API to be used by the embedded language, knowing what should go into the host/guest, how the communication happens between the program and the embedded etc.

dtagames 2 months ago

Lua is terrific and made for exactly what you're doing. It's also used in production in a number of big UGC apps like Roblox and others. The Lua documentation is sparse but pretty complete and the PIL book is considered the standard. See what you think... [0] I played with embedded Lua while writing my mods[1] for the Steam game Foundation and fell in love with the language.

[0] http://www.lua.org/pil/24.html

[1] https://mod.io/g/foundation/m/barrel-ofish

  • brezelgoring 2 months ago

    I got your game in a Humble Bundle some seasons ago and it’s been great! Love designing gardens and elaborate buildings in it, great work!

ysleepy 2 months ago

I'd probably just embed a small js engine. In case it causes trouble you can switch to another implementation.

The hint about loose coupling is good, you want to be able to evolve your app without breaking the plugins/macros.

Decide who is in control. Does the plugin run in the background or may only provide functions the app calls.

make sure you can limit the runtime of the execution of plugins, in case you want to ensure that plugins can't hang/crash your app.

Also think about security, depending on the use case it might become an entry point for malware.

frou_dh 2 months ago

IMO the straightforward part is coming up with functions to expose from the host program to the scripting language. But what's trickier is figuring out which of the program's "domain" datatypes to expose, and if/how the scripting language should interact with such values as anything more than opaque handles.

b20000 2 months ago

read the lua embedding documentation