Skip to content

Commit 222d014

Browse files
committed
COMMON: Allow modules to return objects with methods that take arguments
1 parent 5adc663 commit 222d014

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/common/var.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,3 +803,11 @@ void v_create_func(var_p_t map, const char *name, method cb) {
803803
v_func->v.fn.mcb = NULL;
804804
v_func->v.fn.id = 0;
805805
}
806+
807+
void v_create_callback(var_p_t map, const char *name, callback cb) {
808+
var_p_t v_func = map_add_var(map, name, 0);
809+
v_func->type = V_FUNC;
810+
v_func->v.fn.cb = NULL;
811+
v_func->v.fn.mcb = cb;
812+
v_func->v.fn.id = 0;
813+
}

src/common/var_eval.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
#include "common/var_eval.h"
1717
#include "common/plugins.h"
1818

19+
//
20+
// returns a temporary var that can exist in the calling scope
21+
//
22+
var_p_t v_get_tmp(var_p_t map) {
23+
var_p_t result = map_get(map, MAP_TMP_FIELD);
24+
if (result == NULL) {
25+
result = map_add_var(map, MAP_TMP_FIELD, 0);
26+
}
27+
return result;
28+
}
29+
1930
void v_eval_func(var_p_t self, var_p_t v_func, var_p_t result) {
2031
if (v_func->v.fn.cb != NULL) {
2132
// internal object method
@@ -185,11 +196,7 @@ var_t *code_get_map_element(var_t *map, var_t *field) {
185196
if (udf_rv.type != kwTYPE_RET) {
186197
err_stackmess();
187198
} else {
188-
// result must exist until processed in eval()
189-
var_p_t var = map_get(map, MAP_TMP_FIELD);
190-
if (var == NULL) {
191-
var = map_add_var(map, MAP_TMP_FIELD, 0);
192-
}
199+
var_p_t var = v_get_tmp(map);
193200
v_move(var, udf_rv.x.vdvar.vptr);
194201
v_detach(udf_rv.x.vdvar.vptr);
195202
result = var;
@@ -198,10 +205,7 @@ var_t *code_get_map_element(var_t *map, var_t *field) {
198205
} else if (field->type == V_ARRAY) {
199206
result = code_getvarptr_arridx(field);
200207
} else if (field->type == V_FUNC) {
201-
result = map_get(map, MAP_TMP_FIELD);
202-
if (result == NULL) {
203-
result = map_add_var(map, MAP_TMP_FIELD, 0);
204-
}
208+
result = v_get_tmp(map);
205209
v_init(result);
206210
v_eval_func(map, field, result);
207211
} else {

src/include/var.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,13 @@ int v_strlen(const var_t *v);
503503
*/
504504
void v_create_func(var_p_t map, const char *name, method cb);
505505

506+
/**
507+
* @ingroup var
508+
*
509+
* creates a callback method
510+
*/
511+
void v_create_callback(var_p_t map, const char *name, callback cb);
512+
506513
#if defined(__cplusplus)
507514
}
508515
#endif

0 commit comments

Comments
 (0)