1/*	$NetBSD: lua.h,v 1.8.48.1 2023/08/09 17:42:01 martin Exp $ */
  2
  3/*
  4 * Copyright (c) 2014 by Lourival Vieira Neto <lneto@NetBSD.org>.
  5 * Copyright (c) 2011, 2013 Marc Balmer <mbalmer@NetBSD.org>.
  6 * All rights reserved.
  7 *
  8 * Redistribution and use in source and binary forms, with or without
  9 * modification, are permitted provided that the following conditions
 10 * are met:
 11 * 1. Redistributions of source code must retain the above copyright
 12 *    notice, this list of conditions and the following disclaimer.
 13 * 2. Redistributions in binary form must reproduce the above copyright
 14 *    notice, this list of conditions and the following disclaimer in the
 15 *    documentation and/or other materials provided with the distribution.
 16 * 3. The name of the Author may not be used to endorse or promote products
 17 *    derived from this software without specific prior written permission.
 18 *
 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 29 * SUCH DAMAGE.
 30 */
 31
 32#ifndef _SYS_LUA_H_
 33#define _SYS_LUA_H_
 34
 35#include <sys/param.h>
 36#include <sys/ioccom.h>
 37
 38#include <lua.h>		/* for lua_State */
 39
 40#ifdef _KERNEL
 41#include <sys/condvar.h>
 42#include <sys/mutex.h>
 43#endif
 44
 45#define MAX_LUA_NAME		16
 46#define MAX_LUA_DESC		64
 47#define LUA_MAX_MODNAME		32
 48
 49struct lua_state_info {
 50	char	name[MAX_LUA_NAME];
 51	char	desc[MAX_LUA_DESC];
 52	bool	user;
 53};
 54
 55struct lua_info {
 56	int num_states;		/* total number of created Lua states */
 57	struct lua_state_info *states;
 58};
 59
 60struct lua_create {
 61	char	name[MAX_LUA_NAME];
 62	char	desc[MAX_LUA_DESC];
 63};
 64
 65struct lua_require {
 66	char	state[MAX_LUA_NAME];
 67	char	module[LUA_MAX_MODNAME];
 68};
 69
 70struct lua_load {
 71	char	state[MAX_LUA_NAME];
 72	char	path[MAXPATHLEN];
 73};
 74
 75#define LUAINFO		_IOWR('l', 0, struct lua_info)
 76
 77#define LUACREATE	_IOWR('l', 1, struct lua_create)
 78#define LUADESTROY	_IOWR('l', 2, struct lua_create)
 79
 80/* 'require' a module in a state */
 81#define LUAREQUIRE	_IOWR('l', 3, struct lua_require)
 82
 83/* loading Lua code into a Lua state */
 84#define LUALOAD		_IOWR('l', 4, struct lua_load)
 85
 86#ifdef _KERNEL
 87extern int klua_mod_register(const char *, lua_CFunction);
 88extern int klua_mod_unregister(const char *);
 89
 90typedef struct _klua_State {
 91	lua_State	*L;
 92	kmutex_t	 ks_lock;
 93	bool		 ks_user;	/* state created by user (ioctl) */
 94} klua_State;
 95
 96extern void klua_lock(klua_State *);
 97extern void klua_unlock(klua_State *);
 98
 99extern void klua_close(klua_State *);
100extern klua_State *klua_newstate(lua_Alloc, void *, const char *, const char *,
101		int);
102extern klua_State *kluaL_newstate(const char *, const char *, int);
103#endif
104
105#endif /* _SYS_LUA_H_ */