summaryrefslogtreecommitdiffhomepage
path: root/lib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-03-08 15:38:43 +0100
committerJo-Philipp Wich <jo@mein.io>2021-03-08 19:18:58 +0100
commite196ee6689d1bf082531319185feea134bf888af (patch)
tree6b20bddd7eb4586492c64b5a842b88bd0da97bb1 /lib.c
parent330f7fec6858de97fe762c272ca499764bc6b02b (diff)
lib: retain prototype when passing scopes to include()
Change uc_include() to retain the prototype of the given scope object when processing includes. Also change the default behaviour to register the current VM scope as prototype on the passed scope object so that included code has access to functions such as length(), print() etc. by default. To actually sandbox the included code, the new `proto()` function can be used to create a scope object with an empty prototype: `include(..., proto({ ... }, {}))` Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/lib.c b/lib.c
index 5e4c0a0..536fcd7 100644
--- a/lib.c
+++ b/lib.c
@@ -2125,6 +2125,7 @@ uc_include(uc_vm *vm, size_t nargs)
json_object *rv = NULL;
uc_closure *closure = NULL;
uc_prototype *sc;
+ bool put = false;
size_t i;
char *p;
@@ -2162,8 +2163,12 @@ uc_include(uc_vm *vm, size_t nargs)
return NULL;
}
- if (scope) {
- sc = uc_prototype_new(NULL);
+ if (uc_object_is_type(scope, UC_OBJ_PROTOTYPE)) {
+ sc = uc_object_as_prototype(scope);
+ }
+ else if (scope) {
+ sc = uc_prototype_new(vm->globals);
+ put = true;
json_object_object_foreach(scope, key, val)
json_object_object_add(sc->header.jso, key, uc_value_get(val));
@@ -2177,7 +2182,7 @@ uc_include(uc_vm *vm, size_t nargs)
free(p);
- if (scope)
+ if (put)
uc_value_put(sc->header.jso);
return NULL;