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

or

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));
  lua_remove(L,-2);
  lua_pcall(L,0,1,0);
}

--
Shmuel