public/sourcepawn/sp_vm_api.h
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 03:47:10 2007 +0000 (2007-02-16)
changeset 501 e209a7296936
parent 465 8856d2e7018d
child 525 d36c45088e36
permissions -rw-r--r--
removed this potential feature, not useful enough
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourcePawn, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ================================================================
     7  *
     8  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be 
     9  * used or modified under the Terms and Conditions of its License Agreement, 
    10  * which is found in public/licenses/LICENSE.txt.  As of this notice, derivative 
    11  * works must be licensed under the GNU General Public License (version 2 or 
    12  * greater).  A copy of the GPL is included under public/licenses/GPL.txt.
    13  * 
    14  * To view the latest information, see: http://www.sourcemod.net/license.php
    15  *
    16  * Version: $Id$
    17  */
    18 
    19 #ifndef _INCLUDE_SOURCEPAWN_VM_API_H_
    20 #define _INCLUDE_SOURCEPAWN_VM_API_H_
    21 
    22 /**
    23  * @file sp_vm_api.h
    24  * @brief Contains all of the object structures used in the SourcePawn API.
    25  */
    26 
    27 #include <stdio.h>
    28 #include "sp_vm_types.h"
    29 
    30 /** SourcePawn VM API Version */
    31 #define SOURCEPAWN_VM_API_VERSION		1
    32 
    33 #if defined SOURCEMOD_BUILD
    34 namespace SourceMod
    35 {
    36 	struct IdentityToken_t;
    37 };
    38 #endif
    39 
    40 namespace SourcePawn
    41 {
    42 	class IVirtualMachine;
    43 
    44 	#define SM_PARAM_COPYBACK		(1<<0)		/**< Copy an array/reference back after call */
    45 	#define SM_PARAM_STRING_UTF8	(1<<0)		/**< String should be UTF-8 handled */
    46 	#define SM_PARAM_STRING_COPY	(1<<1)		/**< String should be copied into the plugin */
    47 
    48 	/**
    49 	 * @brief Represents what a function needs to implement in order to be callable.
    50 	 */
    51 	class ICallable
    52 	{
    53 	public:
    54 		/**
    55 		 * @brief Pushes a cell onto the current call.
    56 		 *
    57 		 * @param cell		Parameter value to push.
    58 		 * @return			Error code, if any.
    59 		 */
    60 		virtual int PushCell(cell_t cell) =0;
    61 
    62 		/**
    63 		 * @brief Pushes a cell by reference onto the current call.
    64 		 * NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
    65 		 * NOTE: By reference parameters are cached and thus are not read until execution.
    66 		 *		 This means you cannot push a pointer, change it, and push it again and expect
    67 		 *       two different values to come out.
    68 		 *
    69 		 * @param cell		Address containing parameter value to push.
    70 		 * @param flags		Copy-back flags.
    71 		 * @return			Error code, if any.
    72 		 */
    73 		virtual int PushCellByRef(cell_t *cell, int flags) =0;
    74 
    75 		/**
    76 		 * @brief Pushes a float onto the current call.
    77 		 *
    78 		 * @param number	Parameter value to push.
    79 		 * @return			Error code, if any.
    80 		 */
    81 		virtual int PushFloat(float number) =0;
    82 
    83 		/**
    84 		 * @brief Pushes a float onto the current call by reference.
    85 		 * NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
    86 		 * NOTE: By reference parameters are cached and thus are not read until execution.
    87 		 *		 This means you cannot push a pointer, change it, and push it again and expect
    88 		 *       two different values to come out.
    89 		 *
    90 		 * @param number	Parameter value to push.
    91 		 & @param flags		Copy-back flags.
    92 		 * @return			Error code, if any.
    93 		 */
    94 		virtual int PushFloatByRef(float *number, int flags) =0;
    95 
    96 		/**
    97 		 * @brief Pushes an array of cells onto the current call.  
    98 		 * NOTE: On Execute, the pointer passed will be modified if non-NULL and copy-back
    99 		 * is enabled.
   100 		 * NOTE: By reference parameters are cached and thus are not read until execution.
   101 		 *		 This means you cannot push a pointer, change it, and push it again and expect
   102 		 *       two different values to come out.
   103 		 *
   104 		 * @param inarray	Array to copy, NULL if no initial array should be copied.
   105 		 * @param cells		Number of cells to allocate and optionally read from the input array.
   106 		 * @param phys_addr	Optional return address for physical array, if one was made.
   107 		 * @param flags		Whether or not changes should be copied back to the input array.
   108 		 * @return			Error code, if any.
   109 		 */
   110 		virtual int PushArray(cell_t *inarray, 
   111 								unsigned int cells, 
   112 								cell_t **phys_addr, 
   113 								int flags=0) =0;
   114 
   115 		/**
   116 		 * @brief Pushes a string onto the current call.
   117 		 *
   118 		 * @param string	String to push.
   119 		 * @return			Error code, if any.
   120 		 */
   121 		virtual int PushString(const char *string) =0;
   122 
   123 		/**
   124 		 * @brief Pushes a string or string buffer.
   125 		 * NOTE: On Execute, the pointer passed will be modified if copy-back is enabled.
   126 		 *
   127 		 * @param buffer	Pointer to string buffer.
   128 		 * @param length	Length of buffer.
   129 		 * @param sz_flags	String flags.
   130 		 * @param cp_flags	Copy-back flags.
   131 		 * @return			Error code, if any.
   132 		 */
   133 		virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags) =0;
   134 
   135 		/**
   136 		 * @brief Cancels a function call that is being pushed but not yet executed.
   137 		 * This can be used be reset for CallFunction() use.
   138 		 */
   139 		virtual void Cancel() =0;
   140 	};
   141 
   142 
   143 	/**
   144 	 * @brief Encapsulates a function call in a plugin.
   145 	 *
   146 	 * NOTE: Function calls must be atomic to one execution context.
   147 	 * NOTE: This object should not be deleted.  It lives for the lifetime of the plugin.
   148 	 */
   149 	class IPluginFunction : public ICallable
   150 	{
   151 	public:
   152 		/**
   153 		 * @brief Executes the forward, resets the pushed parameter list, and performs any copybacks.
   154 		 *
   155 		 * @param result		Pointer to store return value in.
   156 		 * @return				Error code, if any.
   157 		 */
   158 		virtual int Execute(cell_t *result) =0;
   159 
   160 		/**
   161 		 * @brief Executes the function with the given parameter array.
   162 		 * Parameters are read in forward order (i.e. index 0 is parameter #1)
   163 		 * NOTE: You will get an error if you attempt to use CallFunction() with
   164 		 * previously pushed parameters.
   165 		 *
   166 		 * @param params		Array of cell parameters.
   167 		 * @param num_params	Number of parameters to push.
   168 		 * @param result		Pointer to store result of function on return.
   169 		 * @return				SourcePawn error code (if any).
   170 		 */
   171 		virtual int CallFunction(const cell_t *params, unsigned int num_params, cell_t *result) =0;
   172 
   173 		/**
   174 		 * @brief Returns which plugin this function belongs to.
   175 		 *
   176 		 * @return				IPluginContext pointer to parent plugin.
   177 		 */
   178 		virtual IPluginContext *GetParentContext() =0;
   179 
   180 		/**
   181 		 * @brief Returns the physical address of a by-reference parameter.
   182 		 *
   183 		 * @param param			Parameter index to read (beginning at 0).
   184 		 * @return				Address, or NULL if invalid parameter specified.
   185 		 */
   186 		virtual cell_t *GetAddressOfPushedParam(unsigned int param) =0;
   187 	};
   188 
   189 
   190 	/**
   191 	 * @brief Interface to managing a debug context at runtime.
   192 	 */
   193 	class IPluginDebugInfo
   194 	{
   195 	public:
   196 		/**
   197 		 * @brief Given a code pointer, finds the file it is associated with.
   198 		 * 
   199 		 * @param addr		Code address offset.
   200 		 * @param filename	Pointer to store filename pointer in.
   201 		 */
   202 		virtual int LookupFile(ucell_t addr, const char **filename) =0;
   203 
   204 		/**
   205 		 * @brief Given a code pointer, finds the function it is associated with.
   206 		 *
   207 		 * @param addr		Code address offset.
   208 		 * @param name		Pointer to store function name pointer in.
   209 		 */
   210 		virtual int LookupFunction(ucell_t addr, const char **name) =0;
   211 
   212 		/**
   213 		 * @brief Given a code pointer, finds the line it is associated with.
   214 		 *
   215 		 * @param addr		Code address offset.
   216 		 * @param line		Pointer to store line number in.
   217 		 */
   218 		virtual int LookupLine(ucell_t addr, uint32_t *line) =0;
   219 	};
   220 
   221 	/**
   222 	 * @brief Interface to managing a context at runtime.
   223 	 */
   224 	class IPluginContext
   225 	{
   226 	public:
   227 		/** Virtual destructor */
   228 		virtual ~IPluginContext() { };
   229 	public:
   230 		/** 
   231 		 * @brief Returns the parent IVirtualMachine.
   232 		 *
   233 		 * @return				Parent virtual machine pointer.
   234 		 */
   235 		virtual IVirtualMachine *GetVirtualMachine() =0;
   236 
   237 		/**
   238 		 * @brief Returns the child sp_context_t structure.
   239 		 *
   240 		 * @return				Child sp_context_t structure.
   241 		 */
   242 		virtual sp_context_t *GetContext() =0;
   243 
   244 		/**
   245 		 * @brief Returns true if the plugin is in debug mode.
   246 		 *
   247 		 * @return				True if in debug mode, false otherwise.
   248 		 */
   249 		virtual bool IsDebugging() =0;
   250 
   251 		/**
   252 		 * @brief Installs a debug break and returns the old one, if any.
   253 		 * This will fail if the plugin is not debugging.
   254 		 *
   255 		 * @param newpfn		New function pointer.
   256 		 * @param oldpfn		Pointer to retrieve old function pointer.
   257 		 */
   258 		virtual int SetDebugBreak(SPVM_DEBUGBREAK newpfn, SPVM_DEBUGBREAK *oldpfn) =0;
   259 
   260 		/**
   261 		 * @brief Returns debug info.
   262 		 *
   263 		 * @return				IPluginDebugInfo, or NULL if no debug info found.
   264 		 */
   265 		virtual IPluginDebugInfo *GetDebugInfo() =0;
   266 
   267 		/**
   268 		 * @brief Allocates memory on the secondary stack of a plugin.
   269 		 * Note that although called a heap, it is in fact a stack.
   270 		 * 
   271 		 * @param cells			Number of cells to allocate.
   272 		 * @param local_addr	Will be filled with data offset to heap.
   273 		 * @param phys_addr		Physical address to heap memory.
   274 		 */
   275 		virtual int HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr) =0;
   276 
   277 		/**
   278 		 * @brief Pops a heap address off the heap stack.  Use this to free memory allocated with
   279 		 *  SP_HeapAlloc().  
   280 		 * Note that in SourcePawn, the heap is in fact a bottom-up stack.  Deallocations
   281 		 *  with this native should be performed in precisely the REVERSE order.
   282 		 *
   283 		 * @param local_addr	Local address to free.
   284  		 */
   285 		virtual int HeapPop(cell_t local_addr) =0;
   286 
   287 		/**
   288 		 * @brief Releases a heap address using a different method than SP_HeapPop().
   289 		 * This allows you to release in any order.  However, if you allocate N 
   290 		 *  objects, release only some of them, then begin allocating again, 
   291 		 *  you cannot go back and starting freeing the originals.  
   292 		 * In other words, for each chain of allocations, if you start deallocating,
   293 		 *  then allocating more in a chain, you must only deallocate from the current 
   294 		 *  allocation chain.  This is basically HeapPop() except on a larger scale.
   295 		 *
   296 		 * @param local_addr	Local address to free.
   297  		 */
   298 		virtual int HeapRelease(cell_t local_addr) =0;
   299 
   300 		/**
   301 		 * @brief Finds a native by name.
   302 		 *
   303 		 * @param name			Name of native.
   304 		 * @param index			Optionally filled with native index number.
   305 		 */
   306 		virtual int FindNativeByName(const char *name, uint32_t *index) =0;
   307 
   308 		/**
   309 		 * @brief Gets native info by index.
   310 		 *
   311 		 * @param index			Index number of native.
   312 		 * @param native		Optionally filled with pointer to native structure.
   313 		 */
   314 		virtual int GetNativeByIndex(uint32_t index, sp_native_t **native) =0;
   315 
   316 		/**
   317 		 * @brief Gets the number of natives.
   318 		 * 
   319 		 * @return				Filled with the number of natives.
   320 		 */
   321 		virtual uint32_t GetNativesNum() =0;
   322 
   323 		/**
   324 		 * @brief Finds a public function by name.
   325 		 *
   326 		 * @param name			Name of public
   327 		 * @param index			Optionally filled with public index number.
   328 		 */
   329 		virtual int FindPublicByName(const char *name, uint32_t *index) =0;
   330 
   331 		/**
   332 		 * @brief Gets public function info by index.
   333 		 * 
   334 		 * @param index			Public function index number.
   335 		 * @param publicptr		Optionally filled with pointer to public structure.
   336 		 */
   337 		virtual int GetPublicByIndex(uint32_t index, sp_public_t **publicptr) =0;
   338 
   339 		/**
   340 		 * @brief Gets the number of public functions.
   341 		 *
   342 		 * @return				Filled with the number of public functions.
   343 		 */
   344 		virtual uint32_t GetPublicsNum() =0;
   345 
   346 		/**
   347 		 * @brief Gets public variable info by index.
   348 		 * 
   349 		 * @param index			Public variable index number.
   350 		 * @param pubvar		Optionally filled with pointer to pubvar structure.
   351 		 */
   352 		virtual int GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar) =0;
   353 
   354 		/**
   355 		 * @brief Finds a public variable by name.
   356 		 *
   357 		 * @param name			Name of pubvar
   358 		 * @param index			Optionally filled with pubvar index number.
   359 		 */
   360 		virtual int FindPubvarByName(const char *name, uint32_t *index) =0;
   361 
   362 		/**
   363 		 * @brief Gets the addresses of a public variable.
   364 		 * 
   365 		 * @param index			Index of public variable.
   366 		 * @param local_addr		Address to store local address in.
   367 		 * @param phys_addr		Address to store physically relocated in.
   368 		 */
   369 		virtual int GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr) =0;
   370 
   371 		/**
   372 		 * @brief Returns the number of public variables.
   373 		 *
   374 		 * @return				Number of public variables.
   375 		 */
   376 		virtual uint32_t GetPubVarsNum() =0;
   377 
   378 		/**
   379 		 * @brief Round-about method of converting a plugin reference to a physical address
   380 		 *
   381 		 * @param local_addr	Local address in plugin.
   382 		 * @param phys_addr		Optionally filled with relocated physical address.
   383 		 */
   384 		virtual int LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr) =0;
   385 
   386 		/**
   387 		 * @brief Converts a local address to a physical string.
   388 		 *
   389 		 * @param local_addr	Local address in plugin.
   390 		 * @param addr			Destination output pointer.
   391 		 */
   392 		virtual int LocalToString(cell_t local_addr, char **addr) =0;
   393 
   394 		/**
   395 		 * @brief Converts a physical string to a local address.
   396 		 *
   397 		 * @param local_addr	Local address in plugin.
   398 		 * @param bytes			Number of chars to write, including NULL terminator.
   399 		 * @param source		Source string to copy.
   400 		 */
   401 		virtual int StringToLocal(cell_t local_addr, size_t bytes, const char *source) =0;
   402 
   403 		/**
   404 		 * @brief Converts a physical UTF-8 string to a local address.
   405 		 * This function is the same as the ANSI version, except it will copy the maximum number of characters possible 
   406 		 *  without accidentally chopping a multi-byte character.
   407 		 *
   408 		 * @param local_addr		Local address in plugin.
   409 		 * @param maxbytes		Number of bytes to write, including NULL terminator.
   410 		 * @param source			Source string to copy.
   411 		 * @param wrtnbytes		Optionally set to the number of actual bytes written.
   412 		 */
   413 		virtual int StringToLocalUTF8(cell_t local_addr, 
   414 									  size_t maxbytes, 
   415 									  const char *source, 
   416 									  size_t *wrtnbytes) =0;
   417 
   418 		/**
   419 		 * @brief Pushes a cell onto the stack.  Increases the parameter count by one.
   420 		 *
   421 		 * @param value			Cell value.
   422 		 */
   423 		virtual int PushCell(cell_t value) =0;
   424 
   425 		/** 
   426 		 * @brief Pushes an array of cells onto the stack.  Increases the parameter count by one.
   427 		 * If the function returns an error it will fail entirely, releasing anything allocated in the process.
   428 		 * Note that this does not release the heap, so you should release it after
   429 		 *  calling Execute().
   430 		 *
   431 		 * @param local_addr	Filled with local address to release.
   432 		 * @param phys_addr		Optionally filled with physical address of new array.
   433 		 * @param array			Cell array to copy.
   434 		 * @param numcells		Number of cells in the array to copy.
   435 		 */
   436 		virtual int PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells) =0;
   437 
   438 		/**
   439 		 * @brief Pushes a string onto the stack (by reference) and increases the parameter count by one.
   440 		 * Note that this does not release the heap, so you should release it after
   441 		 *  calling Execute().
   442 		 *
   443 		 * @param local_addr	Filled with local address to release.
   444 		 * @param phys_addr		Optionally filled with physical address of new array.
   445 		 * @param string		Source string to push.
   446 		 */
   447 		virtual int PushString(cell_t *local_addr, char **phys_addr, const char *string) =0;
   448 
   449 		/**
   450 		 * @brief Individually pushes each cell of an array of cells onto the stack.  Increases the 
   451 		 *  parameter count by the number of cells pushed.
   452 		 * If the function returns an error it will fail entirely, releasing anything allocated in the process.
   453 		 *
   454 		 * @param array			Array of cells to read from.
   455 		 * @param numcells		Number of cells to read.
   456 		 */
   457 		virtual int PushCellsFromArray(cell_t array[], unsigned int numcells) =0;
   458 
   459 		/** 
   460 		 * @brief Binds a list of native names and their function pointers to a context.
   461 		 * If num is 0, the list is read until an entry with a NULL name is reached.
   462 		 * If overwrite is non-zero, already registered natives will be overwritten.
   463 		 * 
   464 		 * @param natives		Array of natives.
   465 		 * @param num			Number of natives in array.
   466 		 * @param overwrite		Toggles overwrite.
   467 		 */
   468 		virtual int BindNatives(const sp_nativeinfo_t *natives, unsigned int num, int overwrite) =0;
   469 
   470 		/**
   471 		 * @brief Binds a single native.  Overwrites any existing bind.
   472 		 * If the context does not contain the native that will be binded the function will return
   473 		 *  with a SP_ERROR_NOT_FOUND error.
   474 		 *
   475 		 * @param native		Pointer to native.
   476 		 */
   477 		virtual int BindNative(const sp_nativeinfo_t *native) =0;
   478 
   479 		/**
   480 		 * @brief Binds a single native to any non-registered native.
   481 		 *
   482 		 * @param native		Native to bind.
   483 		 */
   484 		virtual int BindNativeToAny(SPVM_NATIVE_FUNC native) =0;
   485 
   486 		/**
   487 		 * @brief Executes a function ID located in this context.
   488 		 *
   489 		 * @param code_addr		Address to execute at.
   490 		 * @param result		Pointer to store the return value (required).
   491 		 * @return				Error code (if any) from the VM.
   492 		 */
   493 		virtual int Execute(uint32_t code_addr, cell_t *result) =0;
   494 
   495 		/**
   496 		 * @brief Throws a error and halts any current execution.
   497 		 *
   498 		 * @param error		The error number to set.
   499 		 * @param msg		Custom error message format.  NULL to use default.
   500 		 * @param ...		Message format arguments, if any.
   501 		 */
   502 		virtual void ThrowNativeErrorEx(int error, const char *msg, ...) =0;
   503 
   504 		/**
   505 		 * @brief Throws a generic native error and halts any current execution.
   506 		 *
   507 		 * @param msg		Custom error message format.  NULL to set no message.
   508 		 * @param ...		Message format arguments, if any.
   509 		 * @return			0 for convenience.
   510 		 */
   511 		virtual cell_t ThrowNativeError(const char *msg, ...) =0;
   512 
   513 		/**
   514 		 * @brief Returns a function by name.
   515 		 *
   516 		 * @param public_name		Name of the function.
   517 		 * @return					A new IPluginFunction pointer, NULL if not found.
   518 		 */
   519 		virtual IPluginFunction *GetFunctionByName(const char *public_name) =0;
   520 
   521 		/**
   522 		 * @brief Returns a function by its id.
   523 		 *
   524 		 * @param func_id			Function ID.
   525 		 * @return					A new IPluginFunction pointer, NULL if not found.
   526 		 */
   527 		virtual IPluginFunction *GetFunctionById(funcid_t func_id) =0;
   528 
   529 #if defined SOURCEMOD_BUILD
   530 		/**
   531 		 * @brief Returns the identity token for this context.
   532 		 * Note: This is a helper function for native calls and the Handle System.
   533 		 *
   534 		 * @return			Identity token.
   535 		 */
   536 		virtual SourceMod::IdentityToken_t *GetIdentity() =0;
   537 
   538 		/**
   539 		 * @brief Returns whether the identity is runnable.
   540 		 *
   541 		 * @return			True if runnable, false otherwise.
   542 		 */
   543 		virtual bool IsRunnable() =0;
   544 #endif
   545 	};
   546 
   547 
   548 	/**
   549 	 * @brief Information about a position in a call stack.
   550 	 */
   551 	struct CallStackInfo
   552 	{
   553 		const char *filename;		/**< NULL if not found */
   554 		unsigned int line;			/**< 0 if not found */
   555 		const char *function;		/**< NULL if not found */
   556 	};
   557 
   558 	/**
   559 	 * @brief Retrieves error information from a debug hook.
   560 	 */
   561 	class IContextTrace
   562 	{
   563 	public:
   564 		/**
   565 		 * @brief Returns the integer error code.
   566 		 *
   567 		 * @return			Integer error code.
   568 		 */
   569 		virtual int GetErrorCode() =0;
   570 
   571 		/**
   572 		 * @brief Returns a string describing the error.
   573 		 *
   574 		 * @return			Error string.
   575 		 */
   576 		virtual const char *GetErrorString() =0;
   577 
   578 		/**
   579 		 * @brief Returns whether debug info is available.
   580 		 *
   581 		 * @return			True if debug info is available, false otherwise.
   582 		 */
   583 		virtual bool DebugInfoAvailable() =0;
   584 
   585 		/**
   586 		 * @brief Returns a custom error message.
   587 		 *
   588 		 * @return			A pointer to a custom error message, or NULL otherwise.
   589 		 */
   590 		virtual const char *GetCustomErrorString() =0;
   591 
   592 		/**
   593 		 * @brief Returns trace info for a specific point in the backtrace, if any.
   594 		 * The next subsequent call to GetTraceInfo() will return the next item in the call stack.
   595 		 * Calls are retrieved in descending order (i.e. the first item is at the top of the stack/call sequence).
   596 		 *
   597 		 * @param trace		An ErrorTraceInfo buffer to store information (NULL to ignore).
   598 		 * @return			True if successful, false if there are no more traces.
   599 		 */
   600 		virtual bool GetTraceInfo(CallStackInfo *trace) =0;
   601 
   602 		/**
   603 		 * @brief Resets the trace to its original position (the call on the top of the stack).
   604 		 */
   605 		virtual void ResetTrace() =0;
   606 
   607 		/**
   608 		 * @brief Retrieves the name of the last native called.
   609 		 * Returns NULL if there was no native that caused the error.
   610 		 *
   611 		 * @param index		Optional pointer to store index.
   612 		 * @return			Native name, or NULL if none.
   613 		 */
   614 		virtual const char *GetLastNative(uint32_t *index) =0;
   615 	};
   616 
   617 
   618 	/**
   619 	 * @brief Provides callbacks for debug information.
   620 	 */
   621 	class IDebugListener
   622 	{
   623 	public:
   624 		virtual void OnContextExecuteError(IPluginContext *ctx, IContextTrace *error) =0;
   625 	};
   626 
   627 
   628 	/**
   629 	 * @brief Contains helper functions used by VMs and the host app
   630 	 */
   631 	class ISourcePawnEngine
   632 	{
   633 	public:
   634 		/**
   635 		 * @brief Loads a named file from a file pointer.  
   636 		 * Note: Using this means the memory will be allocated by the VM.
   637 		 * 
   638 		 * @param fp			File pointer.  May be at any offset.  Not closed on return.
   639 		 * @param err		Optional error code pointer.
   640 		 * @return			A new plugin structure.
   641 		 */	
   642 		virtual sp_plugin_t *LoadFromFilePointer(FILE *fp, int *err) =0;
   643 	
   644 		/**
   645 		 * @brief Loads a file from a base memory address.
   646 		 *	
   647 		 * @param base		Base address of the plugin's memory region.
   648 		 * @param plugin	If NULL, a new plugin pointer is returned.
   649 		 *                  Otherwise, the passed pointer is used.
   650 		 * @param err		Optional error code pointer.
   651 		 * @return			The resulting plugin pointer.
   652 		 */
   653 		virtual sp_plugin_t *LoadFromMemory(void *base, sp_plugin_t *plugin, int *err) =0;
   654 
   655 		/**
   656 		 * Frees all of the memory associated with a plugin file.
   657 		 * If allocated using SP_LoadFromMemory, the base and plugin pointer 
   658 		 *  itself are not freed (so this may end up doing nothing).
   659 		 */
   660 		virtual int FreeFromMemory(sp_plugin_t *plugin) =0;
   661 
   662 		/**
   663 		 * @brief Allocates large blocks of temporary memory.
   664 		 * 
   665 		 * @param size		Size of memory to allocate.
   666 		 * @return			Pointer to memory, NULL if allocation failed.
   667 		 */
   668 		virtual void *BaseAlloc(size_t size) =0;
   669 
   670 		/**
   671 		 * @brief Frees memory allocated with BaseAlloc.
   672 		 *
   673 		 * @param memory	Memory address to free.
   674 		 */
   675 		virtual void BaseFree(void *memory) =0;
   676 
   677 		/**
   678 		 * @brief Allocates executable memory.
   679 		 *
   680 		 * @param size		Size of memory to allocate.
   681 		 * @return			Pointer to memory, NULL if allocation failed.
   682 		 */
   683 		virtual void *ExecAlloc(size_t size) =0;
   684 
   685 		/**
   686 		 * @brief Frees executable memory.
   687 		 *
   688 		 * @param address	Address to free.
   689 		 */
   690 		virtual void ExecFree(void *address) =0;
   691 
   692 		/**
   693 		 * @brief Sets the debug listener.  This should only be called once.
   694 		 * If called successively (using manual chaining), only the last function should
   695 		 * attempt to call back into the same plugin.  Otherwise, globally cached states
   696 		 * can be accidentally overwritten.
   697 		 *
   698 		 * @param listener	Pointer to an IDebugListener.
   699 		 * @return			Old IDebugListener, or NULL if none.
   700 		 */
   701 		virtual IDebugListener *SetDebugListener(IDebugListener *listener) =0;
   702 		
   703 		/**
   704 		 * @brief Returns the number of plugins on the call stack.
   705 		 *
   706 		 * @return			Number of contexts in the call stack.
   707 		 */
   708 		virtual unsigned int GetContextCallCount() =0;
   709 	};
   710 
   711 
   712 	/**
   713 	 * @brief Dummy class for encapsulating private compilation data.
   714 	 */
   715 	class ICompilation
   716 	{
   717 	public:
   718 		/** Virtual destructor */
   719 		virtual ~ICompilation() { };
   720 	};
   721 
   722 
   723 	/** 
   724 	 * @brief Outlines the interface a Virtual Machine (JIT) must expose
   725 	 */
   726 	class IVirtualMachine
   727 	{
   728 	public:
   729 		/**
   730 		 * @brief Returns the current API version.
   731 		 */
   732 		virtual unsigned int GetAPIVersion() =0;
   733 
   734 		/**
   735 		 * @brief Returns the string name of a VM implementation.
   736 		 */
   737 		virtual const char *GetVMName() =0;
   738 
   739 		/**
   740 		 * @brief Begins a new compilation
   741 		 * 
   742 		 * @param plugin	Pointer to a plugin structure.
   743 		 * @return			New compilation pointer.
   744 		 */
   745 		virtual ICompilation *StartCompilation(sp_plugin_t *plugin) =0;
   746 
   747 		/**
   748 		 * @brief Sets a compilation option.
   749 		 *
   750 		 * @param co		Pointer to a compilation.
   751 		 * @param key		Option key name.
   752 		 * @param val		Option value string.
   753 		 * @return			True if option could be set, false otherwise.
   754 		 */
   755 		virtual bool SetCompilationOption(ICompilation *co, const char *key, const char *val) =0;
   756 
   757 		/**
   758 		 * @brief Finalizes a compilation into a new sp_context_t.
   759 		 * Note: This will free the ICompilation pointer.
   760 		 *
   761 		 * @param co		Compilation pointer.
   762 		 * @param err		Filled with error code on exit.
   763 		 * @return			New plugin context.
   764 		 */
   765 		virtual sp_context_t *CompileToContext(ICompilation *co, int *err) =0;
   766 
   767 		/**
   768 		 * @brief Aborts a compilation and frees the ICompilation pointer.
   769 		 *
   770 		 * @param co		Compilation pointer.
   771 		 */
   772 		virtual void AbortCompilation(ICompilation *co) =0;
   773 
   774 		/**
   775 		 * @brief Frees any internal variable usage on a context.
   776 		 *
   777 		 * @param ctx		Context structure pointer.
   778 		 */
   779 		virtual void FreeContext(sp_context_t *ctx) =0;
   780 
   781 		/**
   782 		 * @brief Calls the "execute" function on a context.
   783 		 *
   784 		 * @param ctx		Executes a function in a context.
   785 		 * @param code_addr	Index into the code section.
   786 		 * @param result	Pointer to store result into.
   787 		 * @return			Error code (if any).
   788 		 */
   789 		virtual int ContextExecute(sp_context_t *ctx, uint32_t code_addr, cell_t *result) =0;
   790 
   791 		/**
   792 		 * @brief Given a context and a code address, returns the index of the function.
   793 		 * 
   794 		 * @param ctx		Context to search.
   795 		 * @param code_addr	Index into the code section.
   796 		 * @param result	Pointer to store result into.
   797 		 * @return			True if code index is valid, false otherwise.
   798 		 */
   799 		virtual bool FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result) =0;
   800 
   801 		/**
   802 		 * @brief Returns the number of functions defined in the context.
   803 		 *
   804 		 * @param ctx		Context to search.
   805 		 * @return			Number of functions.
   806 		 */
   807 		virtual unsigned int FunctionCount(const sp_context_t *ctx) =0;
   808 
   809 		/**
   810 		 * @brief Returns a version string.
   811 		 *
   812 		 * @return			Versioning string.
   813 		 */
   814 		virtual const char *GetVersionString() =0;
   815 
   816 		/**
   817 		 * @brief Returns a string describing optimizations.
   818 		 *
   819 		 * @return			String describing CPU specific optimizations.
   820 		 */
   821 		virtual const char *GetCPUOptimizations() =0;
   822 
   823 		/**
   824 		 * @brief Given a context and a p-code address, returns the index of the function.
   825 		 * 
   826 		 * @param ctx		Context to search.
   827 		 * @param code_addr	Index into the p-code section.
   828 		 * @param result	Pointer to store result into.
   829 		 * @return			True if code index is valid, false otherwise.
   830 		 */
   831 		virtual bool FunctionPLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result) =0;
   832 	};
   833 };
   834 
   835 #endif //_INCLUDE_SOURCEPAWN_VM_API_H_