Skip to content

Commit becb19b

Browse files
authored
fix: throw JS exception instead of silent warning for disposed native object calls in debug mode (#354)
1 parent a61d35c commit becb19b

1 file changed

Lines changed: 10 additions & 18 deletions

File tree

NativeScript/runtime/ArgConverter.mm

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <Foundation/Foundation.h>
22
#include <sstream>
3-
#include <unordered_set>
43
#include "ArgConverter.h"
54
#include "NativeScriptException.h"
65
#include "DictionaryAdapter.h"
@@ -32,20 +31,16 @@
3231

3332
if (wrapper == nullptr) {
3433
// During fast view churn like HMR in development, JS objects can outlive their
35-
// native wrappers briefly. In Debug, avoid a crash and just skip the native call.
34+
// native wrappers briefly. In Debug, throw a catchable JS error instead of crashing.
3635
// In Release, assert so crash reporting can capture unexpected cases.
3736
if (RuntimeConfig.IsDebug) {
3837
const char* selectorStr = meta ? meta->selectorAsString() : "<unknown>";
3938
const char* jsNameStr = meta ? meta->jsName() : "<unknown>";
4039
const char* classNameStr = klass ? class_getName(klass) : "<unknown>";
41-
// Suppress duplicate logs: only log once per class+selector for this process.
42-
static std::unordered_set<std::string> s_logged;
43-
std::string key = std::string(classNameStr) + ":" + selectorStr;
44-
if (s_logged.insert(key).second) {
45-
Log(@"Note: ignore method on non-native receiver (class: %s, selector: %s, jsName: %s, args: %d). Common during HMR.",
46-
classNameStr, selectorStr, jsNameStr, (int)args.Length());
47-
}
48-
return v8::Undefined(isolate);
40+
std::string errMsg = std::string("Cannot call method '") + jsNameStr +
41+
"' on a disposed native object (class: " + classNameStr +
42+
", selector: " + selectorStr + "). This can happen during HMR or fast view churn.";
43+
throw NativeScriptException(isolate, errMsg);
4944
} else {
5045
tns::Assert(false, isolate);
5146
}
@@ -69,14 +64,11 @@
6964
const char* selectorStr = meta ? meta->selectorAsString() : "<unknown>";
7065
const char* jsNameStr = meta ? meta->jsName() : "<unknown>";
7166
const char* classNameStr = klass ? class_getName(klass) : "<unknown>";
72-
// Suppress duplicate logs: only log once per class+selector for this process.
73-
static std::unordered_set<std::string> s_logged;
74-
std::string key = std::string(classNameStr) + ":" + selectorStr;
75-
if (s_logged.insert(key).second) {
76-
Log(@"Note: ignore receiver wrapper type %d (class: %s, selector: %s, jsName: %s). Common during HMR.",
77-
(int)wrapper->Type(), classNameStr, selectorStr, jsNameStr);
78-
}
79-
return v8::Undefined(isolate);
67+
std::string errMsg = std::string("Unexpected receiver wrapper type ") +
68+
std::to_string((int)wrapper->Type()) + " for method '" + jsNameStr +
69+
"' (class: " + classNameStr + ", selector: " + selectorStr +
70+
"). This can happen during HMR or fast view churn.";
71+
throw NativeScriptException(isolate, errMsg);
8072
} else {
8173
tns::Assert(false, isolate);
8274
}

0 commit comments

Comments
 (0)