Lua语言简易集成开发环境--四:代码调试

代码调试

这个部分是我最头痛的部分,因为想用交互式的去调试Lua脚本,但是实在是不知道怎么去实现这个功能。

代码调试分为:设置断点,开始调试,结束调试以及单步运行结合,可以显示出当前Lua脚本的局部变量。

断点调试是将断点所在的行数之前的代码脚本保存到一个缓存Lua文件中,再用cmd调用这个缓存文件。由于缓存文件中有自己用Lua写好的显示当期局部变量的代码,则可以返回局部变量和值。
如图:
在这里插入图片描述
进行了开始调试,显示了a的值。

再进单步运行,则可以继续向下运行,并返回当前局部变量。
如图:
在这里插入图片描述
返回了b的值。

开始调试代码实现:

st_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		st_1.setWordWrap(true);//自动换行
		st_1.setText("");//每次运行先清空
		try {
			int i=0;
			try {
				File filename = new File("C:\\Users\\ZYF\\Desktop\\cache.lua");
				if(!filename.exists()) {
					filename.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
					}
				FileWriter writer = new FileWriter(filename);
				BufferedWriter out = new BufferedWriter(writer);
				String hook="function locals()\r\n"
						+ "  local variables = {}\r\n"
						+ "  local idx = 1\r\n"
						+ "  while true do\r\n"
						+ "    local ln, lv = debug.getlocal(2, idx)\r\n"
						+ "    if ln ~= nil then\r\n"
						+ "      variables[ln] = lv\r\n"
						+ "    else\r\n"
						+ "      break\r\n"
						+ "    end\r\n"
						+ "    idx = 1 + idx\r\n"
						+ "  end\r\n"
						+ "  return variables\r\n"
						+ "end\r\n";
				out.write(hook);
				while(i<=Line_debug) {
					if(i==Line_debug-1) {
						out.write(st.getLine(i)+"\r\n");
						out.write("for k,v in pairs(locals()) do\r\n"
								+ "    print(\"局部变量\", k, \"值\", v)\r\n"
								+ "end");
						
						break;
					}
					cmd=st.getLine(i)+"\r\n";
					out.write(cmd);
					i++;
				}
				System.out.println("调试断点行"+" "+Line_debug);
				out.flush(); // 把缓存区内容压入文件
				out.close();
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.out.println(UI.parent_path);
			Process exec = Runtime.getRuntime().exec("cmd /c cd "+UI.parent_path+"&&lua cache.lua");
			//Process exec = Runtime.getRuntime().exec(cmd+" print(debug.get info(1))");
			exec.waitFor();
	        final int exitValue = exec.waitFor();
	        if(exitValue==0) {
	        	InputStream input = exec.getInputStream(); //获取cmd段的返回结果
	        	BufferedReader reader = new BufferedReader(new InputStreamReader(input));
	        	String szline=null;
				while((szline= reader.readLine()) != null) {
					szline=st_1.getText()+szline+"\n";
					st_1.setText(szline);
				}
				reader.close(); 
	        }
	        else {
	        	InputStream errinput=exec.getErrorStream();//获取错误消息
	        	BufferedReader errinreader = new BufferedReader(new InputStreamReader(errinput));
	        	String szline=null;
	        	st_1.setText("error--> ");
				while((szline= errinreader.readLine()) != null) {
					szline=st_1.getText()+szline+"\n";
					st_1.setText(szline);
				}
				errinreader.close(); 
	        } 
	        exec.waitFor();
			exec.destroy(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-gene rated catch block
			e.printStackTrace();
		} 

单步运行的代码实现:

st_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		st_1.setWordWrap(true);//自动换行
		st_1.setText("");//每次运行先清空
		if(Line_Step+Count!=0)
		{
			st.setLineBackground(Line_Step+Count-1, 1, SWTResourceManager.getColor(SWT.COLOR_WHITE));
		}
		st.setLineBackground(Line_Step+Count, 1, SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA));
		try {
			try {
				File filename = new File("C:\\Users\\ZYF\\Desktop\\cache_step.lua");
				if(!filename.exists()) {
					filename.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
					}
				FileWriter writer = new FileWriter(filename);
				BufferedWriter out = new BufferedWriter(writer);
				String hook="function locals()\r\n"
						+ "  local variables = {}\r\n"
						+ "  local idx = 1\r\n"
						+ "  while true do\r\n"
						+ "    local ln, lv = debug.getlocal(2, idx)\r\n"
						+ "    if ln ~= nil then\r\n"
						+ "      variables[ln] = lv\r\n"
						+ "    else\r\n"
						+ "      break\r\n"
						+ "    end\r\n"
						+ "    idx = 1 + idx\r\n"
						+ "  end\r\n"
						+ "  return variables\r\n"
						+ "end\r\n";
				out.write(hook);
				int i=0;
				while(i<=Line_Step+Count) {
					if(i==Line_Step+Count) {
						out.write(st.getLine(i)+"\r\n");
						out.write("for k,v in pairs(locals()) do\r\n"
								+ "    print(\"局部变量\", k, \"值\", v)\r\n"
								+ "end");
						
						break;
					}
					cmd=st.getLine(i)+"\r\n";
					out.write(cmd);
					i++;
				}
				//System.out.println("单步运行断点行"+" "+Line_Step);
				out.flush(); // 把缓存区内容压入文件
				out.close();
				} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Process exec = Runtime.getRuntime().exec("cmd /c cd "+UI.parent_path+"&&lua cache_step.lua");
			//Process exec = Runtime.getRuntime().exec(cmd+" print(debug.get info(1))");
			exec.waitFor();
	        final int exitValue = exec.waitFor();
	        if(exitValue==0) {
	        	InputStream input = exec.getInputStream(); //获取cmd段的返回结果
	        	BufferedReader reader = new BufferedReader(new InputStreamReader(input));
	        	String szline=null;
				while((szline= reader.readLine()) != null) {
					szline=st_1.getText()+szline+"\n";
					st_1.setText(szline);
				}
				reader.close(); 
	        }
	        else {
	        	InputStream errinput=exec.getErrorStream();//获取错误消息
	        	BufferedReader errinreader = new BufferedReader(new InputStreamReader(errinput));
	        	String szline=null;
	        	st_1.setText("error--> ");
				while((szline= errinreader.readLine()) != null) {
					szline=st_1.getText()+szline+"\n";
					st_1.setText(szline);
				}
				errinreader.close(); 
	        } 
	        exec.waitFor();
			exec.destroy(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-gene rated catch block
			e.printStackTrace();
		} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值