1 #define SOL_ALL_SAFETIES_ON 1
2 #include <sol/sol.hpp>
3
4
5 class vector {
6 public:
7 double data[3];
8
vector()9 vector() : data { 0, 0, 0 } {
10 }
11
operator [](int i)12 double& operator[](int i) {
13 return data[i];
14 }
15
16
my_index(vector & v,int i)17 static double my_index(vector& v, int i) {
18 return v[i];
19 }
20
my_new_index(vector & v,int i,double x)21 static void my_new_index(vector& v, int i, double x) {
22 v[i] = x;
23 }
24 };
25
main()26 int main() {
27 sol::state lua;
28 lua.open_libraries(sol::lib::base);
29 lua.new_usertype<vector>(
30 "vector", sol::constructors<sol::types<>>(), sol::meta_function::index, &vector::my_index, sol::meta_function::new_index, &vector::my_new_index);
31 lua.script(
32 "v = vector.new()\n"
33 "print(v[1])\n"
34 "v[2] = 3\n"
35 "print(v[2])\n");
36
37 vector& v = lua["v"];
38 sol_c_assert(v[0] == 0.0);
39 sol_c_assert(v[1] == 0.0);
40 sol_c_assert(v[2] == 3.0);
41
42 return 0;
43 }
44