1-- tolua: code class 2-- Written by Waldemar Celes 3-- TeCGraf/PUC-Rio 4-- Jul 1999 5-- $Id$ 6 7-- This code is free software; you can redistribute it and/or modify it. 8-- The software provided hereunder is on an "as is" basis, and 9-- the author has no obligation to provide maintenance, support, updates, 10-- enhancements, or modifications. 11 12 13-- Code class 14-- Represents Lua code to be compiled and included 15-- in the initialization function. 16-- The following fields are stored: 17-- text = text code 18classCode = { 19 text = '', 20 _base = classFeature, 21} 22settag(classCode,tolua_tag) 23 24-- register code 25function classCode:register () 26 -- clean Lua code 27 local s = clean(self.text) 28 if not s then 29 error("parser error in embedded code") 30 end 31 32 -- convert to C 33 output('\n { /* begin embedded lua code */\n') 34 output(' static unsigned char B[] = {\n ') 35 local t={n=0} 36 local b = gsub(s,'(.)',function (c) 37 local e = '' 38 %t.n=%t.n+1 if %t.n==15 then %t.n=0 e='\n ' end 39 return format('%3u,%s',strbyte(c),e) 40 end 41 ) 42 output(b..strbyte(" ")) 43 output('\n };\n') 44 output(' lua_dobuffer(tolua_S,(char*)B,sizeof(B),"tolua: embedded Lua code");') 45 output(' } /* end of embedded lua code */\n\n') 46end 47 48 49-- Print method 50function classCode:print (ident,close) 51 print(ident.."Code{") 52 print(ident.." text = [["..self.text.."]],") 53 print(ident.."}"..close) 54end 55 56 57-- Internal constructor 58function _Code (t) 59 t._base = classCode 60 settag(t,tolua_tag) 61 append(t) 62 return t 63end 64 65-- Constructor 66-- Expects a string representing the code text 67function Code (l) 68 return _Code { 69 text = l 70 } 71end 72 73 74