Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev091 增加自載機制 #158

Closed
wants to merge 5 commits into from
Closed

Dev091 增加自載機制 #158

wants to merge 5 commits into from

Conversation

shewer
Copy link
Contributor

@shewer shewer commented Feb 6, 2022

lua script
<userdata_dir>/lua/date.lua

.custom.yaml
patch:
engine/translators/lua_translator@date

[date]|
page: 1$ (of size 9)
1. [2022年02月06日]日期
2.  2022-02-06 日期
3.  date  n. 日期, 約會, 棗椰樹\n vt. 約會, 定日期\n vi. 註明日期, 過時

LOG(WARNING)
W20220206 14:14:09.254920 286379 lua_gears.cc:38] Lua Component of initialize warning:(  module: date name_space: date ): module can't found

@hchunhui
Copy link
Owner

hchunhui commented Feb 6, 2022

假若想加载的是 sample/lua/charset.lua 这样的怎么办?

@shewer
Copy link
Contributor Author

shewer commented Feb 6, 2022

<userdata_dir>/lua/sample/lua/charset.lua
只好在 rime.lua 中掛上 package.path

slash_char= package.config:sub(1,1)

package.path =package.path .. ("./lua/sample/lua/?.lua" ):gsub("/",slash_char) ) 

@hchunhui
Copy link
Owner

hchunhui commented Feb 6, 2022

不是指 path 的问题。是指 charset.lua 里面有若干个 component,如何自载

@shewer
Copy link
Contributor Author

shewer commented Feb 6, 2022

這是兩個 lua_filter ,在 lua_filter@xxxx@xxxx raw_init() 載入兩個
module 也有點不合理,不然在模組內在主模組載入時
把副模組 放入 _G

-- charset.lua
...
...
_G["comment_filter"] = _G["comment_filter"] or comment_filter
return filter

.custom.yaml

patch:
    engine/filters/+:
         - lua_filter@cherset
         - lua_filter@comment_filter

@hchunhui
Copy link
Owner

hchunhui commented Feb 6, 2022

不太能接受这种取巧的方法,因此目前看来自载功能只能适用于一些特殊情况(require 恰好返回一个 lua component)。

如果我们还是想要这个功能,不如与 lua_filter@xxx@yyy 等分开,另加一类 autoload_lua_filter@xxx@yyy 等。前者不自载,只有写作后者才自载。并且自载需满足一些约定(如require返回需要什么格式)。

还有一个问题:自载是否修改全局空间?如修改它与 rime.lua 全局变量的修改的先后顺序是什么?

@shewer
Copy link
Contributor Author

shewer commented Feb 6, 2022

|不太能接受这种取巧的方法,因此目前看来自载功能只能适用于一些特殊情况(require 恰好返回一个 lua component)。

不大明白 你的意思, 我是按 librime-lua架構
每個 lua_component 的module 都必須掛在全域變數中,僅管變數名不一樣 require 一樣的 module , 指的module都是一樣的
module的內變數 和 全域變數的使用,都在user 且要小心使用

自載只有一個目的 ,把 module 掛上 _G
掛上後就放著不用動 (等於 註冊 了lua_filter@xxx component )
所有 方案都可用 ,利用 config + name_space +env 設定差異化

狀況一 下面 m1 和 m2 指向同一個 module1 內的 functions(init, func,fini)或 func 如同 alias 意義不大
m1= require'module1'
m2 = require 'module1'

狀況二 下面不應該發生,應該避免, 掛上_G的module 不可再覆蓋可用 m3= m3 or require(m3) m3=m3 or require(m4) 來避免
已掛上的覆蓋,以先來後到原則

m3= require 'module3'
m3= require 'module4'

以我的反查filter 用的是 mfilter 差異化的變數 來自 name_space/....., 和 env
lua_filter@mfilter@tr1
lua_filter@mfilter@tr2
lua_filter@mfilter@tr3

