From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: dalanicolai 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:06:48 +0100 Message-ID: References: <83jzohm23r.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000003742d0060e9cf910" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37828"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jan 10 21:07:54 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 1rNerh-0009ek-Tu for ged-emacs-devel@m.gmane-mx.org; Wed, 10 Jan 2024 21:07:54 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNeqv-0004Un-Uf; Wed, 10 Jan 2024 15:07:06 -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 1rNequ-0004U6-Od for emacs-devel@gnu.org; Wed, 10 Jan 2024 15:07:04 -0500 Original-Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rNeqs-0000Sh-Rx; Wed, 10 Jan 2024 15:07:04 -0500 Original-Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-40e43e48a16so42888025e9.2; Wed, 10 Jan 2024 12:07:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704917219; x=1705522019; darn=gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=muqT9Jyb3VbbjnQVaFNcC4wop5DCvgWJK9AUqo/yoyU=; b=E6Ln0rohlKP7T/R0M7Y8luFdnc6O2dDF8Ns6XbUj6jCjM50upxW4FzP5UzKB18igYM UHYAYo5i4GcCwsAZ3AhJWjHtmF6xi7mb5cXQMbf5u7k+qTbR3v/qMf9iYNPn6ZoNkse6 ArFSlbGGmw2nfefsVrG6SCPzkS9pbQZW/DvaWuTVbZ7t8275G+QxAjWJB/Rhwe4RiGVs wkiq4/KrM3WauoGc3Y/TDViTY5HAoNbmSyVTJ4lAaAXkBGFNfSaKEzw/RFb/P+p2KJ4l sYrnBeGdEgYLWvZVTaiqOoFFaiMaPtvAWoz8DXo9W0D1ZZy9IW+mpKErVXt6eEeGY3xV yG/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704917219; x=1705522019; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=muqT9Jyb3VbbjnQVaFNcC4wop5DCvgWJK9AUqo/yoyU=; b=oGVQHQ2d7gk15V/gk7uV6PBScd7Uys8e46AffmjbLIB8pB2r/xosqtCUZLKnkg+HlS cnqlo+YoFWSEOh5umLGcsxMTx08R18mXnJVNWCtEuzQldogAuW3c0/utpaWG7/4B4drS oUAYXRA9o+uPXlmVLK8etOPkg8NadPDvkwMIpiCdGj97Yyc2JpXr6Pm0TzDUNR0GdatU JcgFMUGGL0Au8dKujtyt/yFHXy9UbtNEix1+kiK2pRvjxa2sL4DKWxzkfeHfRNkD0H9I d2W7+RBH8pL6erIXC1NrYBHNs2rbUYwuRfSN8HKVg3nFINQQyS3YWpOV7sDz/CP2c/3X C/Zg== X-Gm-Message-State: AOJu0YyGOY8lGFLjh3ZXzB+99tdCoed4PO7gHbWPGnHJF1X5d+qwApmV 3MBdN08Je9tY7jf8wo5BQDOih3X4Q+6qrd61XSOTj+xuQjw= X-Google-Smtp-Source: AGHT+IH+u6NT0gKJS65zQlxN6RsmA/ehuL06FDHkL6XjvS1QVMOz5yHyfY5ezsfXrnfDZQFtf9gkvdn9IhuxdM+UNz8= X-Received: by 2002:a05:600c:4f4e:b0:40e:4267:cfc4 with SMTP id m14-20020a05600c4f4e00b0040e4267cfc4mr927062wmq.112.1704917219097; Wed, 10 Jan 2024 12:06:59 -0800 (PST) In-Reply-To: <83jzohm23r.fsf@gnu.org> Received-SPF: pass client-ip=2a00:1450:4864:20::32f; envelope-from=dalanicolai@gmail.com; helo=mail-wm1-x32f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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:314822 Archived-At: --0000000000003742d0060e9cf910 Content-Type: text/plain; charset="UTF-8" (I should reply to all :) Ah okay, well I am completely new to C, so I am still finding out things (e.g. I have not 'actively' used ptrdiff yet). But indeed the 'nargs' just give the number of passed args (obviously). I had checked the 'sizeof' the args array when passing an argument and without passing an argument, and it was the same. Then the docs mention that NARGS is the 'required' number of arguments, I had misunderstood it a little (of course the required number is variable, but somehow I assumed it would be 1 in this case). So sometimes I overlook the 'obvious', and because I had spent some time on testing things already, I decided to just ask here. Anyway, as usual, thanks for your quick and helpful answer! On Wed, 10 Jan 2024 at 20:16, Eli Zaretskii wrote: > > 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? > --0000000000003742d0060e9cf910 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
(I should reply to all :)

Ah= okay, well I am completely new to C, so I am still finding out things (e.g= . I have not 'actively' used ptrdiff yet).
But indeed the= 'nargs' just give the number of passed args (obviously).
=

I had checked the 'sizeof' the args array when = passing an argument and without passing an argument,
and it was t= he same. Then the docs mention that = NARGS is the 'required' number of arguments,
I had misund= erstood it a little (of course the required number is variable, but somehow= I assumed it
would be 1 in this case). So sometimes I overlook t= he 'obvious', and because I had spent some time on testing
things already, I decided to just ask here.

Anyw= ay, as usual, thanks for your quick and helpful answer!

On Wed, 10 Jan= 2024 at 20:16, Eli Zaretskii <eliz@gnu.= org> wrote:
> From: dalanicolai <dalanicolai@gmail.com>
> Date: Wed, 10 Jan 2024 20:01:09 +0100
>
> In the module API's 'make-function' we should pass a min-a= rity 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 argu= ment, the 'optional'
> argument does not seem
> to be nil, or any emacs-value at all (I have tested if it might be a N= ULL pointer). I have tested it using a
> 'test-module' with the following code:
>
>=C2=A0 #include <emacs-module.h>
>=C2=A0 int plugin_is_GPL_compatible;
>=C2=A0 static emacs_value
>=C2=A0 test (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *= data)
>=C2=A0 {
>=C2=A0 =C2=A0 int integer =3D env->is_not_nil(env, args[0])? 1 : 0;<= br> >=C2=A0 =C2=A0 return env->make_integer(env, integer);
>=C2=A0 =C2=A0 /* return args[0]; */
>=C2=A0 }
>=C2=A0 int
>=C2=A0 emacs_module_init (struct emacs_runtime *runtime)
>=C2=A0 {
>=C2=A0 =C2=A0 emacs_env *env =3D runtime->get_environment (runtime);=
>=C2=A0 =C2=A0 emacs_value func =3D env->make_function (env, 0, 1, te= st, NULL, NULL);
>=C2=A0 =C2=A0 emacs_value symbol =3D env->intern (env, "test&qu= ot;);
>=C2=A0 =C2=A0 emacs_value args[] =3D {symbol, func};
>=C2=A0 =C2=A0 env->funcall (env, env->intern (env, "defalias= "), 2, args);
>=C2=A0 =C2=A0 return 0;
>=C2=A0 }
>
> 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 th= e 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 =3D 0 in 'test' in this case?
--0000000000003742d0060e9cf910--