Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 736c2eb

Browse files
committed
[[ CodeResources ]] Load from library mapping
This patch loads extension code resources from lbrary mappings if they have been mapped by the IDE or standalone deploy params. If there is no mapping then it falls back to try loading the library by name followed by attempting to load the library using legacy paths. This patch also ensures that iOS device builds retain the engine as the library handle rather than trying to load a library.
1 parent 9d30a0a commit 736c2eb

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

engine/src/exec-extension.cpp

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -229,64 +229,45 @@ void MCEngineLoadExtensionFromData(MCExecContext& ctxt, MCDataRef p_extension_da
229229
}
230230

231231
// This is the callback given to libscript so that it can resolve the absolute
232-
// path of native code libraries used by foreign handlers in the module. At
233-
// the moment we use the resources path of the module, however it will need to be
234-
// changed to a separate location at some point with explicit declaration so that
235-
// iOS linkage and Android placement issues can be resolved.
236-
//
237-
// Currently it expects:
238-
// <resources>
239-
// code/
240-
// mac/<name>.dylib
241-
// linux-x86/<name>.so
242-
// linux-x86_64/<name>.so
243-
// win-x86/<name>.dll
244-
//
232+
// path of native code libraries used by foreign handlers in the module.
233+
245234
static bool MCEngineLoadLibrary(MCScriptModuleRef p_module, MCStringRef p_name, MCSLibraryRef& r_library)
246235
{
247-
// If the module has no resource path, then it has no code.
248-
MCAutoStringRef t_resource_path;
249-
if (!MCEngineLookupResourcePathForModule(p_module, Out(t_resource_path)))
250-
return false;
251-
252236
MCSLibraryRef t_library = nullptr;
253-
if (t_resource_path.IsSet() &&
254-
!MCStringIsEmpty(*t_resource_path))
237+
238+
// extension libraries should be mapped by the IDE or deploy params
239+
MCAutoStringRef t_mapped_path;
240+
if (MCdispatcher->fetchlibrarymapping(p_name, &t_mapped_path))
255241
{
256-
#if defined(__MAC__)
257-
static const char *kLibraryFormat = "%@/code/mac/%@";
258-
#elif defined(__LINUX__) && defined(__32_BIT__)
259-
static const char *kLibraryFormat = "%@/code/linux-x86/%@";
260-
#elif defined(__LINUX__) && defined(__64_BIT__)
261-
static const char *kLibraryFormat = "%@/code/linux-x86_64/%@";
262-
#elif defined(__WINDOWS__)
263-
static const char *kLibraryFormat = "%@/code/win-x86/%@";
264-
#elif defined(__ANDROID__)
265-
static const char *kLibraryFormat = "%@/code/android-armv6/%@";
266-
#elif defined(__IOS__)
267-
static const char *kLibraryFormat = "%@/code/ios/%@";
268-
#elif defined(__EMSCRIPTEN__)
269-
static const char *kLibraryFormat = "%@/code/emscripten/%@";
270-
#else
271-
#error No default code path set for this platform
272-
#endif
273-
MCAutoStringRef t_ext_path;
274-
if (!MCStringFormat(&t_ext_path,
275-
kLibraryFormat,
276-
*t_resource_path,
277-
p_name))
242+
MCAutoStringRef t_map_name;
243+
if (!MCStringFormat(&t_map_name, "./%@", p_name))
244+
return false;
245+
246+
t_library = MCU_library_load(*t_map_name);
247+
248+
// there was a mapping and it failed to load
249+
if (t_library == nullptr)
278250
{
279251
return false;
280252
}
281-
282-
t_library = MCU_library_load(*t_ext_path);
283253
}
284254

255+
// if not mapped then fallback to assuming a full path or a location
256+
// supported by dlopen
285257
if (t_library == nullptr)
286258
{
287259
t_library = MCU_library_load(p_name);
288260
}
289261

262+
#if defined(__IOS__)
263+
// On iOS we fallback to the engine because with the exception of
264+
// dynamic frameworks libraries will be static linked
265+
if (t_library == nullptr)
266+
{
267+
t_library = MCValueRetain(MCScriptGetLibrary());
268+
}
269+
#endif
270+
290271
if (t_library == nullptr)
291272
{
292273
return false;

libscript/include/libscript/script.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ bool MCScriptForEachBuiltinModule(MCScriptForEachBuiltinModuleCallback p_callbac
5353

5454
void MCScriptSetLoadLibraryCallback(MCScriptLoadLibraryCallback callback);
5555

56+
MCSLibraryRef MCScriptGetLibrary(void);
57+
5658
void MCScriptSetWidgetBarrierCallbacks(MCScriptWidgetEnterCallback entry_callback, MCScriptWidgetLeaveCallback leave_callback);
5759

5860
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)