嵌入式Lua“打印"无法在 Visual Studio 的调试模式下工作 [英] Embedded Lua "print" not working in debug mode from Visual Studio

查看:20
本文介绍了嵌入式Lua“打印"无法在 Visual Studio 的调试模式下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Luainterface 2.0.3 将 Lua 嵌入到 c# 应用程序中.

I am using Luainterface 2.0.3 to embed Lua in a c# application.

一切正常,除了在 Visual Studio 的调试模式下,Lua 的 print 函数不会写入控制台(也不会写入输出).

Everything is working fine, except in visual Studio's debug mode, the Lua's print function does not get written to the console (nor to Output).

using System;
using LuaInterface;

namespace Lua1 {
    class Program {
       static void Main(string[] args) {
          Lua lua = new Lua();
          lua.DoString("print 'Hello from Lua!'");
       }
    }
}    

在非调试模式下运行,打印工作正常.

Running it in non debugging mode, print is working fine.

我错过了什么吗?

谢谢!

推荐答案

不幸的是,您可能遇到了 print() 函数中的一个已知缺陷,该函数实际上是用于快速和肮脏的调试在控制台提示符下,缺少一些必要的灵活性.

Unfortunately, you are probably up against a known deficiency in the print() function, which is really intended for quick and dirty debugging at a console prompt, and is missing some necessary flexibility.

luaB_print() 明确使用 C 运行时的 stdout 流作为其目标.由于它在其实现中显式地引用了全局变量 stdout,重定向它的唯一方法是导致该文件句柄被重定向.在 C 程序中,可以通过调用 freopen(stdout,...) 来完成.不幸的是,Lua 中没有可以做到这一点的库存库函数.

The base library function print() implemented by luaB_print() in lbaselib.c explicitly uses the C runtime's stdout stream as its destination. Since it refers to the global variable stdout explicitly in its implementation, the only way to redirect it is to cause that file handle to be redirected. In a C program that can be done by calling freopen(stdout,...). Unfortunately, there isn't a stock library function in Lua that can do that.

io 库在 liolib.c 中实现.它使用函数环境来保存一个打开的文件描述符表,并在其初始化期间创建名为 io.stdinio.stdoutfile 对象> 和 io.stderr 用于三个标准描述符.它还提供名为 io.outputio.input 的函数,以允许修改这两个描述符以指向任何打开的 file 对象(或如果传递了文件名,则为新打开的文件).但是,这些函数只更改函数环境表,不会调用freopen() 来修改C 运行时的FILE 值表.

The io library is implemented in liolib.c. It uses the function environment to hold a table of open file descriptors, and during its initialization it creates file objects named io.stdin, io.stdout and io.stderr for the three standard descriptors. It also provides functions named io.output and io.input to allow those two descriptors to be modified to point to any open file object (or a newly opened file if a file name is passed). However, those functions only change the function environment table and do not call freopen() to modify the C runtime's table of FILE values.

我不知道 LuaInterface 如何处理标准 C 运行时的 stdout 想法.很可能 stdout 没有连接到 VS 调试器中任何有用的东西,因为它可能利用了一些 .NET 功能来捕获正在调试的模块的输出,这些输出可能与无论如何都是C.

I have no idea how LuaInterface attempts to treat the standard C runtime's idea of stdout. It may very well be that stdout is not connected to anything useful in the VS debugger because it probably takes advantage of some .NET feature to capture output from the module being debugged that may not be all that compatible with C in any case.

也就是说,替换标准的print 函数很容易.只需使用 LuaInterface 的现有功能编写一个名为 print 的全局函数,该函数在每个参数上调用 tostring() 并将其传递给任何 .NET 事物是标准输出设备.

That said, it is easy to replace the standard print function. Just use existing features of LuaInterface to write a global function named print that calls tostring() on each argument and passes it to the whatever .NET things is the standard output device.

这篇关于嵌入式Lua“打印"无法在 Visual Studio 的调试模式下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