| 1 | /*	$NetBSD: lua.h,v 1.9.8.1 2026/06/29 19:52:23 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 | |
| 37 | #include <sys/ioccom.h> |
| 38 | #include <sys/stdbool.h> |
| 39 | |
| 40 | #include <lua.h>		/* for lua_State */ |
| 41 | |
| 42 | #ifdef _KERNEL |
| 43 | #include <sys/condvar.h> |
| 44 | #include <sys/mutex.h> |
| 45 | #endif |
| 46 | |
| 47 | #define MAX_LUA_NAME		16 |
| 48 | #define MAX_LUA_DESC		64 |
| 49 | #define LUA_MAX_MODNAME		32 |
| 50 | |
| 51 | struct lua_state_info { |
| 52 | 	char	name[MAX_LUA_NAME]; |
| 53 | 	char	desc[MAX_LUA_DESC]; |
| 54 | 	bool	user; |
| 55 | }; |
| 56 | |
| 57 | struct lua_info { |
| 58 | 	int num_states;		/* total number of created Lua states */ |
| 59 | 	struct lua_state_info *states; |
| 60 | }; |
| 61 | |
| 62 | struct lua_create { |
| 63 | 	char	name[MAX_LUA_NAME]; |
| 64 | 	char	desc[MAX_LUA_DESC]; |
| 65 | }; |
| 66 | |
| 67 | struct lua_require { |
| 68 | 	char	state[MAX_LUA_NAME]; |
| 69 | 	char	module[LUA_MAX_MODNAME]; |
| 70 | }; |
| 71 | |
| 72 | struct lua_load { |
| 73 | 	char	state[MAX_LUA_NAME]; |
| 74 | 	char	path[MAXPATHLEN]; |
| 75 | }; |
| 76 | |
| 77 | #define LUAINFO		_IOWR('l', 0, struct lua_info) |
| 78 | |
| 79 | #define LUACREATE	_IOWR('l', 1, struct lua_create) |
| 80 | #define LUADESTROY	_IOWR('l', 2, struct lua_create) |
| 81 | |
| 82 | /* 'require' a module in a state */ |
| 83 | #define LUAREQUIRE	_IOWR('l', 3, struct lua_require) |
| 84 | |
| 85 | /* loading Lua code into a Lua state */ |
| 86 | #define LUALOAD		_IOWR('l', 4, struct lua_load) |
| 87 | |
| 88 | #ifdef _KERNEL |
| 89 | extern int klua_mod_register(const char *, lua_CFunction); |
| 90 | extern int klua_mod_unregister(const char *); |
| 91 | |
| 92 | typedef struct _klua_State { |
| 93 | 	lua_State	*L; |
| 94 | 	kmutex_t	 ks_lock; |
| 95 | 	bool		 ks_user;	/* state created by user (ioctl) */ |
| 96 | } klua_State; |
| 97 | |
| 98 | extern void klua_lock(klua_State *); |
| 99 | extern void klua_unlock(klua_State *); |
| 100 | |
| 101 | extern void klua_close(klua_State *); |
| 102 | extern klua_State *klua_newstate(lua_Alloc, void *, const char *, const char *, |
| 103 | 		int); |
| 104 | extern klua_State *kluaL_newstate(const char *, const char *, int); |
| 105 | #endif |
| 106 | |
| 107 | #endif /* _SYS_LUA_H_ */ |