1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
/*\ 
||| This file a part of Pike, and is copyright by Fredrik Hubinette 
||| Pike is distributed as GPL (General Public License) 
||| See the files COPYING and DISCLAIMER for more information. 
\*/ 
#include "constants.h" 
#include "macros.h" 
#include "program.h" 
#include "pike_types.h" 
#include "stralloc.h" 
#include "memory.h" 
#include "interpret.h" 
 
static struct hash_table *efun_hash = 0; 
 
struct efun *lookup_efun(struct pike_string *name) 
{ 
  struct hash_entry *h; 
 
  if(!efun_hash) return 0; 
  h=hash_lookup(efun_hash, name); 
  if(!h) return 0; 
  return BASEOF(h, efun, link); 
} 
 
void low_add_efun(struct pike_string *name, struct svalue *fun) 
{ 
  struct efun *parent; 
   
  parent=lookup_efun(name); 
 
  if(!parent) 
  { 
    if(!fun) return; 
 
    parent=ALLOC_STRUCT(efun); 
    copy_shared_string(parent->link.s,name); 
    efun_hash=hash_insert(efun_hash, &parent->link); 
  }else{ 
    free_svalue(& parent->function); 
 
    /* Disable efun */ 
    if(!fun) 
    { 
      efun_hash=hash_unlink(efun_hash, &parent->link); 
      free_string(parent->link.s); 
      free((char *)parent); 
      return; 
    } 
  } 
 
  assign_svalue_no_free(& parent->function, fun); 
} 
 
 
struct callable *make_callable(c_fun fun, 
                               char *name, 
                               char *type, 
                               INT16 flags, 
                               optimize_fun optimize, 
                               docode_fun docode) 
{ 
  struct callable *f; 
  f=ALLOC_STRUCT(callable); 
  f->refs=1; 
  f->function=fun; 
  f->name=make_shared_string(name); 
  f->type=parse_type(type); 
  f->flags=flags; 
  f->docode=docode; 
  f->optimize=optimize; 
  return f; 
} 
 
void really_free_callable(struct callable *fun) 
{ 
  free_string(fun->type); 
  free_string(fun->name); 
  free((char *)fun); 
} 
 
void add_efun2(char *name, 
               c_fun fun, 
               char *type, 
               INT16 flags, 
               optimize_fun optimize, 
               docode_fun docode) 
{ 
  struct svalue s; 
  struct pike_string *n; 
 
  n=make_shared_string(name); 
  s.type=T_FUNCTION; 
  s.subtype=FUNCTION_BUILTIN; 
  s.u.efun=make_callable(fun, name, type, flags, optimize, docode); 
  low_add_efun(n, &s); 
  free_svalue(&s); 
  free_string(n); 
} 
 
void add_efun(char *name, c_fun fun, char *type, INT16 flags) 
{ 
  add_efun2(name,fun,type,flags,0,0); 
} 
 
static void push_efun_entry(struct hash_entry *h) 
{ 
  struct efun *f; 
  check_stack(1); 
  f=BASEOF(h, efun, link); 
  push_string(f->link.s); 
  f->link.s->refs++; 
  copy_svalues_recursively_no_free(sp,& f->function,1,0); 
  sp++; 
} 
 
void push_all_efuns_on_stack() 
{ 
  if(efun_hash) 
    map_hashtable(efun_hash,push_efun_entry); 
} 
 
static void free_one_hashtable_entry(struct hash_entry *h) 
{ 
  struct efun *f; 
  f=BASEOF(h, efun, link); 
  free_svalue(& f->function); 
  free((char *)f); 
} 
 
void cleanup_added_efuns() 
{ 
  if(efun_hash) 
  { 
    free_hashtable(efun_hash,free_one_hashtable_entry); 
    efun_hash=0; 
  } 
   
}