2017-10-25 21:06:30 +02:00
|
|
|
/*
|
2019-05-11 02:55:50 +02:00
|
|
|
* Copyright (C) 2016-2019 Cumulus Networks, Inc.
|
|
|
|
* Donald Sharp, Quentin Young
|
2017-10-25 21:06:30 +02:00
|
|
|
*
|
2019-05-11 02:55:50 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
2017-10-25 21:06:30 +02:00
|
|
|
*
|
2019-05-11 02:55:50 +02:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
2017-10-25 21:06:30 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2019-05-11 02:55:50 +02:00
|
|
|
* with this program; see the file COPYING; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2017-10-25 21:06:30 +02:00
|
|
|
*/
|
2019-05-11 02:55:50 +02:00
|
|
|
#ifndef __FRRLUA_H__
|
|
|
|
#define __FRRLUA_H__
|
2017-10-25 21:06:30 +02:00
|
|
|
|
2020-11-30 23:01:03 +01:00
|
|
|
#include <zebra.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SCRIPTING
|
2017-10-25 21:06:30 +02:00
|
|
|
|
2020-11-29 20:51:52 +01:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lualib.h>
|
|
|
|
#include <lauxlib.h>
|
2017-10-25 21:06:30 +02:00
|
|
|
|
2019-05-11 02:55:50 +02:00
|
|
|
#include "prefix.h"
|
lib: start adding generic scripting stuff
Rather than let Luaisms propagate from the start, this is some generic
wrapper stuff that defines some semantics for interacting with scripts
that aren't specific to the underlying language.
The concept I have in mind for FRR's idea of a script is:
- has a name
- has some inputs, which have types
- has some outputs, which have types
I don't want to even say they have to be files; maybe we can embed
scripts in frr.conf, for example. Similarly the types of inputs and
outputs are probably going to end up being some language-specific setup.
For now, we will stick to this simple model, but the plan is to add full
object support (ie calling back into C).
This shouldn't be misconstrued as prepping for multilingual scripting
support, which is a bad idea for the following reasons:
- Each language would require different FFI methods, and specifically
different object encoders; a lot of code
- Languages have different capabilities that would have to be brought to
parity with each other; a lot of work
- Languages have *vastly* different performance characteristics; bad
impressions, lots of issues we can't do anything about
- Each language would need a dedicated maintainer for the above reasons;
pragmatically difficult
- Supporting multiple languages fractures the community and limits the
audience with which a given script can be shared
The only pro for multilingual support would be ease of use for users not
familiar with Lua but familiar with one of the other supported
languages. This is not enough to outweigh the cons.
In order to get rich scripting capabilities, we need to be able to pass
representations of internal objects to the scripts. For example, a
script that performs some computation based on information about a peer
needs access to some equivalent of `struct peer` for the peer in
question. To transfer these objects from C-space into Lua-space we need
to encode them onto the Lua stack. This patch adds a mapping from
arbitrary type names to the functions that encode objects of that type.
For example, the function that encodes `struct peer` into a Lua table
could be registered with:
bgp_peer_encoder_func(struct frrscript *fs, struct peer *peer)
{
// encode peer to Lua table, push to stack in fs->scriptinfo->L
}
frrscript_register_type_encoder("peer", bgp_peer_encoder_func);
Later on when calling a script that wants a peer, the plan is to be able
to specify the type name like so:
frrscript_call(script, "peer", peer);
Using C-style types for the type names would have been nice, it might be
possible to do this with preprocessor magic or possibly python
preprocessing later on.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
mergeme no stdlib
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-29 01:02:39 +01:00
|
|
|
#include "frrscript.h"
|
2019-05-11 02:55:50 +02:00
|
|
|
|
2019-02-07 23:10:31 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-07-26 17:27:43 +02:00
|
|
|
DECLARE_MTYPE(SCRIPT_RES);
|
|
|
|
|
2021-01-24 14:00:43 +01:00
|
|
|
/*
|
|
|
|
* gcc-10 is complaining about the wrapper function
|
|
|
|
* not being compatible with lua_pushstring returning
|
|
|
|
* a char *. Let's wrapper it here to make our life
|
|
|
|
* easier
|
|
|
|
*/
|
|
|
|
static inline void lua_pushstring_wrapper(lua_State *L, const char *str)
|
|
|
|
{
|
|
|
|
(void)lua_pushstring(L, str);
|
|
|
|
}
|
|
|
|
|
2019-05-11 02:55:50 +02:00
|
|
|
/*
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
* Converts a prefix to a Lua value and pushes it on the stack.
|
2017-10-25 21:06:30 +02:00
|
|
|
*/
|
2020-11-29 23:43:16 +01:00
|
|
|
void lua_pushprefix(lua_State *L, const struct prefix *prefix);
|
2017-10-25 21:06:30 +02:00
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_prefix(lua_State *L, int idx, struct prefix *prefix);
|
|
|
|
|
2019-05-11 03:51:19 +02:00
|
|
|
/*
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
* Converts the Lua value at idx to a prefix.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* struct prefix allocated with MTYPE_TMP
|
|
|
|
*/
|
|
|
|
void *lua_toprefix(lua_State *L, int idx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts an interface to a Lua value and pushes it on the stack.
|
2019-05-11 03:51:19 +02:00
|
|
|
*/
|
2020-11-29 23:43:16 +01:00
|
|
|
void lua_pushinterface(lua_State *L, const struct interface *ifp);
|
2019-05-11 03:51:19 +02:00
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_interface(lua_State *L, int idx, struct interface *ifp);
|
|
|
|
|
2019-05-11 02:55:50 +02:00
|
|
|
/*
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
* Converts the Lua value at idx to an interface.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* struct interface allocated with MTYPE_TMP. This interface is not hooked
|
|
|
|
* to anything, nor is it inserted in the global interface tree.
|
|
|
|
*/
|
|
|
|
void *lua_tointerface(lua_State *L, int idx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts an in_addr to a Lua value and pushes it on the stack.
|
2020-11-29 22:00:36 +01:00
|
|
|
*/
|
2020-11-29 23:43:16 +01:00
|
|
|
void lua_pushinaddr(lua_State *L, const struct in_addr *addr);
|
2020-11-29 22:00:36 +01:00
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_inaddr(lua_State *L, int idx, struct in_addr *addr);
|
|
|
|
|
2020-11-29 22:00:36 +01:00
|
|
|
/*
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
* Converts the Lua value at idx to an in_addr.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* struct in_addr allocated with MTYPE_TMP.
|
|
|
|
*/
|
|
|
|
void *lua_toinaddr(lua_State *L, int idx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts an in6_addr to a Lua value and pushes it on the stack.
|
2020-11-29 22:00:36 +01:00
|
|
|
*/
|
2020-11-29 23:43:16 +01:00
|
|
|
void lua_pushin6addr(lua_State *L, const struct in6_addr *addr);
|
2020-11-29 22:00:36 +01:00
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_in6addr(lua_State *L, int idx, struct in6_addr *addr);
|
|
|
|
|
2021-08-18 14:22:21 +02:00
|
|
|
void lua_pushipaddr(lua_State *L, const struct ipaddr *addr);
|
|
|
|
|
|
|
|
void lua_pushethaddr(lua_State *L, const struct ethaddr *addr);
|
|
|
|
|
2020-11-29 22:00:36 +01:00
|
|
|
/*
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
* Converts the Lua value at idx to an in6_addr.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* struct in6_addr allocated with MTYPE_TMP.
|
|
|
|
*/
|
|
|
|
void *lua_toin6addr(lua_State *L, int idx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts a time_t to a Lua value and pushes it on the stack.
|
2020-11-29 22:00:36 +01:00
|
|
|
*/
|
2020-11-29 23:43:16 +01:00
|
|
|
void lua_pushtimet(lua_State *L, const time_t *time);
|
2020-11-29 22:00:36 +01:00
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_timet(lua_State *L, int idx, time_t *time);
|
|
|
|
|
2020-11-29 22:00:36 +01:00
|
|
|
/*
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
* Converts the Lua value at idx to a time_t.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* time_t allocated with MTYPE_TMP.
|
|
|
|
*/
|
|
|
|
void *lua_totimet(lua_State *L, int idx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts a sockunion to a Lua value and pushes it on the stack.
|
2019-05-11 02:55:50 +02:00
|
|
|
*/
|
2020-11-29 23:43:16 +01:00
|
|
|
void lua_pushsockunion(lua_State *L, const union sockunion *su);
|
2017-10-25 21:06:30 +02:00
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su);
|
|
|
|
|
2020-11-30 00:00:35 +01:00
|
|
|
/*
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
* Converts the Lua value at idx to a sockunion.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* sockunion allocated with MTYPE_TMP.
|
|
|
|
*/
|
|
|
|
void *lua_tosockunion(lua_State *L, int idx);
|
|
|
|
|
2021-08-18 14:25:40 +02:00
|
|
|
void lua_pushnexthop_group(lua_State *L, const struct nexthop_group *ng);
|
|
|
|
|
|
|
|
void lua_pushnexthop(lua_State *L, const struct nexthop *nexthop);
|
|
|
|
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
/*
|
|
|
|
* Converts an int to a Lua value and pushes it on the stack.
|
|
|
|
*/
|
|
|
|
void lua_pushintegerp(lua_State *L, const long long *num);
|
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_integerp(lua_State *L, int idx, long long *num);
|
|
|
|
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
/*
|
|
|
|
* Converts the Lua value at idx to an int.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* int allocated with MTYPE_TMP.
|
|
|
|
*/
|
|
|
|
void *lua_tointegerp(lua_State *L, int idx);
|
|
|
|
|
2021-06-20 02:25:22 +02:00
|
|
|
void lua_decode_stringp(lua_State *L, int idx, char *str);
|
|
|
|
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
/*
|
|
|
|
* Pop string.
|
|
|
|
*
|
|
|
|
* Sets *string to a copy of the string at the top of the stack. The copy is
|
|
|
|
* allocated with MTYPE_TMP and the caller is responsible for freeing it.
|
2020-11-30 00:00:35 +01:00
|
|
|
*/
|
lib: add ability to decode from lua scripts
This implements the ability to get results out from lua scripts after
they've run.
For each C type we support passing to Lua, there is a corresponding
`struct frrscript_codec`. This struct contains a typename field - just a
string identifying the type - and two function pointers. The first
function pointer, encode, takes a lua_State and a pointer to the C value
and pushes some corresponding Lua representation onto the stack. The
second, decode, assumes there is some Lua value on the stack and decodes
it into the corresponding C value.
Each supported type's `struct frrscript_codec` is registered with the
scripting stuff in the library, which creates a mapping between the type
name (string) and the `struct frrscript_codec`. When calling a script,
you specify arguments by passing an array of `struct frrscript_env`.
Each of these structs has a void *, a type name, and a desired binding
name. The type names are used to look up the appropriate function to
encode the pointed-at value onto the Lua stack, then bind the pushed
value to the provided binding name, so that the converted value is
accessible by that name within the script.
Results work in a similar way. After a script runs, call
frrscript_get_result() with the script and a `struct frrscript_env`.
The typename and name fields are used to fetch the Lua value from the
script's environment and use the registered decoder for the typename to
convert the Lua value back into a C value, which is returned from the
function. The caller is responsible for freeing these.
frrscript_call()'s macro foo has been stripped, as the underlying
function now takes fixed arrays. varargs have awful performance
characteristics, they're hard to read, and structs are more defined than
an order sensitive list.
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-11-30 04:09:58 +01:00
|
|
|
void *lua_tostringp(lua_State *L, int idx);
|
2020-11-30 00:00:35 +01:00
|
|
|
|
2021-06-20 02:06:16 +02:00
|
|
|
/*
|
2021-07-23 21:16:25 +02:00
|
|
|
* No-op decoders
|
2021-06-20 02:06:16 +02:00
|
|
|
*/
|
|
|
|
void lua_decode_noop(lua_State *L, int idx, const void *ptr);
|
|
|
|
|
2021-07-23 21:41:12 +02:00
|
|
|
void lua_decode_integer_noop(lua_State *L, int idx, int i);
|
2021-07-17 14:46:10 +02:00
|
|
|
|
2017-10-25 21:06:30 +02:00
|
|
|
/*
|
2019-05-11 02:55:50 +02:00
|
|
|
* Retrieve an integer from table on the top of the stack.
|
|
|
|
*
|
|
|
|
* key
|
|
|
|
* Key of string value in table
|
2017-10-25 21:06:30 +02:00
|
|
|
*/
|
2019-05-11 02:55:50 +02:00
|
|
|
int frrlua_table_get_integer(lua_State *L, const char *key);
|
2019-02-07 23:10:31 +01:00
|
|
|
|
2019-08-09 22:13:07 +02:00
|
|
|
/*
|
|
|
|
* Exports a new table containing bindings to FRR zlog functions into the
|
|
|
|
* global namespace.
|
|
|
|
*
|
|
|
|
* From Lua, these functions may be accessed as:
|
|
|
|
*
|
|
|
|
* - log.debug()
|
|
|
|
* - log.info()
|
|
|
|
* - log.warn()
|
|
|
|
* - log.error()
|
|
|
|
*
|
|
|
|
* They take a single string argument.
|
|
|
|
*/
|
|
|
|
void frrlua_export_logging(lua_State *L);
|
|
|
|
|
2019-08-11 21:44:04 +02:00
|
|
|
/*
|
|
|
|
* Dump Lua stack to a string.
|
|
|
|
*
|
|
|
|
* Return value must be freed with XFREE(MTYPE_TMP, ...);
|
|
|
|
*/
|
|
|
|
char *frrlua_stackdump(lua_State *L);
|
|
|
|
|
2019-02-07 23:10:31 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-30 23:01:03 +01:00
|
|
|
#endif /* HAVE_SCRIPTING */
|
|
|
|
|
2019-05-11 02:55:50 +02:00
|
|
|
#endif /* __FRRLUA_H__ */
|