Enclosed is a small patch that allows lua_run() to return an integer
value. If the return value is not an integer, 1 is returned (like is
true with current git master.) As a side effect, lua scripts can now
exit() the sip-router script by returning 0.
Example:
Lua script:
test_equality = function ( a , b )
if a == b then
return 1
else
return -1
end
sip-router script:
if (lua_run("test_equality", "$fU", "$tU")) {
xdbg("SCRIPT: The values are equal\n");
}
diff --git a/modules/app_lua/app_lua_api.c b/modules/app_lua/app_lua_api.c
index 9d5e662..6d4c840 100644
--- a/modules/app_lua/app_lua_api.c
+++ b/modules/app_lua/app_lua_api.c
@@ -437,7 +437,7 @@ int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
}
}
_sr_L_env.msg = msg;
- ret = lua_pcall(_sr_L_env.LL, n, 0, 0);
+ ret = lua_pcall(_sr_L_env.LL, n, 1, 0);
_sr_L_env.msg = 0;
if(ret!=0)
{
@@ -448,7 +448,13 @@ int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
return -1;
}
- return 1;
+ if (lua_isnumber(_sr_L_env.LL, -1))
+ ret = lua_tointeger(_sr_L_env.LL, -1);
+ else
+ ret = 1;
+
+ lua_pop(_sr_L_env.LL, 1);
+ return ret;
}
void app_lua_dump_stack(lua_State *L)
diff --git a/modules/app_lua/doc/app_lua_admin.xml b/modules/app_lua/doc/app_lua_admin.xml
index cb97e5d..a1af614 100644
--- a/modules/app_lua/doc/app_lua_admin.xml
+++ b/modules/app_lua/doc/app_lua_admin.xml
@@ -240,7 +240,9 @@ if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
Execute the Lua function 'func' giving params as parameters. There
can be up to 3 string parameters. The function must exist in the
script loaded at startup via parameter 'load'. Parameters can be
- strings with pseudo-variables that are evaluated at runtime.
+ strings with pseudo-variables that are evaluated at runtime. If the
+ function returns an integer value it is returned to the script. All
+ other Lua return types are evaluated as 1 (true).
</para>
<example>
<title><function>lua_run</function> usage</title>
_______________________________________________
sr-dev mailing list
[email protected]
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev