summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--ast.c6
-rw-r--r--ast.h6
-rw-r--r--eval.c6
-rw-r--r--lib.c4
-rw-r--r--lib/fs.c65
-rw-r--r--lib/ubus.c14
-rw-r--r--module.h11
7 files changed, 54 insertions, 58 deletions
diff --git a/ast.c b/ast.c
index 8f5abc0..cb6f438 100644
--- a/ast.c
+++ b/ast.c
@@ -174,7 +174,7 @@ obj_free(struct json_object *v, void *ud)
}
struct json_object *
-ut_new_object(struct ut_state *s, struct json_object *proto) {
+ut_new_object(struct json_object *proto) {
struct json_object *val = json_object_new_object();
struct ut_op *op;
@@ -228,7 +228,7 @@ func_to_string(struct json_object *v, struct printbuf *pb, int level, int flags)
}
struct json_object *
-ut_new_func(struct ut_state *s, struct ut_op *decl)
+ut_new_func(struct ut_op *decl)
{
struct json_object *val = json_object_new_object();
struct ut_op *op;
@@ -391,7 +391,7 @@ ut_extended_type_free(struct json_object *v, void *ud)
}
struct json_object *
-ut_set_extended_type(struct ut_state *s, struct json_object *v, struct json_object *proto, const char *name, void *data)
+ut_set_extended_type(struct json_object *v, struct json_object *proto, const char *name, void *data)
{
struct ut_extended_type *et = NULL;
struct ut_op *op;
diff --git a/ast.h b/ast.h
index 9bb6696..9840ad4 100644
--- a/ast.h
+++ b/ast.h
@@ -113,13 +113,13 @@ uint32_t ut_append_op(struct ut_state *s, uint32_t a, uint32_t b);
enum ut_error_type ut_parse(struct ut_state *s, const char *expr);
void ut_free(struct ut_state *s);
-struct json_object *ut_new_func(struct ut_state *s, struct ut_op *decl);
-struct json_object *ut_new_object(struct ut_state *s, struct json_object *proto);
+struct json_object *ut_new_func(struct ut_op *decl);
+struct json_object *ut_new_object(struct json_object *proto);
struct json_object *ut_new_double(double v);
struct json_object *ut_new_null(void);
bool ut_register_extended_type(const char *name, void (*freefn)(void *));
-struct json_object *ut_set_extended_type(struct ut_state *s, struct json_object *v, struct json_object *proto, const char *name, void *data);
+struct json_object *ut_set_extended_type(struct json_object *v, struct json_object *proto, const char *name, void *data);
void **ut_get_extended_type(struct json_object *val, const char *name);
void *ParseAlloc(void *(*mfunc)(size_t));
diff --git a/eval.c b/eval.c
index ab5448a..5bf44ef 100644
--- a/eval.c
+++ b/eval.c
@@ -193,7 +193,7 @@ ut_addscope(struct ut_state *state, uint32_t decl)
state->stack.size++;
}
- scope = ut_new_object(state, NULL);
+ scope = ut_new_object(NULL);
if (!scope)
return ut_exception(state, decl, UT_ERRMSG_OOM);
@@ -832,7 +832,7 @@ ut_execute_list(struct ut_state *state, uint32_t off)
static struct json_object *
ut_execute_object(struct ut_state *state, uint32_t off)
{
- struct json_object *obj = ut_new_object(state, NULL);
+ struct json_object *obj = ut_new_object(NULL);
struct ut_op *key, *val;
if (!obj)
@@ -1211,7 +1211,7 @@ static struct json_object *
ut_execute_function(struct ut_state *state, uint32_t off)
{
struct ut_op *op = ut_get_op(state, off);
- struct json_object *obj = ut_new_func(state, op);
+ struct json_object *obj = ut_new_func(op);
if (!obj)
return ut_exception(state, off, UT_ERRMSG_OOM);
diff --git a/lib.c b/lib.c
index f102505..9dfeb1e 100644
--- a/lib.c
+++ b/lib.c
@@ -278,7 +278,7 @@ ut_c_fn_free(struct json_object *v, void *ud)
}
static bool
-ut_register_function(struct ut_state *state, struct json_object *scope, const char *name, ut_c_fn *fn)
+ut_register_function(struct json_object *scope, const char *name, ut_c_fn *fn)
{
struct json_object *val = json_object_new_object();
struct ut_op *op;
@@ -1819,5 +1819,5 @@ ut_lib_init(struct ut_state *state, struct json_object *scope)
int i;
for (i = 0; i < sizeof(functions) / sizeof(functions[0]); i++)
- ut_register_function(state, scope, functions[i].name, functions[i].func);
+ ut_register_function(scope, functions[i].name, functions[i].func);
}
diff --git a/lib/fs.c b/lib/fs.c
index b3fb5fc..7b9a15c 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -221,7 +221,7 @@ ut_fs_popen(struct ut_state *s, uint32_t off, struct json_object *args)
err_return(ENOMEM);
}
- return ops->set_type(s, fo, proc_proto, "fs.proc", fp);
+ return ops->set_type(fo, proc_proto, "fs.proc", fp);
}
@@ -328,7 +328,7 @@ ut_fs_open(struct ut_state *s, uint32_t off, struct json_object *args)
err_return(ENOMEM);
}
- return ops->set_type(s, fo, file_proto, "fs.file", fp);
+ return ops->set_type(fo, file_proto, "fs.file", fp);
}
@@ -423,7 +423,7 @@ ut_fs_opendir(struct ut_state *s, uint32_t off, struct json_object *args)
err_return(ENOMEM);
}
- return ops->set_type(s, diro, dir_proto, "fs.dir", dp);
+ return ops->set_type(diro, dir_proto, "fs.dir", dp);
}
static struct json_object *
@@ -622,8 +622,28 @@ ut_fs_unlink(struct ut_state *s, uint32_t off, struct json_object *args)
return json_object_new_boolean(true);
}
+static const struct { const char *name; ut_c_fn *func; } proc_fns[] = {
+ { "read", ut_fs_pread },
+ { "write", ut_fs_pwrite },
+ { "close", ut_fs_pclose },
+};
+
+static const struct { const char *name; ut_c_fn *func; } file_fns[] = {
+ { "read", ut_fs_read },
+ { "write", ut_fs_write },
+ { "seek", ut_fs_seek },
+ { "tell", ut_fs_tell },
+ { "close", ut_fs_close },
+};
-static const struct { const char *name; ut_c_fn *func; } functions[] = {
+static const struct { const char *name; ut_c_fn *func; } dir_fns[] = {
+ { "read", ut_fs_readdir },
+ { "seek", ut_fs_seekdir },
+ { "tell", ut_fs_telldir },
+ { "close", ut_fs_closedir },
+};
+
+static const struct { const char *name; ut_c_fn *func; } global_fns[] = {
{ "error", ut_fs_error },
{ "open", ut_fs_open },
{ "opendir", ut_fs_opendir },
@@ -652,40 +672,17 @@ static void close_dir(void *ud) {
void ut_module_init(const struct ut_ops *ut, struct ut_state *s, struct json_object *scope)
{
- int i;
-
ops = ut;
ops->register_type("fs.proc", close_proc);
ops->register_type("fs.file", close_file);
ops->register_type("fs.dir", close_dir);
- for (i = 0; i < ARRAY_SIZE(functions); i++)
- ops->register_function(s, scope, functions[i].name, functions[i].func);
-
- proc_proto = ops->new_object(s, NULL);
+ proc_proto = ops->new_object(NULL);
+ file_proto = ops->new_object(NULL);
+ dir_proto = ops->new_object(NULL);
- if (proc_proto) {
- ops->register_function(s, proc_proto, "read", ut_fs_pread);
- ops->register_function(s, proc_proto, "write", ut_fs_pwrite);
- ops->register_function(s, proc_proto, "close", ut_fs_pclose);
- }
-
- file_proto = ops->new_object(s, NULL);
-
- if (file_proto) {
- ops->register_function(s, file_proto, "read", ut_fs_read);
- ops->register_function(s, file_proto, "write", ut_fs_write);
- ops->register_function(s, file_proto, "seek", ut_fs_seek);
- ops->register_function(s, file_proto, "tell", ut_fs_tell);
- ops->register_function(s, file_proto, "close", ut_fs_close);
- }
-
- dir_proto = ops->new_object(s, NULL);
-
- if (dir_proto) {
- ops->register_function(s, dir_proto, "read", ut_fs_readdir);
- ops->register_function(s, dir_proto, "seek", ut_fs_seekdir);
- ops->register_function(s, dir_proto, "tell", ut_fs_telldir);
- ops->register_function(s, dir_proto, "close", ut_fs_closedir);
- }
+ register_functions(ops, global_fns, scope);
+ register_functions(ops, proc_fns, proc_proto);
+ register_functions(ops, file_fns, file_proto);
+ register_functions(ops, dir_fns, dir_proto);
}
diff --git a/lib/ubus.c b/lib/ubus.c
index 5c56787..55793a2 100644
--- a/lib/ubus.c
+++ b/lib/ubus.c
@@ -171,7 +171,7 @@ ut_ubus_connect(struct ut_state *s, uint32_t off, struct json_object *args)
ubus_add_uloop(c->ctx);
- return ops->set_type(s, co, conn_proto, "ubus.connection", c);
+ return ops->set_type(co, conn_proto, "ubus.connection", c);
}
static void
@@ -317,17 +317,11 @@ static void close_connection(void *ud) {
void ut_module_init(const struct ut_ops *ut, struct ut_state *s, struct json_object *scope)
{
- int i;
-
ops = ut;
ops->register_type("ubus.connection", close_connection);
- for (i = 0; i < ARRAY_SIZE(global_fns); i++)
- ops->register_function(s, scope, global_fns[i].name, global_fns[i].func);
-
- conn_proto = ops->new_object(s, NULL);
+ conn_proto = ops->new_object(NULL);
- if (conn_proto)
- for (i = 0; i < ARRAY_SIZE(global_fns); i++)
- ops->register_function(s, conn_proto, conn_fns[i].name, conn_fns[i].func);
+ register_functions(ops, global_fns, scope);
+ register_functions(ops, conn_fns, conn_proto);
}
diff --git a/module.h b/module.h
index 4787d60..5e73fd9 100644
--- a/module.h
+++ b/module.h
@@ -21,15 +21,20 @@
#include "lib.h"
struct ut_ops {
- bool (*register_function)(struct ut_state *, struct json_object *, const char *, ut_c_fn *);
+ bool (*register_function)(struct json_object *, const char *, ut_c_fn *);
bool (*register_type)(const char *, void (*)(void *));
- struct json_object *(*set_type)(struct ut_state *, struct json_object *, struct json_object *, const char *, void *);
+ struct json_object *(*set_type)(struct json_object *, struct json_object *, const char *, void *);
void **(*get_type)(struct json_object *, const char *);
- struct json_object *(*new_object)(struct ut_state *, struct json_object *);
+ struct json_object *(*new_object)(struct json_object *);
};
extern const struct ut_ops ut;
+#define register_functions(ops, functions, scope) \
+ if (scope) \
+ for (int i = 0; i < ARRAY_SIZE(functions); i++) \
+ ops->register_function(scope, functions[i].name, functions[i].func)
+
void ut_module_init(const struct ut_ops *, struct ut_state *, struct json_object *);
#endif /* __MODULE_H_ */