File: corearray.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (450 lines) | stat: -rw-r--r-- 10,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
   FALCON - The Falcon Programming Language.
   FILE: corearray.cpp

   Language level array implementation
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: sab dic 4 2004

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Implementation of core arrays.
   Core arrays are meant to hold item pointers.
*/

#include <falcon/carray.h>
#include <falcon/memory.h>
#include <falcon/string.h>
#include <falcon/vm.h>
#include <string.h>
#include <falcon/lineardict.h>
#include <falcon/coretable.h>


namespace Falcon {

CoreArray::CoreArray():
   m_bindings(0),
   m_table(0),
   m_tablePos(0)
{
   m_itemarray.owner( this );
}


CoreArray::CoreArray( const CoreArray& other ):
   m_itemarray( other.m_itemarray ),
   m_table( other.m_table ),
   m_tablePos( other.m_tablePos )
{
   m_itemarray.owner( this );

   if ( other.m_bindings != 0 )
   {
      m_bindings = static_cast<CoreDict*>( other.m_bindings->clone() );
      m_bindings->gcMark( mark() );
   }
   else
      m_bindings = 0;
}

CoreArray::CoreArray( uint32 prealloc ):
   m_itemarray( prealloc ),
   m_bindings(0),
   m_table(0),
   m_tablePos(0)
{
   m_itemarray.owner( this );
}

CoreArray::CoreArray( Item *buffer, uint32 size, uint32 alloc ):
   m_itemarray( buffer, size, alloc ),
   m_bindings(0),
   m_table(0),
   m_tablePos(0)
{
   m_itemarray.owner( this );
}

CoreArray::~CoreArray()
{
}

const String& CoreArray::name() const {
   static String name( "Array" );
   return name;
}

void CoreArray::readyFrame( VMachine* vm, uint32 paramCount )
{
   vm->prepareFrame( this, paramCount );
   vm->currentContext()->fself() = this;
}


CoreDict *CoreArray::makeBindings()
{
   if ( m_bindings == 0 )
   {
      m_bindings = new CoreDict( new LinearDict( ) );
      m_bindings->put( new CoreString( "self" ), this );
      m_bindings->gcMark( mark() );
   }

   return m_bindings;
}

Item* CoreArray::getProperty( const String &name )
{
   Item *found = 0;

   if ( m_bindings != 0 )
   {
      found = m_bindings->find( name );
      if ( found )
         return found->dereference();
   }

   // we didn't find it.
   if ( m_table )
   {
      CoreTable *table = reinterpret_cast<CoreTable *>( m_table->getFalconData() );
      uint32 pos = table->getHeaderPos( name );
      if ( pos != CoreTable::noitem )
      {
         found = (*this)[pos].dereference();
         if ( found->isNil() && ! found->isOob() )
            found = table->getHeaderData( pos )->dereference();

      }
   }

   return found;
}

void CoreArray::setProperty( const String &name, const Item &data )
{
   if ( m_bindings != 0 )
   {
      Item* found = m_bindings->find( name );
      if ( found != 0 ) {
         *found = data;
         return;
      }
   }

   if ( m_table != 0 )
   {
      CoreTable *table = reinterpret_cast<CoreTable *>( m_table->getFalconData() );
      uint32 pos = table->getHeaderPos( name );
      if ( pos != CoreTable::noitem )
      {
         *(*this)[pos].dereference() = data;
         return;
      }
   }

   m_bindings = makeBindings();
   Item ref;
   VMachine* vm = VMachine::getCurrent();
   if ( vm )
      vm->referenceItem( ref, *const_cast<Item*>(&data) );

   m_bindings->put( new CoreString( name ), ref );
}


void CoreArray::readProperty( const String &prop, Item &item )
{
   Item *p = getProperty( prop );
   if ( p == 0 )
   {
      // try to find a generic method
      VMachine* vm = VMachine::getCurrent();
      fassert( vm != 0);
      CoreClass* cc = vm->getMetaClass( FLC_ITEM_ARRAY );
      uint32 id;
      if ( cc == 0 || ! cc->properties().findKey( prop, id ) )
      {
         String extra( "Array." );
         extra.A( prop );
         throw new AccessError( ErrorParam( e_prop_acc, __LINE__ ).extra( extra ) );
      }

      p = cc->properties().getValue( id );
      fassert( ! p->isReference() );
   }

   item = *p->dereference();
   item.methodize( this );  // may fail but it's ok
}

void CoreArray::writeProperty( const String &prop, const Item &item )
{
   setProperty( prop, *item.dereference() );
}

void CoreArray::readIndex( const Item &index, Item &target )
{
   switch ( index.type() )
   {
      case FLC_ITEM_INT:
      {
         register int32 pos = (int32) index.asInteger();
         if ( pos < 0 )
         {
            if ( -pos <= (int32) length() )
            {
               target = m_itemarray[length()+pos];
               return;
            }
         }
         else
         {
            if( pos < (int) length() )
            {
               target = m_itemarray[pos];
               return;
            }
         }
      }
      break;

      case FLC_ITEM_NUM:
      {
         register int32 pos = (int32) index.asNumeric();
         if ( pos < 0 )
         {
            if ( -pos <= (int32) length() )
            {
               target = m_itemarray[length()+pos];
               return;
            }
         }
         else
         {
            if( pos < (int) length() )
            {
               target = m_itemarray[pos];
               return;
            }
         }
      }
      break;

      case FLC_ITEM_RANGE:
      {
         // open ranges?
         if ( index.asRangeIsOpen() &&
              ((index.asRangeStart() >= 0 && (int) length() <= index.asRangeStart() ) ||
              (index.asRangeStart() < 0 && (int) length() < -index.asRangeStart() ))
              )
         {
            target = new CoreArray();
            return;
         }

         register int32 end = (int32)(index.asRangeIsOpen() ? length() : index.asRangeEnd());
         CoreArray* array = partition( (int32) index.asRangeStart(), end );
         if ( array != 0 )
         {
            target = array;
            return;
         }
      }
      break;

      case FLC_ITEM_REFERENCE:
         readIndex( index.asReference()->origin(), target );
         return;
   }

   throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "LDP" ) );
}


