前面一片文章中lua出現的bug,其實是lua本身結構問題導致的:
lua中,數值使用double來存儲,包含整形和double。而解析出來的整形也是被強轉為double進行存儲,這樣就會出問題。
舉一個簡單的例子:
double f = (double)0xffffffff;
int a = int(f);
這里的文章說明這個類型轉換問題的緣由。
在Squirrel腳本中就不會有這個問題
local a = 0xffffffff
print( a )
結果為-1
查看其源代碼:
typedef union tagSQObjectValue
{
struct SQTable *pTable;
struct SQArray *pArray;
struct SQClosure *pClosure;
struct SQGenerator *pGenerator;
struct SQNativeClosure *pNativeClosure;
struct SQString *pString;
struct SQUserData *pUserData;
SQInteger nInteger;
SQFloat fFloat;
SQUserPointer pUserPointer;
struct SQFunctionProto *pFunctionProto;
struct SQRefCounted *pRefCounted;
struct SQDelegable *pDelegable;
struct SQVM *pThread;
struct SQClass *pClass;
struct SQInstance *pInstance;
struct SQWeakRef *pWeakRef;
SQRawObjectVal raw;
}SQObjectValue;
可以看到
SQInteger nInteger;
SQFloat fFloat;
是分開存儲的,因此就不會有這個問題
lua解決方法:
1. 將十六進制換為10進制存儲
2. 等待大俠或者官方修改代碼,做出patch