@@ -135,6 +135,8 @@ static int lsession_newstate(lua_State *L)
135
135
strcpy (state -> name , name );
136
136
state -> maxalloc = maxalloc ;
137
137
state -> session = session ;
138
+ state -> isvalid = true;
139
+
138
140
139
141
if (lunatikS_newstate (session , state )) {
140
142
pusherrmsg (L , "Failed to create the state\n" );
@@ -154,6 +156,10 @@ static int lsession_newstate(lua_State *L)
154
156
static int lstate_close (lua_State * L )
155
157
{
156
158
struct lunatik_nl_state * state = getnlstate (L );
159
+
160
+ if (!state -> isvalid )
161
+ return luaL_error (L , "invalid state" );
162
+
157
163
if (lunatik_closestate (state )){
158
164
lua_pushnil (L );
159
165
return 1 ;
@@ -170,10 +176,12 @@ static int lstate_dostring(lua_State *L)
170
176
const char * payload = luaL_checklstring (L , 2 , & len );
171
177
const char * script_name = luaL_optstring (L , 3 , "Lunatik" );
172
178
173
- if (strlen (script_name ) > LUNATIK_SCRIPTNAME_MAXSIZE ) {
174
- printf ("script name too long\n" );
175
- goto error ;
176
- }
179
+ if (strlen (script_name ) > LUNATIK_SCRIPTNAME_MAXSIZE )
180
+ return luaL_argerror (L , 3 , "script name too long" );
181
+
182
+ if (!s -> isvalid )
183
+ return luaL_error (L , "invalid state" );
184
+
177
185
int status = lunatik_dostring (s , payload , script_name , len );
178
186
179
187
if (status )
@@ -189,12 +197,20 @@ static int lstate_dostring(lua_State *L)
189
197
190
198
static int lstate_getname (lua_State * L ) {
191
199
struct lunatik_nl_state * s = getnlstate (L );
200
+
201
+ if (!s -> isvalid )
202
+ return luaL_error (L , "invalid state" );
203
+
192
204
lua_pushstring (L , s -> name );
193
205
return 1 ;
194
206
}
195
207
196
208
static int lstate_getmaxalloc (lua_State * L ) {
197
209
struct lunatik_nl_state * s = getnlstate (L );
210
+
211
+ if (!s -> isvalid )
212
+ return luaL_error (L , "invalid state" );
213
+
198
214
lua_pushinteger (L , s -> maxalloc );
199
215
return 1 ;
200
216
}
@@ -240,6 +256,7 @@ static int lsession_getstate(lua_State *L)
240
256
}
241
257
242
258
* state = * received_state ;
259
+ state -> isvalid = true;
243
260
244
261
if (lunatik_initstate (state )) {
245
262
pusherrmsg (L , "Failed to initialize the state\n" );
@@ -277,6 +294,10 @@ static int lstate_datasend(lua_State *L)
277
294
278
295
if (buffer == NULL ) luaL_argerror (L , 2 , "expected non NULL memory object" );
279
296
297
+ if (!state -> isvalid )
298
+ return luaL_error (L , "invalid state" );
299
+
300
+
280
301
err = lunatik_datasend (state , buffer , size );
281
302
err ? lua_pushnil (L ) : lua_pushboolean (L , true);
282
303
@@ -290,6 +311,9 @@ static int lstate_datareceive(lua_State *L)
290
311
struct lunatik_nl_state * state = getnlstate (L );
291
312
char * memory ;
292
313
314
+ if (!state -> isvalid )
315
+ return luaL_error (L , "invalid state" );
316
+
293
317
if (lunatik_receive (state ))
294
318
goto error ;
295
319
@@ -309,6 +333,9 @@ static int lstate_getcurralloc(lua_State *L)
309
333
{
310
334
struct lunatik_nl_state * state = getnlstate (L );
311
335
336
+ if (!state -> isvalid )
337
+ return luaL_error (L , "invalid state" );
338
+
312
339
if (lunatik_getcurralloc (state )) {
313
340
lua_pushnil (L );
314
341
return 1 ;
@@ -319,6 +346,21 @@ static int lstate_getcurralloc(lua_State *L)
319
346
return 1 ;
320
347
}
321
348
349
+ static int lstate_put (lua_State * L )
350
+ {
351
+ struct lunatik_nl_state * state = getnlstate (L );
352
+
353
+ if (lunatik_putstate (state )) {
354
+ lua_pushnil (L );
355
+ return 1 ;
356
+ }
357
+
358
+ lua_pushboolean (L , true);
359
+ state -> isvalid = false;
360
+
361
+ return 1 ;
362
+ }
363
+
322
364
static const luaL_Reg session_mt [] = {
323
365
{"close" , lsession_close },
324
366
{"getfd" , lsession_getfd },
@@ -337,6 +379,7 @@ static const luaL_Reg state_mt[] = {
337
379
{"close" , lstate_close },
338
380
{"send" , lstate_datasend },
339
381
{"receive" , lstate_datareceive },
382
+ {"put" , lstate_put },
340
383
{NULL , NULL }
341
384
};
342
385
0 commit comments