initial import of dynamic native code for both the JIT and plugins
note: dependency resolution is not done yet! --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40607
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* ===============================================================
|
||||
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
|
||||
* ===============================================================
|
||||
*
|
||||
* This file is part of the SourceMod/SourcePawn SDK. This file may only be used
|
||||
* or modified under the Terms and Conditions of its License Agreement, which is found
|
||||
* in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins
|
||||
* may change at any time. To view the latest information, see:
|
||||
* http://www.sourcemod.net/license.php
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#define SP_ERROR_NONE 0 /**< No error occurred */
|
||||
#define SP_ERROR_FILE_FORMAT 1 /**< File format unrecognized */
|
||||
#define SP_ERROR_DECOMPRESSOR 2 /**< A decompressor was not found */
|
||||
#define SP_ERROR_HEAPLOW 3 /**< Not enough space left on the heap */
|
||||
#define SP_ERROR_PARAM 4 /**< Invalid parameter or parameter type */
|
||||
#define SP_ERROR_INVALID_ADDRESS 5 /**< A memory address was not valid */
|
||||
#define SP_ERROR_NOT_FOUND 6 /**< The object in question was not found */
|
||||
#define SP_ERROR_INDEX 7 /**< Invalid index parameter */
|
||||
#define SP_ERROR_STACKLOW 8 /**< Nnot enough space left on the stack */
|
||||
#define SP_ERROR_NOTDEBUGGING 9 /**< Debug mode was not on or debug section not found */
|
||||
#define SP_ERROR_INVALID_INSTRUCTION 10 /**< Invalid instruction was encountered */
|
||||
#define SP_ERROR_MEMACCESS 11 /**< Invalid memory access */
|
||||
#define SP_ERROR_STACKMIN 12 /**< Stack went beyond its minimum value */
|
||||
#define SP_ERROR_HEAPMIN 13 /**< Heap went beyond its minimum value */
|
||||
#define SP_ERROR_DIVIDE_BY_ZERO 14 /**< Division by zero */
|
||||
#define SP_ERROR_ARRAY_BOUNDS 15 /**< Array index is out of bounds */
|
||||
#define SP_ERROR_INSTRUCTION_PARAM 16 /**< Instruction had an invalid parameter */
|
||||
#define SP_ERROR_STACKLEAK 17 /**< A native leaked an item on the stack */
|
||||
#define SP_ERROR_HEAPLEAK 18 /**< A native leaked an item on the heap */
|
||||
#define SP_ERROR_ARRAY_TOO_BIG 19 /**< A dynamic array is too big */
|
||||
#define SP_ERROR_TRACKER_BOUNDS 20 /**< Tracker stack is out of bounds */
|
||||
#define SP_ERROR_INVALID_NATIVE 21 /**< Native was pending or invalid */
|
||||
#define SP_ERROR_PARAMS_MAX 22 /**< Maximum number of parameters reached */
|
||||
#define SP_ERROR_NATIVE 23 /**< Error originates from a native */
|
||||
#define SP_ERROR_NOT_RUNNABLE 24 /**< Function or plugin is not runnable */
|
||||
#define SP_ERROR_ABORTED 25 /**< Function call was aborted */
|
||||
|
||||
/**
|
||||
* Defines a native function.
|
||||
*
|
||||
* It is not necessary to validate the parameter count
|
||||
*
|
||||
* @param plugin Handle of the calling plugin.
|
||||
* @param numParams Number of parameters passed to the native.
|
||||
* @return Value for the native call to return.
|
||||
*/
|
||||
functag NativeCall public(Handle:plugin, numParams);
|
||||
|
||||
/**
|
||||
* Creates a dynamic native. This should only be called in AskPluginLoad(), or
|
||||
* else you risk not having your native shared with other plugins.
|
||||
*
|
||||
* @param name Name of the dynamic native; must be unique amongst
|
||||
* all other registered dynamic native.s
|
||||
* @param func Function to use as the dynamic native.
|
||||
* @noreturn
|
||||
*/
|
||||
native CreateNative(const String:name[], NativeCall:func);
|
||||
|
||||
/**
|
||||
* Throws an error in the calling plugin of a native, instead of your own plugin.
|
||||
*
|
||||
* @param error Error code to use.
|
||||
* @param fmt Error message format.
|
||||
* @param ... Format arguments.
|
||||
*/
|
||||
native ThrowNativeError(error, const String:fmt[], {Handle,Float,String,_}:...);
|
||||
|
||||
/**
|
||||
* Retrieves the string length from a native parameter string. This is useful
|
||||
* fetching the entire string using dynamic arrays.
|
||||
* @note If this function succeeds, Get/SetNativeString will also succeed.
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @param length Stores the length of the string.
|
||||
* @return SP_ERROR_NONE on sucecss, any other integer on failure.
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native GetNativeStringLength(param, &length);
|
||||
|
||||
/**
|
||||
* Retrieves a string from a native parameter.
|
||||
* @note Output conditions are undefined on failure.
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @param buffer Buffer to store the string in.
|
||||
* @param maxlength Maximum length of the buffer.
|
||||
* @param bytes Optionally store the number of bytes written.
|
||||
* @return SP_ERROR_NONE on success, any other integer on failure.
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native GetNativeString(param, String:buffer[], maxlength, &bytes=0);
|
||||
|
||||
/**
|
||||
* Sets a string in a native parameter.
|
||||
* @note Output conditions are undefined on failure.
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @param source Source string to use.
|
||||
* @param maxlength Maximum number of bytes to write.
|
||||
* @param utf8 If false, string will not be written
|
||||
* with UTF8 safety.
|
||||
* @param bytes Optionally store the number of bytes written.
|
||||
* @return SP_ERROR_NONE on success, any other integer on failure.
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native SetNativeString(param, const String:source[], maxlength, bool:utf8=true, &bytes=0);
|
||||
|
||||
/**
|
||||
* Gets a cell from a native parameter.
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @return Cell value at the parameter number.
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native GetNativeCell(param);
|
||||
|
||||
/**
|
||||
* Gets a cell from a native parameter, by reference.
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @return Cell value at the parameter number.
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native GetNativeCellRef(param);
|
||||
|
||||
/**
|
||||
* Sets a cell from a native parameter, by reference.
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @param value Cell value at the parameter number to set by reference.
|
||||
* @noreturn
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native SetNativeCellRef(param, {Float,Handle,_}:value);
|
||||
|
||||
/**
|
||||
* Gets an array from a native parameter (always by reference).
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @param local Local array to copy into.
|
||||
* @param size Maximum size of local array.
|
||||
* @return SP_ERROR_NONE on success, anything else on failure.
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native GetNativeArray(param, {Float,Handle,_}:local[], size);
|
||||
|
||||
/**
|
||||
* Copies a local array into a native parameter array (always by reference).
|
||||
*
|
||||
* @param param Parameter number, starting from 1.
|
||||
* @param local Local array to copy from.
|
||||
* @param size Size of the local array to copy.
|
||||
* @return SP_ERROR_NONE on success, anything else on failure.
|
||||
* @error Invalid parameter number or calling from a non-native function.
|
||||
*/
|
||||
native SetNativeArray(param, const {Float,Handle,_}:local[], size);
|
||||
|
||||
/**
|
||||
* Formats a string using parameters from a native.
|
||||
*
|
||||
* @note All parameter indexes start at 1.
|
||||
* @note If the input and output buffers overlap, the contents
|
||||
* of the output buffer at the end is undefined.
|
||||
*
|
||||
* @param out_param Output parameter number to write to. If 0, out_string is used.
|
||||
* @param fmt_param Format parameter number. If 0, fmt_string is used.
|
||||
* @param vararg_param First variable parameter number.
|
||||
* @param out_len Output string buffer maximum length (always required).
|
||||
* @param written Optionally stores the number of bytes written.
|
||||
* @param out_string Output string buffer to use if out_param is not used.
|
||||
* @param fmt_string Format string to use if fmt_param is not used.
|
||||
* @return SP_ERROR_NONE on success, anything else on failure.
|
||||
*/
|
||||
native FormatNativeString(out_param,
|
||||
fmt_param,
|
||||
vararg_param,
|
||||
out_len,
|
||||
&written=0,
|
||||
String:out_string[]="",
|
||||
const String:fmt_string[]="");
|
||||
|
||||
stock Float:GetNativeFloat(param, bool:byref)
|
||||
{
|
||||
if (!byref)
|
||||
{
|
||||
return Float:GetNativeCell(param);
|
||||
} else {
|
||||
return Float:GetNativeCellRef(param);
|
||||
}
|
||||
}
|
||||
stock Handle:GetNativeHandle(param, bool:byref)
|
||||
{
|
||||
if (!byref)
|
||||
{
|
||||
return Handle:GetNativeCell(param);
|
||||
} else {
|
||||
return Handle:GetNativeCellRef(param);
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@ struct Plugin
|
||||
#include <clients>
|
||||
#include <usermessages>
|
||||
#include <events>
|
||||
#include <functions>
|
||||
|
||||
/**
|
||||
* Declare this as a struct in your plugin to expose its information.
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
#include <sourcemod>
|
||||
|
||||
public Plugin:myinfo =
|
||||
{
|
||||
name = "FakeNative Testing Lab #1",
|
||||
author = "AlliedModders LLC",
|
||||
description = "Test suite #1 for dynamic natives",
|
||||
version = "1.0.0.0",
|
||||
url = "http://www.sourcemod.net/"
|
||||
};
|
||||
|
||||
public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
|
||||
{
|
||||
CreateNative("TestNative1", __TestNative1);
|
||||
CreateNative("TestNative2", __TestNative2);
|
||||
CreateNative("TestNative3", __TestNative3);
|
||||
CreateNative("TestNative4", __TestNative4);
|
||||
CreateNative("TestNative5", __TestNative5);
|
||||
return true;
|
||||
}
|
||||
|
||||
public __TestNative1(Handle:plugin, numParams)
|
||||
{
|
||||
PrintToServer("TestNative1: Plugin: %x params: %d", plugin, numParams);
|
||||
if (numParams == 4)
|
||||
{
|
||||
ThrowNativeError(SP_ERROR_NATIVE, "Four parameters ARE NOT ALLOWED lol");
|
||||
}
|
||||
return 5;
|
||||
}
|
||||
|
||||
public __TestNative2(Handle:plugin, numParams)
|
||||
{
|
||||
new String:buffer[512];
|
||||
new bytes;
|
||||
|
||||
GetNativeString(1, buffer, sizeof(buffer), bytes);
|
||||
|
||||
for (new i=0; i<bytes; i++)
|
||||
{
|
||||
buffer[i] = buffer[i] + 1;
|
||||
}
|
||||
|
||||
SetNativeString(1, buffer, bytes+1);
|
||||
}
|
||||
|
||||
public __TestNative3(Handle:plugin, numParams)
|
||||
{
|
||||
new val1 = GetNativeCell(1);
|
||||
new val2 = GetNativeCellRef(2);
|
||||
|
||||
SetNativeCellRef(2, val1+val2);
|
||||
}
|
||||
|
||||
public __TestNative4(Handle:plugin, numParams)
|
||||
{
|
||||
new size = GetNativeCell(3);
|
||||
new local[size];
|
||||
|
||||
GetNativeArray(1, local, size);
|
||||
SetNativeArray(2, local, size);
|
||||
}
|
||||
|
||||
public __TestNative5(Handle:plugin, numParams)
|
||||
{
|
||||
new local = GetNativeCell(1);
|
||||
|
||||
if (local)
|
||||
{
|
||||
FormatNativeString(2, 4, 5, GetNativeCell(3));
|
||||
} else {
|
||||
new len = GetNativeCell(3);
|
||||
new fmt_len;
|
||||
new String:buffer[len];
|
||||
|
||||
GetNativeStringLength(4, fmt_len);
|
||||
|
||||
new String:format[fmt_len];
|
||||
|
||||
GetNativeString(2, buffer, len);
|
||||
GetNativeString(4, format, fmt_len);
|
||||
|
||||
FormatNativeString(0, 0, 5, len, _, buffer, format);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
#include <sourcemod>
|
||||
|
||||
native TestNative1(gaben, ...);
|
||||
native TestNative2(String:buffer[]);
|
||||
native TestNative3(value1, &value2);
|
||||
native TestNative4(const input[], output[], size);
|
||||
native TestNative5(bool:local, String:buffer[], maxlength, const String:fmt[], {Handle,Float,String,_}:...);
|
||||
|
||||
public Plugin:myinfo =
|
||||
{
|
||||
name = "FakeNative Testing Lab #2",
|
||||
author = "AlliedModders LLC",
|
||||
description = "Test suite #2 for dynamic natives",
|
||||
version = "1.0.0.0",
|
||||
url = "http://www.sourcemod.net/"
|
||||
};
|
||||
|
||||
public OnPluginStart()
|
||||
{
|
||||
RegServerCmd("test_native1", Test_Native1);
|
||||
RegServerCmd("test_native2", Test_Native2);
|
||||
RegServerCmd("test_native3", Test_Native3);
|
||||
RegServerCmd("test_native4", Test_Native4);
|
||||
RegServerCmd("test_native5", Test_Native5);
|
||||
}
|
||||
|
||||
public Action:Test_Native1(args)
|
||||
{
|
||||
PrintToServer("Returned: %d", TestNative1(1));
|
||||
PrintToServer("Returned: %d", TestNative1(1,2));
|
||||
PrintToServer("Returned: %d", TestNative1(1,2,3,4));
|
||||
|
||||
return Plugin_Handled;
|
||||
}
|
||||
|
||||
public Action:Test_Native2(args)
|
||||
{
|
||||
new String:buffer[] = "IBM";
|
||||
|
||||
PrintToServer("Before: %s", buffer);
|
||||
|
||||
TestNative2(buffer);
|
||||
|
||||
PrintToServer("AfteR: %s", buffer);
|
||||
|
||||
return Plugin_Handled;
|
||||
}
|
||||
|
||||
public Action:Test_Native3(args)
|
||||
{
|
||||
new value1 = 5, value2 = 6
|
||||
|
||||
PrintToServer("Before: %d, %d", value1, value2);
|
||||
|
||||
TestNative3(value1, value2);
|
||||
|
||||
PrintToServer("After: %d, %d", value1, value2);
|
||||
|
||||
return Plugin_Handled;
|
||||
}
|
||||
|
||||
public Action:Test_Native4(args)
|
||||
{
|
||||
new input[5] = {0, 1, 2, 3, 4};
|
||||
new output[5];
|
||||
|
||||
TestNative4(input, output, 5);
|
||||
|
||||
PrintToServer("[0=%d] [1=%d] [2=%d] [3=%d] [4=%d]", input[0], input[1], input[2], input[3], input[4]);
|
||||
PrintToServer("[0=%d] [1=%d] [2=%d] [3=%d] [4=%d]", output[0], output[1], output[2], output[3], output[4]);
|
||||
|
||||
return Plugin_Handled;
|
||||
}
|
||||
|
||||
public Action:Test_Native5(args)
|
||||
{
|
||||
new String:buffer1[512];
|
||||
new String:buffer2[512];
|
||||
|
||||
TestNative5(true, buffer1, sizeof(buffer1), "%d gabens in the %s", 50, "gabpark");
|
||||
TestNative5(true, buffer2, sizeof(buffer2), "%d gabens in the %s", 50, "gabpark");
|
||||
|
||||
PrintToServer("Test 1: %s", buffer1);
|
||||
PrintToServer("Test 2: %s", buffer2);
|
||||
|
||||
return Plugin_Handled;
|
||||
}
|
||||
Reference in New Issue
Block a user