Skip to content

Commit 42a5328

Browse files
authored
fix: prevent crash during debug on fast view churn (like with HMR) (#294)
1 parent b12d552 commit 42a5328

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

NativeScript/runtime/ArgConverter.mm

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include <Foundation/Foundation.h>
22
#include <sstream>
3+
#include <unordered_set>
34
#include "ArgConverter.h"
45
#include "NativeScriptException.h"
56
#include "DictionaryAdapter.h"
67
#include "ObjectManager.h"
78
#include "Interop.h"
89
#include "Helpers.h"
910
#include "Runtime.h"
11+
#include "RuntimeConfig.h"
1012

1113
using namespace v8;
1214
using namespace std;
@@ -27,7 +29,27 @@
2729
bool callSuper = false;
2830
if (instanceMethod) {
2931
BaseDataWrapper* wrapper = tns::GetValue(isolate, receiver);
30-
tns::Assert(wrapper != nullptr, isolate);
32+
33+
if (wrapper == nullptr) {
34+
// 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.
36+
// In Release, assert so crash reporting can capture unexpected cases.
37+
if (RuntimeConfig.IsDebug) {
38+
const char* selectorStr = meta ? meta->selectorAsString() : "<unknown>";
39+
const char* jsNameStr = meta ? meta->jsName() : "<unknown>";
40+
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);
49+
} else {
50+
tns::Assert(false, isolate);
51+
}
52+
}
3153

3254
if (wrapper->Type() == WrapperType::ObjCAllocObject) {
3355
ObjCAllocDataWrapper* allocWrapper = static_cast<ObjCAllocDataWrapper*>(wrapper);
@@ -43,7 +65,21 @@
4365
// For extended classes we will call the base method
4466
callSuper = isMethodCallback && it != cache->ClassPrototypes.end();
4567
} else {
46-
tns::Assert(false, isolate);
68+
if (RuntimeConfig.IsDebug) {
69+
const char* selectorStr = meta ? meta->selectorAsString() : "<unknown>";
70+
const char* jsNameStr = meta ? meta->jsName() : "<unknown>";
71+
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);
80+
} else {
81+
tns::Assert(false, isolate);
82+
}
4783
}
4884
}
4985

@@ -878,7 +914,7 @@
878914
Local<Object> thiz = args.This();
879915
Isolate* isolate = args.GetIsolate();
880916
BaseDataWrapper* wrapper = tns::GetValue(isolate, thiz);
881-
if (wrapper == nullptr && wrapper->Type() != WrapperType::ObjCObject) {
917+
if (wrapper == nullptr || wrapper->Type() != WrapperType::ObjCObject) {
882918
return;
883919
}
884920

0 commit comments

Comments
 (0)