From 6d4c4ce8a6827f9b428ab1084855161d9dff7d8b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Mon, 6 Nov 1995 22:38:26 +0100
Subject: [PATCH] indices and values fixed to support objects

Rev: src/builtin_efuns.c:1.4
Rev: src/object.c:1.3
Rev: src/object.h:1.2
---
 src/builtin_efuns.c |  12 ++++-
 src/object.c        | 105 +++++++++++++++++++++++++++++++-------------
 src/object.h        |   2 +
 3 files changed, 86 insertions(+), 33 deletions(-)

diff --git a/src/builtin_efuns.c b/src/builtin_efuns.c
index 518e0f456c..0a03600374 100644
--- a/src/builtin_efuns.c
+++ b/src/builtin_efuns.c
@@ -830,6 +830,10 @@ void f_indices(INT32 args)
     a=copy_array(sp[-args].u.list->ind);
     break;
 
+  case T_OBJECT:
+    a=object_indices(sp[-args].u.object);
+    break;
+
   default:
     error("Bad argument 1 to indices()\n");
     return; /* make apcc happy */
@@ -869,6 +873,10 @@ void f_values(INT32 args)
       SHORT_ITEM(a)[--size].integer=1;
     break;
 
+  case T_OBJECT:
+    a=object_values(sp[-args].u.object);
+    break;
+
   default:
     error("Bad argument 1 to values()\n");
     return;  /* make apcc happy */
@@ -1296,7 +1304,7 @@ void init_builtin_efuns()
   add_efun("functionp",  f_functionp,  "function(mixed:int)",OPT_TRY_OPTIMIZE);
   add_efun("hash",f_hash,"function(string,int|void:int)",OPT_TRY_OPTIMIZE);
   add_efun("implode",f_implode,"function(array,string|void:string)",OPT_TRY_OPTIMIZE);
-  add_efun("indices",f_indices,"function(string|array:int*)|function(mapping|list:mixed*)",0);
+  add_efun("indices",f_indices,"function(string|array:int*)|function(mapping|list:mixed*)|function(object:string*)",0);
   add_efun("intp",   f_intp,    "function(mixed:int)",OPT_TRY_OPTIMIZE);
   add_efun("listp",   f_listp,   "function(mixed:int)",OPT_TRY_OPTIMIZE);
   add_efun("lower_case",f_lower_case,"function(string:string)",OPT_TRY_OPTIMIZE);
@@ -1325,7 +1333,7 @@ void init_builtin_efuns()
   add_efun("time",f_time,"function(void|int:int)",OPT_EXTERNAL_DEPEND);
   add_efun("trace",f_trace,"function(int:int)",OPT_SIDE_EFFECT);
   add_efun("upper_case",f_upper_case,"function(string:string)",0);
-  add_efun("values",f_values,"function(string|list:int*)|function(array|mapping:mixed*)",0);
+  add_efun("values",f_values,"function(string|list:int*)|function(array|mapping|object:mixed*)",0);
   add_efun("zero_type",f_zero_type,"function(int:int)",0);
 }
 
diff --git a/src/object.c b/src/object.c
index 23312f1aaa..44883029ea 100644
--- a/src/object.c
+++ b/src/object.c
@@ -14,6 +14,7 @@
 #include "memory.h"
 #include "error.h"
 #include "main.h"
+#include "array.h"
 
 struct object *master_object = 0;
 struct object *first_object;
@@ -286,6 +287,40 @@ void object_index_no_free(struct svalue *to,
   }
 }
 
+static void low_object_index(struct svalue *to,struct object *o, INT32 f)
+{
+  struct identifier *i;
+  struct program *p=o->prog;
+
+  i=ID_FROM_INT(p, f);
+
+  if(i->flags & IDENTIFIER_FUNCTION)
+  {
+    to->type=T_FUNCTION;
+    to->subtype=f;
+    to->u.object=o;
+    o->refs++;
+  }
+  else if(i->run_time_type == T_MIXED)
+  {
+    struct svalue *s;
+    s=(struct svalue *)(o->storage+
+			INHERIT_FROM_INT(p, f)->storage_offset +
+			i->func.offset);
+    check_destructed(s);
+    assign_svalue_no_free(to, s);
+  }
+  else
+  {
+    union anything *u;
+    u=(union anything *)(o->storage+
+			 INHERIT_FROM_INT(p, f)->storage_offset +
+			 i->func.offset);
+    check_short_destructed(u,i->run_time_type);
+    assign_from_short_svalue_no_free(to, u, i->run_time_type);
+  }
+}
+
 void object_index(struct svalue *to,
 		  struct object *o,
 		  struct svalue *index)
@@ -310,37 +345,8 @@ void object_index(struct svalue *to,
     to->subtype=NUMBER_UNDEFINED;
     to->u.integer=0;
   }else{
-    struct identifier *i;
-    i=ID_FROM_INT(p, f);
-
-    if(i->flags & IDENTIFIER_FUNCTION)
-    {
-      free_svalue(to);
-      to->type=T_FUNCTION;
-      to->subtype=f;
-      to->u.object=o;
-      o->refs++;
-    }
-    else if(i->run_time_type == T_MIXED)
-    {
-      struct svalue *s;
-      s=(struct svalue *)(o->storage+
-			  INHERIT_FROM_INT(p, f)->storage_offset +
-			  i->func.offset);
-      check_destructed(s);
-
-      assign_svalue(to, s);
-    }
-    else
-    {
-      union anything *u;
-      u=(union anything *)(o->storage+
-			   INHERIT_FROM_INT(p, f)->storage_offset +
-			   i->func.offset);
-      check_short_destructed(u,i->run_time_type);
-      free_svalue(to);
-      assign_from_short_svalue_no_free(to, u, i->run_time_type);
-    }
+    free_svalue(to);
+    low_object_index(to, o, f);
   }
 }
 
@@ -638,3 +644,40 @@ void cleanup_objects()
   free_object(master_object);
   master_object=0;
 }
+
+struct array *object_indices(struct object *o)
+{
+  struct program *p;
+  struct array *a;
+  int e;
+
+  p=o->prog;
+  if(!p)
+    error("indices() on destructed object.\n");
+
+  a=allocate_array_no_init(p->num_identifier_indexes,0, T_STRING);
+  for(e=0;e<(int)p->num_identifier_indexes;e++)
+  {
+    copy_shared_string(SHORT_ITEM(a)[e].string,
+		       ID_FROM_INT(p,p->identifier_index[e])->name);
+  }
+  return a;
+}
+
+struct array *object_values(struct object *o)
+{
+  struct program *p;
+  struct array *a;
+  int e;
+  
+  p=o->prog;
+  if(!p)
+    error("values() on destructed object.\n");
+
+  a=allocate_array_no_init(p->num_identifier_indexes,0, T_MIXED);
+  for(e=0;e<(int)p->num_identifier_indexes;e++)
+  {
+    low_object_index(ITEM(a)+e, o, p->identifier_index[e]);
+  }
+  return a;
+}
diff --git a/src/object.h b/src/object.h
index e0336b2c3b..478e13b3ee 100644
--- a/src/object.h
+++ b/src/object.h
@@ -54,6 +54,8 @@ union anything *object_get_item_ptr(struct object *o,
 void verify_all_objects(int pass);
 int object_equal_p(struct object *a, struct object *b, struct processing *p);
 void cleanup_objects();
+struct array *object_indices(struct object *o);
+struct array *object_values(struct object *o);
 /* Prototypes end here */
 
 #endif /* OBJECT_H */
-- 
GitLab