• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef PANDA_RUNTIME_ETS_RUNTIME_ETS_CALLBACK_H
16 #define PANDA_RUNTIME_ETS_RUNTIME_ETS_CALLBACK_H
17 
18 #include "macros.h"
19 #include "mem/refstorage/reference.h"
20 #include "plugins/ets/runtime/ets_vm.h"
21 #include "plugins/ets/runtime/ets_utils.h"
22 #include "plugins/ets/runtime/ets_coroutine.h"
23 #include "plugins/ets/runtime/types/ets_object.h"
24 #include "plugins/ets/runtime/types/ets_promise.h"
25 #include "runtime/include/callback_queue.h"
26 #include "runtime/mem/vm_handle.h"
27 #include "runtime/mem/refstorage/global_object_storage.h"
28 #include "runtime/include/mem/panda_smart_pointers.h"
29 
30 namespace ark::ets {
31 class EtsCallback final : public Callback {
32 public:
Create(EtsCoroutine * coro,const EtsObject * callback)33     static PandaUniquePtr<Callback> Create(EtsCoroutine *coro, const EtsObject *callback)
34     {
35         auto *refStorage = coro->GetPandaVM()->GetGlobalObjectStorage();
36         auto *callbackRef = refStorage->Add(callback->GetCoreType(), mem::Reference::ObjectType::GLOBAL);
37         ASSERT(callbackRef != nullptr);
38         auto *etsCallback = Runtime::GetCurrent()->GetInternalAllocator()->New<EtsCallback>(callbackRef);
39         return PandaUniquePtr<Callback>(etsCallback);
40     }
41 
Run()42     void Run() override
43     {
44         auto *coro = EtsCoroutine::GetCurrent();
45         auto *refStorage = coro->GetPandaVM()->GetGlobalObjectStorage();
46         auto *callback = EtsObject::FromCoreType(refStorage->Get(callbackRef_));
47 
48         [[maybe_unused]] EtsHandleScope scope(coro);
49         EtsHandle<EtsObject> exception(coro, EtsObject::FromCoreType(coro->GetException()));
50 
51         LambdaUtils::InvokeVoid(coro, callback);
52 
53         if (exception.GetPtr() != nullptr) {
54             coro->SetException(exception.GetPtr()->GetCoreType());
55         }
56     }
57 
~EtsCallback()58     ~EtsCallback() override
59     {
60         auto *refStorage = EtsCoroutine::GetCurrent()->GetPandaVM()->GetGlobalObjectStorage();
61         refStorage->Remove(callbackRef_);
62     }
63 
64     NO_COPY_SEMANTIC(EtsCallback);
65     DEFAULT_MOVE_SEMANTIC(EtsCallback);
66 
67 private:
EtsCallback(mem::Reference * callbackRef)68     explicit EtsCallback(mem::Reference *callbackRef) : callbackRef_(callbackRef) {}
69 
70     mem::Reference *callbackRef_;
71 
72     friend class ark::mem::Allocator;
73 };
74 
75 }  // namespace ark::ets
76 
77 #endif  // PANDA_RUNTIME_ETS_RUNTIME_ETS_CALLBACK_H
78