| 还有一个问题:自载是否修改全局空间?如修改它与 rime.lua 全局变量的修改的先后顺序是什么?
以模組 而言 先來後到 , 誰先掛上_G 誰優先

@hchunhui
Copy link
Owner

hchunhui commented Feb 6, 2022

|不太能接受这种取巧的方法,因此目前看来自载功能只能适用于一些特殊情况(require 恰好返回一个 lua component)。

不大明白 你的意思, 我是按 librime-lua架構 每個 lua_component 的module 都必須掛在全域變數中,僅管變數名不一樣 require 一樣的 module , 指的module都是一樣的 module的內變數 和 全域變數的使用,都在user 且要小心使用

是我没表达清楚,对自载我总体上没意见,只是不能接受如下 “主副模組” 的自载方法:

這是兩個 lua_filter ,在 lua_filter@xxxx@xxxx raw_init() 載入兩個 module 也有點不合理,不然在模組內在主模組載入時 把副模組 放入 _G

patch:
    engine/filters/+:
         - lua_filter@cherset
         - lua_filter@comment_filter

我希望能保留不自载的行为,用于手工处理像 charset 这种特别情况。所以提议自载用 autoload_lua_filter 这样的形式。

| 还有一个问题:自载是否修改全局空间?如修改它与 rime.lua 全局变量的修改的先后顺序是什么? 以模組 而言 先來後到 , 誰先掛上_G 誰優先

如果在 rime.lua 定义了与模组相同的变量,那么最终被使用的是哪一个?

@shewer
Copy link
Contributor Author

shewer commented Feb 6, 2022

| 如果在 rime.lua 定义了与模组相同的变量,那么最终被使用的是哪一个?
rime.lua 的那個 (因爲不是空值)

