@@ -77,6 +77,7 @@ External links:
7777
7878#include "julia.h"
7979#include "julia_internal.h"
80+ #include "julia_gcext.h"
8081#include "builtin_proto.h"
8182#include "processor.h"
8283#include "serialize.h"
@@ -1249,6 +1250,9 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
12491250 ios_write (s -> s , (char * )v , sizeof (void * ) + jl_string_len (v ));
12501251 write_uint8 (s -> s , '\0' ); // null-terminated strings for easier C-compatibility
12511252 }
1253+ else if (jl_is_foreign_type (t ) == 1 ) {
1254+ jl_error ("Cannot serialize instances of foreign datatypes" );
1255+ }
12521256 else if (jl_datatype_nfields (t ) == 0 ) {
12531257 // The object has no fields, so we just snapshot its byte representation
12541258 assert (!t -> layout -> npointers );
@@ -1438,10 +1442,14 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
14381442 if (dt -> layout != NULL ) {
14391443 size_t nf = dt -> layout -> nfields ;
14401444 size_t np = dt -> layout -> npointers ;
1441- size_t fieldsize = jl_fielddesc_size (dt -> layout -> fielddesc_type );
1445+ size_t fieldsize = 0 ;
1446+ uint8_t is_foreign_type = dt -> layout -> fielddesc_type == 3 ;
1447+ if (!is_foreign_type ) {
1448+ fieldsize = jl_fielddesc_size (dt -> layout -> fielddesc_type );
1449+ }
14421450 char * flddesc = (char * )dt -> layout ;
14431451 size_t fldsize = sizeof (jl_datatype_layout_t ) + nf * fieldsize ;
1444- if (dt -> layout -> first_ptr != -1 )
1452+ if (! is_foreign_type && dt -> layout -> first_ptr != -1 )
14451453 fldsize += np << dt -> layout -> fielddesc_type ;
14461454 uintptr_t layout = LLT_ALIGN (ios_pos (s -> const_data ), sizeof (void * ));
14471455 write_padding (s -> const_data , layout - ios_pos (s -> const_data )); // realign stream
@@ -1450,6 +1458,13 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
14501458 arraylist_push (& s -> relocs_list , (void * )(reloc_offset + offsetof(jl_datatype_t , layout ))); // relocation location
14511459 arraylist_push (& s -> relocs_list , (void * )(((uintptr_t )ConstDataRef << RELOC_TAG_OFFSET ) + layout )); // relocation target
14521460 ios_write (s -> const_data , flddesc , fldsize );
1461+ if (is_foreign_type ) {
1462+ // make sure we have space for the extra hidden pointers
1463+ // zero them since they will need to be re-initialized externally
1464+ assert (fldsize == sizeof (jl_datatype_layout_t ));
1465+ jl_fielddescdyn_t dyn = {0 , 0 };
1466+ ios_write (s -> const_data , (char * )& dyn , sizeof (jl_fielddescdyn_t ));
1467+ }
14531468 }
14541469 }
14551470 else if (jl_is_typename (v )) {
0 commit comments