Browse Source

added guid type

Nicolas Cannasse 7 months ago
parent
commit
692f7e771d
3 changed files with 55 additions and 3 deletions
  1. 6 2
      src/hl.h
  2. 6 0
      src/std/buffer.c
  3. 43 1
      src/std/types.c

+ 6 - 2
src/hl.h

@@ -343,8 +343,9 @@ typedef enum {
 	HMETHOD = 20,
 	HMETHOD = 20,
 	HSTRUCT	= 21,
 	HSTRUCT	= 21,
 	HPACKED = 22,
 	HPACKED = 22,
+	HGUID	= 23,
 	// ---------
 	// ---------
-	HLAST	= 23,
+	HLAST	= 24,
 	_H_FORCE_INT = 0x7FFFFFFF
 	_H_FORCE_INT = 0x7FFFFFFF
 } hl_type_kind;
 } hl_type_kind;
 
 
@@ -603,6 +604,7 @@ HL_API int hl_utf8_length( const vbyte *s, int pos );
 HL_API int hl_from_utf8( uchar *out, int outLen, const char *str );
 HL_API int hl_from_utf8( uchar *out, int outLen, const char *str );
 HL_API char *hl_to_utf8( const uchar *bytes );
 HL_API char *hl_to_utf8( const uchar *bytes );
 HL_API uchar *hl_to_utf16( const char *str );
 HL_API uchar *hl_to_utf16( const char *str );
+HL_API uchar *hl_guid_str( int64 guid, uchar buf[14] );
 HL_API vdynamic *hl_virtual_make_value( vvirtual *v );
 HL_API vdynamic *hl_virtual_make_value( vvirtual *v );
 HL_API hl_obj_field *hl_obj_field_fetch( hl_type *t, int fid );
 HL_API hl_obj_field *hl_obj_field_fetch( hl_type *t, int fid );
 
 
@@ -796,7 +798,7 @@ HL_API void hl_throw_buffer( hl_buffer *b );
 // ----------------------- FFI ------------------------------------------------------
 // ----------------------- FFI ------------------------------------------------------
 
 
 // match GNU C++ mangling
 // match GNU C++ mangling
-#define TYPE_STR	"vcsilfdbBDPOATR??X?N?S"
+#define TYPE_STR	"vcsilfdbBDPOATR??X?N?S?g"
 
 
 #undef  _VOID
 #undef  _VOID
 #define _NO_ARG
 #define _NO_ARG
@@ -819,6 +821,7 @@ HL_API void hl_throw_buffer( hl_buffer *b );
 #undef _NULL
 #undef _NULL
 #define _NULL(t)					"N" t
 #define _NULL(t)					"N" t
 #define _STRUCT						"S"
 #define _STRUCT						"S"
+#define _GUID						"g"
 
 
 #undef _STRING
 #undef _STRING
 #define _STRING						_OBJ(_BYTES _I32)
 #define _STRING						_OBJ(_BYTES _I32)
@@ -941,6 +944,7 @@ typedef struct {
 	hl_thread_info **threads;
 	hl_thread_info **threads;
 	hl_mutex *global_lock;
 	hl_mutex *global_lock;
 	hl_mutex *exclusive_lock;
 	hl_mutex *exclusive_lock;
+	void *guid_map;
 } hl_threads_info;
 } hl_threads_info;
 
 
 HL_API hl_thread_info *hl_get_thread();
 HL_API hl_thread_info *hl_get_thread();

+ 6 - 0
src/std/buffer.c

@@ -162,6 +162,9 @@ static void hl_buffer_addr( hl_buffer *b, void *data, hl_type *t, vlist *stack )
 	case HBYTES:
 	case HBYTES:
 		hl_buffer_str(b,*(uchar**)data);
 		hl_buffer_str(b,*(uchar**)data);
 		break;
 		break;
+	case HGUID:
+		hl_buffer_str(b,hl_guid_str(*(int64*)data,buf));
+		break;
 	case HTYPE:
 	case HTYPE:
 	case HREF:
 	case HREF:
 	case HABSTRACT:
 	case HABSTRACT:
