File: item.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 (791 lines) | stat: -rw-r--r-- 16,833 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
   FALCON - The Falcon Programming Language.
   FILE: item.cpp

   Item API implementation
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: mar ott 12 2004

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

   See LICENSE file for licensing details.
*/

/** \file
   Item API implementation
   This File contains the non-inlined access to items.
*/

#include <falcon/item.h>
#include <falcon/memory.h>
#include <falcon/mempool.h>
#include <falcon/common.h>
#include <falcon/symbol.h>
#include <falcon/coreobject.h>
#include <falcon/corefunc.h>
#include <falcon/carray.h>
#include <falcon/garbagepointer.h>
#include <falcon/coredict.h>
#include <falcon/cclass.h>
#include <falcon/membuf.h>
#include <falcon/vmmaps.h>
#include <falcon/error.h>
#include <cstdlib>
#include <cstring>


namespace Falcon
{

inline void assignToVm( Garbageable *gcdata )
{
   gcdata->gcMark( memPool->generation() );
}

//=================================================================
// Garbage markers
Item::Item( byte t, Garbageable *dt )
{
   type( t );
   content( dt );
   assignToVm( dt );
}

void Item::setRange( CoreRange *r )
{
   type( FLC_ITEM_RANGE );
   all.ctx.data.content = r;
   assignToVm( r );
}


void Item::setString( String *str )
{
   type( FLC_ITEM_STRING );

   all.ctx.data.ptr.voidp = str;
   all.ctx.data.ptr.extra = 0;
   if ( str->isCore() )
      assignToVm( &static_cast<CoreString*>(str)->garbage() );
}
/*

void Item::setString( String *str, LiveModule *lm )
{
   type( FLC_ITEM_STRING );
   // must not be a non-garbage string
   fassert( ! str->isCore() );
   all.ctx.data.ptr.voidp = str;
   all.ctx.data.ptr.extra = lm;
}
*/

void Item::setArray( CoreArray *array )
{
   type( FLC_ITEM_ARRAY );
   all.ctx.data.ptr.voidp = array;
   assignToVm( array );
}

void Item::setObject( CoreObject *obj )
{
   type( FLC_ITEM_OBJECT );
   all.ctx.data.ptr.voidp = obj;
   assignToVm( obj );
}


void Item::setDict( CoreDict *dict )
{
   type( FLC_ITEM_DICT );
   all.ctx.data.ptr.voidp = dict;
   assignToVm( dict );
}


void Item::setMemBuf( MemBuf *b )
{
   type( FLC_ITEM_MEMBUF );
   all.ctx.data.ptr.voidp = b;
   assignToVm( b );
}

void Item::setReference( GarbageItem *ref )
{
   type( FLC_ITEM_REFERENCE );
   all.ctx.data.ptr.voidp = ref;
   assignToVm( ref );
}

void Item::setFunction( CoreFunc* cf )
{
   type( FLC_ITEM_FUNC );
   all.ctx.data.ptr.extra = cf;
   assignToVm( cf );
}

void Item::setLBind( String *lbind, GarbageItem *val )
{
   type( FLC_ITEM_LBIND );
   all.ctx.data.ptr.voidp = lbind;
   all.ctx.data.ptr.extra = val;

   if ( lbind->isCore() )
      assignToVm( &static_cast<CoreString*>(lbind)->garbage() );

   if ( val != 0 )
       assignToVm( val );
}

void Item::setMethod( const Item &data, CallPoint *func )
{
   *this = data;
   all.ctx.base.bits.oldType = all.ctx.base.bits.type;
   all.ctx.method = func;
   type( FLC_ITEM_METHOD );
   assignToVm( func );
}

void Item::setClassMethod( CoreObject *obj, CoreClass *cls )
{
   type( FLC_ITEM_CLSMETHOD );
   all.ctx.data.ptr.voidp = obj;
   all.ctx.data.ptr.extra = cls;
   assignToVm( obj );
   assignToVm( cls );
}

void Item::setClass( CoreClass *cls )
{
   type( FLC_ITEM_CLASS );
   // warning: class in extra to be homologue to methodClass()
   all.ctx.data.ptr.extra = cls;
   assignToVm( cls );
}

void Item::setGCPointer( FalconData *ptr )
{
   type( FLC_ITEM_GCPTR );
   all.ctx.data.content = new GarbagePointer( ptr );
   assignToVm( all.ctx.data.content );
   // Done in assingToVM
   //ptr->gcMark( memPool->generation() );
}

void Item::setGCPointer( GarbagePointer *shell )
{
   type( FLC_ITEM_GCPTR );
   all.ctx.data.content = shell;
   assignToVm( shell );
   // Done in assingToVM
   //shell->ptr()->gcMark( memPool->generation() );
}

FalconData *Item::asGCPointer() const
{
   return static_cast<GarbagePointer*>(all.ctx.data.content)->ptr();
}

GarbagePointer *Item::asGCPointerShell() const
{
   return static_cast<GarbagePointer*>(all.ctx.data.content);
}


//====================================================
// Safe items.
//

SafeItem::SafeItem( byte t, Garbageable *dt )
{
   type( t );
   content( dt );
}

void SafeItem::setRange( CoreRange *r )
{
   type( FLC_ITEM_RANGE );
   all.ctx.data.content = r;
}


void SafeItem::setString( String *str )
{
   type( FLC_ITEM_STRING );

   all.ctx.data.ptr.voidp = str;
   all.ctx.data.ptr.extra = 0;
}

void SafeItem::setArray( CoreArray *array )
{
   type( FLC_ITEM_ARRAY );
   all.ctx.data.ptr.voidp = array;
}

void SafeItem::setObject( CoreObject *obj )
{
   type( FLC_ITEM_OBJECT );
   all.ctx.data.ptr.voidp = obj;
}


void SafeItem::setDict( CoreDict *dict )
{
   type( FLC_ITEM_DICT );
   all.ctx.data.ptr.voidp = dict;
}


void SafeItem::setMemBuf( MemBuf *b )
{
   type( FLC_ITEM_MEMBUF );
   all.ctx.data.ptr.voidp = b;
}

void SafeItem::setReference( GarbageItem *ref )
{
   type( FLC_ITEM_REFERENCE );
   all.ctx.data.ptr.voidp = ref;
}

void SafeItem::setFunction( CoreFunc* cf )
{
   type( FLC_ITEM_FUNC );
   all.ctx.data.ptr.extra = cf;
}

void SafeItem::setLBind( String *lbind, GarbageItem *val )
{
   type( FLC_ITEM_LBIND );
   all.ctx.data.ptr.voidp = lbind;
   all.ctx.data.ptr.extra = val;

   if ( val != 0 )
       assignToVm( val );
}

void SafeItem::setMethod( const Item &data, CallPoint *func )
{
   copy( data );
   all.ctx.base.bits.oldType = all.ctx.base.bits.type;
   all.ctx.method = func;
   type( FLC_ITEM_METHOD );
   assignToVm( func );
}

void SafeItem::setClassMethod( CoreObject *obj, CoreClass *cls )
{
   type( FLC_ITEM_CLSMETHOD );
   all.ctx.data.ptr.voidp = obj;
   all.ctx.data.ptr.extra = cls;
}

void SafeItem::setClass( CoreClass *cls )
{
   type( FLC_ITEM_CLASS );
   // warning: class in extra to be omologue to methodClass()
   all.ctx.data.ptr.extra = cls;
}

void SafeItem::setGCPointer( FalconData *ptr )
{
   type( FLC_ITEM_GCPTR );
   all.ctx.data.content = new GarbagePointer( ptr );
}

void SafeItem::setGCPointer( GarbagePointer *shell )
{
   type( FLC_ITEM_GCPTR );
   all.ctx.data.content = shell;
}


//===========================================================================
// Generic item manipulators

bool Item::isTrue() const
{
   switch( dereference()->type() )
   {
      case FLC_ITEM_BOOL:
         return asBoolean() != 0;

      case FLC_ITEM_INT:
         return asInteger() != 0;

      case FLC_ITEM_NUM:
         return asNumeric() != 0.0;

      case FLC_ITEM_RANGE:
         return asRangeStart() != asRangeEnd() || asRangeIsOpen();

      case FLC_ITEM_STRING:
         return asString()->size() != 0;

      case FLC_ITEM_ARRAY:
         return asArray()->length() != 0;

      case FLC_ITEM_DICT:
         return asDict()->length() != 0;

      case FLC_ITEM_FUNC:
      case FLC_ITEM_OBJECT:
      case FLC_ITEM_CLASS:
      case FLC_ITEM_METHOD:
      case FLC_ITEM_MEMBUF:
      case FLC_ITEM_LBIND:
         // methods are always filled, so they are always true.
         return true;
   }

   return false;
}

/*
static int64 s_atoi( const String *cs )
{
   if ( cs->size() == 0 )
      return 0;
   const char *p =  (const char *)cs->getRawStorage() + ( cs->size() -1 );
   uint64 val = 0;
   uint64 base = 1;
   while( p > (const char *)cs->getRawStorage() ) {
      if ( *p < '0' || *p > '9' ) {
         return 0;
      }
      val += (*p-'0') * base;
      p--;
      base *= 10;
   }
   if ( *p == '-' ) return -(int64)val;
   return (int64)(val*base);
}
*/

int64 Item::forceInteger() const
{
   switch( type() ) {
      case FLC_ITEM_INT:
         return asInteger();

      case FLC_ITEM_NUM:
         return (int64) asNumeric();

      case FLC_ITEM_RANGE:
         return (int64)asRangeStart();

      case FLC_ITEM_STRING:
      {
         int64 tgt;
         if ( asString()->parseInt( tgt ) )
            return tgt;
         return 0;
      }
   }
   return 0;
}


int64 Item::forceIntegerEx() const
{
   switch( type() ) {
      case FLC_ITEM_INT:
         return asInteger();

      case FLC_ITEM_NUM:
         return (int64) asNumeric();

   }
   throw new TypeError( ErrorParam( e_param_type, __LINE__ ) );

   // to make some dumb compiler happy
   return 0;
}


numeric Item::forceNumeric() const
{
   switch( type() ) {
      case FLC_ITEM_INT:
         return (numeric) asInteger();

      case FLC_ITEM_NUM:
         return asNumeric();

      case FLC_ITEM_RANGE:
         return (numeric) asRangeStart();

      case FLC_ITEM_STRING:
      {
         double tgt;
         if ( asString()->parseDouble( tgt ) )
            return tgt;
         return 0.0;
      }
   }
   return 0.0;
}


bool Item::isOfClass( const String &className ) const
{
   switch( type() )
   {
      case FLC_ITEM_OBJECT:
         // objects may be classless or derived from exactly one class.
         return asObjectSafe()->derivedFrom( className );

      case FLC_ITEM_CLASS:
         return className == asClass()->symbol()->name() || asClass()->derivedFrom( className );
   }

   return false;
}


void Item::toString( String &target ) const
{
   target.size(0);

   switch( this->type() )
   {
      case FLC_ITEM_NIL:
         target = "Nil";
      break;

      case FLC_ITEM_UNB:
         target = "_";
      break;

      case FLC_ITEM_BOOL:
         target = asBoolean() ? "true" : "false";
      break;


      case FLC_ITEM_INT:
         target.writeNumber( this->asInteger() );
      break;

      case FLC_ITEM_RANGE:
         target = "[";
         target.writeNumber( (int64) this->asRangeStart() );
         target += ":";
         if ( ! this->asRangeIsOpen() )
         {
            target.writeNumber( (int64) this->asRangeEnd() );
            if ( this->asRangeStep() != 0 )
            {
               target += ":";
               target.writeNumber( (int64) this->asRangeStep() );
            }
         }
         target += "]";
      break;

      case FLC_ITEM_NUM:
      {
         target.writeNumber( this->asNumeric(), "%.16g" );
      }
      break;

      case FLC_ITEM_MEMBUF:
         target = "MemBuf( ";
         target.writeNumber( (int64) this->asMemBuf()->length() );
         target += ", ";
            target.writeNumber( (int64) this->asMemBuf()->wordSize() );
         target += " )";
      break;

      case FLC_ITEM_STRING:
         target = *asString();
      break;

      case FLC_ITEM_LBIND:
         if ( isFutureBind() )
         {
            String temp;
            asFutureBind().toString(temp);
            target = *asLBind() + "|" + temp;
         }
         else
            target = "&" + *asLBind();
      break;

      case FLC_ITEM_REFERENCE:
         dereference()->toString( target );
      break;

      case FLC_ITEM_OBJECT:
         target = "Object from " + asObjectSafe()->generator()->symbol()->name();
      break;

      case FLC_ITEM_ARRAY:
         target = "Array";
      break;

      case FLC_ITEM_DICT:
         target = "Dictionary";
      break;

      case FLC_ITEM_FUNC:
         target = "Function " + this->asFunction()->symbol()->name();
      break;

      case FLC_ITEM_CLASS:
         target = "Class " + this->asClass()->symbol()->name();
      break;

      case FLC_ITEM_METHOD:
         {
            Item orig;
            this->getMethodItem( orig );
            String temp;
            orig.dereference()->toString( temp );
            target = "Method (" + temp + ")." + this->asMethodFunc()->name();
         }
      break;

      case FLC_ITEM_CLSMETHOD:
         target = "ClsMethod " + this->asMethodClass()->symbol()->name();
      break;

      default:
         target = "<?>";
   }
}

void Item::typeName( String &target ) const
{
   target.size(0);

   switch( this->type() )
   {
      case FLC_ITEM_NIL:
         target = "Nil";
      break;

      case FLC_ITEM_UNB:
         target = "Unbound";
      break;

      case FLC_ITEM_BOOL:
         target = "Bool";
      break;


      case FLC_ITEM_INT:
         target = "Int";
      break;

      case FLC_ITEM_RANGE:
         target = "Range";
      break;

      case FLC_ITEM_NUM:
         target = "Numeric";
      break;

      case FLC_ITEM_MEMBUF:
         target = "MemBuf";
      break;

      case FLC_ITEM_STRING:
         target = "String";
      break;

      case FLC_ITEM_LBIND:
         target = "LBind";
      break;

      case FLC_ITEM_REFERENCE:
         target = "Reference";
      break;

      case FLC_ITEM_OBJECT:
         target = asObjectSafe()->generator()->symbol()->name();
      break;

      case FLC_ITEM_ARRAY:
         target = "Array";
      break;

      case FLC_ITEM_DICT:
         target = "Dictionary";
      break;

      case FLC_ITEM_FUNC:
         target = "Function";
      break;

      case FLC_ITEM_CLASS:
         target = this->asClass()->symbol()->name();
      break;

      case FLC_ITEM_METHOD:
         target = "Method";
      break;

      case FLC_ITEM_CLSMETHOD:
         target = "ClsMethod";
      break;

      default:
         target = "<?>";
   }
}

bool Item::methodize( const Item &self )
{
   Item *data = dereference();

   switch( data->type() )
   {
      case FLC_ITEM_FUNC:
      {
         data->setMethod( self, data->asFunction() );
      }
      return true;

      case FLC_ITEM_ARRAY:
      {
         CoreArray& arr = *asArray();
         // even if arr[0] is not an array, the check is harmless, as we check by ptr value.
         if ( arr.canBeMethod() && arr.length() > 0 && arr[0].asArray() != &arr && arr[0].isCallable() )
         {
            data->setMethod( self, &arr );
            return true;
         }
      }
      return false;
   }

   return false;
}

bool Item::isCallable() const
{
   if ( isClass() || isFunction() || isMethod() )
      return true;

   if( isObject() )
   {
      return asObjectSafe()->hasProperty( OVERRIDE_OP_CALL );
   }

   //a bit more complex: a callable array...
   if( type() == FLC_ITEM_ARRAY )
   {
      CoreArray& arr = *asArray();
      if ( arr.length() > 0 )
      {
         // avoid infinite recursion.
         // even if arr[0] is not an array, the check is harmless, as we check by ptr value.
         return arr[0].asArray() != &arr && arr[0].isCallable();
      }
   }

   // in all the other cases, the item is not callable
   return false;
}

bool Item::canBeMethod() const
{
   if ( isFunction() || isMethod() )
      return true;

   //a bit more complex: a callable array...
   if( type() == FLC_ITEM_ARRAY )
   {
      CoreArray& arr = *asArray();
      if ( ! arr.canBeMethod() )
         return false;

      if ( arr.length() > 0 )
      {
         // avoid infinite recursion.
         // even if arr[0] is not an array, the check is harmless, as we check by ptr value.
         return arr[0].asArray() != &arr && arr[0].isCallable();
      }
   }

   // in all the other cases, the item is not callable
   return false;
}

const Item &Item::asFutureBind() const {
   return ((GarbageItem*)all.ctx.data.ptr.extra)->origin();
}

Item &Item::asFutureBind() {
   return ((GarbageItem*)all.ctx.data.ptr.extra)->origin();
}

CoreObject *Item::asObject() const {
   if ( ! isObject() )
      throw new CodeError( ErrorParam( e_static_call, __LINE__ ) );

   return (CoreObject *) all.ctx.data.ptr.voidp;
}



bool Item::exactlyEqual( const Item& other ) const
{
   if ( type() != other.type() )
   {
      return false;
   }

   switch( type() )
   {
      case FLC_ITEM_NIL: case FLC_ITEM_UNB:
         return true;
      
      case FLC_ITEM_INT:
         return asInteger() == other.asInteger();
      
      case FLC_ITEM_NUM:
         return asNumeric() == other.asNumeric();
         
      case FLC_ITEM_RANGE:
         if( asRangeIsOpen() != other.asRangeIsOpen() )
            return false;
         if ( asRangeStart() != other.asRangeStart() )
            return false;
         if ( asRangeStep() != other.asRangeStep() )
            return false;
         if ( ! asRangeIsOpen() &&
            (asRangeEnd() != other.asRangeEnd() ) )
            return false;
         return true;
      
      case FLC_ITEM_STRING:
         return *asString() == *other.asString();
      
      case FLC_ITEM_METHOD:
         if ( asMethodFunc() == other.asMethodFunc() )
         {
            return asMethodItem().exactlyEqual(other.asMethodItem() );
         } 
         return false;
      
      case FLC_ITEM_CLSMETHOD:
         if ( asObjectSafe()  != other.asObjectSafe() )
            return false;
         // fallthrough
         
      case FLC_ITEM_FUNC:
      case FLC_ITEM_CLASS:
         return asClass() == other.asClass();
   }
   
   // the default is to check for the voidp element in data
   return asObjectSafe() == other.asObjectSafe(); 
}

}

/* end of item.cpp */