1 #ifndef _INCLUDE_SOURCEPAWN_VM_API_H_
2 #define _INCLUDE_SOURCEPAWN_VM_API_H_
5 #include "sp_vm_types.h"
6 #include "sp_vm_context.h"
13 * Contains helper functions used by VMs and the host app
15 class ISourcePawnEngine
19 * Loads a named file from a file pointer.
20 * Using this means base memory will be allocated by the VM.
22 * @param fp File pointer. May be at any offset. Not closed on return.
23 * @param err Optional error code pointer.
24 * @return A new plugin structure.
26 virtual sp_plugin_t *LoadFromFilePointer(FILE *fp, int *err) =0;
29 * Loads a file from a base memory address.
31 * @param base Base address of the plugin's memory region.
32 * @param plugin If NULL, a new plugin pointer is returned.
33 * Otherwise, the passed pointer is used.
34 * @param err Optional error code pointer.
35 * @return The resulting plugin pointer.
37 virtual sp_plugin_t *LoadFromMemory(void *base, sp_plugin_t *plugin, int *err) =0;
40 * Frees all of the memory associated with a plugin file.
41 * If allocated using SP_LoadFromMemory, the base and plugin pointer
42 * itself are not freed (so this may end up doing nothing).
44 virtual int FreeFromMemory(sp_plugin_t *plugin) =0;
47 * Creates a new IContext from a context handle.
49 * @param ctx Context to use as a basis for the IPluginContext.
50 * @return New IPluginContext handle.
52 virtual IPluginContext *CreateBaseContext(sp_context_t *ctx) =0;
57 * @param ctx Context pointer to free.
59 virtual void FreeBaseContext(IPluginContext *ctx) =0;
62 * Allocates large blocks of temporary memory.
64 * @param size Size of memory to allocate.
65 * @return Pointer to memory, NULL if allocation failed.
67 virtual void *BaseAlloc(size_t size) =0;
70 * Frees memory allocated with BaseAlloc.
72 * @param mem Memory address to free.
74 virtual void BaseFree(void *memory) =0;
77 * Allocates executable memory.
79 * @param size Size of memory to allocate.
80 * @return Pointer to memory, NULL if allocation failed.
82 virtual void *ExecAlloc(size_t size) =0;
85 * Frees executable memory.
87 * @param mem Address to free.
89 virtual void ExecFree(void *address) =0;
95 virtual ~ICompilation() { };
102 * Returns the string name of a VM implementation.
104 virtual const char *GetVMName() =0;
107 * Begins a new compilation
109 * @param plugin Pointer to a plugin structure.
110 * @return New compilation pointer.
112 virtual ICompilation *StartCompilation(sp_plugin_t *plugin) =0;
115 * Sets a compilation option.
117 * @param co Pointer to a compilation.
118 * @param key Option key name.
119 * @param val Option value string.
120 * @return True if option could be set, false otherwise.
122 virtual bool SetCompilationOption(ICompilation *co, const char *key, const char *val) =0;
125 * Finalizes a compilation into a new sp_context_t.
126 * Note: This will free the ICompilation pointer.
128 * @param co Compilation pointer.
129 * @param err Filled with error code on exit.
130 * @return New plugin context.
132 virtual sp_context_t *CompileToContext(ICompilation *co, int *err) =0;
135 * Aborts a compilation and frees the ICompilation pointer.
137 * @param co Compilation pointer.
139 virtual void AbortCompilation(ICompilation *co) =0;
142 * Frees any internal variable usage on a context.
144 * @param ctx Context structure pointer.
146 virtual void FreeContext(sp_context_t *ctx) =0;
149 * Calls the "execute" function on a context.
151 * @param ctx Executes a function in a context.
152 * @param code_idx Index into the code section.
153 * @param result Pointer to store result in.
154 * @return Error code (if any).
156 virtual int ContextExecute(sp_context_t *ctx, uint32_t code_idx, cell_t *result) =0;
160 #endif //_INCLUDE_SOURCEPAWN_VM_API_H_