Newer
Older
Tardis / lang / object.h
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 John Watts and the LuminaSensum contributors

#ifndef OBJECT_H
#define OBJECT_H

#include "types.h"

// Dispatchable object call
struct object_call {
	const char *name;
	void (*handler)(VmState state, Object obj, int priv);
	int priv;
};

// Object class shared between objects
struct object_class {
	void (*cleanup)(VmState state, Object obj);
	struct object_call
		*calls; // Points to array terminated by call with NULL name
};

// Creates an object with a class and allocated private data
Object object_create(VmState state, struct object_class *class, int priv_size);

// Retrieves private data for an object with a specified class
char *object_priv(VmState state, Object object, struct object_class *class);

// Creates a placeholder object for use when no object is present
Object object_none(void);

// Adds a reference to an object
void object_hold(VmState state, Object obj);

// Drops a reference to an object, possibly cleaning up the object
// Sets objptr to object_none
void object_drop(VmState state, Object *objptr);

// Calls a method on an object
void dispatch_call(VmState state, Object obj, const char *name);

// Creates a list of objects with a specific length
// All objects are initialized to object_none
ObjectList object_list_create(VmState state, int length);

// Gets the length of an object list
int object_list_length(VmState state, ObjectList list);

// Sets an element in the object list
// The object's reference is taken from the caller
void object_list_set(VmState state, ObjectList list, int index, Object obj);

// Gets an element from the object list
// A new reference is created for the caller
Object object_list_get(VmState state, ObjectList list, int index);

// Frees a list of objects
// This doesn't drop any references to objects
// Sets list to NULL
void object_list_free(VmState state, ObjectList *list);

#endif