<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 調用其他C函數動態庫 Linux下的動態庫一般都以 .so 結束命名,而Windows下一般都以 .dll 結束命名。Lua作為一種嵌入式語言,和C具有非常好的親緣性,這也是LUA賴以生存、發展的根本,所以Nginx+Lua=Openresty,魔法就這么神奇的發生了。 NgxLuaModule里面盡管提供了十分豐富的API,但他一定不可能滿足我們的形形色色的需求。我們總是要和各種組件、算法等形形色色的第三方庫進行協作。那么如何在Lua中加載動態加載第三方庫,就顯得非常有用。 扯一些額外話題,Lua解釋器目前有兩個最主流分支。 - Lua官方發布的標準版[Lua](http://lua.org/) - Google開發維護的[Luajit](http://luajit.org/index.html) Luajit中加入了Just In Time等編譯技術,是的Lua的解釋、執行效率有非常大的提升。除此以外,還提供了[FFI](http://luajit.org/ext_ffi.html)。 > 什么是FFI? ~~~ The FFI library allows calling external C functions and using C data structures from pure Lua code. ~~~ 通過FFI的方式加載其他C接口動態庫,這樣我們就可以有很多有意思的玩法。 當我們碰到CPU密集運算部分,我們可以把他用C的方式實現一個效率最高的版本,對外到處API,打包成動態庫,通過FFI來完成API調用。這樣我們就可以兼顧程序靈活、執行高效,大大彌補了Luajit自身的不足。 > 使用FFI判斷操作系統 ~~~ local ffi = require("ffi") if ffi.os == "Windows" then print("windows") elseif ffi.os == "OSX" then print("MAC OS X") else print(ffi.os) end ~~~ > 調用zlib壓縮庫 ~~~ local ffi = require("ffi") ffi.cdef[[ unsigned long compressBound(unsigned long sourceLen); int compress2(uint8_t *dest, unsigned long *destLen, const uint8_t *source, unsigned long sourceLen, int level); int uncompress(uint8_t *dest, unsigned long *destLen, const uint8_t *source, unsigned long sourceLen); ]] local zlib = ffi.load(ffi.os == "Windows" and "zlib1" or "z") local function compress(txt) local n = zlib.compressBound(#txt) local buf = ffi.new("uint8_t[?]", n) local buflen = ffi.new("unsigned long[1]", n) local res = zlib.compress2(buf, buflen, txt, #txt, 9) assert(res == 0) return ffi.string(buf, buflen[0]) end local function uncompress(comp, n) local buf = ffi.new("uint8_t[?]", n) local buflen = ffi.new("unsigned long[1]", n) local res = zlib.uncompress(buf, buflen, comp, #comp) assert(res == 0) return ffi.string(buf, buflen[0]) end -- Simple test code. local txt = string.rep("abcd", 1000) print("Uncompressed size: ", #txt) local c = compress(txt) print("Compressed size: ", #c) local txt2 = uncompress(c, #txt) assert(txt2 == txt) ~~~ > 自定義定義C類型的方法 ~~~ local ffi = require("ffi") ffi.cdef[[ typedef struct { double x, y; } point_t; ]] local point local mt = { __add = function(a, b) return point(a.x+b.x, a.y+b.y) end, __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end, __index = { area = function(a) return a.x*a.x + a.y*a.y end, }, } point = ffi.metatype("point_t", mt) local a = point(3, 4) print(a.x, a.y) --> 3 4 print(#a) --> 5 print(a:area()) --> 25 local b = a + point(0.5, 8) print(#b) --> 12.5 ~~~ > Lua和Luajit對比 可以這么說,Luajit應該是全面勝出,無論是功能、效率都是標準Lua不能比的。目前最新版Openresty默認也都使用Luajit。 世界為我所用,總是有驚喜等著你,如果那天你發現自己站在了頂峰,那我們就靜下心來改善一下頂峰,把他推到更高吧。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看