From 6023aeee4193c48958e8cdce579a34d7407df64a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Sat, 18 Jan 1997 13:34:34 -0800
Subject: [PATCH] version(), _refs() and replace_master() added

Rev: src/builtin_functions.c:1.20
Rev: src/main.c:1.17
Rev: src/object.c:1.10
Rev: src/version.c:1.1
Rev: src/version.h:1.1
---
 src/builtin_functions.c | 38 ++++++++++++++++++++++++++++++++++++-
 src/main.c              | 10 +---------
 src/object.c            | 42 +++++++++++++++++++++++++----------------
 src/version.c           | 10 ++++++++++
 src/version.h           |  3 +++
 5 files changed, 77 insertions(+), 26 deletions(-)
 create mode 100644 src/version.c
 create mode 100644 src/version.h

diff --git a/src/builtin_functions.c b/src/builtin_functions.c
index c76fd38695..54f2ea6699 100644
--- a/src/builtin_functions.c
+++ b/src/builtin_functions.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: builtin_functions.c,v 1.19 1997/01/17 19:08:25 hubbe Exp $");
+RCSID("$Id: builtin_functions.c,v 1.20 1997/01/18 21:34:32 hubbe Exp $");
 #include "interpret.h"
 #include "svalue.h"
 #include "macros.h"
@@ -28,6 +28,7 @@ RCSID("$Id: builtin_functions.c,v 1.19 1997/01/17 19:08:25 hubbe Exp $");
 #include "memory.h"
 #include "threads.h"
 #include "time_stuff.h"
+#include "version.h"
 #include <math.h>
 #include <ctype.h>
 
@@ -1569,10 +1570,44 @@ void f__prev(INT32 args)
   }
 }
 
+void f__refs(INT32 args)
+{
+  INT32 i;
+  if(!args) error("Too few arguments to _refs()\n");
+  if(sp[-args].type > MAX_REF_TYPE)
+    error("Bad argument 1 to _refs()\n");
+
+  i=sp[-args].u.refs[0];
+  pop_n_elems(args);
+  push_int(i);
+}
+
+void f_replace_master(INT32 args)
+{
+  if(!args)
+    error("Too few arguments to replace_master()\n");
+  if(sp[-args].type != T_OBJECT)
+    error("Bad argument 1 to replace_master()\n"); 
+ if(!sp[-args].u.object->prog)
+    error("replace_master() called with destructed object.\n");
+    
+  free_object(master_object);
+  master_object=sp[-args].u.object;
+  master_object->refs++;
+
+  free_program(master_program);
+  master_program=master_object->prog;
+  master_program->refs++;
+
+  pop_n_elems(args);
+}
+
 void init_builtin_efuns()
 {
   init_operators();
   
+  add_efun("_refs",f__refs,"function(string|array|mapping|multiset|object|program:int)",OPT_EXTERNAL_DEPEND);
+  add_efun("replace_master",f_replace_master,"function(object:void)",OPT_SIDE_EFFECT);
   add_efun("add_constant",f_add_constant,"function(string,void|mixed:void)",OPT_SIDE_EFFECT);
   add_efun("aggregate",f_aggregate,"function(mixed ...:mixed *)",OPT_TRY_OPTIMIZE);
   add_efun("aggregate_multiset",f_aggregate_multiset,"function(mixed ...:multiset)",OPT_TRY_OPTIMIZE);
@@ -1644,5 +1679,6 @@ void init_builtin_efuns()
 #ifdef GC2
   add_efun("gc",f_gc,"function(:int)",OPT_SIDE_EFFECT);
 #endif
+  add_efun("version", f_version, "function(:string)", 0);
 }
 
diff --git a/src/main.c b/src/main.c
index af2357e998..73b7db2be4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: main.c,v 1.16 1997/01/18 17:47:54 grubba Exp $");
+RCSID("$Id: main.c,v 1.17 1997/01/18 21:34:33 hubbe Exp $");
 #include "types.h"
 #include "backend.h"
 #include "module.h"
@@ -31,7 +31,6 @@ RCSID("$Id: main.c,v 1.16 1997/01/18 17:47:54 grubba Exp $");
 #include <sys/resource.h>
 #endif
 
