bool SQClosure::Load( SQVM *v, SQUserPointer up, SQREADFUNC read ) { _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_HEAD ) ); _CHECK_IO( CheckTag( v, read, up, sizeof( SQChar ) ) ); _CHECK_IO( _funcproto( _function )->Load( v, up, read ) ); _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_TAIL ) ); return true; }
bool ReadObject(HSQUIRRELVM v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &o) { SQObjectType t; _CHECK_IO(SafeRead(v,read,up,&t,sizeof(SQObjectType))); switch(t){ case OT_STRING:{ SQInteger len; _CHECK_IO(SafeRead(v,read,up,&len,sizeof(SQInteger))); _CHECK_IO(SafeRead(v,read,up,_ss(v)->GetScratchPad(rsl(len)),rsl(len))); o=SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1),len); } break; case OT_INTEGER:{ SQInteger i; _CHECK_IO(SafeRead(v,read,up,&i,sizeof(SQInteger))); o = i; break; } case OT_FLOAT:{ SQFloat f; _CHECK_IO(SafeRead(v,read,up,&f,sizeof(SQFloat))); o = f; break; } case OT_NULL: o=_null_; break; default: v->Raise_Error(_SC("cannot serialize a %s"),IdType2Name(t)); return false; } return true; }
bool SQClosure::Save( SQVM *v, SQUserPointer up, SQWRITEFUNC write ) { _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_HEAD ) ); _CHECK_IO( WriteTag( v, write, up, sizeof( SQChar ) ) ); _CHECK_IO( _funcproto( _function )->Save( v, up, write ) ); _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_TAIL ) ); return true; }
bool SQClosure::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret) { _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_HEAD)); _CHECK_IO(CheckTag(v,read,up,sizeof(SQChar))); SQObjectPtr func; _CHECK_IO(SQFunctionProto::Load(v,up,read,func)); _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_TAIL)); ret = SQClosure::Create(_ss(v),_funcproto(func)); return true; }
bool CheckTag( HSQUIRRELVM v, SQWRITEFUNC read, SQUserPointer up, SQInteger tag ) { SQInteger t; _CHECK_IO( SafeRead( v, read, up, &t, sizeof( t ) ) ); if ( t != tag ) { v->Raise_Error( _SC( "invalid or corrupted closure stream" ) ); return false; } return true; }
bool WriteObject( HSQUIRRELVM v, SQUserPointer up, SQWRITEFUNC write, SQObjectPtr &o ) { _CHECK_IO( SafeWrite( v, write, up, &type( o ), sizeof( SQObjectType ) ) ); switch ( type( o ) ) { case OT_STRING: _CHECK_IO( SafeWrite( v, write, up, &_string( o )->_len, sizeof( SQInteger ) ) ); _CHECK_IO( SafeWrite( v, write, up, _stringval( o ), rsl( _string( o )->_len ) ) ); break; case OT_INTEGER: _CHECK_IO( SafeWrite( v, write, up, &_integer( o ), sizeof( SQInteger ) ) );break; case OT_FLOAT: _CHECK_IO( SafeWrite( v, write, up, &_float( o ), sizeof( SQFloat ) ) );break; case OT_NULL: break; default: v->Raise_Error( _SC( "cannot serialize a %s" ), GetTypeName( o ) ); return false; } return true; }
bool SQFunctionProto::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret) { SQInteger i, nliterals,nparameters; SQInteger noutervalues ,nlocalvarinfos ; SQInteger nlineinfos,ninstructions ,nfunctions,ndefaultparams ; SQObjectPtr sourcename, name; SQObjectPtr o; _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(ReadObject(v, up, read, sourcename)); _CHECK_IO(ReadObject(v, up, read, name)); _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeRead(v,read,up, &nliterals, sizeof(nliterals))); _CHECK_IO(SafeRead(v,read,up, &nparameters, sizeof(nparameters))); _CHECK_IO(SafeRead(v,read,up, &noutervalues, sizeof(noutervalues))); _CHECK_IO(SafeRead(v,read,up, &nlocalvarinfos, sizeof(nlocalvarinfos))); _CHECK_IO(SafeRead(v,read,up, &nlineinfos, sizeof(nlineinfos))); _CHECK_IO(SafeRead(v,read,up, &ndefaultparams, sizeof(ndefaultparams))); _CHECK_IO(SafeRead(v,read,up, &ninstructions, sizeof(ninstructions))); _CHECK_IO(SafeRead(v,read,up, &nfunctions, sizeof(nfunctions))); SQFunctionProto *f = SQFunctionProto::Create(ninstructions,nliterals,nparameters, nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams); SQObjectPtr proto = f; //gets a ref in case of failure f->_sourcename = sourcename; f->_name = name; _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); for(i = 0;i < nliterals; i++){ _CHECK_IO(ReadObject(v, up, read, o)); f->_literals[i] = o; } _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); for(i = 0; i < nparameters; i++){ _CHECK_IO(ReadObject(v, up, read, o)); f->_parameters[i] = o; } _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); for(i = 0; i < noutervalues; i++){ SQUnsignedInteger type; SQObjectPtr name; _CHECK_IO(SafeRead(v,read,up, &type, sizeof(SQUnsignedInteger))); _CHECK_IO(ReadObject(v, up, read, o)); _CHECK_IO(ReadObject(v, up, read, name)); f->_outervalues[i] = SQOuterVar(name,o, (SQOuterType)type); } _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); for(i = 0; i < nlocalvarinfos; i++){ SQLocalVarInfo lvi; _CHECK_IO(ReadObject(v, up, read, lvi._name)); _CHECK_IO(SafeRead(v,read,up, &lvi._pos, sizeof(SQUnsignedInteger))); _CHECK_IO(SafeRead(v,read,up, &lvi._start_op, sizeof(SQUnsignedInteger))); _CHECK_IO(SafeRead(v,read,up, &lvi._end_op, sizeof(SQUnsignedInteger))); f->_localvarinfos[i] = lvi; } _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeRead(v,read,up, f->_lineinfos, sizeof(SQLineInfo)*nlineinfos)); _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeRead(v,read,up, f->_defaultparams, sizeof(SQInteger)*ndefaultparams)); _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeRead(v,read,up, f->_instructions, sizeof(SQInstruction)*ninstructions)); _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART)); for(i = 0; i < nfunctions; i++){ _CHECK_IO(_funcproto(o)->Load(v, up, read, o)); f->_functions[i] = o; } _CHECK_IO(SafeRead(v,read,up, &f->_stacksize, sizeof(f->_stacksize))); _CHECK_IO(SafeRead(v,read,up, &f->_bgenerator, sizeof(f->_bgenerator))); _CHECK_IO(SafeRead(v,read,up, &f->_varparams, sizeof(f->_varparams))); ret = f; return true; }
bool SQFunctionProto::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write) { SQInteger i,nliterals = _nliterals,nparameters = _nparameters; SQInteger noutervalues = _noutervalues,nlocalvarinfos = _nlocalvarinfos; SQInteger nlineinfos=_nlineinfos,ninstructions = _ninstructions,nfunctions=_nfunctions; SQInteger ndefaultparams = _ndefaultparams; _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(WriteObject(v,up,write,_sourcename)); _CHECK_IO(WriteObject(v,up,write,_name)); _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeWrite(v,write,up,&nliterals,sizeof(nliterals))); _CHECK_IO(SafeWrite(v,write,up,&nparameters,sizeof(nparameters))); _CHECK_IO(SafeWrite(v,write,up,&noutervalues,sizeof(noutervalues))); _CHECK_IO(SafeWrite(v,write,up,&nlocalvarinfos,sizeof(nlocalvarinfos))); _CHECK_IO(SafeWrite(v,write,up,&nlineinfos,sizeof(nlineinfos))); _CHECK_IO(SafeWrite(v,write,up,&ndefaultparams,sizeof(ndefaultparams))); _CHECK_IO(SafeWrite(v,write,up,&ninstructions,sizeof(ninstructions))); _CHECK_IO(SafeWrite(v,write,up,&nfunctions,sizeof(nfunctions))); _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); for(i=0;i<nliterals;i++){ _CHECK_IO(WriteObject(v,up,write,_literals[i])); } _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); for(i=0;i<nparameters;i++){ _CHECK_IO(WriteObject(v,up,write,_parameters[i])); } _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); for(i=0;i<noutervalues;i++){ _CHECK_IO(SafeWrite(v,write,up,&_outervalues[i]._type,sizeof(SQUnsignedInteger))); _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._src)); _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._name)); } _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); for(i=0;i<nlocalvarinfos;i++){ SQLocalVarInfo &lvi=_localvarinfos[i]; _CHECK_IO(WriteObject(v,up,write,lvi._name)); _CHECK_IO(SafeWrite(v,write,up,&lvi._pos,sizeof(SQUnsignedInteger))); _CHECK_IO(SafeWrite(v,write,up,&lvi._start_op,sizeof(SQUnsignedInteger))); _CHECK_IO(SafeWrite(v,write,up,&lvi._end_op,sizeof(SQUnsignedInteger))); } _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeWrite(v,write,up,_lineinfos,sizeof(SQLineInfo)*nlineinfos)); _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeWrite(v,write,up,_defaultparams,sizeof(SQInteger)*ndefaultparams)); _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); _CHECK_IO(SafeWrite(v,write,up,_instructions,sizeof(SQInstruction)*ninstructions)); _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART)); for(i=0;i<nfunctions;i++){ _CHECK_IO(_funcproto(_functions[i])->Save(v,up,write)); } _CHECK_IO(SafeWrite(v,write,up,&_stacksize,sizeof(_stacksize))); _CHECK_IO(SafeWrite(v,write,up,&_bgenerator,sizeof(_bgenerator))); _CHECK_IO(SafeWrite(v,write,up,&_varparams,sizeof(_varparams))); return true; }
bool SQFunctionProto::Load( SQVM *v, SQUserPointer up, SQREADFUNC read ) { SQInteger i, nsize = _literals.size(); SQObjectPtr o; _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( ReadObject( v, up, read, _sourcename ) ); _CHECK_IO( ReadObject( v, up, read, _name ) ); _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeRead( v, read, up, &nsize, sizeof( nsize ) ) ); for ( i = 0;i < nsize; i++ ) { _CHECK_IO( ReadObject( v, up, read, o ) ); _literals.push_back( o ); } _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeRead( v, read, up, &nsize, sizeof( nsize ) ) ); for ( i = 0; i < nsize; i++ ) { _CHECK_IO( ReadObject( v, up, read, o ) ); _parameters.push_back( o ); } _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeRead( v, read, up, &nsize, sizeof( nsize ) ) ); for ( i = 0; i < nsize; i++ ) { SQUnsignedInteger type; SQObjectPtr name; _CHECK_IO( SafeRead( v, read, up, &type, sizeof( SQUnsignedInteger ) ) ); _CHECK_IO( ReadObject( v, up, read, o ) ); _CHECK_IO( ReadObject( v, up, read, name ) ); _outervalues.push_back( SQOuterVar( name, o, ( SQOuterType )type ) ); } _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeRead( v, read, up, &nsize, sizeof( nsize ) ) ); for ( i = 0; i < nsize; i++ ) { SQLocalVarInfo lvi; _CHECK_IO( ReadObject( v, up, read, lvi._name ) ); _CHECK_IO( SafeRead( v, read, up, &lvi._pos, sizeof( SQUnsignedInteger ) ) ); _CHECK_IO( SafeRead( v, read, up, &lvi._start_op, sizeof( SQUnsignedInteger ) ) ); _CHECK_IO( SafeRead( v, read, up, &lvi._end_op, sizeof( SQUnsignedInteger ) ) ); _localvarinfos.push_back( lvi ); } _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeRead( v, read, up, &nsize, sizeof( nsize ) ) ); _lineinfos.resize( nsize ); _CHECK_IO( SafeRead( v, read, up, &_lineinfos[0], sizeof( SQLineInfo )*nsize ) ); _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeRead( v, read, up, &nsize, sizeof( nsize ) ) ); _instructions.resize( nsize ); _CHECK_IO( SafeRead( v, read, up, &_instructions[0], sizeof( SQInstruction )*nsize ) ); _CHECK_IO( CheckTag( v, read, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeRead( v, read, up, &nsize, sizeof( nsize ) ) ); for ( i = 0; i < nsize; i++ ) { o = SQFunctionProto::Create(); _CHECK_IO( _funcproto( o )->Load( v, up, read ) ); _functions.push_back( o ); } _CHECK_IO( SafeRead( v, read, up, &_stacksize, sizeof( _stacksize ) ) ); _CHECK_IO( SafeRead( v, read, up, &_bgenerator, sizeof( _bgenerator ) ) ); _CHECK_IO( SafeRead( v, read, up, &_varparams, sizeof( _varparams ) ) ); return true; }
bool SQFunctionProto::Save( SQVM *v, SQUserPointer up, SQWRITEFUNC write ) { SQInteger i, nsize = _literals.size(); _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( WriteObject( v, up, write, _sourcename ) ); _CHECK_IO( WriteObject( v, up, write, _name ) ); _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); _CHECK_IO( SafeWrite( v, write, up, &nsize, sizeof( nsize ) ) ); for ( i = 0;i < nsize;i++ ) { _CHECK_IO( WriteObject( v, up, write, _literals[i] ) ); } _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); nsize = _parameters.size(); _CHECK_IO( SafeWrite( v, write, up, &nsize, sizeof( nsize ) ) ); for ( i = 0;i < nsize;i++ ) { _CHECK_IO( WriteObject( v, up, write, _parameters[i] ) ); } _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); nsize = _outervalues.size(); _CHECK_IO( SafeWrite( v, write, up, &nsize, sizeof( nsize ) ) ); for ( i = 0;i < nsize;i++ ) { _CHECK_IO( SafeWrite( v, write, up, &_outervalues[i]._type, sizeof( SQUnsignedInteger ) ) ); _CHECK_IO( WriteObject( v, up, write, _outervalues[i]._src ) ); _CHECK_IO( WriteObject( v, up, write, _outervalues[i]._name ) ); } _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); nsize = _localvarinfos.size(); _CHECK_IO( SafeWrite( v, write, up, &nsize, sizeof( nsize ) ) ); for ( i = 0;i < nsize;i++ ) { SQLocalVarInfo &lvi = _localvarinfos[i]; _CHECK_IO( WriteObject( v, up, write, lvi._name ) ); _CHECK_IO( SafeWrite( v, write, up, &lvi._pos, sizeof( SQUnsignedInteger ) ) ); _CHECK_IO( SafeWrite( v, write, up, &lvi._start_op, sizeof( SQUnsignedInteger ) ) ); _CHECK_IO( SafeWrite( v, write, up, &lvi._end_op, sizeof( SQUnsignedInteger ) ) ); } _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); nsize = _lineinfos.size(); _CHECK_IO( SafeWrite( v, write, up, &nsize, sizeof( nsize ) ) ); _CHECK_IO( SafeWrite( v, write, up, &_lineinfos[0], sizeof( SQLineInfo )*nsize ) ); _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); nsize = _instructions.size(); _CHECK_IO( SafeWrite( v, write, up, &nsize, sizeof( nsize ) ) ); _CHECK_IO( SafeWrite( v, write, up, &_instructions[0], sizeof( SQInstruction )*nsize ) ); _CHECK_IO( WriteTag( v, write, up, SQ_CLOSURESTREAM_PART ) ); nsize = _functions.size(); _CHECK_IO( SafeWrite( v, write, up, &nsize, sizeof( nsize ) ) ); for ( i = 0;i < nsize;i++ ) { _CHECK_IO( _funcproto( _functions[i] )->Save( v, up, write ) ); } _CHECK_IO( SafeWrite( v, write, up, &_stacksize, sizeof( _stacksize ) ) ); _CHECK_IO( SafeWrite( v, write, up, &_bgenerator, sizeof( _bgenerator ) ) ); _CHECK_IO( SafeWrite( v, write, up, &_varparams, sizeof( _varparams ) ) ); return true; }