@@ -173,6 +173,8 @@ extern "C" void OutputBeginManifest(void);
173173extern " C" int IsBootstrapCompile (void );
174174extern " C" int IsNotBytecodeOutput (void );
175175
176+ extern " C" int ForceCBindingsAsBuiltins (void );
177+
176178extern " C" void DependStart (void );
177179extern " C" void DependFinish (void );
178180extern " C" void DependDefineMapping (NameRef module_name, const char *source_file);
@@ -319,17 +321,31 @@ static bool EmitEmittedModules(void)
319321 for (EmittedModule *t_module = s_emitted_modules; t_module != nullptr ; t_module = t_module->next )
320322 {
321323 const char *t_modified_name = t_module->modified_name ;
322-
324+
323325 if (0 > fprintf (t_file,
324- " static __builtin_module_info __%s_module_info =\n {\n 0,\n 0,\n %s_module_data,\n sizeof(%s_module_data),\n %s_Initialize,\n %s_Finalize,\n __builtin_ordinal_map\n };\n " ,
325- t_modified_name,
326- t_modified_name,
326+ " static __builtin_module_info __%s_module_info =\n {\n 0,\n 0,\n %s_module_data,\n sizeof(%s_module_data),\n " ,
327327 t_modified_name,
328328 t_modified_name,
329329 t_modified_name))
330330 return false ;
331331
332- if (0 > fprintf (t_file, " __builtin_module_loader __%s_loader(&__%s_module_info);\n\n " ,
332+ if (!ForceCBindingsAsBuiltins ())
333+ {
334+ if (0 > fprintf (t_file,
335+ " %s_Initialize,\n %s_Finalize,\n " ,
336+ t_modified_name,
337+ t_modified_name))
338+ return false ;
339+ }
340+ else
341+ {
342+ if (0 > fprintf (t_file,
343+ " nullptr,\n nullptr,\n " ))
344+ return false ;
345+ }
346+
347+ if (0 > fprintf (t_file,
348+ " __builtin_ordinal_map\n };\n __builtin_module_loader __%s_loader(&__%s_module_info);\n\n " ,
333349 t_modified_name,
334350 t_modified_name))
335351 return false ;
@@ -355,9 +371,9 @@ struct EmittedBuiltin
355371static EmittedBuiltin *s_emitted_builtins = nullptr ;
356372static uindex_t s_emitted_builtin_count = 0 ;
357373
358- static MCStringRef EmittedBuiltinAdd (NameRef p_symbol_name, uindex_t p_type_index)
374+ static MCStringRef EmittedBuiltinAdd (NameRef p_symbol_name, uindex_t p_type_index, bool p_force )
359375{
360- if (!OutputFileAsC || OutputFileAsAuxC)
376+ if (!p_force && (! OutputFileAsC || OutputFileAsAuxC) )
361377 {
362378 return MCNameGetString (to_mcnameref (p_symbol_name));
363379 }
@@ -808,17 +824,20 @@ EmitEndModuleOutputC (NameRef p_module_name,
808824 if (t_modified_name[i] == ' .' )
809825 t_modified_name[i] = ' _' ;
810826
811- // Emit the initializer reference.
812- if (0 > fprintf (s_output_code_file,
813- " extern \" C\" bool %s_Initialize(void);\n " ,
814- t_modified_name))
815- goto error_cleanup;
816-
817- // Emit the finalizer reference.
818- if (0 > fprintf (s_output_code_file,
819- " extern \" C\" void %s_Finalize(void);\n " ,
820- t_modified_name))
821- goto error_cleanup;
827+ if (!ForceCBindingsAsBuiltins ())
828+ {
829+ // Emit the initializer reference.
830+ if (0 > fprintf (s_output_code_file,
831+ " extern \" C\" bool %s_Initialize(void);\n " ,
832+ t_modified_name))
833+ goto error_cleanup;
834+
835+ // Emit the finalizer reference.
836+ if (0 > fprintf (s_output_code_file,
837+ " extern \" C\" void %s_Finalize(void);\n " ,
838+ t_modified_name))
839+ goto error_cleanup;
840+ }
822841
823842 if (0 > fprintf (t_file, " static unsigned char %s_module_data[] = {" , t_modified_name))
824843 goto error_cleanup;
@@ -1107,10 +1126,42 @@ void EmitVariableDefinition(intptr_t p_index, PositionRef p_position, NameRef p_
11071126
11081127static void EmitForeignHandlerDefinition (intptr_t p_index, PositionRef p_position, NameRef p_name, intptr_t p_type_index, intptr_t p_binding, bool p_force_builtin)
11091128{
1129+ MCAutoStringRef t_binding (to_mcstringref (p_binding));
1130+ MCAutoCustomPointer<struct MCScriptForeignHandlerInfo , MCScriptForeignHandlerInfoRelease> t_info;
1131+ if (!MCScriptForeignHandlerInfoParse (*t_binding, &t_info))
1132+ {
1133+ return ;
1134+ }
1135+
1136+ bool t_force_c_builtin = t_info->language == kMCScriptForeignHandlerLanguageC &&
1137+ ForceCBindingsAsBuiltins ();
1138+
11101139 MCAutoStringRef t_binding_str;
1111- if (strcmp ((const char *)p_binding, " <builtin>" ) == 0 || p_force_builtin)
1140+ if (t_info->language == kMCScriptForeignHandlerLanguageBuiltinC ||
1141+ p_force_builtin ||
1142+ t_force_c_builtin)
11121143 {
1113- t_binding_str = EmittedBuiltinAdd (p_force_builtin ? nameref_from_cstring ((const char *)p_binding) : p_name, p_type_index);
1144+ NameRef t_symbol_name;
1145+ if (t_info->language == kMCScriptForeignHandlerLanguageBuiltinC )
1146+ {
1147+ t_symbol_name = p_name;
1148+ }
1149+ else if (p_force_builtin)
1150+ {
1151+ t_symbol_name = nameref_from_cstring ((const char *)p_binding);
1152+ }
1153+ else
1154+ {
1155+ MCAutoStringRefAsCString t_symbol_c_string;
1156+ if (!t_symbol_c_string . Lock (t_info->c .function ))
1157+ {
1158+ return ;
1159+ }
1160+
1161+ t_symbol_name = nameref_from_cstring (*t_symbol_c_string);
1162+ }
1163+
1164+ t_binding_str = EmittedBuiltinAdd (t_symbol_name, p_type_index, ForceCBindingsAsBuiltins ());
11141165 }
11151166 else
11161167 t_binding_str = to_mcstringref (p_binding);
@@ -1386,7 +1437,7 @@ void EmitForeignType(intptr_t p_binding, intptr_t& r_type_index)
13861437{
13871438 uindex_t t_type_index;
13881439 MCStringRef t_binding_str =
1389- EmittedBuiltinAdd (nameref_from_mcstringref (to_mcstringref (p_binding)), UINDEX_MAX);
1440+ EmittedBuiltinAdd (nameref_from_mcstringref (to_mcstringref (p_binding)), UINDEX_MAX, false );
13901441
13911442 MCScriptAddForeignTypeToModule (s_builder, t_binding_str, t_type_index);
13921443 r_type_index = t_type_index;
0 commit comments