1 /*
2  * Copyright (c) 2021-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 
16 #include "ecmascript/jobs/micro_job_queue.h"
17 
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/jobs/pending_job.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 #include "ecmascript/object_factory.h"
22 #include "ecmascript/tagged_queue.h"
23 
24 namespace panda::ecmascript::job {
GetPromiseQueueSize(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)25 uint32_t MicroJobQueue::GetPromiseQueueSize(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)
26 {
27     [[maybe_unused]] EcmaHandleScope handleScope(thread);
28     JSHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
29     return promiseQueue->Size();
30 }
31 
EnqueueJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue, QueueType queueType, const JSHandle<JSFunction> &job, const JSHandle<TaggedArray> &argv)32 void MicroJobQueue::EnqueueJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue, QueueType queueType,
33     const JSHandle<JSFunction> &job, const JSHandle<TaggedArray> &argv)
34 {
35     // 1. Assert: Type(queueName) is String and its value is the name of a Job Queue recognized by this implementation.
36     // 2. Assert: job is the name of a Job.
37     // 3. Assert: arguments is a List that has the same number of elements as the number of parameters required by job.
38     // 4. Let callerContext be the running execution context.
39     // 5. Let callerRealm be callerContext’s Realm.
40     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
41     [[maybe_unused]] EcmaHandleScope handleScope(thread);
42     JSHandle<PendingJob> pendingJob(factory->NewPendingJob(job, argv));
43     ENQUEUE_JOB_HITRACE(pendingJob, queueType);
44     ENQUEUE_JOB_TRACE(thread, pendingJob);
45 
46     if (queueType == QueueType::QUEUE_PROMISE) {
47         JSHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
48         TaggedQueue *newPromiseQueue = TaggedQueue::Push(thread, promiseQueue, JSHandle<JSTaggedValue>(pendingJob));
49         jobQueue->SetPromiseJobQueue(thread, JSTaggedValue(newPromiseQueue));
50         LOG_ECMA(VERBOSE) << "EnqueueJob length: " << newPromiseQueue->Size();
51     } else if (queueType == QueueType::QUEUE_SCRIPT) {
52         JSHandle<TaggedQueue> scriptQueue(thread, jobQueue->GetScriptJobQueue());
53         TaggedQueue *newScriptQueue = TaggedQueue::Push(thread, scriptQueue, JSHandle<JSTaggedValue>(pendingJob));
54         jobQueue->SetScriptJobQueue(thread, JSTaggedValue(newScriptQueue));
55     }
56 }
57 
ExecutePendingJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)58 void MicroJobQueue::ExecutePendingJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)
59 {
60     [[maybe_unused]] EcmaHandleScope handleScope(thread);
61     JSMutableHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
62     JSMutableHandle<PendingJob> pendingJob(thread, JSTaggedValue::Undefined());
63     while (!promiseQueue->Empty()) {
64         LOG_ECMA(VERBOSE) << "ExecutePendingJob length: " << promiseQueue->Size();
65         pendingJob.Update(promiseQueue->Pop(thread));
66         PendingJob::ExecutePendingJob(pendingJob, thread);
67         if (!thread->IsInConcurrentScope()) {
68             thread->SetTaskInfo(reinterpret_cast<uintptr_t>(nullptr));
69         }
70         if (thread->HasPendingException()) {
71             return;
72         }
73         if (thread->HasTerminated()) {
74             JSHandle<TaggedQueue> emptyQueue(thread->GlobalConstants()->GetHandledEmptyTaggedQueue());
75             jobQueue->SetPromiseJobQueue(thread, emptyQueue);
76             return;
77         }
78         promiseQueue.Update(jobQueue->GetPromiseJobQueue());
79     }
80 
81     JSHandle<TaggedQueue> scriptQueue(thread, jobQueue->GetScriptJobQueue());
82     while (!scriptQueue->Empty()) {
83         pendingJob.Update(scriptQueue->Pop(thread));
84         PendingJob::ExecutePendingJob(pendingJob, thread);
85         if (thread->HasPendingException()) {
86             return;
87         }
88     }
89 }
90 }  // namespace panda::ecmascript::job
91