8#if defined(DART_HOST_OS_WINDOWS)
11#include <combaseapi.h>
25#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
26 defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
32#if defined(USING_SIMULATOR) || (defined(DART_PRECOMPILER) && !defined(TESTING))
34DART_NORETURN
static void SimulatorUnsupported() {
35#if defined(USING_SIMULATOR)
37 "Not supported on simulated architectures.");
44 SimulatorUnsupported();
47 SimulatorUnsupported();
50 SimulatorUnsupported();
53 SimulatorUnsupported();
56 SimulatorUnsupported();
59 SimulatorUnsupported();
62 SimulatorUnsupported();
66 SimulatorUnsupported();
75 char**
error =
nullptr) {
76 char* utils_error =
nullptr;
78 if (utils_error !=
nullptr) {
79 if (
error !=
nullptr) {
81 nullptr,
"Failed to load dynamic library '%s': %s",
82 library_file !=
nullptr ? library_file :
"<process>", utils_error);
89#if defined(DART_HOST_OS_WINDOWS)
91const nullptr_t kWindowsDynamicLibraryProcessPtr =
nullptr;
93void* co_task_mem_allocated =
nullptr;
97void* LookupSymbolInProcess(
const char* symbol,
char**
error) {
99 if (co_task_mem_allocated ==
nullptr) {
100 co_task_mem_allocated = CoTaskMemAlloc(
sizeof(intptr_t));
101 CoTaskMemFree(co_task_mem_allocated);
105 OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
106 GetCurrentProcessId());
107 if (current_process ==
nullptr) {
114 if (EnumProcessModules(current_process, modules,
sizeof(modules),
116 for (intptr_t i = 0; i < (cb_needed /
sizeof(
HMODULE)); i++) {
118 reinterpret_cast<void*
>(GetProcAddress(modules[i], symbol))) {
119 CloseHandle(current_process);
124 CloseHandle(current_process);
128 "None of the loaded modules contained the requested symbol '%s'.",
137#if defined(DART_HOST_OS_WINDOWS)
138 if (handle == kWindowsDynamicLibraryProcessPtr) {
139 return LookupSymbolInProcess(symbol,
error);
146 char*
error =
nullptr;
147#if !defined(DART_HOST_OS_WINDOWS)
150 if (handle ==
nullptr) {
151 LookupSymbolInProcess(symbol, &
error);
156 if (
error !=
nullptr) {
166 char*
error =
nullptr;
168 if (
error !=
nullptr) {
177#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
178 defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
191 if (dlib.IsClosed()) {
193 }
else if (!dlib.CanBeClosed()) {
195 String::New(
"DynamicLibrary.process() and DynamicLibrary.executable() "
196 "can't be closed."));
199 void* handle = dlib.GetHandle();
200 char*
error =
nullptr;
203 if (
error ==
nullptr) {
204 dlib.SetClosed(
true);
218 arguments->NativeArgAt(1));
220 if (dlib.IsClosed()) {
226 void* handle = dlib.GetHandle();
228 char*
error =
nullptr;
229 const uword pointer =
reinterpret_cast<uword>(
231 if (
error !=
nullptr) {
233 "Failed to lookup symbol '%s': %s", argSymbolName.ToCString(),
error));
243 intptr_t handle =
reinterpret_cast<intptr_t
>(dlib.GetHandle());
250 arguments->NativeArgAt(1));
252 void* handle = dlib.GetHandle();
258 const String& lib_url_str) {
278 "Couldn't resolve function: '%s'", symbol.
ToCString());
283#if defined(DART_TARGET_OS_WINDOWS)
285static void ReplaceBackSlashes(
char* cstr) {
286 const intptr_t
length = strlen(cstr);
287 for (
int i = 0; i <
length; i++) {
288 cstr[i] = cstr[i] ==
'\\' ?
'/' : cstr[i];
300#if defined(DART_TARGET_OS_WINDOWS)
303 const char* file_schema_slash =
"file:///";
304 const int file_schema_slash_length = 8;
305 const char* path =
source->script_uri;
306 if (strlen(
source->script_uri) > file_schema_slash_length &&
307 strncmp(
source->script_uri, file_schema_slash,
308 file_schema_slash_length) == 0) {
309 path = (
source->script_uri + file_schema_slash_length);
313 const intptr_t len = strlen(path);
315 snprintf(
path_copy, len + 1,
"%s", path);
336 const auto& native_assets_map =
338 if (!native_assets_map.IsNull()) {
341 if (!lookup.IsNull()) {
342 result = Array::Cast(lookup).ptr();
353 const auto& native_assets_map =
357 if (native_assets_map.IsNull()) {
358 buffer.Printf(
"No available native assets.");
361 buffer.Printf(
"Available native assets: ");
363 NativeAssetsMap::Iterator it(&map);
365 while (it.MoveNext()) {
369 auto entry = it.Current();
370 asset_id ^= map.GetKey(entry);
371 buffer.Printf(
"%s", asset_id.ToCString());
386 const Array& asset_location,
391 const auto& asset_type =
394 if (asset_type.Equals(Symbols::absolute()) ||
395 asset_type.Equals(Symbols::relative()) ||
396 asset_type.Equals(Symbols::system())) {
399 void* handle =
nullptr;
400 if (asset_type.Equals(Symbols::absolute())) {
402 }
else if (asset_type.Equals(Symbols::relative())) {
408 const char* target_uri =
nullptr;
409 char* path_cstr = path.ToMallocCString();
410#if defined(DART_TARGET_OS_WINDOWS)
411 ReplaceBackSlashes(path_cstr);
413 const bool resolved =
414 ResolveUri(path_cstr, platform_script_uri.ToCString(), &target_uri);
418 "Failed to resolve '%s' relative to '%s'.",
419 path.ToCString(), platform_script_uri.ToCString());
424 }
else if (asset_type.Equals(Symbols::system())) {
426 }
else if (asset_type.Equals(Symbols::process())) {
427#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
428 defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
429 handle = RTLD_DEFAULT;
431 handle = kWindowsDynamicLibraryProcessPtr;
433 }
else if (asset_type.Equals(Symbols::executable())) {
438 if (*
error !=
nullptr) {
439 char* inner_error = *
error;
441 "Failed to load dynamic library '%s': %s",
442 path.ToCString(), inner_error);
446 if (*
error !=
nullptr) {
447 char* inner_error = *
error;
449 "Failed to lookup symbol '%s': %s",
465 "Couldn't resolve native function '%s' in '%s' : %s.\n",
480 if (resolver !=
nullptr) {
482 thread, resolver, symbol, args_n,
error);
483 return reinterpret_cast<intptr_t
>(ffi_native_result);
487 const auto& asset_location =
489 if (!asset_location.IsNull()) {
491 return reinterpret_cast<intptr_t
>(asset_result);
495#if !defined(DART_HOST_OS_WINDOWS)
502 if (*
error !=
nullptr) {
505 char* process_lookup_error = *
error;
507 "No asset with id '%s' found. %s "
508 "Attempted to fallback to process lookup. %s",
510 process_lookup_error);
511 free(process_lookup_error);
514 return reinterpret_cast<intptr_t
>(
result);
523 auto*
const zone = thread->zone();
526 char*
error =
nullptr;
529 if (
error !=
nullptr) {
static const String & UnwrapStringHandle(const ReusableObjectHandleScope &reused, Dart_Handle object)
ObjectPtr At(intptr_t index) const
static const Bool & Get(bool value)
static DynamicLibraryPtr New(void *handle, bool canBeClosed, Heap::Space space=Heap::kNew)
static DART_NORETURN void ThrowStateError(const Instance &arg)
static DART_NORETURN void ThrowUnsupportedError(const char *msg)
static DART_NORETURN void ThrowArgumentError(const Instance &arg)
static IntegerPtr NewFromUint64(uint64_t value, Heap::Space space=Heap::kNew)
IsolateGroupSource * source() const
static LibraryPtr LookupLibrary(Thread *thread, const String &url)
Dart_FfiNativeResolver ffi_native_resolver() const
static char * SCreate(Zone *zone, const char *format,...) PRINTF_ATTRIBUTE(2
static ObjectPtr RawCast(ObjectPtr obj)
static PointerPtr New(uword native_address, Heap::Space space=Heap::kNew)
static StringPtr NewFormatted(const char *format,...) PRINTF_ATTRIBUTE(1
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
static const char * ToCString(Thread *thread, StringPtr ptr)
static Thread * Current()
IsolateGroup * isolate_group() const
static void * LoadDynamicLibrary(const char *library_path, char **error=nullptr)
static void UnloadDynamicLibrary(void *library_handle, char **error=nullptr)
static void * ResolveSymbolInDynamicLibrary(void *library_handle, const char *symbol, char **error=nullptr)
struct _Dart_Handle * Dart_Handle
void *(* Dart_FfiNativeResolver)(const char *name, uintptr_t args_n)
#define DARTSCOPE(thread)
static const uint8_t buffer[]
const uint8_t uint32_t uint32_t GError ** error
static ArrayPtr GetAssetLocation(Thread *const thread, const String &asset)
static Dart_FfiNativeResolver GetFfiNativeResolver(Thread *const thread, const String &lib_url_str)
bool ResolveUri(const char *ref_uri, const char *base_uri, const char **target_uri)
static void * FfiResolveAsset(Thread *const thread, const Array &asset_location, const String &symbol, char **error)
void * malloc(size_t size)
static void * FfiResolveWithFfiNativeResolver(Thread *const thread, Dart_FfiNativeResolver resolver, const String &symbol, intptr_t args_n, char **error)
static char * AvailableAssetsToCString(Thread *const thread)
static void ThrowFfiResolveError(const String &symbol, const String &asset, char *error)
static intptr_t FfiResolve(Dart_Handle asset_handle, Dart_Handle symbol_handle, uintptr_t args_n)
ArrayPtr GetNativeAssetsMap(Thread *thread)
static void * LoadDynamicLibrary(const char *library_file, char **error=nullptr)
const int file_schema_length
intptr_t FfiResolveInternal(const String &asset, const String &symbol, uintptr_t args_n, char **error)
static StringPtr GetPlatformScriptPath(Thread *thread)
static bool SymbolExists(void *handle, const char *symbol)
static void * ResolveSymbol(void *handle, const char *symbol, char **error)
#define DEFINE_NATIVE_ENTRY(name, type_argument_count, argument_count)
#define GET_NON_NULL_NATIVE_ARGUMENT(type, name, value)
SKWASM_EXPORT SkPath * path_copy(SkPath *path)