[ruamoko] Create a va_copy for progs

In testing variable fw/precision in PR_Sprintf, I got a nasty reminder
of the limitations of the current progs ABI: passing @args to another QC
function does not work because the args list gets trampled but the
called function's locals. Thus, the need for a va_copy. It's not quite
the same as C's as it returns the destination args instead of copying
like memcpy, but it does copy the list from the source args to a
temporary buffer that is freed when the calling function returns.
This commit is contained in:
Bill Currie 2020-04-01 19:48:05 +09:00
parent a36f72f7b4
commit 535a2363bb
8 changed files with 89 additions and 3 deletions

View File

@ -49,6 +49,8 @@ void RUA_Obj_Init (struct progs_s *pr, int secure);
void RUA_Plist_Init (struct progs_s *pr, int secure);
void RUA_Runtime_Init (struct progs_s *pr, int secure);
void RUA_Script_Init (progs_t *pr, int secure);
void RUA_Set_Init (progs_t *pr, int secure);

View File

@ -18,4 +18,4 @@ libQFruamoko_la_SOURCES= \
pr_cmds.c \
rua_cbuf.c rua_cmd.c rua_cvar.c rua_hash.c rua_init.c \
rua_math.c rua_msgbuf.c rua_obj.c rua_plist.c rua_qfile.c rua_qfs.c \
rua_script.c rua_set.c rua_string.c
rua_runtime.c rua_script.c rua_set.c rua_string.c

View File

@ -45,6 +45,7 @@ static void (*init_funcs[])(progs_t *, int) = {
RUA_Plist_Init,
RUA_QFile_Init,
RUA_QFS_Init,
RUA_Runtime_Init,
RUA_Script_Init,
RUA_Set_Init,
RUA_String_Init,

View File

@ -0,0 +1,76 @@
/*
bi_runtime.c
QuakeC runtime api
Copyright (C) 2020 Bill Currie
Author: Bill Currie <bill@taniwha.org>
Date: 2020/4/1
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "qfalloca.h"
#if defined(_WIN32) && defined(HAVE_MALLOC_H)
#include <malloc.h>
#endif
#include "QF/progs.h"
#include "rua_internal.h"
static void
bi_va_copy (progs_t *pr)
{
__auto_type src_args = (pr_va_list_t *) &P_POINTER (pr, 0);
__auto_type src_list = &G_STRUCT (pr, pr_type_t, src_args->list);
size_t parm_size = pr->pr_param_size * sizeof(pr_type_t);
size_t size = src_args->count * parm_size;
string_t dst_list_block = PR_AllocTempBlock (pr, size);
__auto_type dst_list = (pr_type_t *) PR_GetString (pr, dst_list_block);
memcpy (dst_list, src_list, size);
R_PACKED (pr, pr_va_list_t).count = src_args->count;
R_PACKED (pr, pr_va_list_t).list = PR_SetPointer (pr, dst_list);
}
static builtin_t builtins[] = {
{"va_copy", bi_va_copy, -1},
{0}
};
void
RUA_Runtime_Init (progs_t *pr, int secure)
{
PR_RegisterBuiltins (pr, builtins);
}

View File

@ -100,6 +100,10 @@ typedef enum {
@extern void *PR_FindGlobal (string name); //FIXME where?
// copies the list in src to a temporary buffer that will be freed when the
// calling function returns, and places the pointer to the buffer into the
// returned va_list. The count is copies as-is.
@extern @va_list va_copy(@va_list src);
#endif //__ruamoko_runtime_h_
/**
\}

View File

@ -36,7 +36,7 @@ r_depfiles_remade=
libr_a_SOURCES=\
cbuf.r cmd.r cvar.r hash.r msgbuf.r plist.r qfile.r qfs.r script.r \
sound.r string.r math.r types.r \
sound.r string.r math.r types.r va_list.r \
obj_forward.r \
Object.r Protocol.r \
AutoreleasePool.r Array.r Array+Private.r Entity.r PropertyList.r Set.r

3
ruamoko/lib/va_list.r Normal file
View File

@ -0,0 +1,3 @@
#include <runtime.h>
@va_list va_copy(@va_list src) = #0;

View File

@ -239,7 +239,7 @@ updateScreenCursor (View *view)
{
pos.x += xpos;
pos.y += ypos;
[textContext mvvprintf: pos, fmt, @args];
[textContext mvvprintf: pos, fmt, va_copy (@args)];
}
- (void) mvvprintf: (Point) pos, string fmt, @va_list args