擴充 API

lua-users home
wiki

Lua 的核心是基於 C 語言以及其標準函式庫,因此,就像 C 一樣,它無法存取現代作業系統中存在的許多功能。目前有(以及曾經有過)許多嘗試,想要製作一個 Lua API 來存取 C 中不存在的標準作業系統功能。本頁面旨在引發關於哪些此類功能應被視為「標準」以及此類 Lua API 應該是什麼樣貌的討論。

儘管完整的 POSIX 繫結確實能讓 Lua 程式撰寫成可以在許多平台上執行,但另一個重要的平台(Windows)將會排除在外。嘗試在 Windows 上嫁接類 POSIX API 不容易,也不乾淨。

以下是已知的函式庫、API 和系統的簡要清單和說明

此 API 意圖在現今廣泛使用的作業系統(Windows、MacOSX 和 POSIX 平台)上的獨立 Lua 程式提供更完整的程式設計環境。

一種大致上直截了當的 POSIX 繫結。

LuaFileSystem 提供一種可攜式的方式,用來存取底層目錄結構和檔案屬性。

檔案輸入/輸出和檔案系統;序列通訊;通訊端;事件通知機制(最近新增 IOCP);win32 相關:登錄檔、事件、服務等等。

--

以下是 ExtensionProposal API 和 [lposix] 以及 [LuaFileSystem] 的比較。

比較

環境

-- get environment variable
os.getenv           posix.getenv

-- set/unset environment variable
os.setenv           posix.putenv
                    posix.setenv
                    posix.unsetenv

-- sleep (pause without consuming CPU)
os.sleep            posix.sleep
--[[
    os.sleep specifies that the implementation provide sub-second
    resolution if available.  posix.sleep provides only second resolution.
--]]

-- system information
                    posix.ctermid
                    posix.errno
                    posix.pathconf
                    posix.sysconf
                    posix.ttyname
                    posix.uname

目錄

-- get/set current directory
os.currentdir       posix.getcwd        lfs.currentdir
os.chdir            posix.chdir         lfs.chdir

-- create/delete directories
os.mkdir            posix.mkdir         lfs.mkdir
os.remove           posix.rmdir         lfs.rmdir
--[[
    In both "ex" and POSIX systems, os.remove() will remove any directory
    entry: both files (non-directories) and empty subdirectories.
    The Lua 5.1 reference says that the standard os.remove() function will
    remove a directory, but in fact the Microsoft MSVCRT implementation
    of the C remove() function (on which os.remove() is based) will not
    remove directories.
--]]

-- POSIX directory routines
                    posix.link
                    posix.unlink
                    posix.mkfifo
                    posix.readlink
                    posix.symlink

目錄項目

-- list directory
os.dir              posix.files         lfs.dir
                    posix.dir

-- get file attributes
os.dirent           posix.stat          lfs.attributes
--[[
    The "ex" os.dir() iterator returns a table of directory entry
    attributes including the name, while the posix.files() and lfs.dir()
    iterators return entry names only.

    The best comparison of these three functions is via example:
--]]
  require"ex"
  for e in os.dir() do
    print(e.name, e.size)
  end

  require"lfs"
  for name in lfs.dir() do
    if name~="." and name~=".." then
      print(name, lfs.attributes(name).size)
    end
  end

  require"posix"
  for name in posix.files() do
    if name~="." and name~=".." then
      print(name, posix.stat(name, "size"))
    end
  end
--[[
    os.dir() elides any "." and ".." names while lfs.dir(),
    posix.dir() and posix.files() include them.
    
    posix.dir() is not an iterator; it returns a table with all entry names.
--]]

-- set file attributes
                    posix.utime         lfs.touch
                    posix.chmod
                    posix.chown
--[[
    The "ex" API says only that the os.dirent() table can be extended
    with OS-specific attribute values.  It would not be unreasonable
    to add file date information to the standard fields:
--]]
  local age = os.difftime(os.time(), e.modified)
  e.modified = os.time() -- touch a file

-- Check permissions
                    posix.access

檔案輸入/輸出

-- file locking
file:lock                               lfs.lock
file:unlock                             lfs.unlock

-- create anonymous pipe
io.pipe

流程控制

-- spawn process
os.spawn            posix.fork
proc:wait           posix.exec
                    posix.wait
--[[
    os.spawn supports redirection for standard I/O streams (in,
    out, err) while lposix does not provide full integration with
    POSIX-style descriptors and therefore does not provide a bind
    for the POSIX dup() interface.
--]]

-- signal a process
                    posix.kill

-- process information
                    posix.getprocessid
                    posix.getgroup
                    posix.getlogin
                    posix.getpasswd
                    posix.setgid
                    posix.setuid
                    posix.times
                    posix.umask

請注意,「ex」API(部分地)基於 LuaFileSystem,而且其佈署使用了其部分原始碼。

註解

ExtensionProposalStandardLibraries 以及 http://luaforge.net/projects/stdlib/ 相比如何?-- DavidManura

StandardLibraries 看起來像是用純粹 Lua 寫成的一組模組。截至目前為止,沒有任何模組提供 C 中不存在的功能,而是提供一組標準函式庫以利重複使用。也許擴充 API 的結果應該屬於 stdlib 的一部分。 -- MarkEdgar

如果您現在需要擴充 API,您可以透過 LunaticPython 來使用 Python 的擴充 API

> py = require "python"
> os = py.import("os")
> =os.listdir('.')
['lua.exe', 'lua51.dll', 'luac.exe']
--DavidManura

RecentChanges · 偏好設定
編輯 · 歷史紀錄
最後編輯於 2010 年 9 月 18 日 5:40 pm GMT (diff)