cb22561995-10-11Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
cb22561995-10-11Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
46aa641999-12-29Henrik Grubbström (Grubba) /**/
07513e1996-10-04Fredrik Hübinette (Hubbe) #include "global.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "constants.h"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "program.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "pike_types.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "stralloc.h"
9aa6fa1997-05-19Fredrik Hübinette (Hubbe) #include "pike_memory.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "interpret.h"
244ce61997-01-26Fredrik Hübinette (Hubbe) #include "mapping.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
45ee5d1999-02-10Fredrik Hübinette (Hubbe) #include "block_alloc.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
d68a072001-02-20Henrik Grubbström (Grubba) RCSID("$Id: constants.c,v 1.25 2001/02/19 23:49:59 grubba Exp $");
24ddc71998-03-28Henrik Grubbström (Grubba) 
5f61da2000-04-13Fredrik Hübinette (Hubbe) struct mapping *builtin_constants = 0;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct mapping *get_builtin_constants(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
244ce61997-01-26Fredrik Hübinette (Hubbe)  if(!builtin_constants) builtin_constants=allocate_mapping(20);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
244ce61997-01-26Fredrik Hübinette (Hubbe)  return builtin_constants;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
06983f1996-09-22Fredrik Hübinette (Hubbe) void low_add_efun(struct pike_string *name, struct svalue *fun)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
244ce61997-01-26Fredrik Hübinette (Hubbe)  struct svalue s;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
244ce61997-01-26Fredrik Hübinette (Hubbe)  if(!builtin_constants) builtin_constants=allocate_mapping(20);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
244ce61997-01-26Fredrik Hübinette (Hubbe)  s.type=T_STRING; s.subtype=0; s.u.string=name;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
244ce61997-01-26Fredrik Hübinette (Hubbe)  if(fun) { mapping_insert(builtin_constants, &s, fun); }else{ map_delete(builtin_constants, &s);
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }
cecf511997-02-06Fredrik Hübinette (Hubbe) void low_add_constant(char *name, struct svalue *fun) { struct pike_string *p; p=make_shared_string(name); low_add_efun(p, fun); free_string(p); }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void add_global_program(char *name, struct program *p)
cecf511997-02-06Fredrik Hübinette (Hubbe) { struct svalue s; s.type=T_PROGRAM; s.subtype=0; s.u.program=p; low_add_constant(name, &s); }
45ee5d1999-02-10Fredrik Hübinette (Hubbe) #undef EXIT_BLOCK #define EXIT_BLOCK(X) do { \
d68a072001-02-20Henrik Grubbström (Grubba)  free_type(X->type); \
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  free_string(X->name); \ }while(0) BLOCK_ALLOC(callable,128) /* Eats one ref to 'type' and 'name' */
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct callable *low_make_callable(c_fun fun,
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  struct pike_string *name, struct pike_string *type, INT16 flags, optimize_fun optimize, docode_fun docode)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  struct callable *f=alloc_callable();
5267b71995-08-09Fredrik Hübinette (Hubbe)  f->refs=1; f->function=fun;
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  f->name=name; f->type=type;
5267b71995-08-09Fredrik Hübinette (Hubbe)  f->flags=flags;
7bd0ea1996-02-19Fredrik Hübinette (Hubbe)  f->docode=docode; f->optimize=optimize;
044c621999-04-14Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG {
46aa641999-12-29Henrik Grubbström (Grubba)  struct pike_string *z=check_call(function_type_string,type,0);
044c621999-04-14Fredrik Hübinette (Hubbe)  f->may_return_void= z == void_type_string; if(!z) fatal("Gnapp!\n"); free_string(z); } #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  return f; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct callable *make_callable(c_fun fun,
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  char *name, char *type, INT16 flags, optimize_fun optimize, docode_fun docode)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  return low_make_callable(fun,make_shared_string(name),parse_type(type),flags,optimize,docode);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct callable *add_efun2(char *name,
4d7b181999-12-07Fredrik Hübinette (Hubbe)  c_fun fun, char *type, INT16 flags, optimize_fun optimize, docode_fun docode)
5267b71995-08-09Fredrik Hübinette (Hubbe) { struct svalue s;
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *n;
4d7b181999-12-07Fredrik Hübinette (Hubbe)  struct callable *ret;
5267b71995-08-09Fredrik Hübinette (Hubbe)  n=make_shared_string(name); s.type=T_FUNCTION;
bdb5091996-09-25Fredrik Hübinette (Hubbe)  s.subtype=FUNCTION_BUILTIN;
4d7b181999-12-07Fredrik Hübinette (Hubbe)  ret=s.u.efun=make_callable(fun, name, type, flags, optimize, docode);
5267b71995-08-09Fredrik Hübinette (Hubbe)  low_add_efun(n, &s); free_svalue(&s); free_string(n);
4d7b181999-12-07Fredrik Hübinette (Hubbe)  return ret;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct callable *add_efun(char *name, c_fun fun, char *type, INT16 flags)
7bd0ea1996-02-19Fredrik Hübinette (Hubbe) {
4d7b181999-12-07Fredrik Hübinette (Hubbe)  return add_efun2(name,fun,type,flags,0,0);
7bd0ea1996-02-19Fredrik Hübinette (Hubbe) }
51f0e02000-08-10Henrik Grubbström (Grubba) PMOD_EXPORT struct callable *quick_add_efun(char *name, ptrdiff_t name_length, c_fun fun, char *type, ptrdiff_t type_length, INT16 flags, optimize_fun optimize, docode_fun docode)
45ee5d1999-02-10Fredrik Hübinette (Hubbe) { struct svalue s; struct pike_string *n,*t;
4d7b181999-12-07Fredrik Hübinette (Hubbe)  struct callable *ret;
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  n=make_shared_binary_string(name,name_length); t=make_shared_binary_string(type,type_length); #ifdef DEBUG check_type_string(t); #endif s.type=T_FUNCTION; s.subtype=FUNCTION_BUILTIN; add_ref(n);
4d7b181999-12-07Fredrik Hübinette (Hubbe)  ret=s.u.efun=low_make_callable(fun, n, t, flags, optimize, docode);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  low_add_efun(n, &s); free_svalue(&s); free_string(n);
4d7b181999-12-07Fredrik Hübinette (Hubbe)  return ret;
45ee5d1999-02-10Fredrik Hübinette (Hubbe) }
be478c1997-08-30Henrik Grubbström (Grubba) void cleanup_added_efuns(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
5b7b061999-04-08Fredrik Hübinette (Hubbe) #ifdef DO_PIKE_CLEANUP
244ce61997-01-26Fredrik Hübinette (Hubbe)  if(builtin_constants)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
244ce61997-01-26Fredrik Hübinette (Hubbe)  free_mapping(builtin_constants); builtin_constants=0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
5b7b061999-04-08Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) }