Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions NativeScript/ffi/jni/jsi/metadata/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <set>
#include <cerrno>
#include <unistd.h>
#include <android/log.h>
#include "NativeScriptException.h"
#include "MetadataNode.h"
#include "CallbackHandlers.h"
Expand Down Expand Up @@ -887,13 +888,14 @@ void MetadataNode::CreateTopLevelNamespaces(JsRuntime &rt) {
if (IsJavascriptKeyword(nameSpace)) {
nameSpace = "$" + nameSpace;
}
try {
global.setProperty(rt, nameSpace.c_str(), packageObj);
} catch (JsError &) {
// A host can already own the name as a non-writable global (Expo
// defines `expo`), so fall back to the same $ prefix as keywords.
global.setProperty(rt, ("$" + nameSpace).c_str(), packageObj);
if (global.hasProperty(rt, nameSpace.c_str())) {
string fallback = "$" + nameSpace;
__android_log_print(ANDROID_LOG_WARN, "TNS.Runtime",
"Java package '%s' is exposed as '%s': the global '%s' already exists.",
nameSpace.c_str(), fallback.c_str(), nameSpace.c_str());
nameSpace = fallback;
}
global.setProperty(rt, nameSpace.c_str(), packageObj);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions NativeScript/ffi/jni/napi/metadata/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <set>
#include <cerrno>
#include <unistd.h>
#include <android/log.h>
#include "NativeScriptException.h"
#include "MetadataNode.h"
#include "CallbackHandlers.h"
Expand Down Expand Up @@ -1120,6 +1121,14 @@ void MetadataNode::CreateTopLevelNamespaces(napi_env env) {
if (IsJavascriptKeyword(nameSpace)) {
nameSpace = "$" + nameSpace;
}
bool exists = false;
if (napi_has_named_property(env, global, nameSpace.c_str(), &exists) == napi_ok && exists) {
string fallback = "$" + nameSpace;
__android_log_print(ANDROID_LOG_WARN, "TNS.Runtime",
"Java package '%s' is exposed as '%s': the global '%s' already exists.",
nameSpace.c_str(), fallback.c_str(), nameSpace.c_str());
nameSpace = fallback;
}
NAPI_GUARD(napi_set_named_property(env, global, nameSpace.c_str(), packageObj)) {}
}
}
Expand Down
Loading