@@ -194,36 +194,72 @@ private unsafe bool ExecuteScript(int count, LuaRunner scriptRunner)
194
194
{
195
195
try
196
196
{
197
- object scriptResult = scriptRunner . Run ( count , parseState ) ;
198
- if ( scriptResult != null )
197
+ var scriptResult = scriptRunner . Run ( count , parseState ) ;
198
+ WriteObject ( scriptResult ) ;
199
+ }
200
+ catch ( LuaScriptException ex )
201
+ {
202
+ logger ? . LogError ( ex . InnerException ?? ex , "Error executing Lua script callback" ) ;
203
+ while ( ! RespWriteUtils . WriteError ( "ERR " + ( ex . InnerException ?? ex ) . Message , ref dcurr , dend ) )
204
+ SendAndReset ( ) ;
205
+ return true ;
206
+ }
207
+ catch ( Exception ex )
208
+ {
209
+ logger ? . LogError ( ex , "Error executing Lua script" ) ;
210
+ while ( ! RespWriteUtils . WriteError ( "ERR " + ex . Message , ref dcurr , dend ) )
211
+ SendAndReset ( ) ;
212
+ return true ;
213
+ }
214
+ return true ;
215
+ }
216
+
217
+ void WriteObject ( object scriptResult )
218
+ {
219
+ if ( scriptResult != null )
220
+ {
221
+ if ( scriptResult is string s )
199
222
{
200
- if ( scriptResult is string s )
201
- {
202
- while ( ! RespWriteUtils . WriteAsciiBulkString ( s , ref dcurr , dend ) )
203
- SendAndReset ( ) ;
204
- }
205
- else if ( ( scriptResult as byte ? ) != null && ( byte ) scriptResult == 36 ) //equals to $
206
- {
207
- while ( ! RespWriteUtils . WriteDirect ( ( byte [ ] ) scriptResult , ref dcurr , dend ) )
208
- SendAndReset ( ) ;
209
- }
210
- else if ( scriptResult as Int64 ? != null )
211
- {
212
- while ( ! RespWriteUtils . WriteInteger ( ( Int64 ) scriptResult , ref dcurr , dend ) )
213
- SendAndReset ( ) ;
214
- }
215
- else if ( scriptResult as ArgSlice ? != null )
223
+ while ( ! RespWriteUtils . WriteAsciiBulkString ( s , ref dcurr , dend ) )
224
+ SendAndReset ( ) ;
225
+ }
226
+ else if ( ( scriptResult as byte ? ) != null && ( byte ) scriptResult == 36 ) //equals to $
227
+ {
228
+ while ( ! RespWriteUtils . WriteDirect ( ( byte [ ] ) scriptResult , ref dcurr , dend ) )
229
+ SendAndReset ( ) ;
230
+ }
231
+ else if ( scriptResult is bool b )
232
+ {
233
+ if ( b )
216
234
{
217
- while ( ! RespWriteUtils . WriteBulkString ( ( ( ArgSlice ) scriptResult ) . ToArray ( ) , ref dcurr , dend ) )
235
+ while ( ! RespWriteUtils . WriteInteger ( 1 , ref dcurr , dend ) )
218
236
SendAndReset ( ) ;
219
237
}
220
- else if ( scriptResult as Object [ ] != null )
238
+ else
221
239
{
222
- // Two objects one boolean value and the result from the Lua Call
223
- while ( ! RespWriteUtils . WriteAsciiBulkString ( ( scriptResult as Object [ ] ) [ 1 ] . ToString ( ) . AsSpan ( ) , ref dcurr , dend ) )
240
+ while ( ! RespWriteUtils . WriteDirect ( CmdStrings . RESP_ERRNOTFOUND , ref dcurr , dend ) )
224
241
SendAndReset ( ) ;
225
242
}
226
- else if ( scriptResult is LuaTable luaTable )
243
+ }
244
+ else if ( scriptResult is long l )
245
+ {
246
+ while ( ! RespWriteUtils . WriteInteger ( l , ref dcurr , dend ) )
247
+ SendAndReset ( ) ;
248
+ }
249
+ else if ( scriptResult is ArgSlice a )
250
+ {
251
+ while ( ! RespWriteUtils . WriteBulkString ( a . ReadOnlySpan , ref dcurr , dend ) )
252
+ SendAndReset ( ) ;
253
+ }
254
+ else if ( scriptResult is object [ ] o )
255
+ {
256
+ // Two objects one boolean value and the result from the Lua Call
257
+ while ( ! RespWriteUtils . WriteAsciiBulkString ( o [ 1 ] . ToString ( ) . AsSpan ( ) , ref dcurr , dend ) )
258
+ SendAndReset ( ) ;
259
+ }
260
+ else if ( scriptResult is LuaTable luaTable )
261
+ {
262
+ try
227
263
{
228
264
var retVal = luaTable [ "err" ] ;
229
265
if ( retVal != null )
@@ -241,36 +277,31 @@ private unsafe bool ExecuteScript(int count, LuaRunner scriptRunner)
241
277
}
242
278
else
243
279
{
244
- throw new LuaScriptException ( "Unknown LuaTable return type" , "" ) ;
280
+ int count = luaTable . Values . Count ;
281
+ while ( ! RespWriteUtils . WriteArrayLength ( count , ref dcurr , dend ) )
282
+ SendAndReset ( ) ;
283
+ foreach ( var value in luaTable . Values )
284
+ {
285
+ WriteObject ( value ) ;
286
+ }
245
287
}
246
288
}
247
289
}
248
- else
290
+ finally
249
291
{
250
- throw new LuaScriptException ( "Unknown return type" , "" ) ;
292
+ luaTable . Dispose ( ) ;
251
293
}
252
294
}
253
295
else
254
296
{
255
- while ( ! RespWriteUtils . WriteDirect ( CmdStrings . RESP_ERRNOTFOUND , ref dcurr , dend ) )
256
- SendAndReset ( ) ;
297
+ throw new LuaScriptException ( "Unknown return type" , "" ) ;
257
298
}
258
299
}
259
- catch ( LuaScriptException ex )
260
- {
261
- logger ? . LogError ( ex . InnerException ?? ex , "Error executing Lua script callback" ) ;
262
- while ( ! RespWriteUtils . WriteError ( "ERR " + ( ex . InnerException ?? ex ) . Message , ref dcurr , dend ) )
263
- SendAndReset ( ) ;
264
- return true ;
265
- }
266
- catch ( Exception ex )
300
+ else
267
301
{
268
- logger ? . LogError ( ex , "Error executing Lua script" ) ;
269
- while ( ! RespWriteUtils . WriteError ( "ERR " + ex . Message , ref dcurr , dend ) )
302
+ while ( ! RespWriteUtils . WriteDirect ( CmdStrings . RESP_ERRNOTFOUND , ref dcurr , dend ) )
270
303
SendAndReset ( ) ;
271
- return true ;
272
304
}
273
- return true ;
274
305
}
275
306
}
276
307
}
0 commit comments