1
1
#include " CallbackHandlers.h"
2
- #include " NativeScriptAssert.h"
3
2
#include " MetadataNode.h"
4
3
#include " Util.h"
5
4
#include " V8GlobalHelpers.h"
8
7
#include " JsArgToArrayConverter.h"
9
8
#include " ArgConverter.h"
10
9
#include " v8-profiler.h"
11
- #include " NativeScriptException.h"
12
10
#include < iostream>
13
11
#include < sstream>
14
12
#include < fstream>
20
18
#include < unistd.h>
21
19
#include < dlfcn.h>
22
20
23
-
24
21
using namespace v8 ;
25
22
using namespace std ;
26
23
using namespace tns ;
@@ -670,55 +667,59 @@ void CallbackHandlers::RunOnMainThreadCallback(const FunctionCallbackInfo<v8::Va
670
667
Isolate::Scope isolate_scope (isolate);
671
668
HandleScope handle_scope (isolate);
672
669
673
-
674
670
Local<Context> context = isolate->GetCurrentContext ();
675
671
676
- uint32_t key = ++count_;
672
+ uint64_t key = ++count_;
677
673
Local<v8::Function> callback = args[0 ].As <v8::Function>();
678
- CacheEntry entry (isolate, new Persistent<v8::Function>(isolate, callback));
679
- cache_.emplace (key, entry);
680
- auto key_str = std::to_string (key);
681
- write (Runtime::GetWriter (), key_str.c_str (), 1 );
674
+ CacheEntry entry (isolate, callback, context);
675
+ cache_.emplace (key, std::move (entry));
676
+ auto value = Callback (key);
677
+ auto size = sizeof (Callback);
678
+ auto wrote = write (Runtime::GetWriter (),&value , size);
682
679
}
683
680
684
681
int CallbackHandlers::RunOnMainThreadFdCallback (int fd, int events, void *data) {
685
- std::string buf;
686
- read (fd, &buf, 1 );
682
+ struct Callback value;
683
+ auto size = sizeof (Callback);
684
+ ssize_t nr = read (fd, &value, sizeof (value));
687
685
688
- auto key = std::stoi (buf) ;
686
+ auto key = value. id_ ;
689
687
690
688
auto it = cache_.find (key);
691
689
if (it == cache_.end ()) {
692
690
return 1 ;
693
691
}
694
692
695
693
Isolate *isolate = it->second .isolate_ ;
696
- Persistent<v8::Function> *poCallback = it->second .callback_ ;
697
694
v8::Locker locker (isolate);
698
695
Isolate::Scope isolate_scope (isolate);
699
696
HandleScope handle_scope (isolate);
700
- auto context = v8::Context::New (isolate);
697
+ auto context = it-> second . context_ . Get (isolate);
701
698
Context::Scope context_scope (context);
702
- Local<v8::Function> cb = poCallback-> Get (isolate);
699
+ Local<v8::Function> cb = it-> second . callback_ . Get (isolate);
703
700
Local<Value> result;
704
701
705
- if (!cb->Call (context, context->Global (), 0 , nullptr ).ToLocal (&result)) {
706
- assert (false );
702
+ v8::TryCatch tc (isolate);
703
+
704
+ if (!cb->Call (context, context->Global (), 0 , nullptr ).ToLocal (&result)) {}
705
+
706
+ if (tc.HasCaught ()){
707
+ throw NativeScriptException (tc);
707
708
}
708
709
709
710
RemoveKey (key);
711
+
710
712
return 1 ;
711
713
}
712
714
713
- void CallbackHandlers::RemoveKey (const uint32_t key) {
715
+ void CallbackHandlers::RemoveKey (const uint64_t key) {
714
716
auto it = cache_.find (key);
715
717
if (it == cache_.end ()) {
716
718
return ;
717
719
}
718
720
719
- Persistent<v8::Function> *poCallback = it->second .callback_ ;
720
- poCallback->Reset ();
721
- delete poCallback;
721
+ it->second .callback_ .Reset ();
722
+ it->second .context_ .Reset ();
722
723
cache_.erase (it);
723
724
}
724
725
@@ -1584,12 +1585,16 @@ void CallbackHandlers::TerminateWorkerThread(Isolate *isolate) {
1584
1585
void CallbackHandlers::RemoveIsolateEntries (v8::Isolate *isolate) {
1585
1586
for (auto &item: cache_) {
1586
1587
if (item.second .isolate_ == isolate) {
1588
+ item.second .callback_ .Reset ();
1589
+ item.second .context_ .Reset ();
1587
1590
cache_.erase (item.first );
1588
1591
}
1589
1592
}
1590
1593
1591
1594
for (auto &item: frameCallbackCache_) {
1592
1595
if (item.second .isolate_ == isolate) {
1596
+ item.second .callback_ .Reset ();
1597
+ item.second .context_ .Reset ();
1593
1598
frameCallbackCache_.erase (item.first );
1594
1599
}
1595
1600
}
@@ -1632,7 +1637,7 @@ void CallbackHandlers::PostFrameCallback(const FunctionCallbackInfo<v8::Value> &
1632
1637
v8::Locker locker (isolate);
1633
1638
Isolate::Scope isolate_scope (isolate);
1634
1639
HandleScope handle_scope (isolate);
1635
- auto context = Context::New ( isolate);
1640
+ auto context = isolate-> GetCurrentContext ( );
1636
1641
Context::Scope context_scope (context);
1637
1642
1638
1643
auto func = args[0 ].As <Function>();
@@ -1651,27 +1656,33 @@ void CallbackHandlers::PostFrameCallback(const FunctionCallbackInfo<v8::Value> &
1651
1656
}
1652
1657
}
1653
1658
1654
- Local<v8::Function> callback = args[ 0 ]. As <v8::Function>() ;
1659
+ Local<v8::Function> callback = func ;
1655
1660
uint64_t key = ++frameCallbackCount_;
1656
1661
1657
1662
V8SetPrivateValue (isolate, func, idKey, v8::Number::New (isolate, (double ) key));
1658
1663
1659
- FrameCallbackCacheEntry entry (isolate, new Persistent<v8::Function>(isolate, callback) );
1664
+ FrameCallbackCacheEntry entry (isolate, callback, context );
1660
1665
entry.id = key;
1661
- frameCallbackCache_.emplace (key, entry);
1662
1666
1663
1667
auto finalCallback = [](const v8::WeakCallbackInfo<FrameCallbackCacheEntry> &data) {
1664
1668
auto value = data.GetParameter ();
1665
1669
for (auto &item: frameCallbackCache_) {
1666
1670
if (item.second .id == value->id ) {
1667
- cache_.erase (item.first );
1671
+ item.second .context_ .Reset ();
1672
+ item.second .callback_ .Reset ();
1673
+ frameCallbackCache_.erase (item.first );
1668
1674
break ;
1669
1675
}
1670
1676
}
1671
1677
};
1672
- entry.callback_ ->SetWeak (&entry, finalCallback, v8::WeakCallbackType::kFinalizer );
1673
1678
1674
- PostCallback (args, &entry, context);
1679
+ entry.callback_ .SetWeak (&entry, finalCallback, v8::WeakCallbackType::kFinalizer );
1680
+
1681
+ frameCallbackCache_.emplace (key, std::move (entry));
1682
+
1683
+ auto val = frameCallbackCache_.find (key);
1684
+
1685
+ PostCallback (args, &val->second , context);
1675
1686
}
1676
1687
}
1677
1688
@@ -1685,7 +1696,7 @@ void CallbackHandlers::RemoveFrameCallback(const FunctionCallbackInfo<v8::Value>
1685
1696
v8::Locker locker (isolate);
1686
1697
Isolate::Scope isolate_scope (isolate);
1687
1698
HandleScope handle_scope (isolate);
1688
- auto context = Context::New ( isolate);
1699
+ auto context = isolate-> GetCurrentContext ( );
1689
1700
Context::Scope context_scope (context);
1690
1701
1691
1702
auto func = args[0 ].As <Function>();
@@ -1745,12 +1756,12 @@ void CallbackHandlers::InitChoreographer() {
1745
1756
}
1746
1757
1747
1758
1748
- robin_hood::unordered_map<uint32_t , CallbackHandlers::CacheEntry> CallbackHandlers::cache_;
1759
+ robin_hood::unordered_map<uint64_t , CallbackHandlers::CacheEntry> CallbackHandlers::cache_;
1749
1760
1750
1761
robin_hood::unordered_map<uint64_t , CallbackHandlers::FrameCallbackCacheEntry> CallbackHandlers::frameCallbackCache_;
1751
1762
1752
- _Atomic uint32_t CallbackHandlers::count_ = 0 ;
1753
- _Atomic uint64_t CallbackHandlers::frameCallbackCount_ = 0 ;
1763
+ std:: atomic_int64_t CallbackHandlers::count_ = { 0 } ;
1764
+ std:: atomic_uint64_t CallbackHandlers::frameCallbackCount_ = { 0 } ;
1754
1765
1755
1766
1756
1767
int CallbackHandlers::nextWorkerId = 0 ;
@@ -1770,4 +1781,4 @@ jmethodID CallbackHandlers::INIT_WORKER_METHOD_ID = nullptr;
1770
1781
1771
1782
NumericCasts CallbackHandlers::castFunctions;
1772
1783
ArrayElementAccessor CallbackHandlers::arrayElementAccessor;
1773
- FieldAccessor CallbackHandlers::fieldAccessor;
1784
+ FieldAccessor CallbackHandlers::fieldAccessor;
0 commit comments