Skip to content

Commit

Permalink
vm: cleanup function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
LBCrion committed Dec 24, 2024
1 parent 65ed273 commit ea029c1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
21 changes: 15 additions & 6 deletions src/actionlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ static value_t action_exec_impl ( vm_t *vm, value_t p[], gint np )

static value_t action_function ( vm_t *vm, value_t p[], gint np )
{
GtkWidget *widget = NULL;
vm_function_t *func;
GtkWidget *widget;
window_t *win;
guint16 state;

Expand All @@ -51,12 +52,20 @@ static value_t action_function ( vm_t *vm, value_t p[], gint np )
widget = base_widget_from_id(value_get_string(p[0]));
}

widget = widget? widget : vm->widget;
win = IS_TASKBAR_ITEM(widget)? flow_item_get_source(widget) : vm->win;
state = action_state_build(vm->widget, vm->win);
widget = vm->widget;
win = vm->win;
state = vm->wstate;
vm->widget = widget? widget : vm->widget;
vm->win = IS_TASKBAR_ITEM(widget)? flow_item_get_source(widget) : vm->win;
vm->wstate = action_state_build(vm->widget, vm->win);

if( (func = vm_func_lookup(value_get_string(p[np-1]))) &&
(func->flags & VM_FUNC_USERDEFINED) )
vm_function_call(vm, func->ptr.code);

vm_run_user_defined(value_get_string(p[np-1]), widget, vm->event, win,
&state);
vm->widget = widget;
vm->win = win;
vm->wstate = state;

return value_na;
}
Expand Down
76 changes: 49 additions & 27 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ static value_t vm_pop ( vm_t *vm )
return val;
}

static void vm_stack_unwind ( vm_t *vm, gsize target )
{
value_t v1;

while(vm->stack->len>target)
{
v1 = vm_pop(vm);
value_free(v1);
}
}

static gboolean vm_op_binary ( vm_t *vm )
{
value_t v1, v2, result;
Expand Down Expand Up @@ -108,40 +119,55 @@ gint expr_vm_get_func_params ( vm_t *vm, value_t *params[] )
return np;
}

value_t vm_function_call ( vm_t *vm, GBytes *code )
{
guint8 *saved_code, *saved_ip;
gsize saved_len, saved_stack;

if(!code)
return value_na;

saved_code = vm->code;
saved_ip = vm->ip;
saved_len = vm->len;
saved_stack = vm->stack->len;

vm->code = (guint8 *)g_bytes_get_data(code, &vm->len);
vm_run(vm);

if(vm->expr)
vm->expr->vstate = TRUE;

vm->code = saved_code;
vm->ip = saved_ip;
vm->len = saved_len;
vm_stack_unwind(vm, saved_stack);

return vm_pop(vm);
}

static gboolean vm_function ( vm_t *vm )
{
vm_function_t *func;
value_t v1, result, *stack;
value_t v1, result;
guint8 np = *(vm->ip+1);
guint8 *saved_code, *saved_ip;
gsize saved_len;
gint i;

if(np>vm->stack->len)
return FALSE;
memcpy(&func, vm->ip+2, sizeof(gpointer));
result = value_na;
if(!(func->flags & VM_FUNC_USERDEFINED) && func->ptr.function)
{
stack = (value_t *)vm->stack->data + vm->stack->len - np;
result = func->ptr.function(vm, stack, np);
result = func->ptr.function(vm,
(value_t *)vm->stack->data + vm->stack->len - np, np);
if(vm->expr)
vm->expr->vstate |= !(func->flags & VM_FUNC_DETERMINISTIC);
}
else if((func->flags & VM_FUNC_USERDEFINED) && func->ptr.code)
{
saved_code = vm->code;
saved_ip = vm->ip;
saved_len = vm->len;

vm->code = (guint8 *)g_bytes_get_data(func->ptr.code, &vm->len);
vm_run(vm);
else if(func->flags & VM_FUNC_USERDEFINED)
result = vm_function_call(vm, func->ptr.code);
else
result = value_na;

vm->code = saved_code;
vm->ip = saved_ip;
vm->len = saved_len;
result = vm_pop(vm);
}
expr_dep_add(func->name, vm->expr);
if(np>1)
g_ptr_array_remove_range(vm->pstack, vm->pstack->len-np+1, np-1);
Expand Down Expand Up @@ -274,12 +300,10 @@ static value_t vm_free ( vm_t *vm )
g_message("stack too long");

if(vm->stack->len>0)
while(vm->stack->len>0)
{
v1=vm_pop(vm);
if(vm->stack->len)
value_free(v1);
}
{
vm_stack_unwind(vm, 1);
v1 = vm_pop(vm);
}

if(vm->expr)
vm->expr->stack_depth = MAX(vm->expr->stack_depth, vm->max_stack);
Expand Down Expand Up @@ -339,6 +363,4 @@ void vm_run_user_defined ( gchar *name, GtkWidget *widget, GdkEvent *event,
func = vm_func_lookup(name);
if(func->flags & VM_FUNC_USERDEFINED)
vm_run_action(func->ptr.code, widget, event, win, state);


}
1 change: 1 addition & 0 deletions src/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ gboolean parser_expr_parse ( GScanner *scanner, GByteArray *code );
gboolean parser_macro_add ( GScanner *scanner );
const gchar *parser_identifier_lookup ( gchar *identifier );
value_t vm_expr_eval ( expr_cache_t *expr );
value_t vm_function_call ( vm_t *vm, GBytes *code );
void vm_run_action ( GBytes *code, GtkWidget *w, GdkEvent *e, window_t *win,
guint16 *s);
void vm_run_user_defined ( gchar *action, GtkWidget *widget, GdkEvent *event,
Expand Down

0 comments on commit ea029c1

Please sign in to comment.