@@ -380,6 +383,9 @@ static void hl_buffer_rec( hl_buffer *b, vdynamic *v, vlist *stack ) {
 	case HNULL:
 	case HNULL:
 		hl_buffer_str_sub(b, USTR("_null_"), 6);
 		hl_buffer_str_sub(b, USTR("_null_"), 6);
 		break;
 		break;
+	case HGUID:
+		hl_buffer_str(b,hl_guid_str(v->v.i64,buf));
+		break;
 	default:
 	default:
 		hl_buffer_str_sub(b, buf, usprintf(buf, 32, _PTR_FMT USTR("H"),(int_val)v));
 		hl_buffer_str_sub(b, buf, usprintf(buf, 32, _PTR_FMT USTR("H"),(int_val)v));
 		break;
 		break;

+ 43 - 1
src/std/types.c

@@ -37,7 +37,7 @@ static const uchar *TSTR[] = {
 	USTR("void"), USTR("i8"), USTR("i16"), USTR("i32"), USTR("i64"), USTR("f32"), USTR("f64"),
 	USTR("void"), USTR("i8"), USTR("i16"), USTR("i32"), USTR("i64"), USTR("f32"), USTR("f64"),
 	USTR("bool"), USTR("bytes"), USTR("dynamic"), NULL, NULL,
 	USTR("bool"), USTR("bytes"), USTR("dynamic"), NULL, NULL,
 	USTR("array"), USTR("type"), NULL, NULL, USTR("dynobj"),
 	USTR("array"), USTR("type"), NULL, NULL, USTR("dynobj"),
-	NULL, NULL, NULL, NULL, NULL, NULL
+	NULL, NULL, NULL, NULL, NULL, NULL, USTR("guid")
 };
 };
 
 
 static int T_SIZES[] = {
 static int T_SIZES[] = {
@@ -64,6 +64,7 @@ static int T_SIZES[] = {
 	HL_WSIZE, // METHOD
 	HL_WSIZE, // METHOD
 	HL_WSIZE, // STRUCT
 	HL_WSIZE, // STRUCT
 	0, // PACKED
 	0, // PACKED
+	8, // GUID
 };
 };
 
 
 HL_PRIM int hl_type_size( hl_type *t ) {
 HL_PRIM int hl_type_size( hl_type *t ) {
@@ -86,6 +87,7 @@ HL_PRIM int hl_pad_struct( int size, hl_type *t ) {
 		GET_ALIGN(unsigned int);
 		GET_ALIGN(unsigned int);
 		break;
 		break;
 	case HI64:
 	case HI64:
+	case HGUID:
 		GET_ALIGN(int64);
 		GET_ALIGN(int64);
 		break;
 		break;
 	case HF32:
 	case HF32:
@@ -122,6 +124,7 @@ HL_PRIM bool hl_same_type( hl_type *a, hl_type *b ) {
 	case HDYN:
 	case HDYN:
 	case HARRAY:
 	case HARRAY:
 	case HDYNOBJ:
 	case HDYNOBJ:
+	case HGUID:
 		return true;
 		return true;
 	case HREF:
 	case HREF:
 	case HNULL:
 	case HNULL:
@@ -178,6 +181,7 @@ HL_PRIM bool hl_is_dynamic( hl_type *t ) {
 		false, // HMETHOD
 		false, // HMETHOD
 		false, // HSTRUCT
 		false, // HSTRUCT
 		false, // HPACKED
 		false, // HPACKED
+		false, // HGUID
 	};
 	};
 	return T_IS_DYNAMIC[t->kind];
 	return T_IS_DYNAMIC[t->kind];
 }
 }
@@ -574,6 +578,43 @@ HL_PRIM varray *hl_enum_parameters( venum *e ) {
 	return a;
 	return a;
 }
 }
 
 
+static void *hl_guid_map = NULL;
+extern void *hl_hi64alloc();
+extern void hl_hi64set( void *map, int64 guid, vbyte *name );
+extern vbyte *hl_hi64get( void *map, int64 guid );
+extern void hl_hi64remove( void *map, int64 guid );
+
+HL_PRIM uchar *hl_guid_str( int64 guid, uchar buf[14] ) {
+	static char CHARS[] = "#&0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+	int i;
+	int pos = 0;
+	uchar *name = hl_guid_map ? (uchar*)hl_hi64get(hl_guid_map,guid) : NULL;
+	if( name != NULL )
+		return name;
+	if( guid == 0 )
+		return USTR("0");
+	for(i=0;i<11;i++) {
+		if( i == 4 || i == 8 )
+			buf[pos++] = '-';
+		buf[pos++] = CHARS[(guid >> (60 - i*6))&63];
+	}
+	buf[pos++] = 0;
+	return buf;
+}
+
+HL_PRIM void hl_register_guid_name( int64 guid, vbyte *name ) {
+	if( hl_guid_map == NULL ) {
+		hl_guid_map = hl_hi64alloc();
+		hl_gc_threads_info()->guid_map = hl_guid_map;
+		hl_add_root(&hl_guid_map);
+	}
+	if( name )
+		hl_hi64set(hl_guid_map, guid, name);
+	else
+		hl_hi64remove(hl_guid_map, guid);
+}
+
+
 DEFINE_PRIM(_BYTES, type_str, _TYPE);
 DEFINE_PRIM(_BYTES, type_str, _TYPE);
 DEFINE_PRIM(_BYTES, type_name, _TYPE);
 DEFINE_PRIM(_BYTES, type_name, _TYPE);
 DEFINE_PRIM(_I32, type_args_count, _TYPE);
 DEFINE_PRIM(_I32, type_args_count, _TYPE);
@@ -586,6 +627,7 @@ DEFINE_PRIM(_BOOL, type_enum_eq, _DYN _DYN);
 DEFINE_PRIM(_DYN, alloc_enum_dyn, _TYPE _I32 _ARR _I32);
 DEFINE_PRIM(_DYN, alloc_enum_dyn, _TYPE _I32 _ARR _I32);
 DEFINE_PRIM(_ARR, enum_parameters, _DYN);
 DEFINE_PRIM(_ARR, enum_parameters, _DYN);
 DEFINE_PRIM(_BOOL, type_set_global, _TYPE _DYN);
 DEFINE_PRIM(_BOOL, type_set_global, _TYPE _DYN);
+DEFINE_PRIM(_VOID, register_guid_name, _I64 _BYTES);
 
 
 typedef void hl_mlookup_map;
 typedef void hl_mlookup_map;
 extern hl_mlookup_map *hl_mlookup_alloc();
 extern hl_mlookup_map *hl_mlookup_alloc();