2f285cecc2bcdd740529783fcc8d4f7ea0fa581b
[chaz/yoink] / src / Moof / Script.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_SCRIPT_HH_
30 #define _MOOF_SCRIPT_HH_
31
32 /**
33 * @file Script.hh
34 * A thin wrapper over Lua. This is not meant as a complicated binding package
35 * between C++ and Lua. It does not try to make the boundary invisible. It
36 * does not hide the concept of the Lua stack, but rather provides that
37 * mechanism with a certain level of abstraction while also providing a cleaner,
38 * more consistent API.
39 */
40
41 #include <list>
42 #include <map>
43 #include <string>
44 #include <vector>
45
46 #include <boost/bind.hpp>
47 #include <boost/function.hpp>
48 #include <boost/shared_ptr.hpp>
49
50 #include <lua.hpp>
51
52 #include <Moof/Log.hh>
53
54
55 namespace Mf {
56
57
58 class Script;
59 typedef boost::shared_ptr<Script> ScriptP;
60
61
62 class Script
63 {
64 public:
65
66 typedef boost::function<int(Script&)> Function;
67
68 enum Type
69 {
70 NONE = LUA_TNONE,
71 NIL = LUA_TNIL,
72 BOOLEAN = LUA_TBOOLEAN,
73 LIGHTUSERDATA = LUA_TLIGHTUSERDATA,
74 NUMBER = LUA_TNUMBER,
75 STRING = LUA_TSTRING,
76 TABLE = LUA_TTABLE,
77 FUNCTION = LUA_TFUNCTION,
78 USERDATA = LUA_TUSERDATA,
79 THREAD = LUA_TTHREAD
80 };
81
82 enum Status
83 {
84 SUCCESS = 0,
85 YIELD = LUA_YIELD,
86 RUNTIME_ERROR = LUA_ERRRUN,
87 SYNTAX_ERROR = LUA_ERRSYNTAX,
88 MEMORY_ERROR = LUA_ERRMEM,
89 HANDLER_ERROR = LUA_ERRERR,
90 FILE_ERROR = LUA_ERRFILE
91 };
92
93 enum PseudoIndex
94 {
95 REGISTRY = LUA_REGISTRYINDEX,
96 ENVIRONMENT = LUA_ENVIRONINDEX,
97 GLOBALS = LUA_GLOBALSINDEX
98 };
99
100 /**
101 * This is the most prominent abstraction on top of the standard Lua API.
102 * A Value object represents a value on the stack. More specifically, it
103 * represents a position on the stack. The distinction is only important
104 * when values are moved around on the stack or if the Value represents a
105 * negative index on the stack (the value of which will change as things are
106 * pushed onto and popped from the stack).
107 */
108
109 struct Value
110 {
111 /**
112 * You have direct access to the index of the value on the stack being
113 * represented.
114 */
115
116 int index;
117
118
119 /**
120 * A default-constructed Value is invalid until a valid Value is
121 * assigned to it. The only method that should be called on such a
122 * Value is isValid(), otherwise chaos may ensue. In this case, the
123 * Value will be invalid even if index is manually changed to a valid
124 * index. You have to index the script itself to get a valid Value.
125 */
126 Value(lua_State* s = 0, int i = 0) :
127 index(i),
128 state(s) {}
129
130 /**
131 * A copied value presently points to the same value, except the real
132 * index is used. That means that if a value that refers to a frame
133 * referenced from the top of the stack will have its normalized index
134 * copied into the new value object.
135 */
136
137 Value(const Value& copy) :
138 index(copy.getRealIndex()),
139 state(copy.state) {}
140
141
142 // check the type of the value
143 bool isBoolean() const { return (bool)lua_isboolean(state, index); }
144 bool isFunction() const { return (bool)lua_isfunction(state, index); }
145 bool isNil() const { return (bool)lua_isnil(state, index); }
146 bool isNone() const { return (bool)lua_isnone(state, index); }
147 bool isValid() const { return state != 0 && !isNone(); }
148 bool isNoneOrNil() const { return (bool)lua_isnoneornil(state, index); }
149 bool isNumber() const { return (bool)lua_isnumber(state, index); }
150 bool isString() const { return (bool)lua_isstring(state, index); }
151 bool isTable() const { return (bool)lua_istable(state, index); }
152 bool isThread() const { return (bool)lua_isthread(state, index); }
153 bool isData() const { return (bool)lua_isuserdata(state, index); }
154 bool isLightData() const { return (bool)lua_islightuserdata(state, index); }
155
156 /**
157 * Check the value and throw an error if its the wrong type. There's a
158 * little caveat: This method never returns because it does a long jump.
159 * Consequently, constructed C++ objects which exist on the stack
160 * between the current frame and some lua function will not be
161 * destructed. That's not a problem for objects that only exist on the
162 * stack, but any objects that allocate memory on the heap (such as
163 * containers or strings) will leak. Therefore, you should only call
164 * this method after cleaning up such objects. The best thing to do for
165 * defining functions is to simply check all the parameters at the
166 * get-go before any C++ objects are even constructed.
167 */
168
169 void requireType(Type type) const
170 {
171 if (type != getType())
172 {
173 luaL_typerror(state, index, lua_typename(state, type));
174 }
175 }
176
177 void throwError(const char* error)
178 {
179 luaL_argerror(state, index, error);
180 }
181
182
183 Value& requireBoolean()
184 {
185 if (!isBoolean()) luaL_typerror(state, index, "boolean");
186 return *this;
187 }
188 Value& requireNumber()
189 {
190 if (!isNumber()) luaL_typerror(state, index, "number");
191 return *this;
192 }
193 Value& requireString()
194 {
195 if (!isString()) luaL_typerror(state, index, "string");
196 return *this;
197 }
198 Value& requireTable()
199 {
200 if (!isTable()) luaL_typerror(state, index, "table");
201 return *this;
202 }
203 Value& requireFunction()
204 {
205 if (!isFunction()) luaL_typerror(state, index, "function");
206 return *this;
207 }
208 Value& requireData()
209 {
210 if (!isData()) luaL_typerror(state, index, "data");
211 return *this;
212 }
213 Value& requireNil()
214 {
215 if (!isNil()) luaL_typerror(state, index, "nil");
216 return *this;
217 }
218 Value& requireThread()
219 {
220 if (!isThread()) luaL_typerror(state, index, "thread");
221 return *this;
222 }
223
224
225 /**
226 * Get the type of the value.
227 */
228
229 Type getType() const
230 {
231 return (Type)lua_type(state, index);
232 }
233
234 /**
235 * Get the name of the type of the value as a string.
236 */
237
238 std::string getTypeName() const
239 {
240 return std::string(luaL_typename(state, index));
241 }
242
243
244 /**
245 * Get the length of the value according to the definition given by Lua.
246 */
247
248 size_t getLength() const
249 {
250 return lua_objlen(state, index);
251 }
252
253 int getRealIndex() const
254 {
255 if (index < 0) return lua_gettop(state) + 1 + index;
256 else return index;
257 }
258
259
260 /**
261 * Get a pointer value (for userdata, tables, threads, and functions).
262 */
263
264 const void* getIdentifier() const
265 {
266 return lua_topointer(state, index);
267 }
268
269
270 bool operator == (const Value& rhs) const
271 {
272 return (bool)lua_equal(state, index, rhs.index);
273 }
274 bool operator != (const Value& rhs) const
275 {
276 return !(*this == rhs);
277 }
278 bool operator < (const Value& rhs) const
279 {
280 return (bool)lua_lessthan(state, index, rhs.index);
281 }
282 bool operator <= (const Value& rhs) const
283 {
284 return *this < rhs || *this == rhs;
285 }
286 bool operator > (const Value& rhs) const
287 {
288 return !(*this <= rhs);
289 }
290 bool operator >= (const Value& rhs) const
291 {
292 return !(*this < rhs);
293 }
294 operator bool () const
295 {
296 return (bool)lua_toboolean(state, index);
297 }
298
299 Value& operator = (const Value& rhs)
300 {
301 rhs.pushCopy();
302 replaceWithTop();
303 return *this;
304 }
305
306
307 /**
308 * Convert the underlying value to a C++ type.
309 */
310
311 template <typename T>
312 bool get(T& value) const
313 {
314 if (isNumber())
315 {
316 value = (T)lua_tointeger(state, index);
317 return true;
318 }
319 return false;
320 }
321
322 bool get(float& value) const
323 {
324 if (isNumber())
325 {
326 value = (float)lua_tonumber(state, index);
327 return true;
328 }
329 return false;
330 }
331 bool get(double& value) const
332 {
333 if (isNumber())
334 {
335 value = (double)lua_tonumber(state, index);
336 return true;
337 }
338 return false;
339 }
340
341 bool get(bool& value) const
342 {
343 if (isBoolean())
344 {
345 value = (bool)lua_toboolean(state, index);
346 return true;
347 }
348 return false;
349 }
350
351 bool get(std::string& value) const
352 {
353 if (isString())
354 {
355 size_t size;
356 const char* str = lua_tolstring(state, index, &size);
357 value.assign(str, size);
358 return true;
359 }
360 return false;
361 }
362
363 template <typename T>
364 bool get(std::vector<T>& array) const
365 {
366 if (!isTable()) return false;
367
368 array.clear();
369
370 Value value(state, -1);
371 int realIndex = getRealIndex();
372
373 bool done = false;
374 for (int i = 1; !done; ++i)
375 {
376 lua_rawgeti(state, realIndex, i);
377
378 T v;
379 if (value.get(v)) array.push_back(v);
380 else done = true;
381
382 lua_pop(state, 1);
383 }
384
385 return true;
386 }
387
388 template <typename T>
389 bool get(std::map<std::string,T>& dictionary) const
390 {
391 if (!isTable()) return false;
392
393 dictionary.clear();
394
395 Value key(state, -2);
396 Value value(state, -1);
397 int realIndex = getRealIndex();
398
399 lua_pushnil(state);
400 while (lua_next(state, realIndex) != 0)
401 {
402 std::string k;
403 if (!key.isNumber() && key.get(k))
404 {
405 T v;
406 if (value.get(v)) dictionary[k] = v;
407 }
408 lua_pop(state, 1);
409 }
410 lua_pop(state, 1);
411
412 return true;
413 }
414
415
416
417 /**
418 * Copy the value and push the copy to the stack.
419 */
420
421 void pushCopy() const
422 {
423 lua_pushvalue(state, index);
424 }
425
426 /**
427 * Replace this value with the value at the top of the stack.
428 */
429
430 void replaceWithTop()
431 {
432 lua_replace(state, index);
433 }
434
435 void remove()
436 {
437 lua_remove(state, index);
438 }
439
440 /**
441 * Inserts the top-most value on the stack at position index, shifting other
442 * values as needed.
443 */
444
445 void insertTopHere()
446 {
447 lua_insert(state, index);
448 }
449
450
451 void pushMetatable() const
452 {
453 lua_getmetatable(state, index);
454 }
455
456 void pushField() const
457 {
458 lua_gettable(state, index);
459 }
460
461 void pushField(const std::string& name) const
462 {
463 lua_getfield(state, index, name.c_str());
464 }
465
466
467 private:
468
469 lua_State* state;
470 };
471
472
473 Script() :
474 mState(0)
475 {
476 reset();
477 }
478
479 ~Script()
480 {
481 destroy();
482 }
483
484
485 static ScriptP alloc()
486 {
487 return ScriptP(new Script);
488 }
489
490 void reset()
491 {
492 if (mState) destroy();
493 mState = luaL_newstate();
494 lua_pushlightuserdata(mState, this);
495 lua_setfield(mState, LUA_REGISTRYINDEX, "_script_obj");
496 }
497
498
499 void importStandardLibraries()
500 {
501 luaL_openlibs(mState);
502 }
503
504 void importBaseLibrary()
505 {
506 lua_pushcfunction(mState, luaopen_base);
507 push(LUA_COLIBNAME);
508 call(1, 0);
509 }
510
511 void importPackageLibrary()
512 {
513 lua_pushcfunction(mState, luaopen_package);
514 push(LUA_LOADLIBNAME);
515 call(1, 0);
516 }
517
518 void importStringLibrary()
519 {
520 lua_pushcfunction(mState, luaopen_string);
521 push(LUA_STRLIBNAME);
522 call(1, 0);
523 }
524
525 void importTableLibrary()
526 {
527 lua_pushcfunction(mState, luaopen_table);
528 push(LUA_TABLIBNAME);
529 call(1, 0);
530 }
531
532 void importMathLibrary()
533 {
534 lua_pushcfunction(mState, luaopen_math);
535 push(LUA_MATHLIBNAME);
536 call(1, 0);
537 }
538
539 void importIoLibrary()
540 {
541 lua_pushcfunction(mState, luaopen_io);
542 push(LUA_IOLIBNAME);
543 call(1, 0);
544 }
545
546 void importOsLibrary()
547 {
548 lua_pushcfunction(mState, luaopen_os);
549 push(LUA_OSLIBNAME);
550 call(1, 0);
551 }
552
553 void importDebugLibrary()
554 {
555 lua_pushcfunction(mState, luaopen_debug);
556 push(LUA_DBLIBNAME);
557 call(1, 0);
558 }
559
560
561 void importFunction(const std::string& name, const Function& function)
562 {
563 push(function);
564 lua_setglobal(mState, name.c_str());
565 }
566
567
568 Status doString(const std::string& commands)
569 {
570 return (Status)luaL_dostring(mState, commands.c_str());
571 }
572
573 Status doFile(const std::string& file)
574 {
575 return (Status)luaL_dofile(mState, file.c_str());
576 }
577
578
579 /**
580 * Thread-handling methods.
581 */
582
583 Script pushNewThread()
584 {
585 return Script(mState);
586 }
587
588 void pushThread()
589 {
590 lua_pushthread(mState);
591 }
592
593 Status resume(int nargs)
594 {
595 return (Status)lua_resume(mState, nargs);
596 }
597
598 Status getStatus() const
599 {
600 return (Status)lua_status(mState);
601 }
602
603 int yield(int results)
604 {
605 return lua_yield(mState, results);
606 }
607
608 bool isMainThread() const
609 {
610 return mIsMainThread;
611 }
612
613
614 /**
615 * Throw an error with the value at the top of the stack. This method never
616 * returns because it does a long jump. Consequently, constructed C++
617 * objects which exist on the stack between the current frame and some lua
618 * function will not be destructed. That's not a problem for objects that
619 * only exist on the stack, but any objects that allocate memory on the heap
620 * (such as containers or strings) will leak. Therefore, you should only
621 * call this method after cleaning up such objects.
622 */
623
624 void throwError()
625 {
626 lua_error(mState);
627 }
628
629
630 /**
631 * Get significant values.
632 */
633
634 Value getGlobalTable() const
635 {
636 return Value(mState, GLOBALS);
637 }
638
639 Value getRegistryTable() const
640 {
641 return Value(mState, REGISTRY);
642 }
643
644 Value getEnvironmentTable() const
645 {
646 return Value(mState, ENVIRONMENT);
647 }
648
649 Value getTop() const
650 {
651 return Value(mState, lua_gettop(mState));
652 }
653
654 /**
655 * Get the size of the stack; this is also the index of the top-most value.
656 */
657
658 int getSize() const
659 {
660 return lua_gettop(mState);
661 }
662
663 void setSize(int size)
664 {
665 lua_settop(mState, size);
666 }
667
668 void clear()
669 {
670 setSize(0);
671 }
672
673
674 /**
675 * Makes sure there is at least extra more places on the stack. Returns
676 * false if space couldn't be created. Just like with the regular Lua API,
677 * you are responsible to make sure the stack is big enough to hold whatever
678 * you want to push on it. This is usually only an issue if you're pushing
679 * stuff in a loop.
680 */
681
682 bool checkStack(int extra)
683 {
684 return (bool)lua_checkstack(mState, extra);
685 }
686
687
688 /**
689 * Concatenates the top-most n values on the stack.
690 */
691
692 void concat(int n)
693 {
694 lua_concat(mState, n);
695 }
696
697
698 /**
699 * Push some values onto the stack.
700 */
701
702 template <typename T>
703 void push(T value)
704 {
705 lua_pushinteger(mState, lua_Integer(value));
706 }
707
708 void push(bool value)
709 {
710 lua_pushboolean(mState, int(value));
711 }
712
713 void push(float value)
714 {
715 lua_pushnumber(mState, (lua_Number)value);
716 }
717 void push(double value)
718 {
719 lua_pushnumber(mState, (lua_Number)value);
720 }
721
722 void push(const std::string& value)
723 {
724 lua_pushlstring(mState, value.c_str(), value.length());
725 }
726 void push(const char* value)
727 {
728 lua_pushstring(mState, value);
729 }
730 void push(const char* value, size_t length)
731 {
732 lua_pushlstring(mState, value, length);
733 }
734
735 void push(const Function& function)
736 {
737 mFunctions.push_back(function);
738
739 lua_pushlightuserdata(mState, (void*)&mFunctions.back());
740 lua_pushcclosure(mState, dispatchCall, 1);
741 }
742
743 void push(void* data)
744 {
745 lua_pushlightuserdata(mState, data);
746 }
747
748 void pushNil()
749 {
750 lua_pushnil(mState);
751 }
752
753 void pushFromThread(Script& thread, int n)
754 {
755 lua_xmove(thread.mState, mState, n);
756 }
757
758 Status pushCode(const std::string& filename)
759 {
760 return (Status)luaL_loadfile(mState, filename.c_str());
761 }
762
763 Status pushCode(const std::string& name, const char* buffer, size_t size)
764 {
765 return (Status)luaL_loadbuffer(mState, buffer, size, name.c_str());
766 }
767
768 void* pushNewData(size_t size)
769 {
770 return lua_newuserdata(mState, size);
771 }
772
773 void pushNewTable()
774 {
775 lua_newtable(mState);
776 }
777
778
779 /**
780 * Call a function on the stack. The correct procedure is to push a
781 * function onto the stack followed by nargs arguments. This method will
782 * pop them off upon return, leaving up to nresults return values (default
783 * is any number of return values, depending on the callee).
784 */
785
786 Status call(int nargs, int nresults = LUA_MULTRET)
787 {
788 return (Status)lua_pcall(mState, nargs, nresults, 0);
789 }
790
791
792 /**
793 * Pops n values from the top of the stack.
794 */
795
796 void pop(int n = 1)
797 {
798 lua_pop(mState, n);
799 }
800
801
802 /**
803 * Index into the stack to get a Value.
804 */
805
806 Value operator [] (int index) const
807 {
808 return Value(mState, index);
809 }
810
811
812 /**
813 * Getting and setting fields of a table.
814 */
815
816 void get(const std::string& field, int index = GLOBALS) const
817 {
818 lua_getfield(mState, index, field.c_str());
819 }
820
821 void set(const std::string& field, int index = GLOBALS)
822 {
823 lua_setfield(mState, index, field.c_str());
824 }
825
826
827 /**
828 * Control over the garbage collection process.
829 */
830
831 void collectAll()
832 {
833 lua_gc(mState, LUA_GCCOLLECT, 0);
834 }
835
836 void stopCollector()
837 {
838 lua_gc(mState, LUA_GCSTOP, 0);
839 }
840
841 void restartCollector()
842 {
843 lua_gc(mState, LUA_GCRESTART, 0);
844 }
845
846 int getUsedMemory() const
847 {
848 // in kilobytes
849 return lua_gc(mState, LUA_GCCOUNT, 0);
850 }
851
852 void collectStep(int step)
853 {
854 lua_gc(mState, LUA_GCSTEP, step);
855 }
856
857 void tuneCollector(int pause, int step)
858 {
859 lua_gc(mState, LUA_GCSETPAUSE, pause);
860 lua_gc(mState, LUA_GCSETSTEPMUL, step);
861 }
862
863
864 private:
865
866 Script(lua_State* state) :
867 mState(lua_newthread(state)),
868 mIsMainThread(false) {}
869
870 static int dispatchCall(lua_State* state)
871 {
872 const Function* function = (const Function*)lua_touserdata(state,
873 lua_upvalueindex(1));
874
875 lua_getfield(state, LUA_REGISTRYINDEX, "_script_obj");
876 Script* script = (Script*)lua_touserdata(state, -1);
877 lua_pop(state, 1);
878
879 return (*function)(*script);
880 }
881
882 void destroy()
883 {
884 if (mIsMainThread) lua_close(mState);
885 }
886
887 lua_State* mState;
888 bool mIsMainThread;
889 std::list<Function> mFunctions;
890 };
891
892
893 } // namespace Mf
894
895 #endif // _MOOF_SCRIPT_HH_
896
897 /** vim: set ts=4 sw=4 tw=80: *************************************************/
898
This page took 0.072192 seconds and 3 git commands to generate.