Skip to content

Commit 1518ecd

Browse files
committed
make function arity exclude optional params
1 parent 686399c commit 1518ecd

6 files changed

Lines changed: 6 additions & 14 deletions

File tree

compiler.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,8 +1576,7 @@ static int compile_const_function(struct cstate *state, size_t **free_vars_out,
15761576

15771577
func_out->code = create_code(&inner_state);
15781578
func_out->name = name_id;
1579-
func_out->arity = num_params;
1580-
func_out->num_opt_params = num_opt_params;
1579+
func_out->arity = num_params - num_opt_params;
15811580

15821581
scope_free(inner_fn_state.scope);
15831582

constant.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ struct cb_value cb_const_to_value(const struct cb_const *const_)
136136
}
137137
for (int i = 0; i < const_func->code->nupvalues; i += 1)
138138
func->value.as_user.upvalues[i] = NULL;
139-
func->value.as_user.num_opt_params = const_func->num_opt_params;
140139

141140
ret.type = CB_VALUE_FUNCTION;
142141
ret.val.as_function = func;

constant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct cb_const_struct {
5858
};
5959

6060
struct cb_const_user_function {
61-
size_t name, arity, num_opt_params;
61+
size_t name, arity;
6262
struct cb_code *code;
6363
};
6464

eval.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ static CB_INLINE int do_call(unsigned short num_args, struct cb_frame *frame,
143143
struct cb_value func_val;
144144
struct cb_function *func;
145145
int failed;
146-
int num_opt_params;
147146

148147
func_val = *(*sp - num_args - 1);
149148

@@ -156,10 +155,7 @@ static CB_INLINE int do_call(unsigned short num_args, struct cb_frame *frame,
156155
}
157156

158157
func = func_val.val.as_function;
159-
num_opt_params = func->type == CB_FUNCTION_USER
160-
? func->value.as_user.num_opt_params
161-
: 0;
162-
if (func->arity - num_opt_params > num_args) {
158+
if (func->arity > num_args) {
163159
cb_str s = cb_agent_get_string(func->name);
164160
struct cb_value err;
165161
cb_value_from_fmt(&err, "Too few arguments to function '%s'\n",
@@ -182,9 +178,9 @@ static CB_INLINE int do_call(unsigned short num_args, struct cb_frame *frame,
182178
next_frame.num_args = num_args;
183179
next_frame.module_id = cb_modspec_id(code->modspec);
184180
next_frame.code = code;
185-
next_frame.bp = *sp - *stack - num_args - 1;
181+
next_frame.bp = old_sp - num_args - 1;
186182

187-
ensure_stack(code->stack_size, *sp - *stack);
183+
ensure_stack(code->stack_size, old_sp);
188184
failed = cb_eval(&next_frame);
189185
}
190186

tests/test_iteration.cb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,5 @@ export function test_peekable() {
7979
test::assert(iter::next(it) == 2, "Expected next to be 2");
8080

8181
test::assert(iter::next(it) == 3, "Expected next to be null");
82-
println("PEEK", it.peek());
8382
test::assert(it.peek() == null, "Expected peek to be null");
84-
}
83+
}

value.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ struct cb_code;
6262
struct cb_user_function {
6363
struct cb_code *code;
6464
struct cb_upvalue **upvalues;
65-
size_t num_opt_params;
6665
};
6766

6867
struct cb_function {

0 commit comments

Comments
 (0)