yipengsun / EmbeddedLuaEval

A simple demo to embed Lua in a C++ program

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EmbeddedLuaEval github CI

built with nix

A simple demo to embed Lua in a C++ program.

Motivation

Sometimes we want to define a lot of variables (O(1000)) in C++ and be able to tweak them easily without a recompilation.

A naive attempt would be just define these values in YAML and load them in C++. However, in a more realistic case, those values may depend on previously defined values s.t. writing them all explicitly in YAML can be a pain.

For example:

pi: 3.14
r: 3
area: pi*r^2
circumference: 2*pi*r
d: math.sqrt(area/pi)*2

It would be a pain to manually compute area,circumference,d.

Therefore, a Lua interpreter is embedded in C++ so that these values can be defined just like above, without explicit computation on user's side.

Try out this project

  1. Install nix with flake support
  2. Run nix develop
  3. In the resulting shell, run make
  4. Profit!

On security

By default the embedded Lua interpreter is running in a sandboxed mode, where most of the Lua standard libraries are unloaded so that IO and system calls are forbidden.

The variable definition environment is further restricted s.t. only the following modules/functions are allowed:

  • math
  • print
  • pairs
  • ipairs

Consider the following malicious YAML file (lalaland.yml):

pi: math.pi
warning: >-
    os.execute('echo "Sandbox your Lua!! Next time I will be calling rm -rf!"')

Run with the sandboxed Lua:

foo@bar$ bin/demo -g pi -t double -i ./params/lalaland.yml
PANIC: unprotected error in call to Lua API ([string ""]:1: attempt to index global 'os' (a nil value))

If you feel particularly brave:

foo@bar$ bin/demo -g pi -t double -i ./params/lalaland.yml --sandbox=false
Sandbox your Lua!! Next time I will be calling rm -rf!
Parameter "pi" = 3.14159

Debugging variables

You can tell the embedded Lua to print out certain variables directly in YAML:

debug: print(name, isRectangle)

For more advanced usage, see advanced.yml.

About

A simple demo to embed Lua in a C++ program

License:BSD 2-Clause "Simplified" License


Languages

Language:C++ 72.2%Language:Makefile 12.1%Language:Lua 10.3%Language:Nix 5.3%