summaryrefslogtreecommitdiff
path: root/uscript/val.c
diff options
context:
space:
mode:
Diffstat (limited to 'uscript/val.c')
-rw-r--r--uscript/val.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/uscript/val.c b/uscript/val.c
index 77f7753..f204e20 100644
--- a/uscript/val.c
+++ b/uscript/val.c
@@ -68,6 +68,16 @@ struct us_func *create_func(struct us_proto *proto)
return func;
}
+struct us_cfunc *create_cfunc(struct us_str *name, us_cfunc_sig func, int argc)
+{
+ struct us_cfunc *cfunc = mem_alloc(sizeof(struct us_cfunc));
+ cfunc->name = name;
+ cfunc->c = func;
+ cfunc->argc = argc;
+ init_obj(wrap_cfunc(cfunc), &cfunc->header);
+ return cfunc;
+}
+
struct us_upval *create_upval(struct us_val *val)
{
struct us_upval *upval = mem_alloc(sizeof(struct us_upval));
@@ -106,6 +116,7 @@ void free_val(struct us_val v)
mem_free(func);
break;
}
+ case VAL_CFUNC:
case VAL_UPVAL:
mem_free(get_obj(v));
break;
@@ -140,6 +151,7 @@ bool vals_eql(struct us_val a, struct us_val b)
}
case VAL_ARR:
case VAL_FUNC:
+ case VAL_CFUNC:
case VAL_UPVAL:
case VAL_PROTO: return get_obj(a) == get_obj(b);
}
@@ -219,6 +231,27 @@ char *val_to_str(struct us_val v, int *len_out)
}
case VAL_FUNC:
return val_to_str(wrap_proto(get_func(v)->proto), len_out);
+ case VAL_CFUNC: {
+ const struct us_cfunc *cfunc = get_cfunc(v);
+ int len = snprintf(
+ NULL,
+ 0,
+ STR_FUNC_FMT,
+ cfunc->name->chars,
+ (void*)cfunc
+ );
+ char *str = mem_alloc(sizeof(char) * (len + 1));
+ snprintf(
+ str,
+ len + 1,
+ STR_FUNC_FMT,
+ cfunc->name->chars,
+ (void*)cfunc
+ );
+ if (len_out)
+ *len_out = len;
+ return str;
+ }
case VAL_UPVAL:
return val_to_str(*get_upval(v)->loc, len_out);
}