// 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); // List of objects struct object_list { int length; Object elems[]; }; // Creates a list of objects // This doesn't hold any references to objects // All objects are initialized to object_none struct object_list *object_list_create(int length); // Frees a list of objects // This doesn't drop any references to objects // Sets list to NULL void object_list_free(struct object_list **list); #endif