@@ -111,15 +111,28 @@ function codectx_mt:localvar(init, nres)
111
111
local names = {}
112
112
local nloc = self ._nloc
113
113
nres = nres or 1
114
- for i = 1 , nres do
115
- names [i ] = sformat (' var_%d_%d' , self ._idx , nloc + i )
114
+ for i = 1 , nres do
115
+ names [i ] = sformat (' var_%d_%d' , self ._idx , nloc + i )
116
116
end
117
117
118
118
self :stmt (sformat (' local %s = ' , tab_concat (names , ' , ' )), init or ' nil' )
119
119
self ._nloc = nloc + nres
120
120
return unpack (names )
121
121
end
122
122
123
+ function codectx_mt :localvartab (init , nres )
124
+ local names = {}
125
+ local nloc = self ._nloc
126
+ nres = nres or 1
127
+ for i = 1 , nres do
128
+ names [i ] = sformat (' locals.var_%d_%d' , self ._idx , nloc + i )
129
+ end
130
+
131
+ self :stmt (sformat (' %s = ' , tab_concat (names , ' , ' )), init or ' nil' )
132
+ self ._nloc = nloc + nres
133
+ return unpack (names )
134
+ end
135
+
123
136
function codectx_mt :param (n )
124
137
self ._nparams = mmax (n , self ._nparams )
125
138
return ' p_' .. n
@@ -148,7 +161,7 @@ function codectx_mt:validator(path, schema)
148
161
local root = self ._root
149
162
local var = root ._validators [resolved ]
150
163
if not var then
151
- var = root :localvar (' nil' )
164
+ var = root :localvartab (' nil' )
152
165
root ._validators [resolved ] = var
153
166
root :stmt (sformat (' %s = ' , var ), generate_validator (root :child (ref ), resolved ))
154
167
end
513
526
514
527
generate_validator = function (ctx , schema )
515
528
-- get type informations as they will be necessary anyway
516
- local datatype = ctx :localvar (sformat (' %s(%s)' ,
529
+ local datatype = ctx :localvartab (sformat (' %s(%s)' ,
517
530
ctx :libfunc (' type' ), ctx :param (1 )))
518
- local datakind = ctx :localvar (sformat (' %s == "table" and %s(%s)' ,
531
+ local datakind = ctx :localvartab (sformat (' %s == "table" and %s(%s)' ,
519
532
datatype , ctx :libfunc (' lib.tablekind' ), ctx :param (1 )))
520
533
521
534
if type (schema ) == " table" and schema ._org_val ~= nil then
@@ -661,7 +674,7 @@ generate_validator = function(ctx, schema)
661
674
local propset , addprop_validator -- all properties defined in the object
662
675
if schema .additionalProperties ~= nil and schema .additionalProperties ~= true then
663
676
-- TODO: can be optimized with a static table expression
664
- propset = ctx ._root :localvar (' {}' )
677
+ propset = ctx ._root :localvartab (' {}' )
665
678
if schema .properties then
666
679
for prop , _ in pairs (schema .properties ) do
667
680
ctx ._root :stmt (sformat (' %s[%q] = true' , propset , prop ))
@@ -1122,6 +1135,7 @@ local function generate_main_validator_ctx(schema, options)
1122
1135
-- * the custom callbacks (used to customize various aspects of validation
1123
1136
-- or for dependency injection)
1124
1137
ctx :preface (' local uservalues, lib, custom = ...' )
1138
+ ctx :preface (' local locals = {}' )
1125
1139
ctx :stmt (' return ' , ctx :validator (nil , schema ))
1126
1140
return ctx
1127
1141
end
0 commit comments