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

#ifndef MODULE_H
#define MODULE_H

#include "object.h"
#include "types.h"

// Finds a module by its name
// This internally loads the module and dependencies
// A new reference is created for the caller
Object module_find(VmState state, const char *name);

// Frees all modules loaded internally
// You must call this at the end of your program
void modules_free(VmState state);

// The following section is an internal API for use only with
// the compiler's C code generator. Do not use for regular C code.
#ifdef MODULE_INTERNAL_API

// Operations for each class call
#define OP_BYTECODE 0
#define OP_FIELDGET 1
#define OP_FIELDSET 2

// A call the module class can make
struct module_class_call {
	const char *name;
	const int op;
	const int index;
};

typedef struct module_class_call ModuleClassCall;

// A class in a module
struct module_class {
	const int fields_count;
	const unsigned char **bytecodes;
	const ModuleClassCall *calls;
};

typedef struct module_class ModuleClass;

// Data structure for creating a module
struct module_info {
	const char *name;
	const char **uses;
	const ModuleClass **classes;
	Object (*create)(VmState state, Object use_modules);
};

typedef struct module_info ModuleInfo;

// Gets another module from a module's runtime data
// Creates a new reference for the caller
Object module_runtime_use(VmState state, Object module, int index);

// Creates a class from a module
// Caller passes its module reference
Object module_class_create(VmState state, Object module, int index);

#endif

#endif