Skip to content

Commit bd6aee6

Browse files
committed
feat: provide valid import.meta.url behavior
1 parent b2b65fa commit bd6aee6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

NativeScript/runtime/Runtime.mm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "DisposerPHV.h"
2323
#include "IsolateWrapper.h"
2424

25+
#include <unordered_map>
2526
#include "ModuleBinding.hpp"
2627
#include "ModuleInternalCallbacks.h"
2728
#include "URLImpl.h"
@@ -34,6 +35,50 @@
3435
using namespace v8;
3536
using namespace std;
3637

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+
3782
namespace tns {
3883

3984
std::atomic<int> Runtime::nextIsolateId{0};
@@ -205,6 +250,10 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
205250
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
206251
isolate->SetHostImportModuleDynamicallyCallback(tns::ImportModuleDynamicallyCallback);
207252
#pragma clang diagnostic pop
253+
254+
// Set up import.meta callback
255+
isolate->SetHostInitializeImportMetaObjectCallback(InitializeImportMetaObject);
256+
208257
isolate->AddMessageListener(NativeScriptException::OnUncaughtError);
209258

210259
Local<Context> context = Context::New(isolate, nullptr, globalTemplate);

0 commit comments

Comments
 (0)