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