COCOS2DX using Tolua as a problem with string handling

Source: Internet
Author: User

An issue was found in this process using the COCOS2DX Tolua binding. Suppose that one back or input is std::string when we disagree with the inclusion of binary data, and std::string I agree, this leads to incorrect use etc. This can lead to a series of problems, especially when we need to use std::string information.

The solution to the problem is that the code generated by Tolua does not add a length parameter to std::string, which causes the binary data to be truncated once it is encountered.

The change approach is actually very easy, just need to change the Basic.lua script, add such as the following code can be


Index fac0c7a: 4c184c7 100644
---A/projects/lastmile/classes/lua/basic.lua
+ + B/projects/lastmile/classes/lua/basic.lua
@@ -307,7 +307,16 @@ -307,7 "C" {

using namespace cocos2d;
using namespace Cocos2d::extension;
-using namespace Cocosdenshion;])
+using namespace Cocosdenshion;
+
+static std::string bt_tocppstring (lua_state* tolua_s, int narg, const char* def) {
+ if (lua_gettop (tolua_s) <abs (Narg)) {
+ Return "";
+       }
+ size_t length = 0;
+ Const CHAR* data = lua_tolstring (tolua_s, Narg, &length);
+ Return std::string (data, length);
+}]])

replace ([[/* Exported function */
Tolua_api int Tolua_cocos2d_open (lua_state* tolua_s);] [[[]]]
@@ -323,6 +332,10 @@ -323,6 int tolua_cocos2d_open (lua_state* tolua_s);] [[[]]]

Replace ([[Tolua_usertype (tolua_s, "lua_function");]], [[]])

+ Replace ([[[Tolua_tocppstring]], [[bt_tocppstring]])
+
+ Replace ([[Tolua_pushcppstring (tolua_s, (const char*) tolua_ret)], [[Lua_pushlstring (tolua_s, Tolua_ret.c_str (), Tolua_ret.size ());])
+
Replace ([[Toluafix_pushusertype_ccobject (tolua_s, (void*) Tolua_ret]],
[Int nID = (tolua_ret)?

(int) Tolua_ret->m_uid:-1;
int* Pluaid = (tolua_ret)? &tolua_ret->m_nLuaID:NULL;


In fact it will be with the internal cppstring about what changes push and ToString

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

COCOS2DX using Tolua as a problem with string handling

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.