Skip to content

Commit 4cd4c29

Browse files
authored
fix: ffi calls for macos (#522)
1 parent f1939e8 commit 4cd4c29

File tree

3 files changed

+81
-40
lines changed

3 files changed

+81
-40
lines changed

lua/plenary/path.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,17 @@ local function shorten_len(filename, len, exclude)
431431
end
432432

433433
local shorten = (function()
434+
local fallback = function(filename)
435+
return shorten_len(filename, 1)
436+
end
437+
434438
if jit and path.sep ~= "\\" then
435439
local ffi = require "ffi"
436440
ffi.cdef [[
437441
typedef unsigned char char_u;
438442
void shorten_dir(char_u *str);
439443
]]
440-
return function(filename)
444+
local ffi_func = function(filename)
441445
if not filename or is_uri(filename) then
442446
return filename
443447
end
@@ -447,10 +451,14 @@ local shorten = (function()
447451
ffi.C.shorten_dir(c_str)
448452
return ffi.string(c_str)
449453
end
454+
local ok = pcall(ffi_func, "/tmp/path/file.lua")
455+
if ok then
456+
return ffi_func
457+
else
458+
return fallback
459+
end
450460
end
451-
return function(filename)
452-
return shorten_len(filename, 1)
453-
end
461+
return fallback
454462
end)()
455463

456464
function Path:shorten(len, exclude)

lua/plenary/scandir.lua

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,17 @@ local gen_date = (function()
319319
end)()
320320

321321
local get_username = (function()
322+
local fallback = function(tbl, id)
323+
if not tbl then
324+
return id
325+
end
326+
if tbl[id] then
327+
return tbl[id]
328+
end
329+
tbl[id] = tostring(id)
330+
return id
331+
end
332+
322333
if jit and os_sep ~= "\\" then
323334
local ffi = require "ffi"
324335
ffi.cdef [[
@@ -340,7 +351,7 @@ local get_username = (function()
340351
passwd *getpwuid(uid_t uid);
341352
]]
342353

343-
return function(tbl, id)
354+
local ffi_func = function(tbl, id)
344355
if tbl[id] then
345356
return tbl[id]
346357
end
@@ -354,21 +365,30 @@ local get_username = (function()
354365
tbl[id] = name
355366
return name
356367
end
357-
else
358-
return function(tbl, id)
359-
if not tbl then
360-
return id
361-
end
362-
if tbl[id] then
363-
return tbl[id]
364-
end
365-
tbl[id] = tostring(id)
366-
return id
368+
369+
local ok = pcall(ffi_func, {}, 1000)
370+
if ok then
371+
return ffi_func
372+
else
373+
return fallback
367374
end
375+
else
376+
return fallback
368377
end
369378
end)()
370379

371380
local get_groupname = (function()
381+
local fallback = function(tbl, id)
382+
if not tbl then
383+
return id
384+
end
385+
if tbl[id] then
386+
return tbl[id]
387+
end
388+
tbl[id] = tostring(id)
389+
return id
390+
end
391+
372392
if jit and os_sep ~= "\\" then
373393
local ffi = require "ffi"
374394
ffi.cdef [[
@@ -384,7 +404,7 @@ local get_groupname = (function()
384404
group *getgrgid(gid_t gid);
385405
]]
386406

387-
return function(tbl, id)
407+
local ffi_func = function(tbl, id)
388408
if tbl[id] then
389409
return tbl[id]
390410
end
@@ -398,17 +418,14 @@ local get_groupname = (function()
398418
tbl[id] = name
399419
return name
400420
end
401-
else
402-
return function(tbl, id)
403-
if not tbl then
404-
return id
405-
end
406-
if tbl[id] then
407-
return tbl[id]
408-
end
409-
tbl[id] = tostring(id)
410-
return id
421+
local ok = pcall(ffi_func, {}, 1000)
422+
if ok then
423+
return ffi_func
424+
else
425+
return fallback
411426
end
427+
else
428+
return fallback
412429
end
413430
end)()
414431

lua/plenary/strings.lua

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,48 @@ local path = require("plenary.path").path
33
local M = {}
44

55
M.strdisplaywidth = (function()
6+
local fallback = function(str, col)
7+
str = tostring(str)
8+
if vim.in_fast_event() then
9+
return #str - (col or 0)
10+
end
11+
return vim.fn.strdisplaywidth(str, col)
12+
end
13+
614
if jit and path.sep ~= [[\]] then
715
local ffi = require "ffi"
816
ffi.cdef [[
917
typedef unsigned char char_u;
1018
int linetabsize_col(int startcol, char_u *s);
1119
]]
1220

13-
return function(str, col)
21+
local ffi_func = function(str, col)
1422
str = tostring(str)
1523
local startcol = col or 0
1624
local s = ffi.new("char[?]", #str + 1)
1725
ffi.copy(s, str)
1826
return ffi.C.linetabsize_col(startcol, s) - startcol
1927
end
20-
else
21-
return function(str, col)
22-
str = tostring(str)
23-
if vim.in_fast_event() then
24-
return #str - (col or 0)
25-
end
26-
return vim.fn.strdisplaywidth(str, col)
28+
29+
local ok = pcall(ffi_func, "hello")
30+
if ok then
31+
return ffi_func
32+
else
33+
return fallback
2734
end
35+
else
36+
return fallback
2837
end
2938
end)()
3039

3140
M.strcharpart = (function()
41+
local fallback = function(str, nchar, charlen)
42+
if vim.in_fast_event() then
43+
return str:sub(nchar + 1, charlen)
44+
end
45+
return vim.fn.strcharpart(str, nchar, charlen)
46+
end
47+
3248
if jit and path.sep ~= [[\]] then
3349
local ffi = require "ffi"
3450
ffi.cdef [[
@@ -42,6 +58,11 @@ M.strcharpart = (function()
4258
return ffi.C.utf_ptr2len(c_str)
4359
end
4460

61+
local ok = pcall(utf_ptr2len, "🔭")
62+
if not ok then
63+
return fallback
64+
end
65+
4566
return function(str, nchar, charlen)
4667
local nbyte = 0
4768
if nchar > 0 then
@@ -83,12 +104,7 @@ M.strcharpart = (function()
83104
return str:sub(nbyte + 1, nbyte + len)
84105
end
85106
else
86-
return function(str, nchar, charlen)
87-
if vim.in_fast_event() then
88-
return str:sub(nchar + 1, charlen)
89-
end
90-
return vim.fn.strcharpart(str, nchar, charlen)
91-
end
107+
return fallback
92108
end
93109
end)()
94110

0 commit comments

Comments
 (0)