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