@@ -300,7 +300,8 @@ enum ScriptFuncBindingFlags_t
300300 SF_MEMBER_FUNC = 0x01 ,
301301};
302302
303- typedef bool (*ScriptBindingFunc_t)( void *pFunction, void *pContext, ScriptVariant_t *pArguments, int nArguments, ScriptVariant_t *pReturn );
303+ union ScriptVariantTemporaryStorage_t;
304+ typedef bool (*ScriptBindingFunc_t)( void *pFunction, void *pContext, ScriptVariant_t *pArguments, int nArguments, ScriptVariant_t *pReturn, ScriptVariantTemporaryStorage_t &temporaryReturnStorage );
304305
305306struct ScriptFunctionBinding_t
306307{
@@ -389,93 +390,44 @@ struct ScriptVariant_t
389390 ScriptVariant_t ( bool val ) : m_flags( 0 ), m_type( FIELD_BOOLEAN ) { m_bool = val; }
390391 ScriptVariant_t ( HSCRIPT val ) : m_flags( 0 ), m_type( FIELD_HSCRIPT ) { m_hScript = val; }
391392
392- ScriptVariant_t ( const Vector &val, bool bCopy = false ) : m_flags( 0 ), m_type( FIELD_VECTOR )
393- {
394- if ( !bCopy )
395- {
396- m_pVector = &val;
397- }
398- else
399- {
400- m_pVector = (Vector*)malloc ( sizeof ( Vector ) );
401- new ( (Vector*)m_pVector ) Vector ( val );
402- m_flags |= SV_FREE;
403- }
404- }
405- ScriptVariant_t ( const Vector *val, bool bCopy = false ) : ScriptVariant_t( *val ) { }
393+ ScriptVariant_t ( const Vector &val ) : m_flags( 0 ), m_type( FIELD_VECTOR ) { m_pVector = &val; }
394+ ScriptVariant_t ( const Vector *val ) : ScriptVariant_t( *val ) { }
406395
407- ScriptVariant_t ( const char *val , bool bCopy = false ) : m_flags( 0 ), m_type( FIELD_CSTRING )
408- {
409- if ( !bCopy )
410- {
411- m_pszString = val;
412- }
413- else
414- {
415- m_pszString = strdup ( val );
416- m_flags |= SV_FREE;
417- }
418- }
396+ ScriptVariant_t ( const char *val ) : m_flags( 0 ), m_type( FIELD_CSTRING ) { m_pszString = val; }
419397
420398#ifdef MAPBASE_VSCRIPT
421- ScriptVariant_t ( const QAngle &val, bool bCopy = false ) : m_flags( 0 ), m_type( FIELD_VECTOR )
422- {
423- if ( !bCopy )
424- {
425- m_pAngle = &val;
426- }
427- else
428- {
429- m_pAngle = (QAngle*)malloc ( sizeof ( QAngle ) );
430- new ( (QAngle*)m_pAngle ) QAngle ( val );
431- m_flags |= SV_FREE;
432- }
433- }
434- ScriptVariant_t ( const QAngle *val, bool bCopy = false ) : ScriptVariant_t( *val ) { }
399+ ScriptVariant_t ( const QAngle &val ) : m_flags( 0 ), m_type( FIELD_VECTOR ) { m_pAngle = &val; }
400+ ScriptVariant_t ( const QAngle *val ) : ScriptVariant_t( *val ) { }
435401
436- ScriptVariant_t ( Vector &&val ) : ScriptVariant_t( val, true ) { }
437- ScriptVariant_t ( QAngle &&val ) : ScriptVariant_t( val, true ) { }
402+ ScriptVariant_t ( Vector &&val ) = delete;
403+ ScriptVariant_t ( QAngle &&val ) = delete;
438404#endif
439405
440406 bool IsNull () const { return (m_type == FIELD_VOID ); }
441407
442408 operator int () const { Assert ( m_type == FIELD_INTEGER ); return m_int; }
443409 operator float () const { Assert ( m_type == FIELD_FLOAT ); return m_float; }
444410 operator const char *() const { Assert ( m_type == FIELD_CSTRING ); return ( m_pszString ) ? m_pszString : " " ; }
445- operator const Vector &() const { Assert ( m_type == FIELD_VECTOR ); static Vector vecNull ( 0 , 0 , 0 ); return (m_pVector) ? *m_pVector : vecNull ; }
411+ operator const Vector &() const { Assert ( m_type == FIELD_VECTOR ); return (m_pVector) ? *m_pVector : vec3_origin ; }
446412 operator char () const { Assert ( m_type == FIELD_CHARACTER ); return m_char; }
447413 operator bool () const { Assert ( m_type == FIELD_BOOLEAN ); return m_bool; }
448414 operator HSCRIPT () const { Assert ( m_type == FIELD_HSCRIPT ); return m_hScript; }
449415#ifdef MAPBASE_VSCRIPT
450- operator const QAngle &() const { Assert ( m_type == FIELD_VECTOR ); static QAngle vecNull ( 0 , 0 , 0 ); return (m_pAngle) ? *m_pAngle : vecNull ; }
416+ operator const QAngle &() const { Assert ( m_type == FIELD_VECTOR ); return (m_pAngle) ? *m_pAngle : vec3_angle ; }
451417#endif
452418
453- void operator =( int i ) { m_type = FIELD_INTEGER; m_int = i; }
454- void operator =( float f ) { m_type = FIELD_FLOAT; m_float = f; }
455- void operator =( double f ) { m_type = FIELD_FLOAT; m_float = (float )f; }
456- void operator =( const Vector &vec ) { m_type = FIELD_VECTOR; m_pVector = &vec; }
457- void operator =( const Vector *vec ) { m_type = FIELD_VECTOR; m_pVector = vec; }
458- void operator =( const char *psz ) { m_type = FIELD_CSTRING; m_pszString = psz; }
459- void operator =( char c ) { m_type = FIELD_CHARACTER; m_char = c; }
460- void operator =( bool b ) { m_type = FIELD_BOOLEAN; m_bool = b; }
461- void operator =( HSCRIPT h ) { m_type = FIELD_HSCRIPT; m_hScript = h; }
419+ void operator =( int i ) { m_type = FIELD_INTEGER; m_flags = 0 ; m_int = i; }
420+ void operator =( float f ) { m_type = FIELD_FLOAT; m_flags = 0 ; m_float = f; }
421+ void operator =( double f ) { m_type = FIELD_FLOAT; m_flags = 0 ; m_float = (float )f; }
422+ void operator =( const Vector &vec ) { m_type = FIELD_VECTOR; m_flags = 0 ; m_pVector = &vec; }
423+ void operator =( const Vector *vec ) { m_type = FIELD_VECTOR; m_flags = 0 ; m_pVector = vec; }
424+ void operator =( const char *psz ) { m_type = FIELD_CSTRING; m_flags = 0 ; m_pszString = psz; }
425+ void operator =( char c ) { m_type = FIELD_CHARACTER; m_flags = 0 ; m_char = c; }
426+ void operator =( bool b ) { m_type = FIELD_BOOLEAN; m_flags = 0 ; m_bool = b; }
427+ void operator =( HSCRIPT h ) { m_type = FIELD_HSCRIPT; m_flags = 0 ; m_hScript = h; }
462428#ifdef MAPBASE_VSCRIPT
463- void operator =( const QAngle &vec ) { m_type = FIELD_VECTOR; m_pAngle = &vec; }
464- void operator =( const QAngle *vec ) { m_type = FIELD_VECTOR; m_pAngle = vec; }
465-
466- void operator =( Vector &&vec )
467- {
468- m_pVector = (Vector*)malloc ( sizeof ( Vector ) );
469- new ( (Vector*)m_pVector ) Vector ( vec );
470- m_flags |= SV_FREE;
471- }
472-
473- void operator =( QAngle &&ang )
474- {
475- m_pAngle = (QAngle*)malloc ( sizeof ( QAngle ) );
476- new ( (QAngle*)m_pAngle ) Vector ( ang );
477- m_flags |= SV_FREE;
478- }
429+ void operator =( const QAngle &vec ) { m_type = FIELD_VECTOR; m_flags = 0 ; m_pAngle = &vec; }
430+ void operator =( const QAngle *vec ) { m_type = FIELD_VECTOR; m_flags = 0 ; m_pAngle = vec; }
479431#endif
480432
481433 void Free ()
@@ -641,6 +593,16 @@ struct ScriptVariant_t
641593
642594#define SCRIPT_VARIANT_NULL ScriptVariant_t ()
643595
596+ union ScriptVariantTemporaryStorage_t
597+ {
598+ // members must be initialized via placement-new
599+ ScriptVariantTemporaryStorage_t () { }
600+
601+ // members must have trivial destructor, since no destructor will be invoked
602+ Vector m_vec;
603+ QAngle m_ang;
604+ };
605+
644606#ifdef MAPBASE_VSCRIPT
645607// ---------------------------------------------------------
646608struct ScriptConstantBinding_t
@@ -733,7 +695,7 @@ static inline int ToConstantVariant(int value)
733695// This is used for registering variants (particularly vectors) not tied to existing variables.
734696// The principal difference is that m_data is initted with bCopy set to true.
735697#define ScriptRegisterConstantFromTemp ( pVM, constant, description ) ScriptRegisterConstantFromTempNamed( pVM, constant, #constant, description )
736- #define ScriptRegisterConstantFromTempNamed ( pVM, constant, scriptName, description ) do { static ScriptConstantBinding_t binding; binding.m_pszScriptName = scriptName; binding.m_pszDescription = description; binding.m_data = ScriptVariant_t ( constant, true ); pVM->RegisterConstant ( &binding ); } while (0 )
698+ #define ScriptRegisterConstantFromTempNamed ( pVM, constant, scriptName, description ) do { static const auto constantStorage = constant; static ScriptConstantBinding_t binding; binding.m_pszScriptName = scriptName; binding.m_pszDescription = description; binding.m_data = ScriptVariant_t ( constantStorage ); pVM->RegisterConstant ( &binding ); } while (0 )
737699
738700// -----------------------------------------------------------------------------
739701//
0 commit comments