diff options
author | Sam Gross <sgross@fb.com> | 2015-08-12 21:49:06 +0300 |
---|---|---|
committer | Sam Gross <sgross@fb.com> | 2015-08-12 21:54:55 +0300 |
commit | 28bb3a85c295df72c619c9c99f6a3f3d99d33644 (patch) | |
tree | 6c9951f13dafa1663f1b10a8218e7dbe693a96b1 | |
parent | d21b760cf7d9cde8e9acef0bb9661a788eb23024 (diff) |
Allow FFI number types as sizes to ffi.string
For example:
ffi.string(buf, ffi.new('long', 10))
-rw-r--r-- | ffi.c | 6 | ||||
-rw-r--r-- | test.lua | 11 |
2 files changed, 16 insertions, 1 deletions
@@ -2631,7 +2631,11 @@ static int ffi_string(lua_State* L) } else if (ct.type == INT8_TYPE && ct.pointers == 1) { size_t sz; - if (!lua_isnil(L, 2)) { + if (lua_isuserdata(L, 2)) { + if (!cdata_tointeger(L, 2, &sz)) { + type_error(L, 2, "int", 0, NULL); + } + } else if (!lua_isnil(L, 2)) { sz = (size_t) luaL_checknumber(L, 2); } else if (ct.is_array && !ct.is_variable_array) { @@ -934,6 +934,17 @@ for i=1,3 do assert(structOfStructs.arrays[0].ints[i] == i) end +-- Test ffi.string +local buf = ffi.new('char[5]') +ffi.fill(buf, 4, 97) +buf[4] = 0 + +assert(ffi.string(buf) == 'aaaa') +assert(ffi.string(buf, 4) == 'aaaa') +assert(ffi.string(buf, 2) == 'aa') +assert(ffi.string(buf, 0) == '') +assert(ffi.string(buf, ffi.new('long long', 2)) == 'aa') +assert(ffi.string(buf, ffi.new('int', 2)) == 'aa') print('Test PASSED') |