pinocchio  2.1.3
lua_tables.cpp
1 /*
2  * LuaTables++
3  * Code comming from https://bitbucket.org/rbdl/rbdl
4  * Copyright (c) 2013-2014 Martin Felis <martin@fyxs.org>.
5  * Copyright (c) 2015 Justin Carpentier <jcarpent@laas.fr>
6  * All rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include "pinocchio/parsers/lua/lua_tables.hpp"
29 
30 #include <assert.h>
31 #include <iostream>
32 #include <cstdlib>
33 #include <vector>
34 #include <sstream>
35 #include <cmath>
36 
37 #include <lua.hpp>
38 
39 #include <stdio.h> /* defines FILENAME_MAX */
40 #ifdef WINDOWS
41 #include <direct.h>
42 #define get_current_dir _getcwd
43 #else
44 #include <unistd.h>
45 #define get_current_dir getcwd
46 #endif
47 
48 #if defined(WIN32) || defined (_WIN32)
49 #define DIRECTORY_SEPARATOR "\\"
50 #elif defined(linux) || defined (__linux) || defined(__linux__) || defined(__APPLE__)
51 #define DIRECTORY_SEPARATOR "/"
52 #else
53 #error Platform not supported!
54 #endif
55 
56 using namespace std;
57 
58 std::string get_file_directory (const char* filename)
59 {
60  string name (filename);
61  string result = name.substr(0, name.find_last_of (DIRECTORY_SEPARATOR) + 1);
62 
63  if (result == "")
64  result = "./";
65 #if defined (WIN32) || defined (_WIN32)
66 #warning get_file_directory() not yet tested under Windows!
67  else if (result.substr(1,2) != ":\\")
68  result = string(".\\") + result;
69 #else
70  else if (result.substr(0,string(DIRECTORY_SEPARATOR).size()) != DIRECTORY_SEPARATOR && result[0] != '.')
71  result = string("./") + result;
72 #endif
73 
74  return result;
75 }
76 
77 // char encoded serialize function that is available in plaintext in
78 // utils/serialize.lua. Converted using lua auto.lua serialize.lua
79 const char serialize_std[] = {
80 
81  0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x69, 0x73, 0x6c,
82  0x69, 0x73, 0x74, 0x20, 0x28, 0x74, 0x29, 0x0a,
83  0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x69, 0x74, 0x65, 0x6d, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x20, 0x3d,
84  0x20, 0x30, 0x0a,
85  0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d,
86  0x20, 0x6e, 0x69, 0x6c, 0x0a,
87  0x09, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x2c, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x70, 0x61, 0x69, 0x72, 0x73, 0x28,
88  0x74, 0x29, 0x20, 0x64, 0x6f, 0x0a,
89  0x09, 0x09, 0x69, 0x74, 0x65, 0x6d, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x20, 0x3d, 0x20, 0x69, 0x74, 0x65, 0x6d,
90  0x63, 0x6f, 0x75, 0x6e, 0x74, 0x20, 0x2b, 0x20, 0x31, 0x0a,
91  0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20,
92  0x6e, 0x69, 0x6c, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
93  0x09, 0x09, 0x09, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x79, 0x70,
94  0x65, 0x28, 0x76, 0x29, 0x0a,
95  0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
96  0x09, 0x09, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x76, 0x29, 0x20, 0x7e, 0x3d, 0x20, 0x6c, 0x61,
97  0x73, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x6f, 0x72, 0x20, 0x28, 0x74, 0x79, 0x70, 0x65, 0x28, 0x76,
98  0x29, 0x20, 0x7e, 0x3d, 0x20, 0x22, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x20, 0x61, 0x6e, 0x64, 0x20,
99  0x74, 0x79, 0x70, 0x65, 0x28, 0x76, 0x29, 0x20, 0x7e, 0x3d, 0x20, 0x22, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72,
100  0x22, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x76, 0x29, 0x20, 0x7e, 0x3d, 0x20, 0x22,
101  0x62, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x22, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28,
102  0x76, 0x29, 0x20, 0x7e, 0x3d, 0x20, 0x22, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x74, 0x68, 0x65,
103  0x6e, 0x0a,
104  0x09, 0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x0a,
105  0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
106  0x09, 0x0a,
107  0x09, 0x09, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x79, 0x70, 0x65,
108  0x28, 0x76, 0x29, 0x0a,
109  0x09, 0x65, 0x6e, 0x64, 0x0a,
110  0x09, 0x69, 0x66, 0x20, 0x69, 0x74, 0x65, 0x6d, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x20, 0x7e, 0x3d, 0x20, 0x23,
111  0x74, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
112  0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x0a,
113  0x09, 0x65, 0x6e, 0x64, 0x0a,
114  0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x74, 0x72, 0x75, 0x65, 0x0a,
115  0x65, 0x6e, 0x64, 0x0a,
116  0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x63, 0x6d, 0x70,
117  0x5f, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x69, 0x63, 0x20, 0x28, 0x61, 0x2c, 0x20,
118  0x62, 0x29, 0x0a,
119  0x09, 0x69, 0x66, 0x20, 0x28, 0x74, 0x79, 0x70, 0x65, 0x28, 0x61, 0x29, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x73,
120  0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x62, 0x29,
121  0x20, 0x3d, 0x3d, 0x20, 0x22, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x79,
122  0x70, 0x65, 0x28, 0x61, 0x29, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0x20,
123  0x61, 0x6e, 0x64, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x62, 0x29, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x6e, 0x75,
124  0x6d, 0x62, 0x65, 0x72, 0x22, 0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
125  0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x61, 0x20, 0x3c, 0x20, 0x62, 0x0a,
126  0x09, 0x65, 0x6c, 0x73, 0x65, 0x0a,
127  0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x61, 0x29, 0x20, 0x3c,
128  0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x62, 0x29, 0x0a,
129  0x09, 0x65, 0x6e, 0x64, 0x0a,
130  0x65, 0x6e, 0x64, 0x0a,
131  0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x67, 0x65, 0x6e,
132  0x5f, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x64, 0x69, 0x63, 0x65, 0x73, 0x20, 0x28,
133  0x74, 0x29, 0x0a,
134  0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x64,
135  0x69, 0x63, 0x65, 0x73, 0x20, 0x3d, 0x20, 0x7b, 0x7d, 0x0a,
136  0x09, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x20, 0x69, 0x6e, 0x20, 0x70, 0x61, 0x69, 0x72, 0x73, 0x20, 0x28, 0x74,
137  0x29, 0x20, 0x64, 0x6f, 0x0a,
138  0x09, 0x09, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2e, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x28, 0x6f, 0x72,
139  0x64, 0x65, 0x72, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x64, 0x69, 0x63, 0x65, 0x73, 0x2c, 0x20, 0x6b, 0x29, 0x0a,
140  0x09, 0x65, 0x6e, 0x64, 0x0a,
141  0x09, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2e, 0x73, 0x6f, 0x72, 0x74, 0x20, 0x28, 0x6f, 0x72, 0x64, 0x65, 0x72,
142  0x65, 0x64, 0x5f, 0x69, 0x6e, 0x64, 0x69, 0x63, 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6d, 0x70, 0x5f, 0x61, 0x6c,
143  0x70, 0x68, 0x61, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x69, 0x63, 0x29, 0x0a,
144  0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x5f, 0x69, 0x6e,
145  0x64, 0x69, 0x63, 0x65, 0x73, 0x0a,
146  0x65, 0x6e, 0x64, 0x0a,
147  0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x72, 0x64,
148  0x65, 0x72, 0x65, 0x64, 0x5f, 0x6e, 0x65, 0x78, 0x74, 0x20, 0x28, 0x74, 0x2c, 0x20, 0x73, 0x74, 0x61, 0x74,
149  0x65, 0x29, 0x0a,
150  0x09, 0x69, 0x66, 0x20, 0x73, 0x74, 0x61, 0x74, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x6e, 0x69, 0x6c, 0x20, 0x74,
151  0x68, 0x65, 0x6e, 0x0a,
152  0x09, 0x09, 0x74, 0x2e, 0x5f, 0x5f, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x49, 0x6e, 0x64, 0x69, 0x63,
153  0x65, 0x73, 0x20, 0x3d, 0x20, 0x67, 0x65, 0x6e, 0x5f, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x5f, 0x69,
154  0x6e, 0x64, 0x69, 0x63, 0x65, 0x73, 0x20, 0x28, 0x74, 0x29, 0x0a,
155  0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x3d, 0x20, 0x74, 0x2e, 0x5f, 0x5f,
156  0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x49, 0x6e, 0x64, 0x69, 0x63, 0x65, 0x73, 0x5b, 0x31, 0x5d, 0x0a,
157  0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6b, 0x65, 0x79, 0x2c, 0x20, 0x74, 0x5b, 0x6b, 0x65,
158  0x79, 0x5d, 0x0a,
159  0x09, 0x65, 0x6e, 0x64, 0x0a,
160  0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x3d, 0x20, 0x6e, 0x69, 0x6c, 0x0a,
161  0x09, 0x66, 0x6f, 0x72, 0x20, 0x69, 0x20, 0x3d, 0x20, 0x31, 0x2c, 0x20, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2e,
162  0x67, 0x65, 0x74, 0x6e, 0x28, 0x74, 0x2e, 0x5f, 0x5f, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x49, 0x6e,
163  0x64, 0x69, 0x63, 0x65, 0x73, 0x29, 0x20, 0x64, 0x6f, 0x0a,
164  0x09, 0x09, 0x69, 0x66, 0x20, 0x74, 0x2e, 0x5f, 0x5f, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x49, 0x6e,
165  0x64, 0x69, 0x63, 0x65, 0x73, 0x5b, 0x69, 0x5d, 0x20, 0x3d, 0x3d, 0x20, 0x73, 0x74, 0x61, 0x74, 0x65, 0x20,
166  0x74, 0x68, 0x65, 0x6e, 0x0a,
167  0x09, 0x09, 0x09, 0x6b, 0x65, 0x79, 0x20, 0x3d, 0x20, 0x74, 0x2e, 0x5f, 0x5f, 0x6f, 0x72, 0x64, 0x65, 0x72,
168  0x65, 0x64, 0x49, 0x6e, 0x64, 0x69, 0x63, 0x65, 0x73, 0x5b, 0x69, 0x20, 0x2b, 0x20, 0x31, 0x5d, 0x0a,
169  0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
170  0x09, 0x65, 0x6e, 0x64, 0x0a,
171  0x09, 0x69, 0x66, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
172  0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6b, 0x65, 0x79, 0x2c, 0x20, 0x74, 0x5b, 0x6b, 0x65,
173  0x79, 0x5d, 0x0a,
174  0x09, 0x65, 0x6e, 0x64, 0x0a,
175  0x09, 0x74, 0x2e, 0x5f, 0x5f, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x49, 0x6e, 0x64, 0x69, 0x63, 0x65,
176  0x73, 0x20, 0x3d, 0x20, 0x6e, 0x69, 0x6c, 0x0a,
177  0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x0a,
178  0x65, 0x6e, 0x64, 0x0a,
179  0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x72, 0x64,
180  0x65, 0x72, 0x65, 0x64, 0x5f, 0x70, 0x61, 0x69, 0x72, 0x73, 0x20, 0x28, 0x74, 0x29, 0x0a,
181  0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65, 0x64, 0x5f, 0x6e, 0x65,
182  0x78, 0x74, 0x2c, 0x20, 0x74, 0x2c, 0x20, 0x6e, 0x69, 0x6c, 0x0a,
183  0x65, 0x6e, 0x64, 0x0a,
184  0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65,
185  0x20, 0x28, 0x6f, 0x2c, 0x20, 0x74, 0x61, 0x62, 0x73, 0x2c, 0x20, 0x73, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x29, 0x0a,
186  0x20, 0x20, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x22,
187  0x22, 0x0a,
188  0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x70, 0x61, 0x69, 0x72, 0x73, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x20,
189  0x3d, 0x20, 0x70, 0x61, 0x69, 0x72, 0x73, 0x0a,
190  0x09, 0x69, 0x66, 0x20, 0x73, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
191  0x09, 0x09, 0x70, 0x61, 0x69, 0x72, 0x73, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x20, 0x3d, 0x20, 0x6f, 0x72, 0x64,
192  0x65, 0x72, 0x65, 0x64, 0x5f, 0x70, 0x61, 0x69, 0x72, 0x73, 0x0a,
193  0x09, 0x65, 0x6e, 0x64, 0x0a,
194  0x20, 0x20, 0x0a,
195  0x20, 0x20, 0x69, 0x66, 0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x3d, 0x3d, 0x20, 0x6e, 0x69, 0x6c, 0x20, 0x74,
196  0x68, 0x65, 0x6e, 0x0a,
197  0x20, 0x20, 0x20, 0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x3d, 0x20, 0x22, 0x22, 0x0a,
198  0x20, 0x20, 0x65, 0x6e, 0x64, 0x0a,
199  0x20, 0x20, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x6f, 0x29, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x6e,
200  0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
201  0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c,
202  0x74, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x6f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x28, 0x6f, 0x29, 0x0a,
203  0x20, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x6f, 0x29, 0x20, 0x3d,
204  0x3d, 0x20, 0x22, 0x62, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
205  0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c,
206  0x74, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x6f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x28, 0x6f, 0x29, 0x0a,
207  0x20, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x6f, 0x29, 0x20, 0x3d,
208  0x3d, 0x20, 0x22, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
209  0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c,
210  0x74, 0x20, 0x2e, 0x2e, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2e, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74,
211  0x28, 0x22, 0x25, 0x71, 0x22, 0x2c, 0x20, 0x6f, 0x29, 0x0a,
212  0x09, 0x65, 0x6c, 0x73, 0x65, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x6f, 0x29, 0x20, 0x3d, 0x3d,
213  0x20, 0x22, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x22, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x69, 0x73, 0x6c, 0x69, 0x73,
214  0x74, 0x28, 0x6f, 0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
215  0x09, 0x09, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20,
216  0x2e, 0x2e, 0x20, 0x22, 0x7b, 0x22, 0x0a,
217  0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x77, 0x61, 0x73, 0x5f, 0x73,
218  0x75, 0x62, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x0a,
219  0x09, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x69, 0x2c, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x69, 0x70, 0x61, 0x69, 0x72,
220  0x73, 0x28, 0x6f, 0x29, 0x20, 0x64, 0x6f, 0x0a,
221  0x09, 0x09, 0x09, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x77, 0x61, 0x73, 0x5f, 0x73, 0x75, 0x62, 0x74, 0x61, 0x62,
222  0x6c, 0x65, 0x20, 0x3d, 0x20, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x0a,
223  0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x76, 0x29, 0x20, 0x3d, 0x3d, 0x20, 0x22,
224  0x74, 0x61, 0x62, 0x6c, 0x65, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
225  0x09, 0x09, 0x09, 0x09, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x77, 0x61, 0x73, 0x5f, 0x73, 0x75, 0x62, 0x74, 0x61,
226  0x62, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x0a,
227  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72,
228  0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x5c, 0x6e, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x74,
229  0x61, 0x62, 0x73, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x20, 0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x73, 0x65, 0x72,
230  0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x28, 0x76, 0x2c, 0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x2e, 0x2e, 0x20,
231  0x22, 0x20, 0x20, 0x22, 0x2c, 0x20, 0x73, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x29, 0x20, 0x2e, 0x2e, 0x20, 0x22,
232  0x2c, 0x22, 0x0a,
233  0x09, 0x09, 0x09, 0x65, 0x6c, 0x73, 0x65, 0x0a,
234  0x09, 0x09, 0x09, 0x09, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c,
235  0x74, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x6f, 0x73, 0x74, 0x72, 0x69,
236  0x6e, 0x67, 0x28, 0x76, 0x29, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x2c, 0x22, 0x0a,
237  0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
238  0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
239  0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x77, 0x61, 0x73, 0x5f, 0x73, 0x75, 0x62, 0x74,
240  0x61, 0x62, 0x6c, 0x65, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
241  0x09, 0x09, 0x09, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74,
242  0x20, 0x2e, 0x2e, 0x20, 0x22, 0x5c, 0x6e, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x61, 0x62, 0x73, 0x0a,
243  0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
244  0x09, 0x09, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20,
245  0x2e, 0x2e, 0x20, 0x22, 0x7d, 0x22, 0x0a,
246  0x20, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x6f, 0x29, 0x20, 0x3d,
247  0x3d, 0x20, 0x22, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
248  0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x6f, 0x2e, 0x64, 0x6f, 0x6e, 0x74, 0x5f, 0x73, 0x65, 0x72, 0x69,
249  0x61, 0x6c, 0x69, 0x7a, 0x65, 0x5f, 0x6d, 0x65, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
250  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x22, 0x7b, 0x7d, 0x22, 0x0a,
251  0x20, 0x20, 0x20, 0x20, 0x65, 0x6e, 0x64, 0x0a,
252  0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c,
253  0x74, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x7b, 0x5c, 0x6e, 0x22, 0x0a,
254  0x20, 0x20, 0x20, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x2c, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x70, 0x61, 0x69,
255  0x72, 0x73, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x28, 0x6f, 0x29, 0x20, 0x64, 0x6f, 0x0a,
256  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x76, 0x29, 0x20, 0x7e,
257  0x3d, 0x20, 0x22, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
258  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x2d, 0x2d, 0x20, 0x6d, 0x61, 0x6b, 0x65, 0x20, 0x73, 0x75,
259  0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x74, 0x20, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x65, 0x64, 0x20, 0x6b,
260  0x65, 0x79, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x70, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x6c, 0x79, 0x20, 0x61,
261  0x72, 0x65, 0x20, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x69, 0x66, 0x69, 0x65, 0x64, 0x0a,
262  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x6b, 0x29,
263  0x20, 0x3d, 0x3d, 0x20, 0x22, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
264  0x09, 0x09, 0x09, 0x09, 0x20, 0x20, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x76, 0x29, 0x20, 0x3d,
265  0x3d, 0x20, 0x22, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
266  0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20,
267  0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x2e,
268  0x2e, 0x20, 0x22, 0x20, 0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x5b, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x74,
269  0x6f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x28, 0x6b, 0x29, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x5d, 0x20, 0x3d,
270  0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x6f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x28, 0x76, 0x29, 0x20,
271  0x2e, 0x2e, 0x20, 0x22, 0x2c, 0x5c, 0x6e, 0x22, 0x0a,
272  0x09, 0x09, 0x09, 0x09, 0x09, 0x65, 0x6c, 0x73, 0x65, 0x0a,
273  0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20,
274  0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x2e,
275  0x2e, 0x20, 0x22, 0x20, 0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x5b, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x74,
276  0x6f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x28, 0x6b, 0x29, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x5d, 0x20, 0x3d,
277  0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x28, 0x76, 0x2c,
278  0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x20, 0x20, 0x22, 0x2c, 0x20, 0x73, 0x6f, 0x72,
279  0x74, 0x65, 0x64, 0x29, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x2c, 0x5c, 0x6e, 0x22, 0x0a,
280  0x09, 0x09, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
281  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x0a,
282  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d,
283  0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x2e, 0x2e,
284  0x20, 0x22, 0x20, 0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x6b, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x20, 0x3d, 0x20,
285  0x22, 0x20, 0x2e, 0x2e, 0x20, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x28, 0x76, 0x2c, 0x20,
286  0x74, 0x61, 0x62, 0x73, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x20, 0x20, 0x22, 0x2c, 0x20, 0x73, 0x6f, 0x72, 0x74,
287  0x65, 0x64, 0x29, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x2c, 0x5c, 0x6e, 0x22, 0x0a,
288  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x65, 0x6e, 0x64, 0x0a,
289  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x65, 0x6e, 0x64, 0x0a,
290  0x20, 0x20, 0x20, 0x20, 0x65, 0x6e, 0x64, 0x0a,
291  0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c,
292  0x74, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x61, 0x62, 0x73, 0x20, 0x2e, 0x2e, 0x20, 0x22, 0x7d, 0x22, 0x0a,
293  0x20, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x0a,
294  0x20, 0x20, 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x28, 0x22, 0x6e, 0x6f, 0x74, 0x20, 0x73, 0x65,
295  0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x20, 0x6f, 0x66,
296  0x20, 0x74, 0x79, 0x70, 0x65, 0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x6f, 0x29,
297  0x20, 0x29, 0x0a,
298  0x20, 0x20, 0x65, 0x6e, 0x64, 0x0a,
299  0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x0a,
300  0x65, 0x6e, 0x64, 0x0a,
301  0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x0a,
302 };
303 
304 //
305 // Lua Helper Functions
306 //
307 void bail(lua_State * L, const char *msg)
308 {
309  std::cerr << msg << lua_tostring(L, -1) << endl;
310  abort();
311 }
312 
313 void stack_print (const char *file, int line, lua_State * L)
314 {
315  cout << file << ":" << line << ": stack size: " << lua_gettop(L) << endl;
316  for (int i = 1; i < lua_gettop(L) + 1; i++) {
317  cout << file << ":" << line << ": ";
318  cout << i << ": ";
319  if (lua_istable (L, i))
320  cout << " table" << endl;
321  else if (lua_isnumber (L, i))
322  cout << " number: " << lua_tonumber (L, i) << endl;
323  else if (lua_isuserdata (L, i)) {
324  void* userdata = (void*) lua_touserdata (L, i);
325  cout << " userdata (" << userdata << ")" << endl;
326  } else if (lua_isstring (L, i))
327  cout << " string: " << lua_tostring(L, i) << endl;
328  else if (lua_isfunction (L, i))
329  cout << " function" << endl;
330  else if (lua_isnil (L, i))
331  cout << " nil" << endl;
332  else
333  cout << " unknown: " << lua_typename (L, lua_type (L, i)) << endl;
334  }
335 }
336 
337 void l_push_LuaKey (lua_State * L, const LuaKey & key)
338 {
339  if (key.type == LuaKey::Integer)
340  lua_pushnumber (L, key.int_value);
341  else
342  lua_pushstring(L, key.string_value.c_str());
343 }
344 
345 bool query_key_stack (lua_State * L, std::vector<LuaKey> key_stack)
346 {
347  for (int i = (int)key_stack.size() - 1; i >= 0; i--) {
348  // get the global value when the result of a lua expression was not
349  // pushed onto the stack via the return statement.
350  if (lua_gettop(L) == 0) {
351  lua_getglobal (L, key_stack[key_stack.size() - 1].string_value.c_str());
352 
353  if (lua_isnil(L, -1)) {
354  return false;
355  }
356 
357  continue;
358  }
359 
360  l_push_LuaKey (L, key_stack[(size_t) i]);
361 
362  lua_gettable (L, -2);
363 
364  // return if key is not found
365  if (lua_isnil(L, -1)) {
366  return false;
367  }
368  }
369 
370  return true;
371 }
372 
373 void create_key_stack (lua_State * L, std::vector<LuaKey> key_stack)
374 {
375  for (size_t i = key_stack.size() - 1; i > 0; i--) {
376  // get the global value when the result of a lua expression was not
377  // pushed onto the stack via the return statement.
378  if (lua_gettop(L) == 0) {
379  lua_getglobal (L, key_stack[key_stack.size() - 1].string_value.c_str());
380 
381  if (lua_isnil(L, -1)) {
382  lua_pop(L, 1);
383  lua_newtable(L);
384  lua_pushvalue(L, -1);
385  lua_setglobal(L, key_stack[key_stack.size() - 1].string_value.c_str());
386  }
387 
388  continue;
389  }
390 
391  l_push_LuaKey (L, key_stack[i]);
392 
393  lua_pushvalue (L, -1);
394  lua_gettable (L, -3);
395 
396  if (lua_isnil(L, -1)) {
397  // parent, key, nil
398  lua_pop(L, 1); // parent, key
399  lua_newtable(L); // parent, key, table
400  lua_insert(L, -2); // parent, table, key
401  lua_pushvalue(L, -2); // parent, table, key, table
402  lua_settable (L, -4); // parent, table
403  }
404  }
405 }
406 
407 //
408 // LuaTableNode
409 //
410 std::vector<LuaKey> LuaTableNode::getKeyStack()
411 {
412  std::vector<LuaKey> result;
413 
414  const LuaTableNode *node_ptr = this;
415 
416  do {
417  result.push_back (node_ptr->key);
418  node_ptr = node_ptr->parent;
419  } while (node_ptr != NULL);
420 
421  return result;
422 }
423 
424 std::string LuaTableNode::keyStackToString()
425 {
426  std::vector<LuaKey> key_stack = getKeyStack();
427 
428  ostringstream result_stream ("");
429  for (int i = (int)key_stack.size() - 1; i >= 0; i--) {
430  if (key_stack[(size_t) i].type == LuaKey::String)
431  result_stream << "[\"" << key_stack[(size_t) i].string_value << "\"]";
432  else
433  result_stream << "[" << key_stack[(size_t) i].int_value << "]";
434  }
435 
436  return result_stream.str();
437 }
438 
439 bool LuaTableNode::stackQueryValue()
440 {
441  lua_State * L = luaTable->L;
442  stackTop = lua_gettop(L);
443 
444  std::vector<LuaKey> key_stack = getKeyStack();
445 
446  return query_key_stack (L, key_stack);
447 }
448 
449 void LuaTableNode::stackCreateValue()
450 {
451  lua_State * L = luaTable->L;
452  stackTop = lua_gettop(L);
453 
454  std::vector<LuaKey> key_stack = getKeyStack();
455 
456  create_key_stack (L, key_stack);
457 }
458 
459 LuaTable LuaTableNode::stackQueryTable()
460 {
461  lua_State * L = luaTable->L;
462  stackTop = lua_gettop(L);
463 
464  std::vector<LuaKey> key_stack = getKeyStack();
465 
466  if (!query_key_stack (L, key_stack)) {
467  std::cerr << "Error: could not query table " << key << "." << std::endl;
468  abort();
469  }
470 
471  return LuaTable::fromLuaState (L);
472 }
473 
474 LuaTable LuaTableNode::stackCreateLuaTable()
475 {
476  lua_State * L = luaTable->L;
477  stackTop = lua_gettop(L);
478 
479  std::vector<LuaKey> key_stack = getKeyStack();
480 
481  create_key_stack (L, key_stack);
482 
483  // create new table for the CustomType
484  lua_newtable(luaTable->L); // parent, CustomTable
485  // add table of CustomType to the parent
486  stackPushKey(); // parent, CustomTable, key
487  lua_pushvalue(luaTable->L, -2); // parent, CustomTable, key, CustomTable
488  lua_settable(luaTable->L, -4);
489 
490  return LuaTable::fromLuaState (L);
491 }
492 
493 void LuaTableNode::stackPushKey()
494 {
495  l_push_LuaKey (luaTable->L, key);
496 }
497 
498 void LuaTableNode::stackRestore()
499 {
500  lua_pop (luaTable->L, lua_gettop(luaTable->L) - stackTop);
501 }
502 
503 bool LuaTableNode::exists()
504 {
505  bool result = true;
506 
507  if (!stackQueryValue())
508  result = false;
509 
510  stackRestore();
511 
512  return result;
513 }
514 
515 void LuaTableNode::remove()
516 {
517  if (stackQueryValue()) {
518  lua_pop(luaTable->L, 1);
519 
520  if (lua_gettop(luaTable->L) != 0) {
521  l_push_LuaKey (luaTable->L, key);
522  lua_pushnil (luaTable->L);
523  lua_settable (luaTable->L, -3);
524  } else {
525  lua_pushnil (luaTable->L);
526  lua_setglobal (luaTable->L, key.string_value.c_str());
527  }
528  }
529 
530  stackRestore();
531 }
532 
533 size_t LuaTableNode::length()
534 {
535  size_t result = 0;
536 
537  if (stackQueryValue())
538  {
539 #ifdef PINOCCHIO_LUA_VERSION_GREATER_5_2
540  result = lua_rawlen(luaTable->L, -1);
541 #else
542  result = lua_objlen(luaTable->L, -1);
543 #endif
544  }
545 
546  stackRestore();
547 
548  return result;
549 }
550 
551 std::vector<LuaKey> LuaTableNode::keys()
552 {
553  std::vector<LuaKey> result;
554 
555  if (stackQueryValue()) {
556  // loop over all keys
557  lua_pushnil(luaTable->L);
558  while (lua_next(luaTable->L, -2) != 0) {
559  if (lua_isnumber(luaTable->L, -2)) {
560  double number = lua_tonumber (luaTable->L, -2);
561  double frac;
562  if (modf (number, &frac) == 0) {
563  LuaKey key (static_cast<int>(number));
564  result.push_back (key);
565  }
566  } else if (lua_isstring (luaTable->L, -2)) {
567  LuaKey key (lua_tostring(luaTable->L, -2));
568  result.push_back (key);
569  } else {
570  cerr << "Warning: invalid LuaKey type for key " << lua_typename(luaTable->L, lua_type(luaTable->L, -2)) << "!" << endl;
571  }
572 
573  lua_pop(luaTable->L, 1);
574  }
575  }
576 
577  stackRestore();
578 
579  return result;
580 }
581 
582 
583 template<> bool LuaTableNode::getDefault<bool>(const bool & default_value)
584 {
585  bool result = default_value;
586 
587  if (stackQueryValue()) {
588  result = lua_toboolean (luaTable->L, -1);
589  }
590 
591  stackRestore();
592 
593  return result;
594 }
595 
596 template<> float LuaTableNode::getDefault<float>(const float & default_value)
597 {
598  float result = default_value;
599 
600  if (stackQueryValue()) {
601  result = static_cast<float>(lua_tonumber (luaTable->L, -1));
602  }
603 
604  stackRestore();
605 
606  return result;
607 }
608 
609 template<> double LuaTableNode::getDefault<double>(const double & default_value)
610 {
611  double result = default_value;
612 
613  if (stackQueryValue()) {
614  result = lua_tonumber (luaTable->L, -1);
615  }
616 
617  stackRestore();
618 
619  return result;
620 }
621 
622 template<> std::string LuaTableNode::getDefault<std::string>(const std::string & default_value)
623 {
624  std::string result = default_value;
625 
626  if (stackQueryValue() && lua_isstring(luaTable->L, -1)) {
627  result = lua_tostring (luaTable->L, -1);
628  }
629 
630  stackRestore();
631 
632  return result;
633 }
634 
635 template<> void LuaTableNode::set<bool>(const bool & value)
636 {
637  stackCreateValue();
638 
639  l_push_LuaKey (luaTable->L, key);
640  lua_pushboolean(luaTable->L, value);
641  // stack: parent, key, value
642  lua_settable (luaTable->L, -3);
643 
644  stackRestore();
645 }
646 
647 template<> void LuaTableNode::set<float>(const float & value)
648 {
649  stackCreateValue();
650 
651  l_push_LuaKey (luaTable->L, key);
652  lua_pushnumber(luaTable->L, static_cast<double>(value));
653  // stack: parent, key, value
654  lua_settable (luaTable->L, -3);
655 
656  stackRestore();
657 }
658 
659 template<> void LuaTableNode::set<double>(const double & value)
660 {
661  stackCreateValue();
662 
663  l_push_LuaKey (luaTable->L, key);
664  lua_pushnumber(luaTable->L, value);
665  // stack: parent, key, value
666  lua_settable (luaTable->L, -3);
667 
668  stackRestore();
669 }
670 
671 template<> void LuaTableNode::set<std::string>(const std::string & value)
672 {
673  stackCreateValue();
674 
675  l_push_LuaKey (luaTable->L, key);
676  lua_pushstring(luaTable->L, value.c_str());
677  // stack: parent, key, value
678  lua_settable (luaTable->L, -3);
679 
680  stackRestore();
681 }
682 
683 //
684 // LuaTable
685 //
686 LuaTable::~LuaTable()
687 {
688  if (deleteLuaState) {
689  lua_close(L);
690  L = NULL;
691  }
692 }
693 
694 int LuaTable::length()
695 {
696  if ((lua_gettop(L) == 0) || (lua_type (L, -1) != LUA_TTABLE)) {
697  cerr << "Error: cannot query table length. No table on stack!" << endl;
698  abort();
699  }
700  size_t result = 0;
701 
702 #ifdef PINOCCHIO_LUA_VERSION_GREATER_5_2
703  result = lua_rawlen(L, -1);
704 #else
705  result = lua_objlen(L, -1);
706 #endif
707 
708  return (int)result;
709 }
710 
711 LuaTable & LuaTable::operator= (const LuaTable & luatable)
712 {
713  if (this != &luatable) {
714  if (deleteLuaState && L != luatable.L) {
715  lua_close (luatable.L);
716  }
717  filename = luatable.filename;
718  L = luatable.L;
719  deleteLuaState = luatable.deleteLuaState;
720  }
721 
722  return *this;
723 }
724 
725 LuaTable LuaTable::fromFile (const char* _filename)
726 {
727  LuaTable result;
728 
729  result.filename = _filename;
730  result.L = luaL_newstate();
731  result.deleteLuaState = true;
732  luaL_openlibs(result.L);
733 
734  // Add the directory of _filename to package.path
735  result.addSearchPath(get_file_directory (_filename).c_str());
736 
737  // run the file we
738  if (luaL_dofile (result.L, _filename)) {
739  bail (result.L, "Error running file: ");
740  }
741 
742  return result;
743 }
744 
745 LuaTable LuaTable::fromLuaExpression (const char* lua_expr)
746 {
747  LuaTable result;
748 
749  result.L = luaL_newstate();
750  result.deleteLuaState = true;
751  luaL_openlibs(result.L);
752 
753  if (luaL_loadstring (result.L, lua_expr)) {
754  bail (result.L, "Error compiling expression!");
755  }
756 
757  if (lua_pcall (result.L, 0, LUA_MULTRET, 0)) {
758  bail (result.L, "Error running expression!");
759  }
760 
761  return result;
762 }
763 
764 LuaTable LuaTable::fromLuaState (lua_State* L)
765 {
766  LuaTable result;
767 
768  result.L = L;
769  result.deleteLuaState = false;
770 
771  return result;
772 }
773 
774 void LuaTable::addSearchPath(const char* path)
775 {
776  if (L == NULL) {
777  cerr << "Error: Cannot add search path: Lua state is not initialized!" << endl;
778  abort();
779  }
780 
781  lua_getglobal(L, "package");
782  lua_getfield (L, -1, "path");
783  if (lua_type(L, -1) != LUA_TSTRING) {
784  cerr << "Error: could not get package.path!" << endl;
785  abort();
786  }
787 
788  string package_path = lua_tostring (L, -1);
789  package_path = package_path + string(path) + "?.lua;";
790 
791  lua_pushstring(L, package_path.c_str());
792  lua_setfield (L, -3, "path");
793 
794  lua_pop(L, 2);
795 }
796 
797 std::string LuaTable::serialize()
798 {
799  std::string result;
800 
801  int current_top = lua_gettop(L);
802  if (lua_gettop(L) != 0) {
803  if (luaL_loadstring(L, serialize_std)) {
804  bail (L, "Error loading serialization function: ");
805  }
806 
807  if (lua_pcall(L, 0, 0, 0)) {
808  bail (L, "Error compiling serialization function: " );
809  }
810 
811  lua_getglobal (L, "serialize");
812  assert (lua_isfunction (L, -1));
813  lua_pushvalue (L, -2);
814  if (lua_pcall (L, 1, 1, 0)) {
815  bail (L, "Error while serializing: ");
816  }
817  result = string("return ") + lua_tostring (L, -1);
818  } else {
819  cerr << "Cannot serialize global Lua state!" << endl;
820  abort();
821  }
822 
823  lua_pop (L, lua_gettop(L) - current_top);
824 
825  return result;
826 }
827 
828 std::string LuaTable::orderedSerialize()
829 {
830  std::string result;
831 
832  int current_top = lua_gettop(L);
833  if (lua_gettop(L) != 0) {
834  if (luaL_loadstring(L, serialize_std)) {
835  bail (L, "Error loading serialization function: ");
836  }
837 
838  if (lua_pcall(L, 0, 0, 0)) {
839  bail (L, "Error compiling serialization function: " );
840  }
841 
842  lua_getglobal (L, "serialize");
843  assert (lua_isfunction (L, -1));
844  lua_pushvalue (L, -2);
845  lua_pushstring (L, "");
846  lua_pushboolean (L, true);
847  if (lua_pcall (L, 3, 1, 0)) {
848  bail (L, "Error while serializing: ");
849  }
850  result = string("return ") + lua_tostring (L, -1);
851  } else {
852  cerr << "Cannot serialize global Lua state!" << endl;
853  abort();
854  }
855 
856  lua_pop (L, lua_gettop(L) - current_top);
857 
858  return result;
859 }
std::string name(const LieGroupGenericTpl< LieGroupCollection > &lg)
Visit a LieGroupVariant to get the name of it.
STL namespace.