lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Eugen-Andrei Gavriloaie wrote:

Is there a method of evaluating an expression in the present lua context? Here is what I want:

lua_evaluate_expression(L,"applications[2].type"); // expect to find `mysql` string pushed into the stack after this call


lua_evaluate_expression(L,"#applications"); // expect to find value of 2 pushed into the stack after this call

This is supposed to push the result of the expression into the stack.

The following is not very efficient, but seems to do what you want
(not tested; error checks to be added):

void lua_evaluate_expression(lua_State *L, const char *expr) {
  lua_pushfstring(L, "return %s", expr);
  luaL_loadstring(L, lua_tostring(L,-1));