Newer
Older
Tardis / lang / object.h
// SPDX-License-Identifier: MIT
// Copyright (c) 2024 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;
};

// Function for dispatching an object call
typedef void (*object_caller)(VmState, Object, const char *);

// 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
	object_caller caller; // Optional custom dispatcher
};

// 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);

#endif