# HG changeset patch # User rubidium # Date 1213703169 0 # Node ID 4dd4f4327c3a5d9ed41e14d430e3fb5b6cb95808 # Parent 13fd0364b2c63944325c1ff2581d43de0d229960 (svn r13546) [NoAI] -Codechange: add support to pass arrays with integers to C++ functions. diff -r 13fd0364b2c6 -r 4dd4f4327c3a src/ai/api/squirrel_export.awk --- a/src/ai/api/squirrel_export.awk Mon Jun 16 14:43:19 2008 +0000 +++ b/src/ai/api/squirrel_export.awk Tue Jun 17 11:46:09 2008 +0000 @@ -332,6 +332,10 @@ types = types "s" } else if (match(params[len], "^void")) { types = types "p" + } else if (match(params[len], "^Array")) { + types = types "a" + } else if (match(params[len], "^struct Array")) { + types = types "a" } else { types = types "x" } diff -r 13fd0364b2c6 -r 4dd4f4327c3a src/squirrel_helper.hpp --- a/src/squirrel_helper.hpp Mon Jun 16 14:43:19 2008 +0000 +++ b/src/squirrel_helper.hpp Tue Jun 17 11:46:09 2008 +0000 @@ -9,6 +9,7 @@ #include "core/math_func.hpp" #include "economy_type.h" #include "misc/smallvec.h" +#include "squirrel_helper_type.hpp" /** * The Squirrel convert routines @@ -95,6 +96,35 @@ template <> inline const char *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { const SQChar *tmp; sq_getstring (vm, index, &tmp); char *tmp_str = strdup(FS2OTTD(tmp)); *ptr->Append() = (void *)tmp_str; return tmp_str; } template <> inline void *GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } + template <> inline Array *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) + { + SQObject obj; + sq_getstackobj(vm, index, &obj); + sq_pushobject(vm, obj); + sq_pushnull(vm); + + SmallVector data; + + while (SQ_SUCCEEDED(sq_next(vm, -2))) { + SQInteger tmp; + if (SQ_SUCCEEDED(sq_getinteger(vm, -1, &tmp))) { + *data.Append() = (int32)tmp; + } else { + // TODO: handle following error properly: "array member is not numeric; ignoring member" + } + + sq_pop(vm, 2); + } + sq_pop(vm, 2); + + Array *arr = (Array*)MallocT(sizeof(Array) + sizeof(int32) * data.Length()); + arr->size = data.Length(); + memcpy(arr->array, data.Begin(), sizeof(int32) * data.Length()); + + *ptr->Append() = arr; + return arr; + } + /** * Helper class to recognize the function type (retval type, args) and use the proper specialization * for SQ callback. The partial specializations for the second arg (Tis_void_retval) are not possible diff -r 13fd0364b2c6 -r 4dd4f4327c3a src/squirrel_helper_type.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/squirrel_helper_type.hpp Tue Jun 17 11:46:09 2008 +0000 @@ -0,0 +1,13 @@ +/* $Id$ */ + +/** @file squirrel_helper_type.hpp Helper structs for converting Squirrel data structures to C++. */ + +#ifndef SQUIRREL_HELPER_TYPE_HPP +#define SQUIRREL_HELPER_TYPE_HPP + +struct Array { + int32 size; + int32 array[VARARRAY_SIZE]; +}; + +#endif /* SQUIRREL_HELPER_TYPE_HPP */