@@ -297,47 +297,153 @@ public static DynValue traceback(ScriptExecutionContext executionContext, Callba
297297 //}
298298
299299
300- //[MoonSharpMethod]
301- //public static DynValue getinfo(ScriptExecutionContext executionContext, CallbackArguments args)
302- //{
303- // Coroutine cor = executionContext.GetCallingCoroutine();
304- // int vfArgIdx = 0;
305-
306- // if (args[0].Type == DataType.Thread)
307- // cor = args[0].Coroutine;
300+ [ MoonSharpModuleMethod ]
301+ public static DynValue getinfo ( ScriptExecutionContext executionContext , CallbackArguments args )
302+ {
303+ Coroutine cor = executionContext . GetCallingCoroutine ( ) ;
304+ int vfArgIdx = 0 ;
308305
309- // DynValue vf = args[vfArgIdx+0];
310- // DynValue vwhat = args[vfArgIdx+1] ;
306+ if ( args [ 0 ] . Type == DataType . Thread )
307+ cor = args [ 0 ] . Coroutine ;
311308
312- // args.AsType(vfArgIdx + 1, "getinfo", DataType.String, true);
309+ DynValue vf = args [ vfArgIdx + 0 ] ;
310+ DynValue vwhat = args [ vfArgIdx + 1 ] ;
311+
312+ //args.AsType(vfArgIdx + 1, "getinfo", DataType.String, true);
313313
314- // string what = vwhat.CastToString() ?? "nfSlu";
314+ string what = vwhat . CastToString ( ) ?? "nfSlu" ;
315315
316- // DynValue vt = DynValue.NewTable(executionContext.GetScript());
317- // Table t = vt.Table;
316+ DynValue vt = DynValue . NewTable ( executionContext . GetScript ( ) ) ;
317+ Table t = vt . Table ;
318+ if ( vf . Type == DataType . Function || vf . Type == DataType . ClrFunction )
319+ {
320+ GetInfoAboutFunction ( t , vf , what , executionContext ) ;
321+ return vt ;
322+ }
323+ else if ( vf . Type == DataType . Number )
324+ {
325+ var stackTrace = cor . GetStackTrace ( ( int ) vf . Number ) ;
326+ if ( stackTrace . Length <= 0 )
327+ return DynValue . Nil ;
328+ var info = stackTrace [ 0 ] ;
329+ if ( what . Contains ( "S" ) )
330+ {
331+ var source = executionContext . OwnerScript . GetSourceCode ( info . Location . SourceIdx ) ;
332+ if ( source != null )
333+ {
334+ t [ "source" ] = source != null ? source . Name : "[C]" ;
335+ t [ "short_src" ] = source . Name . Substring ( 0 , Math . Min ( source . Name . Length , 60 ) ) ;
336+ t [ "what" ] = "Lua" ;
337+ }
338+ else
339+ {
340+ t [ "source" ] = "[C]" ;
341+ t [ "short_src" ] = "[C]" ;
342+ t [ "what" ] = "C" ;
343+ }
344+ }
318345
319- // if (vf.Type == DataType.Function)
320- // {
321- // Closure f = vf.Function;
322- // executionContext.GetInfoForFunction
323- // }
324- // else if (vf.Type == DataType.ClrFunction)
325- // {
346+ if ( what . Contains ( "L" ) )
347+ {
326348
327- // }
328- // else if (vf.Type == DataType.Number || vf.Type == DataType.String)
329- // {
349+ }
330350
331- // }
332- // else
333- // {
334- // args.AsType(vfArgIdx + 0, "getinfo", DataType.Number, true);
335- // }
351+ if ( what . Contains ( "n" ) )
352+ {
336353
337- // return vt;
354+ }
338355
356+ if ( what . Contains ( "u" ) )
357+ {
339358
340- //}
359+ }
360+ t [ "linedefined" ] = info . Location . FromLine ;
361+ t [ "lastlinedefined" ] = info . Location . ToLine ;
362+ return vt ;
363+ }
364+ return DynValue . Nil ;
365+ }
366+
367+ private static void GetInfoAboutFunction ( Table infoTable , DynValue function , string what , ScriptExecutionContext executionContext )
368+ {
369+ if ( function . Type == DataType . Function )
370+ {
371+ Closure f = function . Function ;
372+ var sourceRef = executionContext . GetFunctionSourceCodeRef ( f ) ;
373+ var source = executionContext . OwnerScript . GetSourceCode ( sourceRef . SourceIdx ) ;
374+ if ( what . Contains ( "S" ) )
375+ {
376+ infoTable [ "source" ] = source . Name ;
377+ infoTable [ "short_src" ] = source . Name . Substring ( 0 , Math . Min ( source . Name . Length , 60 ) ) ;
378+
379+ infoTable [ "what" ] = "Lua" ;
380+ }
381+
382+ if ( what . Contains ( "L" ) )
383+ {
384+ var lines = new Table ( executionContext . OwnerScript ) ;
385+ for ( int i = sourceRef . FromLine ; i <= sourceRef . ToLine ; i ++ )
386+ lines . Append ( DynValue . NewString ( source . Lines [ i ] ) ) ;
387+ infoTable [ "activelines" ] = lines ;
388+ }
389+
390+ if ( what . Contains ( "n" ) )
391+ {
392+ var name = executionContext . GetFunctionName ( f ) ;
393+ infoTable [ "name" ] = name ;
394+ var symbolRef = executionContext . FindSymbolByName ( name ) ;
395+ switch ( symbolRef . Type )
396+ {
397+ case SymbolRefType . Global :
398+ infoTable [ "namewhat" ] = "global" ;
399+ break ;
400+ case SymbolRefType . Local :
401+ infoTable [ "namewhat" ] = "local" ;
402+ break ;
403+ case SymbolRefType . DefaultEnv :
404+ infoTable [ "namewhat" ] = "field" ;
405+ break ;
406+ }
407+ }
408+
409+ if ( what . Contains ( "u" ) )
410+ {
411+ infoTable [ "nups" ] = f . GetUpvaluesCount ( ) ;
412+ }
413+
414+ infoTable [ "linedefined" ] = sourceRef . FromLine ;
415+ infoTable [ "lastlinedefined" ] = sourceRef . ToLine ;
416+ }
417+ else if ( function . Type == DataType . ClrFunction )
418+ {
419+ CallbackFunction f = function . Callback ;
420+ if ( what . Contains ( "S" ) )
421+ {
422+ infoTable [ "source" ] = "[C]" ;
423+ infoTable [ "short_src" ] = "[C]" ;
424+ infoTable [ "what" ] = "C" ;
425+ }
426+
427+ if ( what . Contains ( "n" ) )
428+ {
429+ var symbolRef = executionContext . FindSymbolByName ( f . Name ) ;
430+ infoTable [ "name" ] = f . Name ;
431+ switch ( symbolRef . Type )
432+ {
433+ case SymbolRefType . Global :
434+ infoTable [ "namewhat" ] = "global" ;
435+ break ;
436+ case SymbolRefType . Local :
437+ infoTable [ "namewhat" ] = "local" ;
438+ break ;
439+ case SymbolRefType . DefaultEnv :
440+ infoTable [ "namewhat" ] = "field" ;
441+ break ;
442+ }
443+ }
444+ }
445+
446+ }
341447
342448 }
343449}
0 commit comments