Skip to content

Commit d480d17

Browse files
Update sqdbg
1 parent c42f5b6 commit d480d17

8 files changed

Lines changed: 4494 additions & 1969 deletions

File tree

sp/src/vscript/sqdbg/include/sqdbg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#endif
3636

3737
struct SQDebugServer;
38-
typedef SQDebugServer* HSQDEBUGSERVER;
38+
typedef struct SQDebugServer *HSQDEBUGSERVER;
3939

4040
#ifdef __cplusplus
4141
extern "C" {

sp/src/vscript/sqdbg/sqdbg/debug.h

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@
88

99
#if 0
1010

11+
#if defined(_WIN32) && !defined(__MINGW32__)
12+
#define DebuggerBreak() __debugbreak()
13+
#else
14+
#define DebuggerBreak() asm("int3")
15+
#endif
16+
17+
#ifdef _WIN32
18+
#define __IsDebuggerPresent() IsDebuggerPresent()
19+
#else
20+
#define __IsDebuggerPresent() 0
21+
#endif
22+
1123
#ifdef _DEBUG
12-
#ifdef _WIN32
24+
#if defined(_WIN32) && !defined(__MINGW32__)
1325
#include <crtdbg.h>
1426

15-
bool __IsDebuggerPresent();
1627
const char *GetModuleBaseName();
1728

18-
#define DebuggerBreak() do { if ( __IsDebuggerPresent() ) __debugbreak(); } while(0)
19-
2029
#define Assert( x ) \
2130
do { \
2231
__CAT( L, __LINE__ ): \
23-
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), #x)) ) \
32+
if ( !(x) && \
33+
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), "%s", #x)) ) \
2434
{ \
2535
if ( !__IsDebuggerPresent() ) \
2636
goto __CAT( L, __LINE__ ); \
@@ -31,7 +41,8 @@
3141
#define AssertMsg( x, msg ) \
3242
do { \
3343
__CAT( L, __LINE__ ): \
34-
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg)) ) \
44+
if ( !(x) && \
45+
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg)) ) \
3546
{ \
3647
if ( !__IsDebuggerPresent() ) \
3748
goto __CAT( L, __LINE__ ); \
@@ -42,7 +53,8 @@
4253
#define AssertMsg1( x, msg, a1 ) \
4354
do { \
4455
__CAT( L, __LINE__ ): \
45-
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1)) ) \
56+
if ( !(x) && \
57+
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1)) ) \
4658
{ \
4759
if ( !__IsDebuggerPresent() ) \
4860
goto __CAT( L, __LINE__ ); \
@@ -53,17 +65,19 @@
5365
#define AssertMsg2( x, msg, a1, a2 ) \
5466
do { \
5567
__CAT( L, __LINE__ ): \
56-
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1, a2)) ) \
68+
if ( !(x) && \
69+
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1, a2)) ) \
5770
{ \
5871
if ( !__IsDebuggerPresent() ) \
5972
goto __CAT( L, __LINE__ ); \
6073
__debugbreak(); \
6174
} \
6275
} while(0)
6376
#else
64-
extern "C" int printf(const char *, ...);
65-
66-
#define DebuggerBreak() asm("int3")
77+
#ifndef __MINGW32__
78+
extern "C"
79+
#endif
80+
int printf(const char *, ...);
6781

6882
#define Assert( x ) \
6983
do { \
@@ -88,8 +102,7 @@
88102
if ( !(x) ) \
89103
{ \
90104
::printf("Assertion failed %s:%d: ", __FILE__, __LINE__); \
91-
::printf(msg, a1); \
92-
::printf("\n"); \
105+
::printf(msg "\n", a1); \
93106
DebuggerBreak(); \
94107
} \
95108
} while(0)
@@ -99,24 +112,22 @@
99112
if ( !(x) ) \
100113
{ \
101114
::printf("Assertion failed %s:%d: ", __FILE__, __LINE__); \
102-
::printf(msg, a1, a2); \
103-
::printf("\n"); \
115+
::printf(msg "\n", a1, a2); \
104116
DebuggerBreak(); \
105117
} \
106118
} while(0)
107119
#endif
108120
#define Verify( x ) Assert(x)
109-
#define STATIC_ASSERT( x ) static_assert( x, #x )
110121
#else
111-
#define DebuggerBreak() ((void)0)
112122
#define Assert( x ) ((void)0)
113123
#define AssertMsg( x, msg ) ((void)0)
114124
#define AssertMsg1( x, msg, a1 ) ((void)0)
115125
#define AssertMsg2( x, msg, a1, a2 ) ((void)0)
116126
#define Verify( x ) x
117-
#define STATIC_ASSERT( x )
118127
#endif // _DEBUG
119128