-#define VERSION		"Pike v0.4pl2"
 
 char *master_file;
 
@@ -248,12 +247,6 @@ void main(int argc, char **argv, char **env)
   f_exit(1);
 }
 
-/* string __version() */
-static void f___version(INT32 args)
-{
-  pop_n_elems(args);
-  push_text(VERSION);
-}
 
 void init_main_efuns()
 {
@@ -265,7 +258,6 @@ void init_main_efuns()
   init_signals();
   th_init();
   init_dynamic_load();
-  add_efun("__version", f___version, "function(:string)", 0);
 }
 
 void init_main_programs()
diff --git a/src/object.c b/src/object.c
index af5716ca00..a12a3c81f4 100644
--- a/src/object.c
+++ b/src/object.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: object.c,v 1.9 1997/01/18 04:44:40 hubbe Exp $");
+RCSID("$Id: object.c,v 1.10 1997/01/18 21:34:33 hubbe Exp $");
 #include "object.h"
 #include "dynamic_buffer.h"
 #include "interpret.h"
@@ -21,6 +21,7 @@ RCSID("$Id: object.c,v 1.9 1997/01/18 04:44:40 hubbe Exp $");
 #include "callback.h"
 
 struct object *master_object = 0;
+struct program *master_program =0;
 struct object *first_object;
 
 struct object fake_object = { 1 }; /* start with one reference */
@@ -32,7 +33,7 @@ void setup_fake_object()
   fake_object.refs=0xffffff;
 }
 
-struct object *clone(struct program *p, int args)
+static struct object *low_clone(struct program *p)
 {
   int e;
   struct object *o;
@@ -99,25 +100,23 @@ struct object *clone(struct program *p, int args)
   free_object(frame.current_object);
   fp = frame.parent_frame;
 
-  if(!master_object) 
-  {
-    master_object=o;
-    o->refs++;
-  }
+  return o;
+}
+
+struct object *clone(struct program *p, int args)
+{
+  struct object *o=low_clone(p);
 
   apply_lfun(o,LFUN___INIT,0);
   pop_stack();
   apply_lfun(o,LFUN_CREATE,args);
   pop_stack();
-
   return o;
 }
 
-
 struct object *get_master()
 {
   extern char *master_file;
-  struct program *master_prog;
   struct pike_string *master_name;
   static int inside=0;
 
@@ -133,12 +132,21 @@ struct object *get_master()
   }
 
   inside = 1;
-  master_name=make_shared_string(master_file);
-  master_prog=compile_file(master_name);
-  free_string(master_name);
-  if(!master_prog) return 0;
-  free_object(clone(master_prog,0));
-  free_program(master_prog);
+
+  if(!master_program)
+  {
+    master_name=make_shared_string(master_file);
+    master_program=compile_file(master_name);
+    free_string(master_name);
+    if(!master_program) return 0;
+  }
+  master_object=clone(master_program,0);
+
+  apply_lfun(master_object,LFUN___INIT,0);
+  pop_stack();
+  apply_lfun(master_object,LFUN_CREATE,0);
+  pop_stack();
+  
   inside = 0;
   return master_object;
 }
@@ -690,6 +698,8 @@ void cleanup_objects()
 
   free_object(master_object);
   master_object=0;
+  free_program(master_program);
+  master_program=0;
 }
 
 struct array *object_indices(struct object *o)
diff --git a/src/version.c b/src/version.c
new file mode 100644
index 0000000000..c75fddc33e
--- /dev/null
+++ b/src/version.c
@@ -0,0 +1,10 @@
+#include "global.h"
+#include "svalue.h"
+#include "interpret.h"
+#include "stralloc.h"
+
+void f_version(INT32 args)
+{
+  pop_n_elems(args);
+  push_text("Pike v0.4pl2");
+}
diff --git a/src/version.h b/src/version.h
new file mode 100644
index 0000000000..301b8aad4f
--- /dev/null
+++ b/src/version.h
@@ -0,0 +1,3 @@
+/* Prototypes begin here */
+void f_version(INT32 args);
+/* Prototypes end here */
-- 
GitLab