make the builtins an array of pointers so the hash table doesn't get royally

screwed up
This commit is contained in:
Bill Currie 2002-01-19 03:31:51 +00:00
parent 6ab3329b94
commit 6af9a5d8e6
4 changed files with 19 additions and 20 deletions

View file

@ -143,6 +143,7 @@ typedef void (*builtin_proc) (progs_t *pr);
typedef struct { typedef struct {
const char *name; const char *name;
builtin_proc proc; builtin_proc proc;
int first_statement;
} builtin_t; } builtin_t;
ddef_t *PR_FindGlobal (progs_t *pr, const char *name); ddef_t *PR_FindGlobal (progs_t *pr, const char *name);
@ -277,7 +278,7 @@ struct progs_s {
int (*prune_edict)(progs_t *pr, edict_t *ent); int (*prune_edict)(progs_t *pr, edict_t *ent);
void (*free_edict)(progs_t *pr, edict_t *ent); void (*free_edict)(progs_t *pr, edict_t *ent);
builtin_t *builtins; builtin_t **builtins;
int numbuiltins; int numbuiltins;
// debug info // debug info

View file

@ -1287,35 +1287,39 @@ PR_AddBuiltin (progs_t *pr, const char *name, builtin_proc builtin, int num)
{ {
int i; int i;
if (!pr->builtin_hash)
pr->builtin_hash = Hash_NewTable (1021, builtin_get_key, 0, pr);
if (pr->numbuiltins == 0) { if (pr->numbuiltins == 0) {
pr->builtins = calloc (PR_AUTOBUILTIN, sizeof (builtin_t)); pr->builtins = calloc (PR_AUTOBUILTIN, sizeof (builtin_t*));
pr->numbuiltins = PR_AUTOBUILTIN; pr->numbuiltins = PR_AUTOBUILTIN;
if (!pr->builtins) if (!pr->builtins)
PR_Error (pr, "PR_AddBuiltin: memory allocation error!\n"); PR_Error (pr, "PR_AddBuiltin: memory allocation error!\n");
pr->builtin_hash = Hash_NewTable (1021, builtin_get_key, 0, pr);
} }
if (num < 0) { if (num < 0) {
for (i = PR_AUTOBUILTIN; for (i = PR_AUTOBUILTIN;
i < pr->numbuiltins && pr->builtins[i].proc; i++) i < pr->numbuiltins && pr->builtins[i]; i++)
; ;
if (i >= pr->numbuiltins) { if (i >= pr->numbuiltins) {
pr->numbuiltins++; pr->numbuiltins++;
pr->builtins = realloc (pr->builtins, pr->builtins = realloc (pr->builtins,
pr->numbuiltins * sizeof (builtin_t)); pr->numbuiltins * sizeof (builtin_t*));
if (!pr->builtins) if (!pr->builtins)
PR_Error (pr, "PR_AddBuiltin: memory allocation error!\n"); PR_Error (pr, "PR_AddBuiltin: memory allocation error!\n");
} }
} else { } else {
if (num >= PR_AUTOBUILTIN || num == 0) if (num >= PR_AUTOBUILTIN || num == 0)
PR_Error (pr, "PR_AddBuiltin: invalid builtin number.\n"); PR_Error (pr, "PR_AddBuiltin: invalid builtin number.\n");
if (pr->builtins[num].proc) if (pr->builtins[num])
PR_Error (pr, "PR_AddBuiltin: builtin number already exists.\n"); PR_Error (pr, "PR_AddBuiltin: builtin number already exists.\n");
i = num; i = num;
} }
pr->builtins[i].proc = builtin; pr->builtins[i] = malloc (sizeof (builtin_t));
pr->builtins[i].name = name; pr->builtins[i]->proc = builtin;
Hash_Add (pr->builtin_hash, &pr->builtins[i]); pr->builtins[i]->name = name;
pr->builtins[i]->first_statement = i;
Hash_Add (pr->builtin_hash, pr->builtins[i]);
} }
builtin_t * builtin_t *
@ -1405,7 +1409,7 @@ PR_RelocateBuiltins (progs_t *pr)
pr->progs_name, bi_name); pr->progs_name, bi_name);
return 0; return 0;
} }
func->first_statement = -(bi - pr->builtins); func->first_statement = -bi->first_statement;
} }
return 1; return 1;
} }

View file

@ -710,21 +710,15 @@ PR_ExecuteProgram (progs_t * pr, func_t fnum)
pr->pr_argc = st->op - OP_CALL0; pr->pr_argc = st->op - OP_CALL0;
if (!OPA.func_var) if (!OPA.func_var)
PR_RunError (pr, "NULL function"); PR_RunError (pr, "NULL function");
if (OPA.func_var < 0) { // dynamic builtin
int i = -OPA.func_var;
if (i >= pr->numbuiltins || !pr->builtins[i].proc)
PR_RunError (pr, "Bad builtin call number");
pr->builtins[i].proc (pr);
break;
}
newf = &pr->pr_functions[OPA.func_var]; newf = &pr->pr_functions[OPA.func_var];
if (newf->first_statement < 0) { if (newf->first_statement < 0) {
// negative statements are built in functions // negative statements are built in functions
int i = -newf->first_statement; int i = -newf->first_statement;
if (i >= pr->numbuiltins || !pr->builtins[i].proc) if (i >= pr->numbuiltins || !pr->builtins[i]
|| !pr->builtins[i]->proc)
PR_RunError (pr, "Bad builtin call number"); PR_RunError (pr, "Bad builtin call number");
pr->builtins[i].proc (pr); pr->builtins[i]->proc (pr);
break; break;
} }

View file

@ -1599,7 +1599,7 @@ SV_PR_Cmds_Init ()
{ {
PR_Cmds_Init (&sv_pr_state); PR_Cmds_Init (&sv_pr_state);
sv_pr_state.builtins[45].proc = 0; // (override standard builtin) sv_pr_state.builtins[45] = 0; // (override standard builtin)
PR_AddBuiltin (&sv_pr_state, "cvar", PF_sv_cvar, 45); PR_AddBuiltin (&sv_pr_state, "cvar", PF_sv_cvar, 45);
// float (string s) cvar // float (string s) cvar