• Create Account

Implementation of simple (static) array/strings

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

#1ziomau  Members

115
Like
0Likes
Like

Posted 02 June 2014 - 01:32 AM

Dear all,

I need to run the scripts in a real time environment where there is a limited implementation of the standard C++ library, and particularly a very poor stream class.

This causes a lot of errors when trying to include the add-on implementation of the string factory and class.

I have two choices to get out of this problem:

1) Implement a different string factory not using streams

2) Implement a different string native object

As for point 1) I know what to do, and for sure this is the quickest and easiest solution, but let me investigate further the second option, that for me is more interesting because it may solve a different category of problems too (see later).

The basic question is: how much is complex to add to the language a simple static "C" type array?

In order to have something like:

int foo[20];

char buf[100];

SomeDefinedType[3];

... and possibly ...

double Matrix[3][3];

This addition whould automatically solve my string problem too, by using char arrays, and all standard "C" routine strcat, strcpy, strstr, ... by adding them as external calls or even implementing in a script module.

Having the "C" array, can solve my other problem too.

Sometimes I have some data structures, that I want to map and exchange between the script enviromìnment and the host interpreter. These structure come from far ago and I cannot easily change them without breaking a lot of code.

some of those structures are defined like:

struct msg {

int Id;

int len;

char data[24];

};

As it is, I cannot exchange this data type, other than copying over one type to the other. I cannot even include a common definition file to have it defined into the two environment, because the above structure whould not compile in as.

Enough for the description, now the real question:

I am willing to help adding this feature, but I need some help to start.

Where I need to start looking first to add this thing to the compiler?

Any suggestion? Disclamers?

Thanks a lot.

Mau.

#2Andreas Jonsson  Moderators

4444
Like
0Likes
Like

Posted 02 June 2014 - 11:19 AM

The biggest obstacle with adding "simple" static arrays in AngelScript is the memory model. In C++ the memory model is totally manual, meaning the programmer is responsible for making sure the array is allocated correctly with the appropriate size, and deciding whether to use on-stack or on-heap memory. Another problem is that in C++ the static arrays are usually passed around as simple pointers, with no information about the actual size of the array.

The way that I've in mind to implement support for static arrays in AngelScript is to 'embed' the size of the array in the type information. This means that the array cannot have an undefined size, like it can in C++, but at least I will be able to support passing arrays between AngelScript and C++ as long as the registered interface defines the required size. It should be possible to do an implicit cast to a smaller array type from any starting element, as long as the larger array has enough elements after the starting element to contain the smaller array. The static arrays would obviously have to be defined as value types, i.e. no reference counting and no dynamic creation in the script language.

In response to your other problem:

Structures like this

struct msg {
int Id;
int len;
char data[24];
};


can be registered with AngelScript by using property accessors to expose the member array.

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

#3ziomau  Members

115
Like
0Likes
Like

Posted 04 June 2014 - 07:44 AM

Thanks a lot for your answer, and thanks for pointing me to the array access function too.

The problem with access functions is that expressions using array elements becomes soon unreadable.

Even simple expressions like:

msg m;

int aWord = m.data[0]  | (m.data[1] << 8)


becomes:

msg m;

int aWord = m.get_array(0) | (m.get_array(1) << 8);


But anyway, ... we can live with it.

Regards.

Mau.

#4Andreas Jonsson  Moderators

4444
Like
0Likes
Like

Posted 04 June 2014 - 12:07 PM

If your access methods is declared as 'type get_array(int)' and 'set_array(int, type)', then you'll be able to write the script like this:

msg m;
m.array[0] = 42;
int aWord = m.array[0];


The compiler will internally translate it to the following, but the writer doesn't need to worry about it:

msg m;
m.set_array(0, 42);
int aWord = m.get_array(0);


The only drawback with accessors is that you (currently) can't both read and update the value in the same operation, so compound assignment, e.g. += or increment operators ++, won't work.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.