1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-11 14:21:10 +02:00

Change `defined?' to accept a module as its second argument.

Reported by Daniel Kraft <d@domob.eu>.

* doc/ref/api-binding.texi (Binding Reflection): Update documentation of
  `defined?'.

* libguile/evalext.c (scm_defined_p): Expect a module as the second
  argument, not a lexical environment.
This commit is contained in:
Ludovic Courtès 2009-08-10 19:24:34 +02:00
parent 32be5735cd
commit dab1ed3767
2 changed files with 21 additions and 41 deletions

View file

@ -1,6 +1,6 @@
@c -*-texinfo-*- @c -*-texinfo-*-
@c This is part of the GNU Guile Reference Manual. @c This is part of the GNU Guile Reference Manual.
@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009
@c Free Software Foundation, Inc. @c Free Software Foundation, Inc.
@c See the file guile.texi for copying conditions. @c See the file guile.texi for copying conditions.
@ -271,10 +271,16 @@ with duplicate bindings.
Guile provides a procedure for checking whether a symbol is bound in the Guile provides a procedure for checking whether a symbol is bound in the
top level environment. top level environment.
@c NJFIXME explain [env] @deffn {Scheme Procedure} defined? sym [module]
@deffn {Scheme Procedure} defined? sym [env] @deffnx {C Function} scm_defined_p (sym, module)
@deffnx {C Function} scm_defined_p (sym, env) Return @code{#t} if @var{sym} is defined in the module @var{module} or
Return @code{#t} if @var{sym} is defined in the lexical environment @var{env}. When @var{env} is not specified, look in the top-level environment as defined by the current module. the current module when @var{module} is not specified; otherwise return
@code{#f}.
Up to Guile 1.8, the second optional argument had to be @dfn{lexical
environment} as returned by @code{the-environment}, for example. The
behavior of this function remains unchanged when the second argument is
omitted.
@end deffn @end deffn

View file

@ -31,49 +31,23 @@
#include "libguile/evalext.h" #include "libguile/evalext.h"
SCM_DEFINE (scm_defined_p, "defined?", 1, 1, 0, SCM_DEFINE (scm_defined_p, "defined?", 1, 1, 0,
(SCM sym, SCM env), (SCM sym, SCM module),
"Return @code{#t} if @var{sym} is defined in the lexical " "Return @code{#t} if @var{sym} is defined in the module "
"environment @var{env}. When @var{env} is not specified, " "@var{module} or the current module when @var{module} is not"
"look in the top-level environment as defined by the " "specified.")
"current module.")
#define FUNC_NAME s_scm_defined_p #define FUNC_NAME s_scm_defined_p
{ {
SCM var; SCM var;
SCM_VALIDATE_SYMBOL (1, sym); SCM_VALIDATE_SYMBOL (1, sym);
if (SCM_UNBNDP (env)) if (SCM_UNBNDP (module))
var = scm_sym2var (sym, scm_current_module_lookup_closure (), module = scm_current_module ();
SCM_BOOL_F);
else else
{ SCM_VALIDATE_MODULE (2, module);
SCM frames = env;
register SCM b; var = scm_module_variable (module, sym);
for (; SCM_NIMP (frames); frames = SCM_CDR (frames))
{
SCM_ASSERT (scm_is_pair (frames), env, SCM_ARG2, FUNC_NAME);
b = SCM_CAR (frames);
if (scm_is_true (scm_procedure_p (b)))
break;
SCM_ASSERT (scm_is_pair (b), env, SCM_ARG2, FUNC_NAME);
for (b = SCM_CAR (b); SCM_NIMP (b); b = SCM_CDR (b))
{
if (!scm_is_pair (b))
{
if (scm_is_eq (b, sym))
return SCM_BOOL_T;
else
break;
}
if (scm_is_eq (SCM_CAR (b), sym))
return SCM_BOOL_T;
}
}
var = scm_sym2var (sym,
SCM_NIMP (frames) ? SCM_CAR (frames) : SCM_BOOL_F,
SCM_BOOL_F);
}
return (scm_is_false (var) || SCM_UNBNDP (SCM_VARIABLE_REF (var)) return (scm_is_false (var) || SCM_UNBNDP (SCM_VARIABLE_REF (var))
? SCM_BOOL_F ? SCM_BOOL_F
: SCM_BOOL_T); : SCM_BOOL_T);