Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/facebook/luaffifb.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Gross <sgross@fb.com>2015-08-12 21:49:06 +0300
committerSam Gross <sgross@fb.com>2015-08-12 21:54:55 +0300
commit28bb3a85c295df72c619c9c99f6a3f3d99d33644 (patch)
tree6c9951f13dafa1663f1b10a8218e7dbe693a96b1
parentd21b760cf7d9cde8e9acef0bb9661a788eb23024 (diff)
Allow FFI number types as sizes to ffi.string
For example: ffi.string(buf, ffi.new('long', 10))
-rw-r--r--ffi.c6
-rw-r--r--test.lua11
2 files changed, 16 insertions, 1 deletions
diff --git a/ffi.c b/ffi.c
index 15904b9..91f592a 100644
--- a/ffi.c
+++ b/ffi.c
@@ -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) {
diff --git a/test.lua b/test.lua
index f4246ad..c9e036f 100644
--- a/test.lua
+++ b/test.lua
@@ -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')