passing anonymous arrays from Angelscript to C

Started by
2 comments, last by shabtronic 8 years, 7 months ago

Hi There

I'm trying to pass arrays from angelscript to C - and having a few problems

The problems being it doesn't work at all - doesn't fill the vec array - and probably will eventually crash - I can't really tell what is going - because the C code is compiled at run time and my shabby system has no really debugging at the moment.

here's my C code:


typedef struct
{
int maxElements;
int numElements;
unsigned char  data[1];
}  SArrayBuffer;


typedef struct
{
int       refCount;
_Bool      gcFlag;
void*     objType;
SArrayBuffer*     buffer;
int       elementSize;
int       subTypeId;
} ASarray;

void Init(void)
{
Debug("TCC init called");
//asRegisterGlobalFunction("float TestFunk(float)",TestFunk);
//asRegisterGlobalFunction("vec3 vectest(vec3)",vectest);
//asRegisterGlobalFunction("vec3 vecpointer(const vec3 &in)",vecpointer);
asRegisterGlobalFunction("void vecfill(vec3[]@,int)",vecfill);
}

void vecfill(ASarray *a,int l)
{
vec3* v=(vec3*)a->buffer->data;


for (int a=0;a<l;a++)
{
v[a].x=counter++;
v[a].y=counter++;
v[a].z=counter++;
}
}

here's my angelscript code


array<vec3> TestArray(50);


vecfill(TestArray,2);

I am doing it this way because 1) I want to be able to pass arrays anonymously without having to define a specific class or struct

2) I'm using TCC so the C code is compile at runtime - it's actually called from angelscript:


ExecuteTCC("./NewDir2/Test.c","Init");

I realise this is probably as dodgy as hell - but it's not a commercial thing, pure R&D - so I just hope it is possible.

thanks!

Advertisement

What exactly are the problems you're having here?

You forgot to tell us what the actual problems are smile.png

I'm guessing you're actually trying to manipulate the CScriptArray object directly through memory operations. If that is the case, your ASArray struct is missing the hidden virtual function table pointer that C++ adds to the class behind the scenes due to the existence of a virtual method, i.e.


typedef struct 
{ 
  void*         virtualFunctionTable;
  int           refCount; 
  _Bool         gcFlag; 
  void*         objType; 
  SArrayBuffer* buffer;
  int           elementSize; 
  int           subTypeId; 
} ASarray;

You'll also need to carefully verify the alignment of the members in the struct. It is likely that it will be correct, but there is no guarantee that TCC will align the members exactly the same way that whatever compiler you use to compile the application. It might be necessary to add some padding between members to get the proper alignment. ?

?

?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks Andreas - I thought it might be something like the vptr - but assumed the lack any inheritance would nuke it :) I will try it later on today - I really hope it works - because

having that anonymity saves me a fair bit of typing and fits with my reckless R&D code style :)

This topic is closed to new replies.

Advertisement