From 535a2363bb464b92cc41c232380a5e91feeb4096 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 1 Apr 2020 19:48:05 +0900 Subject: [PATCH] [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. --- include/rua_internal.h | 2 + libs/ruamoko/Makefile.am | 2 +- libs/ruamoko/rua_init.c | 1 + libs/ruamoko/rua_runtime.c | 76 ++++++++++++++++++++++++++++++++++++++ ruamoko/include/runtime.h | 4 ++ ruamoko/lib/Makefile.am | 2 +- ruamoko/lib/va_list.r | 3 ++ ruamoko/qwaq/ui/view.r | 2 +- 8 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 libs/ruamoko/rua_runtime.c create mode 100644 ruamoko/lib/va_list.r diff --git a/include/rua_internal.h b/include/rua_internal.h index 158d6e477..7bde98a77 100644 --- a/include/rua_internal.h +++ b/include/rua_internal.h @@ -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); diff --git a/libs/ruamoko/Makefile.am b/libs/ruamoko/Makefile.am index 15d4c4360..2d4777c14 100644 --- a/libs/ruamoko/Makefile.am +++ b/libs/ruamoko/Makefile.am @@ -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 diff --git a/libs/ruamoko/rua_init.c b/libs/ruamoko/rua_init.c index 7741405a5..6e4333998 100644 --- a/libs/ruamoko/rua_init.c +++ b/libs/ruamoko/rua_init.c @@ -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, diff --git a/libs/ruamoko/rua_runtime.c b/libs/ruamoko/rua_runtime.c new file mode 100644 index 000000000..697fc720b --- /dev/null +++ b/libs/ruamoko/rua_runtime.c @@ -0,0 +1,76 @@ +/* + bi_runtime.c + + QuakeC runtime api + + Copyright (C) 2020 Bill Currie + + Author: Bill Currie + 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 +#endif +#ifdef HAVE_STRINGS_H +# include +#endif +#include + +#include "qfalloca.h" + +#if defined(_WIN32) && defined(HAVE_MALLOC_H) +#include +#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); +} diff --git a/ruamoko/include/runtime.h b/ruamoko/include/runtime.h index 80e0aff7e..276d270fb 100644 --- a/ruamoko/include/runtime.h +++ b/ruamoko/include/runtime.h @@ -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_ /** \} diff --git a/ruamoko/lib/Makefile.am b/ruamoko/lib/Makefile.am index 2f67f04fd..cf0e50154 100644 --- a/ruamoko/lib/Makefile.am +++ b/ruamoko/lib/Makefile.am @@ -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 diff --git a/ruamoko/lib/va_list.r b/ruamoko/lib/va_list.r new file mode 100644 index 000000000..399e05933 --- /dev/null +++ b/ruamoko/lib/va_list.r @@ -0,0 +1,3 @@ +#include + +@va_list va_copy(@va_list src) = #0; diff --git a/ruamoko/qwaq/ui/view.r b/ruamoko/qwaq/ui/view.r index 31005878f..579f804c1 100644 --- a/ruamoko/qwaq/ui/view.r +++ b/ruamoko/qwaq/ui/view.r @@ -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