aboutsummaryrefslogtreecommitdiff
path: root/oop_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'oop_example.c')
-rw-r--r--oop_example.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/oop_example.c b/oop_example.c
new file mode 100644
index 0000000..fecac14
--- /dev/null
+++ b/oop_example.c
@@ -0,0 +1,89 @@
+#include <math.h>
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+#include "luaT/luaT.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct {
+ double x, y;
+ int arr[100];
+} Point;
+
+static int point_get_sinx (lua_State *L) {
+ Point *p = luaT_checkudata(L, 1, "nerv.point");
+ lua_pushnumber(L, sin(p->x));
+ return 1;
+}
+
+static int point_set_x (lua_State *L) {
+ Point *p = luaT_checkudata(L, 1, "nerv.point");
+ p->x = luaL_checknumber(L, 2);
+ return 0;
+}
+
+
+static int point_get_y(lua_State *L) {
+ Point *p = luaT_checkudata(L, 1, "nerv.point");
+ lua_pushnumber(L, sin(p->x));
+ return 1;
+}
+
+static int point_newindex(lua_State *L) {
+ Point *p = luaT_checkudata(L, 1, "nerv.point");
+ 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, "nerv.point");
+ 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, "nerv.point");
+ 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);
+}