Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named functions #22

Closed
Sikwan opened this issue Mar 20, 2014 · 4 comments
Closed

Named functions #22

Sikwan opened this issue Mar 20, 2014 · 4 comments
Labels

Comments

@Sikwan
Copy link

Sikwan commented Mar 20, 2014

Hi !

I am using lupa to validate some LUA expressions persisted to a data store from a python webapp. The thing is my expressions contains some custom functions defined elsewhere. I am wondering is there is a way to define them before running the expression I am trying to validate ?

EG:

expr = "field('brand') == 'audi'"

Here field is a custom function, I am just trying to enforce that the LUA syntax is valid, not execute exactly the whole function so I figured I had to do something like that :

lua = LuaRuntime(unpack_returned_tuples=True)
lua.eval('function field(x) return x end')
print lua.eval(expr)

But I keep having this error :

Traceback (most recent call last):
  File "test-lua.py", line 8, in <module>
    lua.eval('function field((x) return x end')
  File "_lupa.pyx", line 168, in lupa._lupa.LuaRuntime.eval (lupa/_lupa.c:3399)
  File "_lupa.pyx", line 950, in lupa._lupa.run_lua (lupa/_lupa.c:13350)
lupa._lupa.LuaSyntaxError: error loading code: [string "<python>"]:1: '(' expected near 'field'

Am I doing something wrong or is this just not possible ?

Thanks a lot

@fried
Copy link
Contributor

fried commented Mar 20, 2014

Hey use lua.execute() lua.eval() is the same except it places a return statement in front of your expression

@scoder
Copy link
Owner

scoder commented Mar 20, 2014

As you can see from the traceback, your actual code duplicates the opening "(" in the function. Thus the error. lua.eval() works fine for defining functions (or any expression, really). It prepends the code with "return ", as fried said. execute() is for running arbitrary code that is not an expression.

@scoder
Copy link
Owner

scoder commented Mar 20, 2014

If you really want named functions, you can write

func = lua.execute('function test(x) return x end; return test')

@Sikwan
Copy link
Author

Sikwan commented Mar 21, 2014

Works like a charm thanks a lot. I thought we couldn't reuse what was in "execute" in the same runtime, not sure why.

Thanks for the answer and the amazing work !

Cheers

@Sikwan Sikwan closed this as completed Mar 21, 2014
@scoder scoder added the invalid label Nov 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants