/** setSlot( CLASS:Class, POSITION:Small, METHOD:Method ) 1. The method is inserted into the correct position in the class's slot array. To start with, only one method per slot will be permitted. 2. A call to setMethod is then made with an unsafe access function as the method's function. The values are passed on the stack. No stack checks are needed as the size of the argument lists of the two functions are the same. */ Ref * sysSetSlot( Ref * pc, MachineClass * vm ) { if ( vm->count != 3 ) throw Ginger::Mishap( "Wrong number of arguments" ); Ref method = vm->fastPop(); Ref position = vm->fastPop(); Ref gclass = vm->fastPop(); if ( !IsMethod( method ) ) throw Ginger::Mishap( "Method needed" ).culprit( "Method", refToString( method ) ); if ( !IsSmall( position ) ) throw Ginger::Mishap( "Small needed" ).culprit( "Position", refToString( position ) ); if ( !IsClass( gclass ) ) throw Ginger::Mishap( "Class needed" ).culprit( "Class", refToString( gclass ) ); long pos = SmallToLong( position ); long nfields = SmallToLong( RefToPtr4( gclass )[ CLASS_OFFSET_NFIELDS ] ); if ( not( 1 <= pos && pos <= nfields ) ) { throw Ginger::Mishap( "Position out of range" ). culprit( "Position", pos ). culprit( "Number of fields", nfields ) ; } // Update the class-slot. INDEX( INDEX( gclass, CLASS_OFFSET_SLOTS ), pos ) = method; // Push onto the stack to get protection from garbage collection. vm->fastPush( gclass ); vm->fastPush( method ); // ENDFUNCTION does not in fact cause a garbage collection, as it // forces the heap to grow. However this is a more accurate way // to write the code. // // The following block should not be in-lined but extracted as a // service function. { CodeGen codegen = vm->codegen(); // TODO: Supply a useful name. codegen->vmiFUNCTION( 1, 1 ); codegen->vmiFIELD( pos ); codegen->vmiSYS_RETURN(); vm->fastPush( codegen->vmiENDFUNCTION() ); } // We do not need to modify vm->count, it's already 3. // Simply chain into sysSetMethod. return sysSetMethod( pc, vm ); }
Ref * sysNewMethod( Ref * pc, MachineClass * vm ) { // newMethod( name:String, ninputs:Small, noutputs:Small ) -> m:Method if ( vm->count != 3 ) throw Ginger::Mishap( "Wrong number of arguments" ); Ref noutputs = vm->fastPop(); Ref ninputs = vm->fastPop(); Ref name = vm->fastPop(); if ( !IsSmall( noutputs ) || !IsSmall( ninputs ) ) throw Ginger::Mishap( "Invalid arguments" ).culprit( "#Outputs", refToString( noutputs ) ).culprit( "Inputs", refToString( ninputs ) ); CodeGen codegen = vm->codegen(); codegen->vmiFUNCTION( name == SYS_ABSENT ? std::string( EMPTY_FN_NAME ) : refToString( name ), SmallToLong( ninputs ), SmallToLong( noutputs ) ); codegen->vmiINVOKE(); Ref r = codegen->vmiENDFUNCTION( sysMethodKey ); vm->fastPush( r ); // No check needed, as stack has room for 3. return pc; }
long sizeAfterKeyOfVectorLayout( Ref * key ) { return SmallToLong( key[ VECTOR_OFFSET_LENGTH ] ); //return ToULong( *( key - 1 ) ) >> TAG; }
long getLong() const { return SmallToLong( this->ref ); }