From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 4D6446EC5D; Wed, 7 Apr 2021 23:37:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4D6446EC5D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1617827841; bh=ukbKz5auzOzdIx68bYwiH91E1Hh25jCiyrjMPCD8wv8=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=dLhqJgr7lbBmsmwunfVAKWaXt7Jf9+uILVARsva8vUoGZ2dz07mFrdPJMMNCJF53T IMAtrA8z4Qol1sCeasx9bDkBYSxcao8XydqD1yYAcWSpxbBTa8sEf9rGbG+YpfRf9O 8sQkQwxIjPk6uVR8Jctxi1rLvRB3EW+AsNf65LJU= Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id B835C6EC5D for ; Wed, 7 Apr 2021 23:37:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B835C6EC5D Received: by mail-lf1-f49.google.com with SMTP id j18so224913lfg.5 for ; Wed, 07 Apr 2021 13:37:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Dtz465sMfc3U9GTOy54eGa0RELenPmgwFco8rEWFOyc=; b=bHrHKU7ZVg3tk3cdNglzl3Pwve/geaswt6RAN80Q7y/nXvEIi/vcwXI7WLjbc5bZ45 gXea7vu8jtKVxJtFmTHkWTwUb9mJQYoHnLw2dtCK/yTChU4Fw0A7kB77wvmoYKl2Trsy QLkW3UGlvUYTA5Af/Fpk4HnfLMV5sIpu+M2ZNXzcFj1vov/vOqQzR6sKcLWMGgymFZkH Bb8Br4lwPsF0hza+lMqse1wIlOw1V712QK6X5OQKmZope/+2GcOraXTvyZjraPZbM72Q DycvoNai0S5AE1uXT8f3S24zNLz1uDCTDbPpvZNL545x5MeTSEYDrHobFlbpnTd9O/6I v5+w== X-Gm-Message-State: AOAM5318ixD253jXuNf56FTxToJaIrd7qy6b9oK+jkBxFGq+51fxlxwb GpZ/s/lgs/oS3shFIqQZq3HPq+vaLsE= X-Google-Smtp-Source: ABdhPJzTJvd62mztD/zwR9zdwUPLeoz1gbXO7RUKsmjS+TFbdeAPUhTw+WwcQOqO+zoTuuz4MOXgGw== X-Received: by 2002:a05:6512:2089:: with SMTP id t9mr3603971lfr.118.1617827837523; Wed, 07 Apr 2021 13:37:17 -0700 (PDT) Received: from grain.localdomain ([5.18.199.94]) by smtp.gmail.com with ESMTPSA id x19sm2593784lfr.198.2021.04.07.13.37.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Apr 2021 13:37:16 -0700 (PDT) Received: by grain.localdomain (Postfix, from userid 1000) id B561F560152; Wed, 7 Apr 2021 23:37:15 +0300 (MSK) Date: Wed, 7 Apr 2021 23:37:15 +0300 To: Vladislav Shpilevoy Message-ID: References: <20210402123420.885834-1-gorcunov@gmail.com> <20210402123420.885834-7-gorcunov@gmail.com> <8d3a4d20-2887-7423-777f-2af808b65495@tarantool.org> <02f89e4e-70ff-215f-7cc4-3331adbb3c94@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <02f89e4e-70ff-215f-7cc4-3331adbb3c94@tarantool.org> User-Agent: Mutt/2.0.5 (2021-01-21) Subject: Re: [Tarantool-patches] [PATCH v20 6/7] box: implement box.lib module X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Cyrill Gorcunov via Tarantool-patches Reply-To: Cyrill Gorcunov Cc: tml Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" On Wed, Apr 07, 2021 at 10:28:09PM +0200, Vladislav Shpilevoy wrote: > >> > >> Actually this snippet comes from popen code where > >> we have wrappers on lua level and test for metamethods. > >> While it doesn't hurt it makes no much sense in current > >> code, thanks! Will drop. > > > > Heh, in real we need these to handle metamethods such as > > "module:load". I added a comment into the code. > > For methods you need to use the metatype. It is defined as > luaL_register_type(), where you can add methods, not just > __index. The latter is more for, well, indexing. Getting an > attribute, for example. See an example in lua/popen.c. > > Methods should not end up in __index. Wait. Here is what I have now void box_lua_lib_init(struct lua_State *L) { func_hash = mh_strnptr_new(); if (func_hash == NULL) panic("box.lib: Can't allocate func hash table"); static const struct luaL_Reg top_methods[] = { { "load", lbox_module_load }, { NULL, NULL }, }; luaL_register(L, "box.lib", top_methods); lua_pop(L, 1); static const struct luaL_Reg lbox_module_methods[] = { { "unload", lbox_module_unload }, { "load", lbox_module_load_func }, { "__index", lbox_module_index }, { "__serialize", lbox_module_serialize }, { "__gc", lbox_module_gc }, { NULL, NULL }, }; luaL_register_type(L, uname_lib, lbox_module_methods); ... } If I don't lookup for key being some nonnil value in __index (ie those 5 lines) then module:load simply stop working. As far as I understand the __index is called when Lua looks up for a key in table. It sends "load" there and since it is in metatable it executes lbox_module_load_func as it should. What I'm missing?