diff --git a/doc/gawk.texi b/doc/gawk.texi index 4d088fd3..2ed5f131 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -37019,10 +37019,14 @@ value type, as appropriate. This behavior is summarized in @multitable @columnfractions .50 .50 @headitem @tab Type of Actual Value @end multitable +@ignore +@c 12/2023: Texinfo.tex has morphed enough that this produces +@c bad output, so comment it out. @c 10/2014: Thanks to Karl Berry for this bit to reduce the space: @tex \vglue-1.1\baselineskip @end tex +@end ignore @c @multitable @columnfractions .166 .166 .198 .15 .15 .166 @multitable {Requested} {Undefined} {Number} {Number} {Scalar} {Regex} {Number} {Array} {Undefined} @headitem @tab @tab String @tab Strnum @tab Number @tab Regex @tab Bool @tab Array @tab Undefined @@ -37068,6 +37072,33 @@ value type, as appropriate. This behavior is summarized in @end ifplaintext @end float +There are a number of points of note: + +@itemize @bullet +@item +A request for @code{AWK_UNDEFINED} always returns true, filling in +the actual type of the particular value. You can think of this +as a sort of ``wildcard'' request. + +@item +Requesting an @code{AWK_STRING} causes @command{gawk} to convert any +scalar value to a string result, and that is what is retuned. + +@item +Requesting an @code{AWK_NUMBER} causes @command{gawk} to convert any +scalar value, except for a regexp, to a numeric result, and +that is what is retuned. + +Conversion between string and number in the API thus parallels how +@command{gawk} behaves in running code. + +@item +The API functions do @emph{not} distinguish between @code{"undefined"} +and @code{"unassigned"} as returned by @code{typeof()} (@pxref{Dynamic +Typing Gawk}). @code{AWK_UNDEFINED} serves for both. This is unlikely +to change, as the documentation and code are already complicated enough. +@end itemize + @node Accessing Parameters @subsection Accessing and Updating Parameters diff --git a/gawkapi.c b/gawkapi.c index 20d131aa..fe6573e0 100644 --- a/gawkapi.c +++ b/gawkapi.c @@ -72,16 +72,17 @@ api_get_argument(awk_ext_id_t id, size_t count, if (arg->type == Node_var_new || arg->type == Node_elem_new) { if (wanted == AWK_UNDEFINED) return awk_true; - else if (wanted == AWK_ARRAY) { + else if (wanted == AWK_ARRAY) goto array; - } else { + else goto scalar; - } } /* at this point, we have real type */ if (arg->type == Node_var_array || arg->type == Node_array_ref) { - if (wanted != AWK_ARRAY && wanted != AWK_UNDEFINED) + if (wanted == AWK_UNDEFINED) + return awk_true; + else if (wanted != AWK_ARRAY) return awk_false; goto array; } else @@ -624,11 +625,9 @@ node_to_awk_value(NODE *node, awk_value_t *val, awk_valtype_t wanted) val->val_type = AWK_REGEX; break; case NUMBER|STRING: - if (node == Nnull_string) { - val->val_type = AWK_UNDEFINED; - break; - } - /* fall through */ + // this can come from a Node_elem_new, as well as Nnull_string + val->val_type = AWK_UNDEFINED; + break; default: warning(_("node_to_awk_value detected invalid flags combination `%s'; please file a bug report"), flags2str(node->flags)); val->val_type = AWK_UNDEFINED; @@ -661,11 +660,9 @@ node_to_awk_value(NODE *node, awk_value_t *val, awk_valtype_t wanted) ret = awk_true; break; case NUMBER|STRING: - if (node == Nnull_string) { - val->val_type = AWK_UNDEFINED; - break; - } - /* fall through */ + // this can come from a Node_elem_new, as well as Nnull_string + val->val_type = AWK_UNDEFINED; + break; default: warning(_("node_to_awk_value detected invalid flags combination `%s'; please file a bug report"), flags2str(node->flags)); val->val_type = AWK_UNDEFINED; @@ -691,11 +688,9 @@ node_to_awk_value(NODE *node, awk_value_t *val, awk_valtype_t wanted) val->val_type = AWK_REGEX; break; case NUMBER|STRING: - if (node == Nnull_string) { - val->val_type = AWK_UNDEFINED; - break; - } - /* fall through */ + // this can come from a Node_elem_new, as well as Nnull_string + val->val_type = AWK_UNDEFINED; + break; default: warning(_("node_to_awk_value detected invalid flags combination `%s'; please file a bug report"), flags2str(node->flags)); val->val_type = AWK_UNDEFINED; @@ -727,12 +722,10 @@ node_to_awk_value(NODE *node, awk_value_t *val, awk_valtype_t wanted) ret = awk_true; break; case NUMBER|STRING: - if (node == Nnull_string) { - val->val_type = AWK_UNDEFINED; - ret = awk_true; - break; - } - /* fall through */ + // this can come from a Node_elem_new, as well as Nnull_string + val->val_type = AWK_UNDEFINED; + ret = awk_true; + break; default: warning(_("node_to_awk_value detected invalid flags combination `%s'; please file a bug report"), flags2str(node->flags)); val->val_type = AWK_UNDEFINED;