Skip to content

Commit d1d7f3e

Browse files
committed
refactoring
1 parent bfe188f commit d1d7f3e

File tree

3 files changed

+66
-63
lines changed

3 files changed

+66
-63
lines changed

nattlua/other/fs.lua

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,16 @@ if not jit then
55

66
elseif jit.arch ~= "Windows" then
77
local ffi = require("ffi")
8-
ffi.cdef("char *strerror(int);")
98

10-
function fs.last_error()
11-
local num = ffi.errno()
12-
local err = ffi.string(ffi.C.strerror(num))
13-
return err == "" and tostring(num) or err
14-
end
15-
else
16-
local ffi = require("ffi")
17-
ffi.cdef("uint32_t GetLastError();")
18-
ffi.cdef[[
19-
uint32_t FormatMessageA(
20-
uint32_t dwFlags,
21-
const void* lpSource,
22-
uint32_t dwMessageId,
23-
uint32_t dwLanguageId,
24-
char* lpBuffer,
25-
uint32_t nSize,
26-
va_list *Arguments
27-
);
28-
]]
29-
local error_str = ffi.new("uint8_t[?]", 1024)
30-
local FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
31-
local FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
32-
local error_flags = bit.bor(FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS)
33-
34-
function fs.last_error()
35-
local code = ffi.C.GetLastError()
36-
local numout = ffi.C.FormatMessageA(error_flags, nil, code, 0, error_str, 1023, nil)
37-
local err = numout ~= 0 and ffi.string(error_str, numout)
38-
39-
if err and err:sub(-2) == "\r\n" then return err:sub(0, -3) end
40-
41-
return err
42-
end
43-
end
44-
45-
if not jit then
9+
do
10+
ffi.cdef("char *strerror(int);")
4611

47-
elseif jit.arch ~= "Windows" then
48-
local ffi = require("ffi")
12+
function fs.last_error()
13+
local num = ffi.errno()
14+
local err = ffi.string(ffi.C.strerror(num))
15+
return err == "" and tostring(num) or err
16+
end
17+
end
4918

5019
do -- attributes
5120
local stat_struct
@@ -361,6 +330,37 @@ elseif jit.arch ~= "Windows" then
361330
end
362331
else
363332
local ffi = require("ffi")
333+
334+
do
335+
local ffi = require("ffi")
336+
ffi.cdef("uint32_t GetLastError();")
337+
ffi.cdef[[
338+
uint32_t FormatMessageA(
339+
uint32_t dwFlags,
340+
const void* lpSource,
341+
uint32_t dwMessageId,
342+
uint32_t dwLanguageId,
343+
char* lpBuffer,
344+
uint32_t nSize,
345+
va_list *Arguments
346+
);
347+
]]
348+
local error_str = ffi.new("uint8_t[?]", 1024)
349+
local FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
350+
local FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
351+
local error_flags = bit.bor(FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS)
352+
353+
function fs.last_error()
354+
local code = ffi.C.GetLastError()
355+
local numout = ffi.C.FormatMessageA(error_flags, nil, code, 0, error_str, 1023, nil)
356+
local err = numout ~= 0 and ffi.string(error_str, numout)
357+
358+
if err and err:sub(-2) == "\r\n" then return err:sub(0, -3) end
359+
360+
return err
361+
end
362+
end
363+
364364
local DIRECTORY = 0x10
365365
local time_struct = ffi.typeof([[
366366
struct {

nattlua/other/integer.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
local strip_integer = (
3+
not jit and
4+
(
5+
_VERSION == "Lua 5.1" or
6+
_VERSION == "Lua 5.2" or
7+
_VERSION == "Lua 5.3" or
8+
_VERSION == "Lua 5.4"
9+
)
10+
)--[[# as boolean]]
11+
12+
local function string_to_integer(str--[[#: string]])--[[#: number]]
13+
if strip_integer then
14+
str = str:lower():sub(-3)
15+
16+
if str == "ull" then
17+
str = str:sub(1, -4)
18+
elseif str:sub(-2) == "ll" then
19+
str = str:sub(1, -3)
20+
end
21+
end
22+
23+
return assert(loadstring("return " .. str))()
24+
end
25+
26+
return string_to_integer

nattlua/types/number.lua

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local type = _G.type
99
local error_messages = require("nattlua.error_messages")
1010
local bit = require("nattlua.other.bit")
1111
local loadstring = require("nattlua.other.loadstring")
12+
local string_to_integer = require("nattlua.other.integer")
1213
local jit = _G.jit
1314
local False = require("nattlua.types.symbol").False
1415
local META = require("nattlua.types.base")()
@@ -316,30 +317,6 @@ do
316317
end
317318
end
318319

319-
local strip_integer = (
320-
not jit and
321-
(
322-
_VERSION == "Lua 5.1" or
323-
_VERSION == "Lua 5.2" or
324-
_VERSION == "Lua 5.3" or
325-
_VERSION == "Lua 5.4"
326-
)
327-
)--[[# as boolean]]
328-
329-
local function string_to_integer(str--[[#: string]])--[[#: number]]
330-
if strip_integer then
331-
str = str:lower():sub(-3)
332-
333-
if str == "ull" then
334-
str = str:sub(1, -4)
335-
elseif str:sub(-2) == "ll" then
336-
str = str:sub(1, -3)
337-
end
338-
end
339-
340-
return assert(loadstring("return " .. str))()
341-
end
342-
343320
function META:IsNumeric()
344321
return true
345322
end

0 commit comments

Comments
 (0)