robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
3.5 KiB133 linesraw
1---@diagnostic disable: no-unknown
2local mpack = vim.mpack
3
4local funcsfname = arg[1]
5local metadata_file = arg[2]
6local funcs_file = arg[3]
7local eval_file = arg[4]
8
9--Will generate funcs.generated.h with definition of functions static const array.
10
11local hashy = require 'gen.hashy'
12
13local hashpipe = assert(io.open(funcsfname, 'wb'))
14
15hashpipe:write([[
16#include "nvim/arglist.h"
17#include "nvim/channel.h"
18#include "nvim/cmdexpand.h"
19#include "nvim/cmdhist.h"
20#include "nvim/diff.h"
21#include "nvim/digraph.h"
22#include "nvim/eval.h"
23#include "nvim/eval/buffer.h"
24#include "nvim/eval/deprecated.h"
25#include "nvim/eval/fs.h"
26#include "nvim/eval/funcs.h"
27#include "nvim/eval/list.h"
28#include "nvim/eval/typval.h"
29#include "nvim/eval/vars.h"
30#include "nvim/eval/window.h"
31#include "nvim/ex_docmd.h"
32#include "nvim/ex_getln.h"
33#include "nvim/fold.h"
34#include "nvim/fuzzy.h"
35#include "nvim/indent.h"
36#include "nvim/indent_c.h"
37#include "nvim/input.h"
38#include "nvim/insexpand.h"
39#include "nvim/mapping.h"
40#include "nvim/match.h"
41#include "nvim/mbyte.h"
42#include "nvim/menu.h"
43#include "nvim/mouse.h"
44#include "nvim/move.h"
45#include "nvim/quickfix.h"
46#include "nvim/runtime.h"
47#include "nvim/search.h"
48#include "nvim/sign.h"
49#include "nvim/state.h"
50#include "nvim/strings.h"
51#include "nvim/testing.h"
52#include "nvim/undo.h"
53
54]])
55
56local funcs = loadfile(eval_file)().funcs
57for name, func in pairs(funcs) do
58  local n = (func.func_float and 1 or 0) + (func.func_lua and 1 or 0) + (func.func and 1 or 0)
59  assert(n <= 1, name .. ': only one of func, func_float, func_lua can be set')
60
61  if func.func_float then
62    func.func = 'float_op_wrapper'
63    func.data = '{ .func_float = &' .. func.func_float .. ' }'
64  elseif func.func_lua then
65    func.func = 'lua_wrapper'
66    func.data = '{ .func_lua = "' .. func.func_lua .. '" }'
67  end
68end
69
70local metadata = mpack.decode(io.open(metadata_file, 'rb'):read('*all'))
71for _, fun in ipairs(metadata) do
72  if fun.eval then
73    local args
74    if type(fun.opts_idx) == 'number' then
75      args = { fun.opts_idx - 1, #fun.parameters }
76    else
77      args = #fun.parameters
78    end
79    funcs[fun.name] = {
80      args = args,
81      func = 'api_wrapper',
82      data = '{ .func_api = &method_handlers[' .. fun.handler_id .. '] }',
83    }
84  end
85end
86
87local func_names = vim.tbl_filter(function(name)
88  return name:match('__%d*$') == nil
89end, vim.tbl_keys(funcs))
90
91table.sort(func_names)
92
93local funcsdata = assert(io.open(funcs_file, 'w'))
94funcsdata:write(mpack.encode(func_names))
95funcsdata:close()
96
97local neworder, hashfun = hashy.hashy_hash('find_internal_func', func_names, function(idx)
98  return 'functions[' .. idx .. '].name'
99end)
100
101hashpipe:write('static const EvalFuncDef functions[] = {\n')
102
103for _, name in ipairs(neworder) do
104  local def = funcs[name]
105  local min_args = 0
106  local max_args = 0 --- @type integer|string
107  local def_args = def.args
108  if type(def_args) == 'number' then
109    min_args, max_args = def_args, def_args
110  elseif type(def_args) == 'table' then
111    min_args = def_args[1] or 0
112    max_args = def_args[2] or 'MAX_FUNC_ARGS'
113  end
114  local base = def.base or 'BASE_NONE'
115  local func = def.func or ('f_' .. name)
116  local data = def.data or '{ .null = NULL }'
117  local fast = def.fast and 'true' or 'false'
118  hashpipe:write(
119    ('  { "%s", %s, %s, %s, %s, &%s, %s },\n'):format(
120      name,
121      min_args,
122      max_args,
123      base,
124      fast,
125      func,
126      data
127    )
128  )
129end
130hashpipe:write('  { NULL, 0, 0, BASE_NONE, false, NULL, { .null = NULL } },\n')
131hashpipe:write('};\n\n')
132hashpipe:write(hashfun)
133hashpipe:close()