129+
#define STATIC_ASSERT( x ) static_assert( x, #x )
130+
120131
#endif
121132

122133
#include <tier0/dbg.h>
@@ -126,7 +137,7 @@
126137
// Misdefined for GCC in platform.h
127138
#undef UNREACHABLE
128139

129-
#ifdef _WIN32
140+
#ifdef _MSC_VER
130141
#define UNREACHABLE() do { Assert(!"UNREACHABLE"); __assume(0); } while(0)
131142
#else
132143
#define UNREACHABLE() do { Assert(!"UNREACHABLE"); __builtin_unreachable(); } while(0)

sp/src/vscript/sqdbg/sqdbg/json.h

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
#ifndef SQDBG_JSON_H
77
#define SQDBG_JSON_H
88

9-
// Most messages are going to require less than 256 bytes,
10-
// only approaching 1024 on large breakpoint requests
11-
#define JSON_SCRATCH_CHUNK_SIZE 1024
12-
139
typedef enum
1410
{
1511
JSON_NULL = 0x0000,
@@ -62,12 +58,12 @@ class json_array_t
6258
{
6359
public:
6460
const char *m_pBase;
65-
CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *m_Allocator;
66-
int *m_Elements;
61+
CScratch< true > *m_Allocator;
62+
scratchindex_t *m_Elements;
6763
unsigned short m_nElementCount;
6864
unsigned short m_nElementsSize;
6965

70-
void Init( const char *base, CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *allocator )
66+
void Init( const char *base, CScratch< true > *allocator )
7167
{
7268
m_pBase = base;
7369
m_Allocator = allocator;
@@ -81,16 +77,16 @@ class json_array_t
8177
{
8278
// doesn't free old ptr, this is an uncommon operation and extra allocation is fine
8379
int oldsize = m_nElementsSize;
84-
int *oldptr = m_Elements;
80+
scratchindex_t *oldptr = m_Elements;
8581

8682
m_nElementsSize = !m_nElementsSize ? 8 : ( m_nElementsSize << 1 );
87-
m_Elements = (int*)m_Allocator->Alloc( m_nElementsSize * sizeof(int) );
83+
m_Elements = (scratchindex_t*)m_Allocator->Alloc( m_nElementsSize * sizeof(*m_Elements) );
8884

8985
if ( oldsize )
90-
memcpy( m_Elements, oldptr, oldsize * sizeof(int) );
86+
memcpy( m_Elements, oldptr, oldsize * sizeof(*m_Elements) );
9187
}
9288

93-
int index;
89+
scratchindex_t index;
9490
json_value_t *ret = (json_value_t*)m_Allocator->Alloc( sizeof(json_value_t), &index );
9591
m_Elements[ m_nElementCount++ ] = index;
9692
return ret;
@@ -138,12 +134,12 @@ class json_table_t
138134
{
139135
public:
140136
const char *m_pBase;
141-
CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *m_Allocator;
142-
int *m_Elements;
137+
CScratch< true > *m_Allocator;
138+
scratchindex_t *m_Elements;
143139
unsigned short m_nElementCount;
144140
unsigned short m_nElementsSize;
145141

146-
void Init( const char *base, CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *allocator )
142+
void Init( const char *base, CScratch< true > *allocator )
147143
{
148144
m_pBase = base;
149145
m_Allocator = allocator;
@@ -174,16 +170,16 @@ class json_table_t
174170
if ( m_nElementCount == m_nElementsSize )
175171
{
176172
int oldsize = m_nElementsSize;
177-
int *oldptr = m_Elements;
173+
scratchindex_t *oldptr = m_Elements;
178174

179175
m_nElementsSize = !m_nElementsSize ? 8 : ( m_nElementsSize << 1 );
180-
m_Elements = (int*)m_Allocator->Alloc( m_nElementsSize * sizeof(int) );
176+
m_Elements = (scratchindex_t*)m_Allocator->Alloc( m_nElementsSize * sizeof(*m_Elements) );
181177

182178
if ( oldsize )
183-
memcpy( m_Elements, oldptr, oldsize * sizeof(int) );
179+
memcpy( m_Elements, oldptr, oldsize * sizeof(*m_Elements) );
184180
}
185181

186-
int index;
182+
scratchindex_t index;
187183
json_field_t *ret = (json_field_t*)m_Allocator->Alloc( sizeof(json_field_t), &index );
188184
m_Elements[ m_nElementCount++ ] = index;
189185
return ret;
@@ -275,7 +271,12 @@ static inline void PutStr( CBuffer *buffer, const string_t &str )
275271
#ifdef SQDBG_VALIDATE_SENT_MSG
276272
for ( unsigned int i = 0; i < str.len; i++ )
277273
{
278-
if ( str.ptr[i] == '\\' && ( str.ptr[i+1] == '\\' || str.ptr[i+1] == '\"' ) )
274+
if ( str.ptr[i] == '\\' &&
275+
( str.ptr[i+1] == '\\' ||
276+
str.ptr[i+1] == '\"' ||
277+
str.ptr[i+1] == 'n' ||
278+
str.ptr[i+1] == 'r' ||
279+
str.ptr[i+1] == 't' ) )
279280
{
280281
i++;
281282
continue;
@@ -314,7 +315,7 @@ static inline void PutStr( CBuffer *buffer, const string_t &str, bool quote )
314315
default:
315316
if ( !IN_RANGE_CHAR( *c, 0x20, 0x7E ) )
316317
{
317-
int ret = IsValidUTF8( (unsigned char*)c, i + 1 );
318+
int ret = IsValidUTF8( c, i + 1 );
318319
if ( ret != 0 )
319320
{
320321
i -= ret - 1;
@@ -410,7 +411,7 @@ static inline void PutStr( CBuffer *buffer, const string_t &str, bool quote )
410411
default:
411412
if ( !IN_RANGE_CHAR( *c, 0x20, 0x7E ) )
412413
{
413-
int ret = IsValidUTF8( (unsigned char*)c, i + 1 );
414+
int ret = IsValidUTF8( c, i + 1 );
414415
if ( ret != 0 )
415416
{
416417
memcpy( mem + idx, c + 1, ret - 1 );
@@ -425,19 +426,26 @@ static inline void PutStr( CBuffer *buffer, const string_t &str, bool quote )
425426
if ( !quote )
426427
{
427428
mem[idx++] = 'u';
428-
idx += printhex< true, false >(
429+
uint16_t val = (uint16_t)*(unsigned char*)c;
430+
idx += printhex< false >(
429431
mem + idx,
430432
buffer->Capacity() - idx,
431-
(uint16_t)*(unsigned char*)c );
433+
val );
432434
}
433435
else
434436
{
435437
mem[idx++] = '\\';
438+
#ifdef SQUNICODE
439+
mem[idx++] = 'u';
440+
uint16_t val = (uint16_t)*(unsigned char*)c;
441+
#else
436442
mem[idx++] = 'x';
437-
idx += printhex< true, false >(
443+
unsigned char val = *(unsigned char*)c;
444+
#endif
445+
idx += printhex< false >(
438446
mem + idx,
439447
buffer->Capacity() - idx,
440-
(SQUnsignedChar)*(unsigned char*)c );
448+
val );
441449
}
442450
}
443451
}
@@ -504,12 +512,12 @@ static inline void PutInt( CBuffer *buffer, I val )
504512
buffer->size += len;
505513
}
506514

507-
template < bool padding, typename I >
508-
static inline void PutHex( CBuffer *buffer, I val )
515+
template < typename I >
516+
static inline void PutHex( CBuffer *buffer, I val, bool padding )
509517
{
510518
STATIC_ASSERT( IS_UNSIGNED( I ) );
511-
buffer->base.Ensure( buffer->Size() + countdigits<16>( val ) + 1 );
512-
int len = printhex< padding >( buffer->Base() + buffer->Size(), buffer->Capacity() - buffer->Size(), val );
519+
buffer->base.Ensure( buffer->Size() + ( padding ? sizeof(I) * 2 : countdigits<16>( val ) ) + 2 );
520+
int len = printhex( buffer->Base() + buffer->Size(), buffer->Capacity() - buffer->Size(), val, -(int)padding );
513521
buffer->size += len;
514522
}
515523

@@ -571,14 +579,7 @@ struct jstringbuf_t
571579
template < typename I >
572580
void PutHex( I val, bool padding = true )
573581
{
574-
if ( padding )
575-
{
576-
::PutHex< true >( m_pBuffer, val );
577-
}
578-
else
579-
{
580-
::PutHex< false >( m_pBuffer, val );
581-
}
582+
::PutHex( m_pBuffer, val, padding );
582583
}
583584
};
584585

@@ -704,7 +705,7 @@ class wjson_table_t : public wjson_t
704705
}
705706
else
706707
{
707-
PutHex< false >( m_pBuffer, cast_unsigned( I, val ) );
708+
PutHex( m_pBuffer, cast_unsigned( val ), false );
708709
}
709710
PutChar( m_pBuffer, ']' );
710711
PutChar( m_pBuffer, '\"' );
@@ -788,7 +789,7 @@ class JSONParser
788789
char *m_cur;
789790
char *m_end;
790791
char *m_start;
791-
CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *m_Allocator;
792+
CScratch< true > *m_Allocator;
792793
char *m_error;
793794

794795
enum
@@ -805,7 +806,7 @@ class JSONParser
805806
};
806807

807808
public:
808-
JSONParser( CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *allocator, char *ptr, int len, json_table_t *pTable ) :
809+
JSONParser( CScratch< true > *allocator, char *ptr, int len, json_table_t *pTable ) :
809810
m_cur( ptr ),
810811
m_end( ptr + len + 1 ),
811812
m_start( ptr ),
@@ -855,7 +856,7 @@ class JSONParser
855856
else
856857
{
857858
buf = m_Allocator->Alloc(5);
858-
int i = printhex< true, true, false >( buf, 5, (unsigned char)token );
859+
int i = printhex< true, false >( buf, 5, (unsigned char)token );
859860
Assert( i == 4 );
860861
buf[i] = 0;
861862
}
@@ -1041,10 +1042,12 @@ class JSONParser
10411042
}
10421043

10431044
#define _shift( bytesWritten, bytesRead ) \
1045+
do { \
10441046
Assert( (bytesWritten) < (bytesRead) ); \
10451047
memmove( cur + (bytesWritten), cur + (bytesRead), end - ( cur + (bytesRead) ) ); \
10461048
cur += (bytesWritten); \
1047-
end -= (bytesRead) - (bytesWritten);
1049+
end -= (bytesRead) - (bytesWritten); \
1050+
} while (0)
10481051

10491052
switch ( cur[1] )
10501053
{
@@ -1222,7 +1225,7 @@ class JSONParser
12221225

12231226
json_field_t *kv = pTable->NewElement();
12241227

1225-
Assert( token.ptr - m_start < (ostr_t::index_t)-1 );
1228+
Assert( (ostr_t::index_t)( token.ptr - m_start ) < (ostr_t::index_t)-1 );
12261229
kv->key.ofs = token.ptr - m_start;
12271230
kv->key.len = (ostr_t::index_t)token.len;
12281231

@@ -1315,7 +1318,7 @@ class JSONParser
13151318
return type;
13161319
case Token_String:
13171320
value->type = JSON_STRING;
1318-
Assert( token.ptr - m_start < (ostr_t::index_t)-1 );
1321+
Assert( (ostr_t::index_t)( token.ptr - m_start ) < (ostr_t::index_t)-1 );
13191322
value->_string.ofs = token.ptr - m_start;
13201323
value->_string.len = (ostr_t::index_t)token.len;
13211324
return type;

0 commit comments

Comments
 (0)