summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-24 23:44:28 +0800
committerruki <[email protected]>2019-07-24 16:26:53 +0800
commit192a6448fbd67535dc97b8d308bef53ba61adee8 (patch)
tree6479aa3cc30d89b495304881dfb6af5fef04ac45
parent7a6febd132b8a00f7548eba2c41b464b8086033a (diff)
rename io.file and add io._filelock meta
-rw-r--r--core/src/xmake/machine.c117
-rw-r--r--xmake/core/base/io.lua11
-rw-r--r--xmake/core/sandbox/modules/io.lua12
3 files changed, 85 insertions, 55 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index ff0758882..b2aa162d3 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -243,6 +243,12 @@ static luaL_Reg const g_io_file_functions[] =
, { tb_null, tb_null }
};
+// the filelock functions
+static luaL_Reg const g_io_filelock_functions[] =
+{
+ { tb_null, tb_null }
+};
+
// the path functions
static luaL_Reg const g_path_functions[] =
{
@@ -510,45 +516,71 @@ static tb_void_t xm_machine_init_arch(xm_machine_t* machine)
#if defined(TB_CONFIG_OS_WINDOWS)
- // the GetNativeSystemInfo function type
- typedef void (WINAPI *GetNativeSystemInfo_t)(LPSYSTEM_INFO);
+ // the GetNativeSystemInfo function type
+ typedef void (WINAPI *GetNativeSystemInfo_t)(LPSYSTEM_INFO);
- // get system info
- SYSTEM_INFO systeminfo = {0};
- GetNativeSystemInfo_t pGetNativeSystemInfo = tb_null;
- tb_dynamic_ref_t kernel32 = tb_dynamic_init("kernel32.dll");
- if (kernel32) pGetNativeSystemInfo = (GetNativeSystemInfo_t)tb_dynamic_func(kernel32, "GetNativeSystemInfo");
- if (pGetNativeSystemInfo) pGetNativeSystemInfo(&systeminfo);
- else GetSystemInfo(&systeminfo);
+ // get system info
+ SYSTEM_INFO systeminfo = {0};
+ GetNativeSystemInfo_t pGetNativeSystemInfo = tb_null;
+ tb_dynamic_ref_t kernel32 = tb_dynamic_init("kernel32.dll");
+ if (kernel32) pGetNativeSystemInfo = (GetNativeSystemInfo_t)tb_dynamic_func(kernel32, "GetNativeSystemInfo");
+ if (pGetNativeSystemInfo) pGetNativeSystemInfo(&systeminfo);
+ else GetSystemInfo(&systeminfo);
- // init architecture
- switch (systeminfo.wProcessorArchitecture)
- {
- case PROCESSOR_ARCHITECTURE_AMD64:
- lua_pushstring(machine->lua, "x64");
- break;
- case PROCESSOR_ARCHITECTURE_ARM:
- lua_pushstring(machine->lua, "arm");
- break;
- case PROCESSOR_ARCHITECTURE_INTEL:
- lua_pushstring(machine->lua, "x86");
- break;
- default:
+ // init architecture
+ switch (systeminfo.wProcessorArchitecture)
+ {
+ case PROCESSOR_ARCHITECTURE_AMD64:
+ lua_pushstring(machine->lua, "x64");
+ break;
+ case PROCESSOR_ARCHITECTURE_ARM:
+ lua_pushstring(machine->lua, "arm");
+ break;
+ case PROCESSOR_ARCHITECTURE_INTEL:
+ lua_pushstring(machine->lua, "x86");
+ break;
+ default:
# ifdef TB_ARCH_x64
- lua_pushstring(machine->lua, "x64");
+ lua_pushstring(machine->lua, "x64");
# else
- lua_pushstring(machine->lua, "x86");
+ lua_pushstring(machine->lua, "x86");
# endif
- break;
- }
+ break;
+ }
#elif defined(TB_ARCH_x64)
- lua_pushstring(machine->lua, "x86_64");
+ lua_pushstring(machine->lua, "x86_64");
#elif defined(TB_ARCH_x86)
- lua_pushstring(machine->lua, "i386");
+ lua_pushstring(machine->lua, "i386");
#else
- lua_pushstring(machine->lua, TB_ARCH_STRING);
+ lua_pushstring(machine->lua, TB_ARCH_STRING);
#endif
- lua_setglobal(machine->lua, "_ARCH");
+ lua_setglobal(machine->lua, "_ARCH");
+}
+static tb_void_t xm_machine_register_metatable(xm_machine_t* machine, tb_char_t const* module, tb_char_t const* metaname, tb_char_t const* metatype, luaL_Reg const* funcs)
+{
+ // check
+ tb_assert_and_check_return(machine && machine->lua);
+
+ /* register metatable and functions
+ *
+ * metatype module.metaname = metatable {__index = metatable, funcs ...}
+ *
+ * e.g. XM_IO_FILE* io.file = metatable {__index = metatable, funcs ...}
+ */
+ luaL_newmetatable(machine->lua, metatype);
+ // stack: {metatable}, {metatable}
+ lua_pushvalue(machine->lua, -1);
+ // stack: {metatable, __index = {metatable}}
+ lua_setfield(machine->lua, -2, "__index");
+ // stack: {metatable}, {io}
+ lua_getglobal(machine->lua, module);
+ // stack: {metatable}, {io}, {metatable}
+ lua_pushvalue(machine->lua, -2);
+ // stack: {metatable}, {io, file = {metatable}}
+ lua_setfield(machine->lua, -2, metaname);
+ // stack: {metatable}
+ lua_pop(machine->lua, 1);
+ luaL_register(machine->lua, tb_null, funcs);
}
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -578,24 +610,13 @@ xm_machine_ref_t xm_machine_init()
// bind io functions
luaL_register(machine->lua, "io", g_io_functions);
- // bind file functions
- // stack: {metatable}
- luaL_newmetatable(machine->lua, "XM_IO_FILE*");
- // stack: {metatable}, {metatable}
- lua_pushvalue(machine->lua, -1);
- // stack: {metatable, __index = {metatable}}
- lua_setfield(machine->lua, -2, "__index");
- // stack: {metatable}, {io}
- lua_getglobal(machine->lua, "io");
- // stack: {metatable}, {io}, {metatable}
- lua_pushvalue(machine->lua, -2);
- // stack: {metatable}, {io, file = {metatable}}
- lua_setfield(machine->lua, -2, "file");
- // stack: {metatable}
- lua_pop(machine->lua, 1);
- // stack:
- luaL_register(machine->lua, NULL, g_io_file_functions);
- // add stdin,stdout,stderr to io
+ // bind io.file (metatable) functions
+ xm_machine_register_metatable(machine, "io", "_file", "XM_IO_FILE*", g_io_file_functions);
+
+ // bind io.filelock (metatable) functions
+ xm_machine_register_metatable(machine, "io", "_filelock", "XM_IO_FILELOCK*", g_io_filelock_functions);
+
+ // add stdin, stdout, stderr to io
xm_io_std(machine->lua);
// bind path functions
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index e14cd32e9..4fde3b385 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -20,7 +20,7 @@
-- define module
local io = io or {}
-local _file = _file or io.file or {}
+local _file = _file or io._file or {}
-- load modules
local path = require("base/path")
@@ -142,6 +142,7 @@ end
-- write data to file
function io.writefile(filepath, data, opt)
+ -- init option
opt = opt or {}
-- open file
@@ -172,8 +173,9 @@ function io.open(filepath, mode, opt)
-- check
assert(filepath)
+ -- init option and mode
+ opt = opt or {}
mode = mode or "r"
- opt = opt or {}
-- open it
local handle = io._open(filepath, mode .. (opt.encoding or ""))
@@ -196,6 +198,7 @@ function io.save(filepath, object, opt)
-- check
assert(filepath and object)
+ -- init option
opt = opt or {}
-- ensure directory
@@ -232,6 +235,7 @@ function io.load(filepath, opt)
-- check
assert(filepath)
+ -- init option
opt = opt or {}
-- open the file
@@ -254,6 +258,7 @@ end
-- gsub the given file and return replaced data
function io.gsub(filepath, pattern, replace, opt)
+ -- init option
opt = opt or {}
-- read all data from file
@@ -282,6 +287,7 @@ end
-- cat the given file
function io.cat(filepath, linecount, opt)
+ -- init option
opt = opt or {}
-- open file
@@ -312,6 +318,7 @@ end
-- tail the given file
function io.tail(filepath, linecount, opt)
+ -- init option
opt = opt or {}
-- all?
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index 49cb1b509..7aead5368 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -26,9 +26,11 @@ local raise = require("sandbox/modules/raise")
local vformat = require("sandbox/modules/vformat")
-- define module
-local sandbox_io = sandbox_io or {}
-local sandbox_io_file = sandbox_io.file or {}
-sandbox_io.file = sandbox_io_file
+local sandbox_io = sandbox_io or {}
+local sandbox_io_file = sandbox_io._file or {}
+local sandbox_io_filelock = sandbox_io._filelock or {}
+sandbox_io._file = sandbox_io_file
+sandbox_io._filelock = sandbox_io_filelock
-- inherit some builtin interfaces
sandbox_io.lines = io.lines
@@ -38,7 +40,7 @@ sandbox_io.isatty = io.isatty
-- inherit matatable of file
if sandbox_io_file.__index ~= sandbox_io_file then
sandbox_io_file.__index = sandbox_io_file
- for k, v in pairs(io.file) do
+ for k, v in pairs(io._file) do
if type(v) == "function" then
sandbox_io_file[k] = function(s, ...)
local result, err = v(s._FILE, ...)
@@ -54,7 +56,7 @@ if sandbox_io_file.__index ~= sandbox_io_file then
end
end
-- file:lines does not use its second return value for error
- sandbox_io_file.lines = io.file.lines
+ sandbox_io_file.lines = io._file.lines
end
-- get file size