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: What is the recommended way to find out the number of arguments passed to a module function? Date: Wed, 10 Jan 2024 20:01:09 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000760b68060e9c0e70" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19340"; mail-complaints-to="usenet@ciao.gmane.io" To: Emacs Devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jan 10 20:02:25 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 1rNdqL-0004t1-4K for ged-emacs-devel@m.gmane-mx.org; Wed, 10 Jan 2024 20:02:25 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNdpf-0000Ht-Aq; Wed, 10 Jan 2024 14:01:43 -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 1rNdpb-0000HE-LW for emacs-devel@gnu.org; Wed, 10 Jan 2024 14:01:40 -0500 Original-Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rNdpY-0001xc-9n for emacs-devel@gnu.org; Wed, 10 Jan 2024 14:01:37 -0500 Original-Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40e552dfa5dso13681615e9.1 for ; Wed, 10 Jan 2024 11:01:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704913281; x=1705518081; darn=gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=o9Pgt7J6THsDyjXcG4bvO2InZPe4ib8vUmcpe1FSuPw=; b=YAvCHu0rxO3PFrUiRKIDsxafjU1hqLhD6XeKZv9RE0gAVtSCo15r7uUV+Pw36WQWbw qpMBM/p1QNpyIQ+VPPVnFTo9RAYcamRbpN6JwQnX107CrLIDZKiMZz8uCiJKrQXL6mJa xmXZrcAyCuV5F5Ep88G2rRvanvyywZjRetHY6LlJeyI7HpLlYT8dzuwBGLjfklhXAkmI wlgqDIIjfmR7wVcoiCS8HhKyxRz6+22ItmzHiucSjUKeMBl5zwvFWvWb/FVmOSUtwrQh gmJpkUTeZjYvwTxLtgkReZfuy01oVSYSt4Ti1MXIduXM/lfSKIwyG8xXJoyzXn9qrCTt DR/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704913281; x=1705518081; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=o9Pgt7J6THsDyjXcG4bvO2InZPe4ib8vUmcpe1FSuPw=; b=kDbukbYA3HWvcUd7Bnq4ULINr0Nck0QaQT+9JKc821UkTRxx9Pid5oMpVQf8u46UAS I0Tpv+uI/CiWof4yEhaN4grp6J2UV+X/CROFA5Su4I/6ZczmQzgsFdFWU470ix0oj2a7 AjrV/k21hZ0GLmlwQpA5zQYzEsTUksBPiJvVLbcGyta3JL4pG0rmaU380ArdxjiRYBxF uTxTvfcNRr18ejaWcCmACJPSW4YieKsBYL2+mA7D8d6vamkLxxXqkh3ZtodTEZ43/JUE PWzeEx+kCXzIeoI6OwLgISREijT2VmCX+Q3Teegy1AC9xqnL0X0+dK189cT/eHKZhLRX EL9w== X-Gm-Message-State: AOJu0YwGHE3lJPwQIa0xLj5O1ojoYcNa+ekZI4mOvUblSikQY2lXsK7E F+FbrbhiWrnMmjsFykK3C4VuEZPFNfsnB8t7Comw3DvCPNM= X-Google-Smtp-Source: AGHT+IE6v4uzN8HMa9+eLMhEaDax8s/7M0lpFrm2WuhOmca1GYxNNivz8dJM/Vhv5PIpX95I05mamjhednH6KA2oyIg= X-Received: by 2002:a05:600c:5403:b0:40e:4374:e7c9 with SMTP id he3-20020a05600c540300b0040e4374e7c9mr791959wmb.179.1704913280566; Wed, 10 Jan 2024 11:01:20 -0800 (PST) Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=dalanicolai@gmail.com; helo=mail-wm1-x335.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:314820 Archived-At: --000000000000760b68060e9c0e70 Content-Type: text/plain; charset="UTF-8" I am trying to write a dynamic module. 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. Now, of course, I could 'fix it' by creating a lisp function that always passes an argument, but I wonder if this is the 'only' solution. Does the module API provide a way to check for the 'optional' argument? --000000000000760b68060e9c0e70 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I am trying to write a dynamic module.

=
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 t= o 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 &#= 39;test-module' with the following code:

#include <emacs-module.h>
= int plugin_is_GPL_compatible;
static emacs_value
test (emacs_env *env= , ptrdiff_t nargs, emacs_value *args, void *data)
{
=C2=A0 int intege= r =3D env->is_not_nil(env, args[0])? 1 : 0;
=C2=A0 return env->mak= e_integer(env, integer);
=C2=A0 /* return args[0]; */
}
int
ema= cs_module_init (struct emacs_runtime *runtime)
{
=C2=A0 emacs_env *en= v =3D runtime->get_environment (runtime);
=C2=A0 emacs_value func =3D= env->make_function (env, 0, 1, test, NULL, NULL);
=C2=A0 emacs_value= symbol =3D env->intern (env, "test");
=C2=A0 emacs_value a= rgs[] =3D {symbol, func};
=C2=A0 env->funcall (env, env->intern (e= nv, "defalias"), 2, args);
=C2=A0 return 0;
}<= div>
The 'test' function checks if the value of the a= rgument is non-nil, and 'returns' a 1 if it is and a 0 otherwise. I= t works fine when passing an argument, e.g. t or nil, but Emacs crashes whe= n I don't pass an argument. Also, I tried to simply return the value (b= y replacing the return line with the line in the comment below it), which r= eturns the value successfully when I pass an argument, but again Emacs cras= hes when I don't pass any argument.

Now, of co= urse, I could 'fix it' by creating a lisp function that always pass= es an argument, but I wonder if this is the 'only' solution. Does t= he module API provide a way to check for the 'optional' argument?
--000000000000760b68060e9c0e70--