raw_init 中 只有 在 rime.lua 沒有載入時,如果rime.lua定義了
_G[date] = require(date)
lua_type(L, -1) 不是 nil 進入正常程序

  lua_getglobal(L, t.klass.c_str());
  if (lua_type(L, -1) == LUA_TNIL){
    /*
      if _G[klass] == nil then 
           
          _G[klass] = require (klass)
         lua_getglobal(L, t.klass.c_str());  // 再次放入 stack
      end
   */
//---- 回到正常程序 -----
//....

@shewer
Copy link
Contributor Author

shewer commented Feb 11, 2022

試用幾天 ,感覺良好 。在.custom.yaml 加上 component
直接開檔寫 code

@shewer
Copy link
Contributor Author

shewer commented Feb 19, 2022

不是指 path 的问题。是指 charset.lua 里面有若干个 component,如何自载

即使有多個 component user 還是要在rime.lua 調用到 _G 程序
且此 charset.lua 的return value 井不符合 lua_component initialize 程序,create lua_component 會失敗

-- 详见 `lua/charset.lua`

以上例製作多模組必要patch 兩個檔 rime.lua & custom.yaml
才能運作
如果其他user誤用發生時機是在 custom.yaml 用上lua_component@charset ,且沒有在rime.lua 設定_G["charset"]時, 但是最終是失敗的因爲 不符 component initialize 需求( func or [func , init , fini]

我覺得 auto_lua_filter 也是只能載入一個自身一個 在engine.cc裡
生成 processors segments translators filters 時,是無法在當下再插入 rime/librime#513
如果要在filters 動態插入必須在開始create filters時把 config_item("engine/filters") 處理好.
而 auto_lua_filter 如果使用多 filter 模組也是無法增加 filters 內的數量

我在自己的專案中 是利用 lua_processor@init_proc 在processor init 時
動態調整 engine/segments engine/translators engine/filters
在需要 lua_processor時 把 lua_processor 做成 lua_processor@init_proc
下的次模組 init_proc的 init func fini 都會將 args 委派給 次模組

@shewer
Copy link
Contributor Author

shewer commented Sep 10, 2022

不是指 path 的问题。
是指 charset.lua 里面有若干个 component,如何自载

the other way , can you accept that?

--charset.lua
return { filter = charset_filter,
	 comment_filter = charset_comment_filter }

change to
lua/charset/filter.lua
lua/charset/comment_filter.lua

yaml
[email protected] -- auto require 'charset.filter'
[email protected]_filter -- auto require 'charset.comment_filter'

@hchunhui
Copy link
Owner

重新回顾了一下。还是希望能在保留原来不自动加载的前提下,扩展自动加载。

比如之前提议的 autoload_lua_filter@filter,或者是加一个标记如 lua_filter@+filter lua_filter@*filter 之类的。只有新的配置方法才会自动加载,现有的配置不会尝试auto require。并且若在自动模式下auto require失败,应该直接报错而不是忽略。

分开两种模式(自动与非自动),行为上更加清楚和确定。比如不会因为 rime.lua 中的变量与文件名意外重名,导致出乎意料的问题。

the other way , can you accept that?

--charset.lua
return { filter = charset_filter,
	 comment_filter = charset_comment_filter }

change to lua/charset/filter.lua lua/charset/comment_filter.lua

yaml [email protected] -- auto require 'charset.filter' [email protected]_filter -- auto require 'charset.comment_filter'

在新增的自动模式里面,接受。

@shewer
Copy link
Contributor Author

shewer commented Sep 11, 2022

重新回顾了一下。还是希望能在保留原来不自动加载的前提下,扩展自动加载。

比如之前提议的 autoload_lua_filter@filter,或者是加一个标记如 lua_filter@+filter lua_filter@*filter 之类的。只有新的配置方法才会自动加载,现有的配置不会尝试auto require。并且若在自动模式下auto require失败,应该直接报错而不是忽略。

auto_lua_xxx 應該是可以用在主動載入 file , 但是要放在 local table or function
只是想不到該如何 local module = require 'module'
限制在 lua_xxxx 的 獨立環境中
local _ENV 可能是個解決方案

  1. create local _ENV={}
  2. clone _G[] to _ENV[]
  3. module = require module

比如之前提议的 autoload_lua_filter@filter,或者是加一个标记如 lua_filter@+filter lua_filter@*filter 之类的。只有新的配置方法才会自动加载,现有的配置不会尝试auto require。并且若在自动模式下auto require失败,应该直接报错而不是忽略。

不管是auto_lua_xxx and lua_xxx, module name
如果都是從 _G[ ...] 取用
使用者在 module_name 重名還是會造成混亂的

分开两种模式(自动与非自动),行为上更加清楚和确定。比如不会因为 rime.lua 中的变量与文件名意外重名,导致出乎意料的问题。

自載井非主動 覆蓋載入 ,而是 engine 建構lua_xxx 時找不到 _G[module] 時 觸發救援機制 嘗試 require '' -> _G[module]

require 'charset.lua'
lua_filter@charset_filter
lua_filter@charset_comment_filter

user 在 engine/filters: 且未在rime.lua 淮備好

  • lua_filter@charset_filter
  • lua_filter@charset_comment_filter

此兩個會載入失敗

救援自載機制也會失敗,
require 'charset_filter' -- charset_filter.lua
require 'charset_comment_filter' -- charset_comment_filter.lua

@shewer
Copy link
Contributor Author

shewer commented Sep 11, 2022

現在在lua_xxxx 找不到 _G[module] : type table or function
在 log.error 時 , init func runtime error
掛個 handle 如何

目前我使用 lua script 也井非主動自載 , 而是在 lua_processor init 時 取得 engine.schema.config 時檢查以下 剩餘 lua_xxxx@module_name@xxx, _G[module_name] 是否空值 , 嘗試用 module_name 載入( require , check type , 掛入_G[module_name] )
也是在 rime.lua 前置作業後

@hchunhui
Copy link
Owner

已由 #189 实现。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants