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
|
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "../common.h"
const char *point_tname = "nerv.Point";
typedef struct {
double x, y;
int arr[100];
} Point;
static int point_get_sinx (lua_State *L) {
Point *p = luaT_checkudata(L, 1, point_tname);
lua_pushnumber(L, sin(p->x));
return 1;
}
static int point_set_x (lua_State *L) {
Point *p = luaT_checkudata(L, 1, point_tname);
p->x = luaL_checknumber(L, 2);
return 0;
}
static int point_get_y(lua_State *L) {
Point *p = luaT_checkudata(L, 1, point_tname);
lua_pushnumber(L, sin(p->x));
return 1;
}
static int point_newindex(lua_State *L) {
Point *p = luaT_checkudata(L, 1, point_tname);
if (lua_isnumber(L, 2))
{
int d = luaL_checkinteger(L, 2);
double v = luaL_checknumber(L, 3);
if (0 <= d && d < 100)
p->arr[d] = v;
lua_pushboolean(L, 1);
return 1;
}
else
{
lua_pushboolean(L, 0);
return 1;
}
}
static int point_index(lua_State *L) {
Point *p = luaT_checkudata(L, 1, point_tname);
if (lua_isnumber(L, 2))
{
int d = luaL_checkinteger(L, 2);
if (0 <= d && d < 100)
lua_pushnumber(L, p->arr[d]);
lua_pushboolean(L, 1);
return 2;
}
else
{
lua_pushboolean(L, 0);
return 1;
}
}
int point_new(lua_State *L) {
Point *self = (Point *)malloc(sizeof(Point));
self->x = 0;
self->y = 0;
luaT_pushudata(L, self, point_tname);
return 1;
}
static const luaL_Reg point[] = {
{"get_sinx", point_get_sinx},
{"set_x", point_set_x},
{"get_y", point_get_y},
{"__index__", point_index},
{"__newindex__", point_newindex},
{NULL, NULL}
};
void nerv_point_init(lua_State *L) {
luaT_newmetatable(L, "nerv.Point", NULL, point_new, NULL, NULL);
luaL_register(L, NULL, point);
lua_pop(L, 1);
}
|