Sign in to follow this  

passing anonymous arrays from Angelscript to C

This topic is 824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Edited by shabtronic

Share this post


Link to post
Share on other sites

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. ?

?

?

Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

This topic is 824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this