File: coreslot.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 (439 lines) | stat: -rw-r--r-- 10,146 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
/*
   FALCON - The Falcon Programming Language.
   FILE: coreslot.cpp

   Core Slot - Messaging slot system.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sun, 11 Jan 2009 18:28:35 +0100

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

   See LICENSE file for licensing details.
*/

#include <falcon/coreslot.h>
#include <falcon/vm.h>
#include <falcon/traits.h>
#include <falcon/deepitem.h>
#include <falcon/vmmsg.h>
#include <falcon/iterator.h>

#include <stdio.h>

namespace Falcon {

bool coreslot_broadcast_internal( VMachine *vm )
{
   Iterator *ci = dyncast< Iterator *>( vm->local(0)->asGCPointer() );
   VMMessage* msg = 0;
   Item *msgItem = vm->local(4);
   if( msgItem->isInteger() )
   {
      msg = (VMMessage*) msgItem->asInteger();
   }

   if ( ! ci->hasCurrent() )
   {
      // child call?
      if ( vm->local(5)->isNil() )
      {
         // were we called after a message?
         if( msg != 0 )
         {
            msg->onMsgComplete( true );
            delete msg;
         }

         // let the GC pointer to take care of the ietrator.
         return false;
      }

      // we have a child that wants to get the message too.
      ci = dyncast< Iterator *>( vm->local(5)->asGCPointer() );
      *vm->local(0) = *vm->local(5);
      vm->local(5)->setNil();
   }

   Item current = ci->getCurrent();
   int baseParamCount = 0;

   if ( ! current.isCallable() )
   {
      const String& name = *vm->local(3)->asString();
      if( name.size() != 0 )
      {
         if( current.isComposed() )
         {
            // may throw
            try {
               current.asDeepItem()->readProperty( "on_" + name, current );
            }
            catch( Error* err )
            {
               // see if we can get "__on_event"
               try
               {
                  current.asDeepItem()->readProperty( "__on_event", current );
                  // yes? -- ignore the previous error.
                  err->decref();
                  // and push the event name
                  vm->pushParam( new CoreString(name) );
                  baseParamCount = 1;
               }
               catch( Error* err1 )
               {
                  // no? -- throw the original error
                  err1->decref();
                  throw err;
               }
            }
         }
         else
         {
            throw new CodeError( ErrorParam( e_non_callable, __LINE__ ).extra( "broadcast" ) );
         }
      }
      else
      {
         // ignore this item.
         ci->next();
         return true;
      }
   }

   int64 pfirst = vm->local(1)->asInteger();
   if( pfirst < 0 )
   {
      Item cache = *vm->local( 2 );
      vm->pushParam( cache );
      vm->callFrame( current, 1 + baseParamCount );
   }
   else
   {
      int64 paramCount = 0;

      if( msg == 0 )
      {
         paramCount = vm->local(2)->asInteger();
         for( int32 i = 0; i < paramCount; i++ )
         {
            // we don't want to dereference the parameter, so we
            // access it directly instead of use vm->param()
            Item cache = vm->currentFrame()->m_params[ i + pfirst ];
            vm->pushParam( cache );
         }
      }
      else
      {
         paramCount = msg->paramCount();
         for ( uint32 i = 0; i < paramCount; ++i )
         {
            vm->pushParam( *msg->param(i) );
         }
      }

      vm->callFrame( current, (int32)paramCount + baseParamCount );
   }

   ci->next();
   return true;
}


CoreSlot::~CoreSlot()
{
   if( m_children != 0 )
   {
      // to avoid double deletion, we remove the child from the map
      /*
      I disable it because I am not sure it's possible to make loops,
      and the deletor takes already care of the items.
      while( ! m_children->empty() )
      {
         MapIterator iter = m_children->begin();
         CoreSlot* sl = iter.currentValue();
         m_children->erase( iter );
         sl->decref();
      }*/

      delete m_children;
      m_children = 0;
   }
}


void CoreSlot::prepareBroadcast( VMContext *vmc, uint32 pfirst, uint32 pcount, VMMessage *msg, String* msgName )
{
   // no listeners?
   if( empty() )
   {
      // have we receiving a named message and do we have a child?
      if( msgName != 0 )
      {
         CoreSlot* child = getChild( *msgName, false );
         if( child != 0 )
         {
            child->prepareBroadcast( vmc, pfirst, pcount, msg, msgName );
         }
      }

      return;
   }

   Iterator* iter = new Iterator( this );

   // we don't need to set the slot as owner, as we're sure it stays in range
   // (slots are marked) on themselves.
   vmc->addLocals( 6 );
   vmc->local(0)->setGCPointer( iter );
   *vmc->local(1) = (int64) pfirst;
   *vmc->local(2) = (int64) pcount;
   *vmc->local(3) = new CoreString( msgName == 0 ? m_name : *msgName );

   if ( msg != 0 )
   {
      // store it as an opaque pointer.
      vmc->local(4)->setInteger( (int64) msg );
   }

   // have we received a named message and do we have a child?
   if( msgName != 0 )
   {
      CoreSlot* child = getChild( *msgName, false );
      if( child != 0 )
      {
         vmc->local(5)->setGCPointer( new Iterator(child) );
      }
   }

   vmc->returnHandler( &coreslot_broadcast_internal );
}


bool CoreSlot::remove( const Item &subscriber )
{
   Iterator iter( this );

   while( iter.hasCurrent() )
   {
      if ( iter.getCurrent() == subscriber )
      {
         erase( iter );
         return true;
      }

      iter.next();
   }

   return false;
}

CoreSlot *CoreSlot::clone() const
{
   incref();
   return const_cast<CoreSlot*>(this);
}


CoreSlot* CoreSlot::getChild( const String& name, bool create )
{
   if ( m_children == 0 )
   {
      if ( ! create )
         return 0;

      // create the children map
      m_children = new Map( &traits::t_string(), &traits::t_coreslotptr() );
   }

   // we have already a map.
   void* vchild = m_children->find( &name );

   if( vchild == 0 )
   {
      if ( ! create )
         return 0;

      CoreSlot* child = new CoreSlot( name );
      m_children->insert( &name, child );
      return child;
   }

   // no need to incref it if it's already in the map!

   return *(CoreSlot**) vchild;
}


void CoreSlot::gcMark( uint32 mark )
{
   if ( m_bHasAssert )
      memPool->markItem( m_assertion );

   ItemList::gcMark( mark );
}

void CoreSlot::setAssertion( VMachine* vm, const Item &a )
{
   setAssertion( a );
   if ( ! empty() )
   {
      vm->addLocals( 6 ); // Warning -- we add 5 to nil the msg ptr callback at local(4).
      Iterator* iter = new Iterator( this );
      // we don't need to set the slot as owner, as we're sure it stays in range
      // (slots are marked) on themselves.
      vm->local(0)->setGCPointer( iter );
      *vm->local(1) = (int64) -1;
      *vm->local(2) = m_assertion;
      *vm->local(3) = new CoreString( m_name );
      // local(4) must stay nil; it's used by broadcast_internal as msg callback pointer.
      // local(4) must stay nil; it's used by broadcast_internal as msg callback pointer.


      vm->returnHandler( &coreslot_broadcast_internal );
   }
}


void CoreSlot::incref() const
{
   m_mtx.lock();
   m_refcount++;
   m_mtx.unlock();
}

void CoreSlot::decref()
{
   m_mtx.lock();
   bool bdel = --m_refcount == 0;
   m_mtx.unlock();

   if (bdel)
   {
      delete this;
   }
}

void CoreSlot::getIterator( Iterator& tgt, bool tail ) const
{
   ItemList::getIterator( tgt, tail );
   const_cast<CoreSlot*>(this)->incref();
}
void CoreSlot::copyIterator( Iterator& tgt, const Iterator& source ) const
{
   ItemList::copyIterator( tgt, source );
   const_cast<CoreSlot*>(this)->incref();
}

void CoreSlot::disposeIterator( Iterator& tgt ) const
{
   ItemList::disposeIterator( tgt );
   const_cast<CoreSlot*>(this)->decref();
}

//=============================================================
// Carrier for VM
//

CoreSlotCarrier::CoreSlotCarrier( const CoreClass* generator, CoreSlot* cs, bool bSeralizing ):
   FalconObject( generator, bSeralizing )
{
   if( cs != 0 )
   {
      cs->incref();
      setUserData( cs );
   }
}

CoreSlotCarrier::CoreSlotCarrier( const CoreSlotCarrier &other ):
   FalconObject( other )
{
   // FalconObject takes care of cloning (increffing) the inner data.
}

CoreSlotCarrier::~CoreSlotCarrier()
{
    CoreSlot* cs = (CoreSlot*) m_user_data;
    cs->decref();
    // sterilize downward destructors
    m_user_data = 0;
}

void CoreSlotCarrier::setSlot( CoreSlot* cs )
{
   CoreSlot* old_cs = (CoreSlot*) m_user_data;
   if ( old_cs  != 0 )
      old_cs->decref();
   cs->incref();

   setUserData( cs );
}


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

CoreObject* CoreSlotFactory( const CoreClass *cls, void *user_data, bool bDeserial )
{
   return new CoreSlotCarrier( cls, (CoreSlot *) user_data );
}

//=============================================================
// Traits for maps
//
namespace traits
{
   CoreSlotPtrTraits &t_coreslotptr() { static CoreSlotPtrTraits dt; return dt; }
}


uint32 CoreSlotPtrTraits::memSize() const
{
   return sizeof( CoreSlot * );
}

void CoreSlotPtrTraits::init( void *itemZone ) const
{
   itemZone = 0;
}

void CoreSlotPtrTraits::copy( void *targetZone, const void *sourceZone ) const
{
   CoreSlot **target = (CoreSlot **) targetZone;
   CoreSlot *source = (CoreSlot *) sourceZone;

   *target = source;
   if( source != 0 )
      source->incref();
}

int CoreSlotPtrTraits::compare( const void *firstz, const void *secondz ) const
{
   if ( sizeof(int) == sizeof(void*))
      return (int)(((int64)firstz) - ((int64)secondz));
   else
      return (((int64)firstz) > ((int64)secondz)) ? -1 :
	     (((int64)firstz) < ((int64)secondz)) ? 1 : 0;
}

void CoreSlotPtrTraits::destroy( void *item ) const
{
   CoreSlot *ptr = *(CoreSlot **) item;
   ptr->decref();
}

bool CoreSlotPtrTraits::owning() const
{
   return true;
}



}

/* end of coreslot.cpp */