|
22 | 22 | #include "DisposerPHV.h"
|
23 | 23 | #include "IsolateWrapper.h"
|
24 | 24 |
|
| 25 | +#include <unordered_map> |
25 | 26 | #include "ModuleBinding.hpp"
|
26 | 27 | #include "ModuleInternalCallbacks.h"
|
27 | 28 | #include "URLImpl.h"
|
|
34 | 35 | using namespace v8;
|
35 | 36 | using namespace std;
|
36 | 37 |
|
| 38 | +// Import meta callback to support import.meta.url |
| 39 | +static void InitializeImportMetaObject(Local<Context> context, Local<Module> module, |
| 40 | + Local<Object> meta) { |
| 41 | + Isolate* isolate = context->GetIsolate(); |
| 42 | + |
| 43 | + // Look up the module path in the global module registry |
| 44 | + std::string modulePath; |
| 45 | + |
| 46 | + for (auto& kv : tns::g_moduleRegistry) { |
| 47 | + Local<Module> registered = kv.second.Get(isolate); |
| 48 | + if (registered == module) { |
| 49 | + modulePath = kv.first; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Debug logging |
| 55 | + NSLog(@"[import.meta] Module lookup: found path = %s", |
| 56 | + modulePath.empty() ? "(empty)" : modulePath.c_str()); |
| 57 | + NSLog(@"[import.meta] Registry size: %zu", tns::g_moduleRegistry.size()); |
| 58 | + |
| 59 | + // Convert file path to file:// URL |
| 60 | + std::string moduleUrl; |
| 61 | + if (!modulePath.empty()) { |
| 62 | + // Remove base directory and create file:// URL |
| 63 | + std::string base = tns::ReplaceAll(modulePath, RuntimeConfig.BaseDir, ""); |
| 64 | + moduleUrl = "file://" + base; |
| 65 | + } else { |
| 66 | + // Fallback URL if module not found in registry |
| 67 | + moduleUrl = "file:///app/"; |
| 68 | + } |
| 69 | + |
| 70 | + NSLog(@"[import.meta] Final URL: %s", moduleUrl.c_str()); |
| 71 | + |
| 72 | + Local<String> url = |
| 73 | + String::NewFromUtf8(isolate, moduleUrl.c_str(), NewStringType::kNormal).ToLocalChecked(); |
| 74 | + |
| 75 | + // Set import.meta.url property |
| 76 | + meta->CreateDataProperty( |
| 77 | + context, String::NewFromUtf8(isolate, "url", NewStringType::kNormal).ToLocalChecked(), |
| 78 | + url) |
| 79 | + .Check(); |
| 80 | +} |
| 81 | + |
37 | 82 | namespace tns {
|
38 | 83 |
|
39 | 84 | std::atomic<int> Runtime::nextIsolateId{0};
|
@@ -205,6 +250,10 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
|
205 | 250 | #pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
206 | 251 | isolate->SetHostImportModuleDynamicallyCallback(tns::ImportModuleDynamicallyCallback);
|
207 | 252 | #pragma clang diagnostic pop
|
| 253 | + |
| 254 | + // Set up import.meta callback |
| 255 | + isolate->SetHostInitializeImportMetaObjectCallback(InitializeImportMetaObject); |
| 256 | + |
208 | 257 | isolate->AddMessageListener(NativeScriptException::OnUncaughtError);
|
209 | 258 |
|
210 | 259 | Local<Context> context = Context::New(isolate, nullptr, globalTemplate);
|
|
0 commit comments