summaryrefslogtreecommitdiff
path: root/script/core/command/reloadFFIMeta.lua
blob: d00929adb87406007b435fc58110bc704ec82dba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local config      = require 'config'
local ws          = require 'workspace'
local fs          = require 'bee.filesystem'
local scope       = require 'workspace.scope'
local SDBMHash    = require 'SDBMHash'
local searchCode  = require 'plugins.ffi.searchCode'
local cdefRerence = require 'plugins.ffi.cdefRerence'
local ffi         = require 'plugins.ffi'

local function createDir(uri)
    local dir     = scope.getScope(uri).uri or 'default'
    local fileDir = fs.path(METAPATH) / ('%08x'):format(SDBMHash():hash(dir))
    if fs.exists(fileDir) then
        return fileDir, true
    end
    fs.create_directories(fileDir)
    return fileDir
end

---@async
return function (uri)
    if config.get(uri, 'Lua.runtime.version') ~= 'LuaJIT' then
        return
    end

    ws.awaitReady(uri)

    local fileDir, exists = createDir(uri)

    local refs = cdefRerence()
    if not refs or #refs == 0 then
        return
    end

    for i, v in ipairs(refs) do
        local target_uri = v.uri
        local codes = searchCode(refs, target_uri)
        if not codes then
            return
        end

        ffi.build_single(codes, fileDir, target_uri)
    end

    if not exists then
        local client = require 'client'
        client.setConfig {
            {
                key    = 'Lua.workspace.library',
                action = 'add',
                value  = tostring(fileDir),
                uri    = uri,
            }
        }
    end
end