CoreArray* CoreArray::partition( int32 start, int32 end ) const
{
   int32 size;
   Item *buffer;

   if ( start < 0 )
      start = m_itemarray.m_size + start;
   if ( start < 0 || start >= (int32) m_itemarray.m_size )
      return 0;

   if ( end < 0 )
      end = m_itemarray.m_size + end;
   if ( end < 0 || end > (int32) m_itemarray.m_size )
      return 0;

   if( end < start ) {
      size = start - end + 1;
      buffer = (Item *) memAlloc( m_itemarray.esize( size ) );

      for( int i = 0; i < size; i ++ )
         buffer[i] = m_itemarray.m_data[start - i];
   }
   else {
      if( end == start ) {
         return new CoreArray;
      }
      size = end - start;
      buffer = (Item *) memAlloc( m_itemarray.esize( size ) );
      memcpy( buffer, m_itemarray.m_data + start, m_itemarray.esize( size )  );
   }

   return new CoreArray( buffer, size, size );
}

CoreArray *CoreArray::clone() const
{
   return new CoreArray( *this );
}


void CoreArray::writeIndex( const Item &index, const Item &target )
{
  switch ( index.type() )
   {
      case FLC_ITEM_INT:
      {
         register int32 pos = (int32) index.asInteger();
         if ( pos < 0 )
         {
            if ( -pos <= (int32) length() )
            {
               if ( target.isString() )
                  m_itemarray[length()+pos] = new CoreString( *target.asString() );
               else
                  m_itemarray[length()+pos] = target;
               return;
            }
         }
         else
         {
            if( pos < (int) length() )
            {
               if ( target.isString() )
                  m_itemarray[pos] = new CoreString( *target.asString() );
               else
                  m_itemarray[pos] = target;
               return;
            }
         }
      }
      break;

      case FLC_ITEM_NUM:
      {
         register int32 pos = (int32) index.asNumeric();
         if ( pos < 0 )
         {
            if ( -pos <= (int32) length() )
            {
               if ( target.isString() )
                  m_itemarray[length()+pos] = new CoreString( *target.asString() );
               else
                  m_itemarray[length()+pos] = target;
               return;
            }
         }
         else
         {
            if( pos < (int) length() )
            {
                if ( target.isString() )
                  m_itemarray[pos] = new CoreString( *target.asString() );
               else
                  m_itemarray[pos] = target;
               return;
            }
         }
      }
      break;

      case FLC_ITEM_RANGE:
      {
         int32 end = (int32)(index.asRangeIsOpen() ? length() : index.asRangeEnd());
         int32 start = (int32) index.asRangeStart();
         const Item *tgt = target.dereference();

         if( tgt->isArray() )
         {
            if( change( *target.asArray(), (int32)start, (int32)end ) )
               return;
         }
         else
         {
            // if it's not a plain insert...
            if ( start != end )
            {
               // before it's too late.
               if ( start <  0 )
                  start = length() + start;

               if ( end <  0 )
                  end = length() + end;

               if( ! remove( start, end ) )
                  throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "STP" ) );

               if ( start > end )
                  start = end;
            }

            if ( tgt->isString() )
            {
                if ( insert( new CoreString( *tgt->asString() ), start ) )
                  return;
            }
            else {
               if( insert( *tgt, start ) )
                  return;
            }
         }

      }
      break;

      case FLC_ITEM_REFERENCE:
         writeIndex( index.asReference()->origin(), target );
         return;
   }


   throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "STP" ) );
}

void CoreArray::gcMark( uint32 gen )
{
   CoreArray *array = this;

   if( array->mark() != gen )
   {
      array->mark(gen);
      array->items().gcMark(gen);

      // mark also the bindings
      if ( array->bindings() != 0 )
      {
         array->bindings()->gcMark( gen );
      }

      // and also the table
      if ( array->table() != 0 && array->table()->mark() != gen )
      {
         array->table()->gcMark( gen );
      }
   }
}

}

/* end of corearray.cpp */