aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-10-27 19:33:28 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-10-27 19:33:28 -0400
commit71d6787541e829781e17d90f6b57eabcb17fb8a9 (patch)
treed73e8646a66593c23fe4d4d8c1e951e9a46ca63b /compile.c
parent0d615443dc452f85f3d6b1b2c82d92a9c8db1fff (diff)
Bugfix: for array:find() and array:sorted() and array:binary_search(),
do an ARRAY_COPY() if a user closure is being passed in, because the closure could mutate the array and the semantics of those functions should be to return information based on a snapshot
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/compile.c b/compile.c
index d8cbf8b4..3f8a8d3a 100644
--- a/compile.c
+++ b/compile.c
@@ -2669,7 +2669,9 @@ CORD compile(env_t *env, ast_t *ast)
(void)compile_arguments(env, ast, NULL, call->args);
return CORD_all("Array$shuffled(", self, ", ", padded_item_size, ")");
} else if (streq(call->name, "sort") || streq(call->name, "sorted")) {
- CORD self = compile_to_pointer_depth(env, call->self, streq(call->name, "sort") ? 1 : 0, false);
+ CORD self = streq(call->name, "sort")
+ ? compile_to_pointer_depth(env, call->self, 1, false)
+ : compile_to_pointer_depth(env, call->self, 0, call->args != NULL);
CORD comparison;
if (call->args) {
type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
@@ -2720,7 +2722,7 @@ CORD compile(env_t *env, ast_t *ast)
CORD arg_code = compile_arguments(env, ast, arg_spec, call->args);
return CORD_all("Array$heap_pop_value(", self, ", ", arg_code, ", ", padded_item_size, ", ", compile_type(item_t), ")");
} else if (streq(call->name, "binary_search")) {
- CORD self = compile_to_pointer_depth(env, call->self, 0, false);
+ CORD self = compile_to_pointer_depth(env, call->self, 0, call->args != NULL);
type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)),
.ret=Type(IntType, .bits=TYPE_IBITS32));
@@ -2742,7 +2744,7 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_all("Array$find_value(", self, ", ", compile_arguments(env, ast, arg_spec, call->args),
", ", compile_type_info(env, self_value_t), ")");
} else if (streq(call->name, "first")) {
- CORD self = compile_to_pointer_depth(env, call->self, 0, false);
+ CORD self = compile_to_pointer_depth(env, call->self, 0, call->args != NULL);
type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
type_t *predicate_type = Type(
ClosureType, .fn=Type(FunctionType, .args=new(arg_t, .name="item", .type=item_ptr), .ret=Type(BoolType)));