Lua is not in. Net (not many people seem to be studying it)

Source: Internet
Author: User

I haven't gotten into a blog for a long time. I have been busy with my work since I changed my job. I also think that my personal abilities are relatively limited. But what I can't help but is that many things have oppressed myself on the technical level, the whole thing that is never touched or unfamiliar at all ,.

However, I can still learn a lot about Lua. I should say that I only learned about Lua from the new magic game. At that time, I thought it was a kind of script invented by Kim cool. It was really cool! Not because of technical considerations, I was too lazy to study it. It was originally invented by a guy from Brazil. Originally, as a C extension, it finally became a cool game player, wow (I don't like it) allows players to customize their clients through Lua scripts, which is a strong choice.

Check on the Internet. Hey, there are a lot of LUA-related things, which are still very famous. At least many games prefer Lua as the script engine, lua is a lightweight Virtual Machine implemented by pure ansi c. It has a simple structure and is elegant! I don't care how beautiful it is, but now I really want it, mainly because the customer needs to be able to capture webpage data according to their requirements and form a table in the expected format, this can be difficult for me. I have not studied spider, but I understand it, but I certainly cannot find a suitable one on the Internet. At least not one of our customers has such a need, this is too troublesome for maintenance. I can fetch webpages, analyze webpage data, and capture specific parts for storage as required. However, there are a lot of customer crawling requirements, and if the website changes, you can re-modify the program, compile and release the program, and the workload can be imagined. What's important is that there are many customers across the country, and that maintenance is even more troublesome, and people will crash, so Lua entered my perspective, and I wanted to implement a simple, you can configure a capture template to capture webpage data (some of the page data here must have complicated extraction logic, and it is definitely not good to write it in a program), but think about it, I write the anti-compiler myself, but it is totally useless. This can still be implemented by the work, but is it suitable for this temporary patchwork, it is estimated that you want to ensure stable work for a short period of time, and you have to have strong functions, ah, depressed people.

So we searched Lua everywhere. NET application, but it is a pity that, in addition to Lua's own PDF programming guide. there is really little information about the rich Lua applications under. net. Even I did not find a few articles in my hope blog. I found a luainterface, this is indeed something for us. net has contributed to the Lua application, but what I said on the internet is that there is a multi-core bug that will crash. virtual machines, that guy is really not interesting enough. The connection to a modified version cannot be closed, I don't mention any modification to the problem. I think it's a big deal. I want the customer's machine to be a single-core CPU! Luainterface is not the Lua virtual machine itself. It is an interface encapsulation for Lua. You have a Lua-hosted C ++ compiled DLL, several C ++ runtime files must be copied together before running.

I did not directly use this interface. I ran to codeplex and found a luanet. It was good. I was very excited to look at the demo. net class, the method is registered to the Lua Virtual Machine to expand the Lua Virtual Machine API, this is not exactly what I want? A Virtual Machine engine, coupled with a bunch of particle interfaces for web page extraction and analysis, uses the Lua script to capture the module configuration, so that you can flexibly capture tasks for Web pages, even the program should not be followed. directly sending an XML capture template can cope with the changes, and it can be modified at will. There is also a simple smart form framework I have made. If I add it to the Lua engine, will it not increase in power,

However, when I was writing the script IDE (simple character _ ^), I found that the script syntax error was reported for the Chinese character string parameters on Dog Day, and I didn't know where to say the error, after all, I can analyze Chinese Web pages. This damn ANSI has not considered the feelings of the Chinese people. I have been depressed for two days, and Google and Baidu have all been biased due to Lua. there are not many net applications, and no one has raised this bug. I think, just read the C code of Lua and change it to pure C #. How good is this, I even thought about the name, it was sharplua ^ _ ^. However, due to time constraints, the company does not allow me to have enough time to rewrite a C # virtual machine, if you can solve this problem, you can't do it. You only need to debug luainterface and find it. net layer structure cannot find errors, after all, people report a script syntax error, that is to say, this execution has already entered the Lua virtual machine part of the code, that part is C ++, I am not familiar with C ++ (I think C is still a good one) and find the open-source luainterface. Download: The lua511 contains the C implementation code of Lua. The project has been completed for a long time. You need to adjust the compiler settings directly before you can perform the following operations, after checking several functions, I finally found that the code that can be processed in Chinese can be regarded as incorrect, if Lua is stored in C ++ For unmanaged compilation (this refers to lua5. 1. The original C open-source version of 4). After compilation, there was no problem at all, but this error was reported in. net using/CLR to compile the program. For example

Lua5.1.4 vs2005 C ++ compilation unmanaged

The running diagram is as follows:

No error at all, Khan

However, in. net, the script syntax error of a string that has not ended is reported,

If print ('jakskdjfjaf') is used; if it is not Chinese, there is no problem at all.

No matter the built-in API of The Lua VM called print has a problem, including the Chinese strings of all the variables you define or the parameters of the function, there is no doubt that a script error occurs,

Finally, we tracked the C ++ code to the following code in luaxlib. C:

Typedef struct loads {
Const char * s;
Size_t size;
} Loads;


Static const char * gets (lua_state * l, void * UD, size_t * size ){
Loads * ls = (loads *) UD; // here * UD is a memory address that Lua stores after lexical parsing as a string expression.
(Void) L;
If (LS-> size = 0) return NULL; // * What is the size? It is an int * pointer parameter that stores the length of the substring expression.
* Size = LS-> size; // compare and view the code kernel 5.1.4 here, but it can be in a non-hosted C ++, And the managed compilation won't work.
Ls-> size = 0;
Return LS-> S;
}

After tracking this, I repeatedly looked at it and finally found the problem, for example, declaring an expression MSG = 'loe'; this statement, Lua here

(Loads *) in the Structure Obtained by UD, the length is actually 9. Since the char of C is 8 bits, it is obvious that Chinese occupies 2 bytes, the actual number of bytes should be 11, which is a small problem. As a result, Lua syntax parses the expression based on the length of 9, and just obtains the word 'love', followed '; two characters are lost, and the result is a non-ending string expression after the syntax check. I think the (loads *) UD here does not correctly recognize the length of the string, I still despise Lua's author and write an Insecure code. It should be completed using the strlen function of the system. Hey hey, according to my own ideas, I modified this C code,

Static const char * gets (lua_state * l, void * UD, size_t * size ){
Loads * ls = (loads *) UD;
(Void) L;
If (LS-> size = 0)
{
Return NULL;
}
Else
{
Ls-> size = strlen (LS-> S); // modify the location
}
* Size = LS-> size;
Ls-> size = 0;
Return LS-> S;
}
Nnd: The problem has finally been solved. Finally, you can use Lua with peace of mind. I think Lua is really a good thing. If you need a script engine to embed it into your C # application, play Lua, it is indeed a good thing. An embedded script engine is open-source and free of charge.

This essay is about throwing stones, Lua's. net does not have many applications. Some people want to engage in sharplua's pure C # virtual machine, so I will enjoy the results ^ _ ^. Finally, some friends who know how to modify the locks on the luainterface multi-core CPU, you can leave a message for me (leave the modified local core code)

 

Http://www.webkey.cn/code/view.asp? Id = 7323

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.