forked from luainkernel/lunatik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunatik_obj.c
156 lines (127 loc) · 4.34 KB
/
lunatik_obj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2023-2024 ring-0 Ltda.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <lua.h>
#include <lauxlib.h>
#include "lunatik.h"
#ifdef LUNATIK_RUNTIME
lunatik_object_t *lunatik_newobject(lua_State *L, const lunatik_class_t *class, size_t size)
{
lunatik_object_t **pobject = lunatik_newpobject(L, 1);
lunatik_object_t *object = lunatik_checkalloc(L, sizeof(lunatik_object_t));
lunatik_checkclass(L, class);
lunatik_setobject(L, object, class, lunatik_toruntime(L)->sleep);
lunatik_setclass(L, class);
object->private = class->pointer ? NULL : lunatik_checkalloc(L, size);
*pobject = object;
return object;
}
EXPORT_SYMBOL(lunatik_newobject);
lunatik_object_t **lunatik_checkpobject(lua_State *L, int ix)
{
lunatik_object_t **pobject;
lunatik_class_t *class;
luaL_argcheck(L, lua_isuserdata(L, ix) && lua_getiuservalue(L, ix, 1) != LUA_TNONE &&
(class = (lunatik_class_t *)lua_touserdata(L, -1)) != NULL, ix, "object expected");
pobject = (lunatik_object_t **)luaL_checkudata(L, ix, class->name);
lunatik_checknull(L, *pobject, ix);
lua_pop(L, 1); /* class */
return pobject;
}
EXPORT_SYMBOL(lunatik_checkpobject);
void lunatik_cloneobject(lua_State *L, lunatik_object_t *object)
{
lunatik_object_t **pobject = lunatik_newpobject(L, 1);
const lunatik_class_t *class = object->class;
lunatik_checkclass(L, class);
lunatik_setclass(L, class);
*pobject = object;
}
EXPORT_SYMBOL(lunatik_cloneobject);
static inline void lunatik_releaseprivate(const lunatik_class_t *class, void *private)
{
void (*release)(void *) = class->release;
if (release)
release(private);
if (!class->pointer)
lunatik_free(private);
}
int lunatik_closeobject(lua_State *L)
{
lunatik_object_t *object = lunatik_checkobject(L, 1);
void *private;
lunatik_lock(object);
private = object->private;
object->private = NULL;
lunatik_unlock(object);
lunatik_checknull(L, private, 1);
lunatik_releaseprivate(object->class, private);
return 0;
}
EXPORT_SYMBOL(lunatik_closeobject);
void lunatik_releaseobject(struct kref *kref)
{
lunatik_object_t *object = container_of(kref, lunatik_object_t, kref);
void *private = object->private;
if (private != NULL)
lunatik_releaseprivate(object->class, private);
lunatik_freelock(object);
kfree(object);
}
EXPORT_SYMBOL(lunatik_releaseobject);
int lunatik_deleteobject(lua_State *L)
{
lunatik_object_t **pobject = lunatik_checkpobject(L, 1);
lunatik_object_t *object = *pobject;
BUG_ON(!object);
lunatik_putobject(object);
*pobject = NULL;
return 0;
}
EXPORT_SYMBOL(lunatik_deleteobject);
static int lunatik_monitor(lua_State *L)
{
int ret, n = lua_gettop(L);
lunatik_object_t *object = lunatik_checkobject(L, 1);
lua_pushvalue(L, lua_upvalueindex(1)); /* method */
lua_insert(L, 1); /* stack: method, object, args */
lunatik_lock(object);
ret = lua_pcall(L, n, LUA_MULTRET, 0);
lunatik_unlock(object);
if (ret != LUA_OK)
lua_error(L);
return lua_gettop(L);
}
int lunatik_monitorobject(lua_State *L)
{
lua_getmetatable(L, 1);
lua_insert(L, 2); /* stack: object, metatable, key */
if (lua_rawget(L, 2) == LUA_TFUNCTION) {
lua_CFunction method = lua_tocfunction(L, -1);
if (likely(method != lunatik_deleteobject && method != lunatik_closeobject))
lua_pushcclosure(L, lunatik_monitor, 1);
}
return 1;
}
EXPORT_SYMBOL(lunatik_monitorobject);
#endif /* LUNATIK_RUNTIME */