Skip to content

Commit

Permalink
vm: replace marks with a shadow stack
Browse files Browse the repository at this point in the history
  • Loading branch information
LBCrion committed Dec 17, 2024
1 parent e45f4b4 commit ecbabf5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
21 changes: 15 additions & 6 deletions src/actionlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,47 +298,56 @@ static value_t action_setbarvisibility ( vm_t *vm, value_t p[], gint np )
static value_t action_setvalue ( vm_t *vm, value_t p[], gint np )
{
GtkWidget *widget;
guint8 *mark;

vm_param_check_np_range(vm, np, 1, 2, "SetValue");
vm_param_check_string(vm, p, 0, "SetValue");

if(!vm->mark)
if(!vm->pstack->len)
return value_na;

mark = vm->pstack->pdata[vm->pstack->len-1];

if( (widget = np==2?base_widget_from_id(value_get_string(p[0])):vm->widget) )
base_widget_set_value(widget, vm->mark+1, vm->ip-vm->mark-1);
base_widget_set_value(widget, mark, vm->ip - mark);

return value_na;
}

static value_t action_setstyle ( vm_t *vm, value_t p[], gint np )
{
GtkWidget *widget;
guint8 *mark;

vm_param_check_np_range(vm, np, 1, 2, "SetValue");
vm_param_check_string(vm, p, 0, "SetValue");

if(!vm->mark)
if(!vm->pstack->len)
return value_na;

mark = vm->pstack->pdata[vm->pstack->len-1];

if( (widget = np==2?base_widget_from_id(value_get_string(p[0])):vm->widget) )
base_widget_set_style(widget, vm->mark+1, vm->ip-vm->mark-1);
base_widget_set_style(widget, mark, vm->ip - mark);

return value_na;
}

static value_t action_settooltip ( vm_t *vm, value_t p[], gint np )
{
GtkWidget *widget;
guint8 *mark;

vm_param_check_np_range(vm, np, 1, 2, "SetValue");
vm_param_check_string(vm, p, 0, "SetValue");

if(!vm->mark)
if(!vm->pstack->len)
return value_na;

mark = vm->pstack->pdata[vm->pstack->len-1];

if( (widget = np==2?base_widget_from_id(value_get_string(p[0])):vm->widget) )
base_widget_set_style(widget, vm->mark+1, vm->ip-vm->mark-1);
base_widget_set_tooltip(widget, mark, vm->ip - mark);

return value_na;
}
Expand Down
4 changes: 0 additions & 4 deletions src/vm/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "util/string.h"
#include "vm/vm.h"

static guint8 mark = EXPR_OP_MARK;
static GHashTable *macros;

static gint parser_emit_jump ( GByteArray *code, guint8 op )
Expand Down Expand Up @@ -135,7 +134,6 @@ static gboolean parser_function ( GScanner *scanner, GByteArray *code )
if(g_scanner_peek_next_token(scanner)!=')')
do
{
g_byte_array_append(code, &mark, 1);
if(!parser_expr_parse(scanner, code))
return FALSE;
np++;
Expand Down Expand Up @@ -402,7 +400,6 @@ GByteArray *parser_action_compat ( gchar *action, gchar *expr1, gchar *expr2,
{
scanner = parser_scanner_new();
g_scanner_input_text(scanner, expr1, strlen(expr1));
g_byte_array_append(code, &mark, 1);
if(!parser_expr_parse(scanner, code))
{
g_byte_array_free(code, TRUE);
Expand All @@ -415,7 +412,6 @@ GByteArray *parser_action_compat ( gchar *action, gchar *expr1, gchar *expr2,
{
scanner = parser_scanner_new();
g_scanner_input_text(scanner, expr2, strlen(expr2));
g_byte_array_append(code, &mark, 1);
if(!parser_expr_parse(scanner, code))
{
g_byte_array_free(code, TRUE);
Expand Down
16 changes: 13 additions & 3 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ static gboolean vm_op_binary ( vm_t *vm )
vm_push(vm, result);
value_free(v1);
value_free(v2);
g_ptr_array_remove_index(vm->pstack, vm->pstack->len-1);

return TRUE;
}
Expand Down Expand Up @@ -191,6 +192,10 @@ static gboolean vm_function ( vm_t *vm )
vm->expr->vstate = TRUE;
}
expr_dep_add(name, vm->expr);
if(np>1)
g_ptr_array_remove_range(vm->pstack, vm->pstack->len-np+1, np-1);
else if(!np)
g_ptr_array_add(vm->pstack, vm->ip);
vm->ip += sizeof(gpointer)+1;

for(i=0; i<np; i++)
Expand All @@ -213,13 +218,15 @@ static void vm_variable ( vm_t *vm )
expr_dep_add(name, vm->expr);

vm_push(vm, value);
g_ptr_array_add(vm->pstack, vm->ip);
vm->ip += sizeof(gpointer);
}

static void vm_immediate ( vm_t *vm )
{
value_t v1;

g_ptr_array_add(vm->pstack, vm->ip);
if(*(vm->ip+1) != EXPR_TYPE_STRING)
{
memcpy(&v1, vm->ip+1, sizeof(value_t));
Expand Down Expand Up @@ -247,7 +254,10 @@ static value_t vm_run ( vm_t *vm )

if(!vm->stack)
vm->stack = g_array_sized_new(FALSE, FALSE, sizeof(value_t),
MAX(1, vm->expr?vm->expr->stack_depth:1));
MAX(1, vm->expr? vm->expr->stack_depth : 1));
if(!vm->pstack)
vm->pstack = g_ptr_array_sized_new(
MAX(1, vm->expr? vm->expr->stack_depth: 1));

for(vm->ip = vm->code; (vm->ip-vm->code)<vm->len; vm->ip++)
{
Expand All @@ -260,15 +270,14 @@ static value_t vm_run ( vm_t *vm )
vm_variable(vm);
else if(*vm->ip == EXPR_OP_FUNCTION)
vm_function(vm);
else if(*vm->ip == EXPR_OP_MARK)
vm->mark = vm->ip;
else if(*vm->ip == EXPR_OP_JMP)
{
memcpy(&jmp, vm->ip+1, sizeof(gint));
vm->ip+=jmp+sizeof(gint);
}
else if(*vm->ip == EXPR_OP_JZ)
{
g_ptr_array_remove_index(vm->pstack, vm->pstack->len-1);
v1 = vm_pop(vm);
if(!value_is_numeric(v1) || !v1.value.numeric)
{
Expand Down Expand Up @@ -320,6 +329,7 @@ static value_t vm_free ( vm_t *vm )
vm->expr->stack_depth = MAX(vm->expr->stack_depth, vm->max_stack);

g_array_unref(vm->stack);
g_ptr_array_free(vm->pstack, TRUE);
g_free(vm);

return v1;
Expand Down
5 changes: 2 additions & 3 deletions src/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ enum expr_instruction_t {
EXPR_OP_JMP,
EXPR_OP_CACHED,
EXPR_OP_VARIABLE,
EXPR_OP_FUNCTION,
EXPR_OP_MARK
EXPR_OP_FUNCTION
};

typedef struct {
guint8 *ip;
guint8 *code;
guint8 *mark;
gsize len;
GArray *stack;
GPtrArray *pstack;
gint max_stack;
gboolean use_cached;
guint16 wstate;
Expand Down

0 comments on commit ecbabf5

Please sign in to comment.