@@ -6,8 +6,6 @@ local ipairs = ipairs
66local unpack = unpack or table.unpack
77local sformat = string.format
88local mmax , mmodf = math.max , math.modf
9- local coro_wrap = coroutine.wrap
10- local coro_yield = coroutine.yield
119local DEBUG = os and os.getenv and os.getenv (' DEBUG' ) == ' 1'
1210local tab_concat = table.concat
1311local tab_insert = table.insert
@@ -136,70 +134,60 @@ end
136134
137135-- load doesn't like at all empty string, but sometimes it is easier to add
138136-- some in the chunk buffer
139- local function yield_chunk (chunk )
137+ local function insert_code (chunk , code_table )
140138 if chunk and chunk ~= ' ' then
141- coro_yield ( chunk )
139+ tab_insert ( code_table , chunk )
142140 end
143141end
144142
145- function codectx_mt :_generate ()
143+ function codectx_mt :_generate (code_table )
146144 local indent = ' '
147145 if self ._root == self then
148146 for _ , stmt in ipairs (self ._preface ) do
149- yield_chunk (indent )
147+ insert_code (indent , code_table )
150148 if getmetatable (stmt ) == codectx_mt then
151- stmt :_generate ()
149+ stmt :_generate (code_table )
152150 else
153- yield_chunk (stmt )
151+ insert_code (stmt , code_table )
154152 end
155153 end
156154 else
157- coro_yield (' function(' )
155+ insert_code (' function(' , code_table )
158156 for i = 1 , self ._nparams do
159- yield_chunk (' p_' .. i )
160- if i ~= self ._nparams then yield_chunk (' , ' ) end
157+ insert_code (' p_' .. i , code_table )
158+ if i ~= self ._nparams then insert_code (' , ' , code_table ) end
161159 end
162- yield_chunk (' )\n ' )
160+ insert_code (' )\n ' , code_table )
163161 indent = string.rep (' ' , self ._idx )
164162 end
165163
166164 for _ , stmt in ipairs (self ._body ) do
167- yield_chunk (indent )
165+ insert_code (indent , code_table )
168166 if getmetatable (stmt ) == codectx_mt then
169- stmt :_generate ()
167+ stmt :_generate (code_table )
170168 else
171- yield_chunk (stmt )
169+ insert_code (stmt , code_table )
172170 end
173171 end
174172
175173 if self ._root ~= self then
176- yield_chunk (' end' )
174+ insert_code (' end' , code_table )
177175 end
178176end
179177
180178function codectx_mt :_get_loader ()
181- return coro_wrap ( function ()
182- self :_generate ()
183- end )
179+ self . _code_table = {}
180+ self :_generate (self . _code_table )
181+ return self . _code_table
184182end
185183
186184function codectx_mt :as_string ()
187- local buf , n = {}, 0
188- for chunk in self :_get_loader () do
189- n = n + 1
190- buf [n ] = chunk
191- end
192- return tab_concat (buf )
185+ return tab_concat (self ._code_table )
193186end
194187
195188function codectx_mt :as_func (name , ...)
196- local buf , n = {}, 0
197- for chunk in self :_get_loader () do
198- n = n + 1
199- buf [n ] = chunk
200- end
201-
202- local loader , err = loadstring (tab_concat (buf , " " ), ' jsonschema:' .. (name or ' anonymous' ))
189+ self :_get_loader ()
190+ local loader , err = loadstring (tab_concat (self ._code_table , " " ), ' jsonschema:' .. (name or ' anonymous' ))
203191 if loader then
204192 local validator
205193 validator , err = loader (self ._uservalues , ... )
@@ -240,6 +228,7 @@ local function codectx(schema, options)
240228 local self = setmetatable ({
241229 _schema = store .new (schema , options .external_resolver ),
242230 _id = schema .id ,
231+ _code_table = {},
243232 _path = ' ' ,
244233 _idx = 0 ,
245234 -- code generation
0 commit comments