Files
Chloride/src/runtime/objects/functions/functions.c
2025-08-09 06:40:16 +01:00

50 lines
1.9 KiB
C

/*
* SPDX-FileCopyrightText: 2025 William Bell
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "../../runtime.h"
#include "../object.h"
#include "../string/string.h"
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
ArgonObject *ARGON_FUNCTION_TYPE = NULL;
ArgonObject *create_argon_native_function(char*name, native_fn native_fn) {
ArgonObject *object = new_object();
add_field(object, "__class__", ARGON_FUNCTION_TYPE);
object->type = TYPE_NATIVE_FUNCTION;
add_field(object, "__name__", new_string_object(name, strlen(name)));
object->value.native_fn = native_fn;
return object;
}
void load_argon_function(Translated *translated, RuntimeState *state,
struct Stack *stack) {
ArgonObject *object = new_object();
add_field(object, "__class__", ARGON_FUNCTION_TYPE);
object->type = TYPE_FUNCTION;
uint64_t offset = pop_bytecode(translated, state);
uint64_t length = pop_bytecode(translated, state);
add_field(object, "__name__", new_string_object(arena_get(&translated->constants, offset), length));
object->value.argon_fn.translated = *translated;
object->value.argon_fn.number_of_parameters = pop_bytecode(translated, state);
object->value.argon_fn.parameters =
ar_alloc(object->value.argon_fn.number_of_parameters * sizeof(struct string_struct));
for (size_t i = 0; i < object->value.argon_fn.number_of_parameters; i++) {
offset = pop_bytecode(translated, state);
length = pop_bytecode(translated, state);
object->value.argon_fn.parameters[i].data = arena_get(&translated->constants, offset);
object->value.argon_fn.parameters[i].length = length;
}
offset = pop_bytecode(translated, state);
length = pop_bytecode(translated, state);
object->value.argon_fn.bytecode = arena_get(&translated->constants, offset);
object->value.argon_fn.bytecode_length = length;
object->value.argon_fn.stack = stack;
state->registers[0]=object;
}