16 #include "../clib/clib.h" 17 #include "../clib/fixalloc.h" 18 #include "../clib/logfacility.h" 19 #include "../clib/random.h" 20 #include "../clib/rawtypes.h" 21 #include "../clib/refptr.h" 22 #include "../clib/stlutil.h" 35 #include <unordered_map> 50 return sizeof(
BObjectRef ) +
get()->sizeEstimate();
57 return sizeof(
BObject ) + objimp.get()->sizeEstimate();
111 return new BError(
"Unknown object type '" + std::string( 1, typech ) +
"'" );
116 return new BError(
"Unable to extract type character" );
131 return new BObject( objimp->copy() );
136 return *objimp != *( obj.
objimp );
140 return *objimp == *( obj.
objimp );
144 return *objimp < *( obj.
objimp );
148 return *objimp <= *( obj.
objimp );
152 return *objimp > *( obj.
objimp );
156 return *objimp >= *( obj.
objimp );
161 typedef std::unordered_map<unsigned int, BObjectImp*>
bobjectimps;
172 INFO_PRINT <<
"bobjectimp instances: " << bobjectimp_instances.size() <<
"\n";
173 for ( bobjectimps::iterator itr = bobjectimp_instances.begin(); itr != bobjectimp_instances.end();
181 #if !INLINE_BOBJECTIMP_CTOR 251 return "FunctionReference";
274 return (
this == &objimp );
294 return (
this < &objimp );
304 return *
this == objimp || *
this < objimp;
311 return !( *
this == objimp || *
this < objimp );
318 return !( *
this < objimp );
325 return !( *
this == objimp );
338 if ( indices.empty() )
341 return ( *ref ).impptr()->OperMultiSubscript( indices );
349 if ( indices.empty() )
357 return ( *ref ).impptr()->OperMultiSubscript( indices );
858 obj.
setimp(
new BError(
"Object is not a 'container'" ) );
918 return new BError( std::string(
"Method '" ) + methodname +
"' not found" );
923 os <<
"Method id '" <<
id <<
"' (" <<
getObjMethod(
id )->
code <<
") not found";
928 return BObjectRef(
new BError( std::string(
"Member '" ) + membername +
"' not found" ) );
1042 for (
const auto& elem :
ref_arr )
1044 size += elem.sizeEstimate();
1046 size += 3 *
sizeof( std::string* ) +
name_arr.capacity() *
sizeof( std::string );
1047 for (
const auto& elem :
name_arr )
1049 size += elem.capacity();
1071 for (
unsigned i = 0; i <
ref_arr.size(); ++i )
1078 if ( thisobj != NULL && thatobj != NULL )
1080 const BObjectImp& thisimp = thisobj->impref();
1081 const BObjectImp& thatimp = thatobj->impref();
1083 if ( thisimp == thatimp )
1088 else if ( thisobj == NULL && thatobj == NULL )
1102 assert( index > 0 );
1106 if ( ref.
get() == NULL )
1108 return ref.
get()->impptr();
1117 unsigned index = (unsigned)lng.
value();
1120 else if ( index <= 0 )
1121 return new BError(
"Array index out of bounds" );
1128 if ( refobj != NULL )
1130 refobj->setimp( new_target );
1136 return ref->impptr();
1151 std::unique_ptr<ObjArray> result(
new ObjArray( *
this ) );
1153 return result.release();
1157 std::unique_ptr<ObjArray> result(
new ObjArray( *
this ) );
1159 return result.release();
1163 std::unique_ptr<ObjArray> result(
new ObjArray( *
this ) );
1165 return result.release();
1169 std::unique_ptr<ObjArray> result(
new ObjArray( *
this ) );
1171 return result.release();
1176 std::unique_ptr<ObjArray> result(
new ObjArray( *
this ) );
1178 for (
const auto& elem : objimp.
ref_arr )
1197 return result.release();
1255 BObject& length_obj = *length_ref;
1256 BObject& start_obj = *start_ref;
1268 index = (unsigned)lng.
value();
1269 if ( index == 0 || index >
ref_arr.size() )
1281 end = (unsigned)lng.
value();
1282 if ( end == 0 || end >
ref_arr.size() )
1328 unsigned index = (unsigned)lng.
value();
1333 else if ( index <= 0 )
1337 if ( ref.
get() == NULL )
1358 const std::string&
name = ( *itr );
1359 if ( stricmp( name.c_str(), membername ) == 0 )
1373 const std::string&
name = ( *itr );
1374 if ( stricmp( name.c_str(), membername ) == 0 )
1377 ref_arr[i].get()->setimp( target );
1388 if ( stricmp( name, elem.c_str() ) == 0 )
1393 name_arr.push_back( name );
1409 for (
const auto& elem :
ref_arr )
1431 for (
auto itr =
ref_arr.begin(), end =
ref_arr.end(); itr != end; ++itr )
1439 <<
" in array{}' check. Invalid data at index " << ( itr -
ref_arr.begin() ) + 1
1443 else if ( *( bo->impptr() ) == imp )
1445 return ( static_cast<long>( ( itr -
ref_arr.begin() ) + 1 ) );
1459 if ( b1 == NULL || b2 == NULL )
1460 return ( &x1 < &x2 );
1474 return new BLong( static_cast<int>(
ref_arr.size() ) );
1476 return new BError(
"array.size() doesn't take parameters." );
1487 return new BLong( 1 );
1495 return new BError(
"array.erase(index) requires a parameter." );
1504 if ( ex.
getParam( 0, idx ) && idx >= 0 )
1506 bool exists = ( idx <= (int)
ref_arr.size() );
1507 return new BLong( exists ? 1 : 0 );
1511 return new BError(
"Invalid parameter type" );
1515 return new BError(
"array.exists(index) requires a parameter." );
1525 if ( ex.
getParam( 0, idx, 1, static_cast<int>(
ref_arr.size() + 1 ) ) &&
1536 return new BError(
"Invalid parameter type" );
1540 return new BError(
"array.insert(index,value) requires two parameters." );
1552 return new BLong( 1 );
1556 return new BError(
"Invalid parameter type" );
1560 return new BError(
"array.shrink(nelems) requires a parameter." );
1573 return new BLong( 1 );
1577 return new BError(
"Invalid parameter type" );
1581 return new BError(
"array.append(value) requires a parameter." );
1590 return new BLong( 1 );
1593 return new BError(
"array.reverse() doesn't take parameters." );
1602 return new BLong( 1 );
1607 if ( !ex.
getParam( 0, sub_index ) )
1608 return new BError(
"Invalid parameter type" );
1609 if ( sub_index < 1 )
1610 return new BError(
"Invalid sub_index value" );
1611 for (
const auto& ref :
ref_arr )
1613 if ( ref.get() ==
nullptr || !ref.get()->isa(
OTArray ) )
1614 return new BError(
"Invalid array" );
1615 auto sub_arr =
static_cast<ObjArray*
>( ref.get()->impptr() );
1616 if ( sub_arr->ref_arr.size() <
static_cast<size_t>( sub_index ) )
1617 return new BError(
"Subindex to large" );
1619 sort( ref_arr.begin(), ref_arr.end(),
1621 auto sub_arr1 =
static_cast<ObjArray*
>( x1.
get()->impptr() );
1622 auto sub_arr2 =
static_cast<ObjArray*
>( x2.get()->impptr() );
1623 auto sub1 = sub_arr1->
ref_arr[sub_index - 1];
1624 auto sub2 = sub_arr2->ref_arr[sub_index - 1];
1625 const BObject* b1 = sub1.get();
1626 const BObject* b2 = sub2.get();
1627 if ( b1 ==
nullptr || b2 ==
nullptr )
1628 return ( &x1 < &x2 );
1629 return ( *b1 < *b2 );
1631 return new BLong( 1 );
1634 return new BError(
"array.sort(sub_index=0) takes at most one parameter." );
1646 if ( ref.
get() == NULL )
1648 return ref.
get()->impptr();
1652 return new BError(
"array.randomentry() doesn't take parameters." );
1663 return new BError(
"Invalid parameter type" );
1664 if ( shift_by == 0 )
1665 return new BLong( 0 );
1670 if (
ref_arr.empty() || std::abs( shift_by ) > (int)
ref_arr.size() )
1671 return new BLong( 0 );
1678 return new BLong( 1 );
1688 "array.sorted_insert(obj, sub_index:=0, reverse:=0) takes at least one parameter." );
1691 return new BError(
"Invalid parameter type" );
1692 bool reverse =
false;
1696 if ( !ex.
getParam( 1, sub_index ) )
1697 return new BError(
"Invalid parameter type" );
1698 if ( sub_index < 0 )
1699 return new BError(
"Invalid sub_index value" );
1704 if ( !ex.
getParam( 2, reverseparam ) )
1705 return new BError(
"Invalid parameter type" );
1706 reverse = reverseparam != 0;
1717 if ( b1 ==
nullptr || b2 ==
nullptr )
1718 return ( &x1 > &x2 );
1736 if ( x2.get() ==
nullptr || !x2.get()->isa(
OTArray ) )
1738 auto sub_arr1 =
static_cast<ObjArray*
>( x1.
get()->impptr() );
1739 auto sub_arr2 =
static_cast<ObjArray*
>( x2.get()->impptr() );
1740 if ( sub_arr1->ref_arr.size() <
static_cast<size_t>( sub_index ) )
1742 if ( sub_arr2->ref_arr.size() <
static_cast<size_t>( sub_index ) )
1744 auto sub1 = sub_arr1->
ref_arr[sub_index - 1];
1745 auto sub2 = sub_arr2->ref_arr[sub_index - 1];
1746 const BObject* b1 = sub1.get();
1747 const BObject* b2 = sub2.get();
1750 if ( b1 ==
nullptr || b2 ==
nullptr )
1751 return ( &x1 < &x2 );
1752 return ( *b1 < *b2 );
1756 if ( b1 ==
nullptr || b2 ==
nullptr )
1757 return ( &x1 > &x2 );
1758 return ( *b1 > *b2 );
1770 return new BLong( 1 );
1782 if ( objmethod != NULL )
1790 os <<
"a" <<
ref_arr.size() <<
":";
1791 for (
const auto& elem :
ref_arr )
1809 if ( !( is >> arrsize >> colon ) )
1811 return new BError(
"Unable to unpack array elemcount" );
1813 if ( (
int)arrsize < 0 )
1815 return new BError(
"Unable to unpack array elemcount. Invalid length!" );
1819 return new BError(
"Unable to unpack array elemcount. Bad format. Colon not found!" );
1821 std::unique_ptr<ObjArray> arr(
new ObjArray );
1822 arr->ref_arr.resize( arrsize );
1823 for (
unsigned i = 0; i < arrsize; ++i )
1828 arr->ref_arr[i].set(
new BObject( imp ) );
1831 return arr.release();
1872 return std::string(
"<appobj:" ) +
typeOf() +
">";
1880 #if BOBJECTIMP_DEBUG 1894 return new BError(
"Error extracting Boolean value" );
1900 os <<
"b" << (
bval_ ? 1 : 0 );
1906 os <<
"b" << (
bval_ ? 1 : 0 );
1932 return bval_ ?
"true" :
"false";
1939 num_params_( param_count ),
1940 script_name_( scriptname )
1971 return "FunctionObject";
1977 if ( objmethod !=
nullptr )
1998 if ( objmethod ==
nullptr )
virtual BObjectImp * selfBitShiftRightObjImp(const BObjectImp &objimp) const
virtual BObjectImp * selfBitOrObj(const BObjectImp &objimp) const
virtual BObjectRef get_member(const char *membername)
std::atomic< int > eobject_imp_count
static BObjectImp * unpack(std::istream &is)
Clib::fixed_allocator< sizeof(Double), 256 > double_alloc
virtual std::string getStringRep() const POL_OVERRIDE
virtual std::string getStringRep() const =0
virtual BObjectImp * bitnot() const
virtual BObjectImp * selfBitAndObjImp(const BObjectImp &objimp) const
virtual BObjectImp * selfTimesObj(const BObjectImp &objimp) const
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
std::string scripts_thread_script
bool isa(BObjectType type) const
virtual BObjectImp * selfBitOrObjImp(const BObjectImp &objimp) const
void ins_nop(const Instruction &ins)
BApplicPtr(const BApplicObjType *pointer_type, void *ptr)
virtual BObjectImp * selfBitXorObj(const BObjectImp &objimp) const
virtual BObjectImp * selfBitShiftRightObj(const BObjectImp &objimp) const
virtual long contains(const BObjectImp &objimp) const
const BApplicObjType * pointer_type_
std::atomic< int > eobject_imp_constructions
virtual BObjectRef operDotPlus(const char *name) POL_OVERRIDE
size_t sizeEstimate() const
virtual long contains(const BObjectImp &imp) const POL_OVERRIDE
virtual BObjectImp * inverse() const
virtual size_t sizeEstimate() const POL_OVERRIDE
static BObjectImp * unpack(const char *pstr)
virtual bool isTrue() const POL_OVERRIDE
virtual BObjectImp * copy() const =0
virtual BObjectImp * array_assign(BObjectImp *idx, BObjectImp *target, bool copy) POL_OVERRIDE
#define OSTRINGSTREAM_STR(x)
bool operator>=(const BObject &obj) const
virtual bool operator!=(const BObjectImp &objimp) const
virtual BObjectImp * selfBitShiftLeftObj(const BObjectImp &objimp) const
virtual u8 typeOfInt() const
virtual void operInsertInto(BObject &obj, const BObjectImp &objimp)
unsigned int instance() const
virtual BObjectImp * call_method_id(const int id, Executor &ex, bool forcebuiltin=false) POL_OVERRIDE
virtual BObjectImp * call_method(const char *methodname, Executor &ex) POL_OVERRIDE
ObjMethod * getKnownObjMethod(const char *token)
virtual void operModulusEqual(BObject &obj, BObjectImp &objimp)
Clib::fixed_allocator< sizeof(BLong), 256 > blong_alloc
virtual BObjectImp * copy() const POL_OVERRIDE
virtual BObjectImp * selfBitAndObj(const BObjectImp &objimp) const
virtual void packonto(std::ostream &os) const POL_OVERRIDE
static BObjectImp * unpack(std::istream &is)
virtual std::string pack() const
virtual BObjectRef get_member(const char *membername) POL_OVERRIDE
virtual BObjectRef operDotPlus(const char *name)
BObjectRef(BObject *pobj=NULL)
virtual BObjectImp * copy() const POL_OVERRIDE
int display_bobjectimp_instance(BObjectImp *imp)
Cont::const_iterator const_iterator
virtual void operDivideEqual(BObject &obj, BObjectImp &objimp)
virtual std::string getStringRep() const POL_OVERRIDE
virtual std::string getStringRep() const POL_OVERRIDE
virtual BObjectImp * selfBitShiftLeftObjImp(const BObjectImp &objimp) const
bool operator<=(const BObject &obj) const
virtual std::string pack() const POL_OVERRIDE
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
virtual BObjectRef OperSubscript(const BObject &obj) POL_OVERRIDE
void setimp(BObjectImp *imp)
virtual bool operator==(const BObjectImp &objimp) const
virtual bool isTrue() const
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
virtual BObjectRef get_member_id(const int id)
const BObjectImp * imp_at(unsigned index) const
virtual BObjectRef set_member(const char *membername, BObjectImp *valueimp, bool copy)
virtual BObjectImp * copy() const POL_OVERRIDE
virtual BObjectImp * selfDividedByObj(const BObjectImp &objimp) const
Clib::fixed_allocator< sizeof(UninitObject), 256 > uninit_alloc
virtual bool operator<(const BObjectImp &objimp) const
virtual BObjectImp * call_method_id(const int id, Executor &ex, bool forcebuiltin=false) POL_OVERRIDE
bool validCall(const int id, Executor &ex, Instruction *inst) const
virtual BObjectImp * copy() const POL_OVERRIDE
virtual bool isTrue() const POL_OVERRIDE
const BApplicObjType * pointer_type() const
virtual void operPlusEqual(BObject &obj, BObjectImp &objimp)
BFunctionRef(int progcounter, int param_count, const std::string &scriptname)
virtual size_t sizeEstimate() const POL_OVERRIDE
static Clib::SpinLock bobjectimp_lock
virtual bool operator<=(const BObjectImp &objimp) const
virtual BObjectRef OperSubscript(const BObject &obj)
size_t sizeEstimate() const
static BObjectImp * unpack(std::istream &is)
bool operator>(const BObject &obj) const
virtual std::string getStringRep() const POL_OVERRIDE
bool operator!=(const BObject &obj) const
BBoolean(bool bval=false)
void display_bobjectimp_instances()
virtual bool operator<(const BObjectImp &objimp) const POL_OVERRIDE
static UninitObject * create()
virtual std::string getFormattedStringRep() const
virtual void packonto(std::ostream &os) const POL_OVERRIDE
virtual void operMinusEqual(BObject &obj, BObjectImp &objimp)
ObjMember * getObjMember(int id)
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
ObjMethod * getObjMethod(int id)
static BObjectImp * unpack(const char *pstr)
virtual BObjectRef set_member_id(const int id, BObjectImp *valueimp, bool copy)
virtual size_t sizeEstimate() const POL_OVERRIDE
virtual BObjectImp * call_method(const char *methodname, Executor &ex)
static BObjectImp * unpack(std::istream &is)
static BObjectImp * unpack(const char *pstr)
bool operator==(const BObject &obj) const
virtual BObjectImp * call_method_id(const int id, Executor &ex, bool forcebuiltin=false)
bool operator()(const BObjectRef &x1, const BObjectRef &x2) const
virtual bool isTrue() const POL_OVERRIDE
ref_ptr< BObjectImp > objimp
virtual bool operator>(const BObjectImp &objimp) const
virtual void printOn(std::ostream &) const POL_OVERRIDE
virtual size_t sizeEstimate() const POL_OVERRIDE
virtual BObjectImp * copy() const POL_OVERRIDE
virtual BObjectImp * selfMinusObjImp(const BObjectImp &objimp) const
virtual BObjectRef operDotQMark(const char *name)
virtual BObjectRef OperMultiSubscript(std::stack< BObjectRef > &indices)
virtual size_t sizeEstimate() const POL_OVERRIDE
virtual BObjectImp * copy() const POL_OVERRIDE
virtual void packonto(std::ostream &os) const
void addElement(BObjectImp *imp)
static BObjectImp * unpackWithLen(std::istream &is)
const std::string & scriptname() const
virtual BObjectImp * selfTimesObjImp(const BObjectImp &objimp) const
virtual BObjectImp * call_method(const char *methodname, Executor &ex) POL_OVERRIDE
virtual void printOn(std::ostream &) const POL_OVERRIDE
virtual BObjectRef OperMultiSubscriptAssign(std::stack< BObjectRef > &indices, BObjectImp *target)
virtual BObjectImp * selfPlusObj(const BObjectImp &objimp) const POL_OVERRIDE
virtual BObjectImp * copy() const POL_OVERRIDE
virtual BObjectImp * selfMinusObj(const BObjectImp &objimp) const
static ref_ptr< BObjectImp > SharedInstanceOwner
bool operator<(const BObject &obj) const
NameCont::const_iterator const_name_iterator
virtual BObjectImp * selfModulusObjImp(const BObjectImp &objimp) const
virtual BObjectImp * selfPlusObjImp(const BObjectImp &other) const POL_OVERRIDE
BObjectImp(BObjectType type)
bobjectimps bobjectimp_instances
virtual BObjectRef OperMultiSubscript(std::stack< BObjectRef > &indices) POL_OVERRIDE
virtual std::string getStringRep() const POL_OVERRIDE
virtual BObjectImp * selfBitXorObjImp(const BObjectImp &objimp) const
virtual BObjectRef set_member(const char *membername, BObjectImp *value, bool copy) POL_OVERRIDE
virtual BObjectImp * selfModulusObj(const BObjectImp &objimp) const
std::lock_guard< SpinLock > SpinLockGuard
virtual BObjectRef operDotMinus(const char *name)
Clib::fixed_allocator< sizeof(BObject), 256 > bobject_alloc
virtual BObjectImp * array_assign(BObjectImp *idx, BObjectImp *target, bool copy)
static BObjectImp * unpack(const char *pstr)
virtual void operInsertInto(BObject &obj, const BObjectImp &objimp) POL_OVERRIDE
virtual BObjectImp * selfDividedByObjImp(const BObjectImp &objimp) const
virtual const char * typeOf() const
virtual bool operator>=(const BObjectImp &objimp) const
std::unordered_map< unsigned int, BObjectImp * > bobjectimps
static const char * typestr(BObjectType typ)
static UninitObject * SharedInstance
virtual BObjectImp * selfPlusObj(const BObjectImp &objimp) const
virtual void operTimesEqual(BObject &obj, BObjectImp &objimp)
BObjectImp * getParamImp(unsigned param)
virtual BObjectImp * selfPlusObjImp(const BObjectImp &objimp) const
static BObjectImp * unpack(std::istream &is)
static unsigned int instances_
bool getParam(unsigned param, int &value)
virtual BObjectImp * copy() const POL_OVERRIDE
virtual BObject operator-() const