summaryrefslogtreecommitdiffhomepage
path: root/lib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-07-28 11:07:07 +0200
committerJo-Philipp Wich <jo@mein.io>2022-07-30 00:41:56 +0200
commit70ae3040fb384e7a77ef43ca6b426269f9acdcab (patch)
treecb318ac8ea5dd70598d07cee8d480ba6cff0eff5 /lib.c
parent3c104f5312604a6a1a0dd80528cc937159bc57ef (diff)
lib: honor constant flag of arrays
Reject modifications on array values with a type exception when the constant flag is set on the array operated upon. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/lib.c b/lib.c
index 4d37531..0dee073 100644
--- a/lib.c
+++ b/lib.c
@@ -369,6 +369,23 @@ uc_rindex(uc_vm_t *vm, size_t nargs)
return uc_index(vm, nargs, true);
}
+static bool
+assert_mutable_array(uc_vm_t *vm, uc_value_t *val)
+{
+ if (ucv_type(val) != UC_ARRAY)
+ return false;
+
+ if (ucv_is_constant(val)) {
+ uc_vm_raise_exception(vm, EXCEPTION_TYPE,
+ "%s value is immutable",
+ ucv_typename(val));
+
+ return false;
+ }
+
+ return true;
+}
+
static uc_value_t *
uc_push(uc_vm_t *vm, size_t nargs)
{
@@ -376,7 +393,7 @@ uc_push(uc_vm_t *vm, size_t nargs)
uc_value_t *item = NULL;
size_t arridx;
- if (ucv_type(arr) != UC_ARRAY)
+ if (!assert_mutable_array(vm, arr))
return NULL;
for (arridx = 1; arridx < nargs; arridx++) {
@@ -392,6 +409,9 @@ uc_pop(uc_vm_t *vm, size_t nargs)
{
uc_value_t *arr = uc_fn_arg(0);
+ if (!assert_mutable_array(vm, arr))
+ return NULL;
+
return ucv_array_pop(arr);
}
@@ -400,6 +420,9 @@ uc_shift(uc_vm_t *vm, size_t nargs)
{
uc_value_t *arr = uc_fn_arg(0);
+ if (!assert_mutable_array(vm, arr))
+ return NULL;
+
return ucv_array_shift(arr);
}
@@ -410,7 +433,7 @@ uc_unshift(uc_vm_t *vm, size_t nargs)
uc_value_t *item = NULL;
size_t i;
- if (ucv_type(arr) != UC_ARRAY)
+ if (!assert_mutable_array(vm, arr))
return NULL;
for (i = 1; i < nargs; i++) {
@@ -755,6 +778,9 @@ uc_reverse(uc_vm_t *vm, size_t nargs)
char *dup, *p;
if (ucv_type(obj) == UC_ARRAY) {
+ if (!assert_mutable_array(vm, obj))
+ return NULL;
+
rv = ucv_array_new(vm);
for (arridx = ucv_array_length(obj); arridx > 0; arridx--)
@@ -851,7 +877,7 @@ uc_sort(uc_vm_t *vm, size_t nargs)
uc_value_t *arr = uc_fn_arg(0);
uc_value_t *fn = uc_fn_arg(1);
- if (ucv_type(arr) != UC_ARRAY)
+ if (!assert_mutable_array(vm, arr))
return NULL;
sort_ctx.vm = vm;
@@ -870,7 +896,7 @@ uc_splice(uc_vm_t *vm, size_t nargs)
int64_t remlen = ucv_to_integer(uc_fn_arg(2));
size_t arrlen, addlen, idx;
- if (ucv_type(arr) != UC_ARRAY)
+ if (!assert_mutable_array(vm, arr))
return NULL;
arrlen = ucv_array_length(arr);