hchunhui / librime-lua

Extending RIME with Lua scripts
BSD 3-Clause "New" or "Revised" License
303 stars 43 forks source link

请问能否添加 接口 方便判断 librime-lua 版本 #207

Open LawssssCat opened 1 year ago

LawssssCat commented 1 year ago

想判断 librime-lua 版本,看是否支持某接口,做一些异常捕获。

现有方案好像是通过判断是否有 某方法 来判断版本:

https://github.com/shewer/librime-lua-script/blob/5e55b8a386f355282e8a4be16a0b06d8e1423800/lua/tools/rime_api.lua

local function Version()
  local ver
  if Opencc and Opencc('s2t.json').convert_word then ----------- 构造
    return 200
  elseif rime_api.regex_match then
    return 197
  elseif rime_api.get_distribution_name then
    return 185
  elseif LevelDb then
    return 177
  elseif Opencc then
    return 147
  elseif KeySequence and KeySequence().repr then --------- 构造
    return 139
  elseif  ConfigMap and ConfigMap().keys then ----------------- 构造
    return 127
  elseif Projection then
    return 102
  elseif KeyEvent then
    return 100
  elseif Memory then
    return 80
  elseif rime_api.get_user_data_dir then
    return 9
  elseif log then
    return 9
  else
    return 0
  end
end

但是这样的话,判断 对象属性 时就必须要把对象构造出来

可是有的对象是没法构造出来的,或者构造出来非常耗资源 比方说 memory

所以否能 『在构造前』 判断对象是否有某属性?

LawssssCat commented 1 year ago

比如添加一个方法返回注册信息?

{
  Candidate: {
    methods: [
      "get_dynamic_type",
      "get_genuine",
      "get_genuines",
      "to_shadow_candidate",
      "to_uniquified_candidate",
      "append",
    ]
    vars_get: [
      "type",
      "start",
      "_start",
      "_end",
      "quality",
      "text",
      "comment",
      "preedit",
    ]
    ss: [
      "type",
      "start",
      "_start",
      "_end",
      "quality",
      "text",
      "comment",
      "preedit",
    ]
  },
  Translation: {
    ...
  }
}
}
LawssssCat commented 1 year ago

或者 直接返回一个随 代码 更新的 版本号?

rime_api:get_librime_lua_version() -- 18
shewer commented 1 year ago

debug.getregistry() -- return hash table { 7LuaTypeIPKN4rime9CandidateEE = userdata .., .....} 每個 Reg 都會註冊各型別 Candidate *Candidate .... userdada -- metatable 就是 註冊的 vars_set vars_get var_method


for k,v in next, debug.getregistry() do 
    if type(k) == 'string' and k:match('ConfigMap') then
       print(k,v)
    end
end
--[[
7LuaTypeISt10shared_ptrIN4rime9ConfigMapEEE     table: 0x55ce4cc3b200
7LuaTypeIRN4rime9ConfigMapEE    table: 0x55ce4cc3a800
7LuaTypeIKN4rime9ConfigMapEE    table: 0x55ce4cc3ab50
7LuaTypeIRKN4rime9ConfigMapEE   table: 0x55ce4cc3aea0
7LuaTypeISt10shared_ptrIKN4rime9ConfigMapEEE    table: 0x55ce4cc3b560
7LuaTypeIN4rime9ConfigMapEE     table: 0x55ce4cc3a3f0
7LuaTypeIPKN4rime9ConfigMapEE   table: 0x55ce4cc3bc00
7LuaTypeIPN4rime9ConfigMapEE    table: 0x55ce4cc3b8b0
--]]
shewer commented 1 year ago

想判断 librime-lua 版本,看是否支持某接口,做一些异常捕获。

现有方案好像是通过判断是否有 某方法 来判断版本:

https://github.com/shewer/librime-lua-script/blob/5e55b8a386f355282e8a4be16a0b06d8e1423800/lua/tools/rime_api.lua

local function Version()
  local ver
  if Opencc and Opencc('s2t.json').convert_word then ----------- 构造
    return 200
  elseif rime_api.regex_match then
    return 197
  elseif rime_api.get_distribution_name then
    return 185
  elseif LevelDb then
    return 177
  elseif Opencc then
    return 147
  elseif KeySequence and KeySequence().repr then --------- 构造
    return 139
  elseif  ConfigMap and ConfigMap().keys then ----------------- 构造
    return 127
  elseif Projection then
    return 102
  elseif KeyEvent then
    return 100
  elseif Memory then
    return 80
  elseif rime_api.get_user_data_dir then
    return 9
  elseif log then
    return 9
  else
    return 0
  end
end

但是这样的话,判断 对象属性 时就必须要把对象构造出来

可是有的对象是没法构造出来的,或者构造出来非常耗资源 比方说 memory

所以否能 『在构造前』 判断对象是否有某属性?

麻煩且要用 debug.getregistry() ConfigMap , KeySequence 還好 Opencc 可能浪費

hchunhui commented 1 year ago

最简单是加一个版本号。返回属性列表需要找时间看一下好不好做。用 debug 接口判断不建议,因为得到的名字是随编译器改变的,是实现细节,不应该依赖它。