From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: What is the recommended way to find out the number of arguments passed to a module function? Date: Wed, 10 Jan 2024 21:15:36 +0200 Message-ID: <83jzohm23r.fsf@gnu.org> References: Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16764"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: dalanicolai Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jan 10 20:17:17 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rNe4j-00044R-KC for ged-emacs-devel@m.gmane-mx.org; Wed, 10 Jan 2024 20:17:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNe3z-0004Fn-UM; Wed, 10 Jan 2024 14:16:31 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rNe3y-0004EP-0Z for emacs-devel@gnu.org; Wed, 10 Jan 2024 14:16:31 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rNe3x-0008J0-Lm; Wed, 10 Jan 2024 14:16:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=D6syalaryP5YgMSebZEwalt5JOsLYJlTlPlVLNJhMT8=; b=cUy6fMl2YLJf SsLez3Gp7KLUF3kxzRNTLvj83gznvF3JSnLTdAnbrwsv3I3Mar/tiCI8byH+cILXezDrzgW0i4Xbo xB1Pos7At+rrhziGDRbjriqab9PSEiBDBFHP/7BrZMrr7NePQz8G6zlK3vAtIwQpsw023w//8JYHx mvuLeCuMoLnRhE+IV/aZQ9wwzA+hUJI91sXKfIW/T8etbJQbhQisT/F7lSHVUCW+N47K+iSEouRqx +AyZEe/ILcySAOoqDk0EMe6bFU7Fq/H8vRH8pDMyEspvqiIEudbqwg+PYhrQSznLBgnRvy50+CIhD DkTsDQHwHkZy9Mz/xw591g==; In-Reply-To: (message from dalanicolai on Wed, 10 Jan 2024 20:01:09 +0100) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:314821 Archived-At: > From: dalanicolai > Date: Wed, 10 Jan 2024 20:01:09 +0100 > > In the module API's 'make-function' we should pass a min-arity and a max-arity. > However, it is unclear to me what is the recommended way to check for the number of > arguments passed to some module function, as when not passing any argument, the 'optional' > argument does not seem > to be nil, or any emacs-value at all (I have tested if it might be a NULL pointer). I have tested it using a > 'test-module' with the following code: > > #include > int plugin_is_GPL_compatible; > static emacs_value > test (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data) > { > int integer = env->is_not_nil(env, args[0])? 1 : 0; > return env->make_integer(env, integer); > /* return args[0]; */ > } > int > emacs_module_init (struct emacs_runtime *runtime) > { > emacs_env *env = runtime->get_environment (runtime); > emacs_value func = env->make_function (env, 0, 1, test, NULL, NULL); > emacs_value symbol = env->intern (env, "test"); > emacs_value args[] = {symbol, func}; > env->funcall (env, env->intern (env, "defalias"), 2, args); > return 0; > } > > The 'test' function checks if the value of the argument is non-nil, and 'returns' a 1 if it is and a 0 > otherwise. It works fine when passing an argument, e.g. t or nil, but Emacs crashes when I don't pass > an argument. Also, I tried to simply return the value (by replacing the return line with the line in the > comment below it), which returns the value successfully when I pass an argument, but again Emacs > crashes when I don't pass any argument. I guess I'm confused: if you call your function with zero arguments, then why do you expect to find anything useful in the args[] array, or even assume that the args[] array can be accessed? Don't you get nargs = 0 in 'test' in this case?