1 /*
2 * Copyright (c) 2021 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/ecma_string.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/jobs/hitrace_scope.h"
21 #include "ecmascript/jobs/pending_job.h"
22 #include "ecmascript/js_arguments.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_tagged_value.h"
25 #include "ecmascript/js_thread.h"
26 #include "ecmascript/object_factory.h"
27 #include "ecmascript/tagged_queue.h"
28
29 namespace panda::ecmascript::job {
GetPromiseQueueSize(JSThread * thread,JSHandle<MicroJobQueue> jobQueue)30 uint32_t MicroJobQueue::GetPromiseQueueSize(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)
31 {
32 [[maybe_unused]] EcmaHandleScope handleScope(thread);
33 JSHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
34 return promiseQueue->Size();
35 }
36
EnqueueJob(JSThread * thread,JSHandle<MicroJobQueue> jobQueue,QueueType queueType,const JSHandle<JSFunction> & job,const JSHandle<TaggedArray> & argv)37 void MicroJobQueue::EnqueueJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue, QueueType queueType,
38 const JSHandle<JSFunction> &job, const JSHandle<TaggedArray> &argv)
39 {
40 // 1. Assert: Type(queueName) is String and its value is the name of a Job Queue recognized by this implementation.
41 // 2. Assert: job is the name of a Job.
42 // 3. Assert: arguments is a List that has the same number of elements as the number of parameters required by job.
43 // 4. Let callerContext be the running execution context.
44 // 5. Let callerRealm be callerContext’s Realm.
45 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
46 [[maybe_unused]] EcmaHandleScope handleScope(thread);
47 JSHandle<PendingJob> pendingJob(factory->NewPendingJob(job, argv));
48 ENQUEUE_JOB_HITRACE(pendingJob, queueType);
49 if (queueType == QueueType::QUEUE_PROMISE) {
50 JSHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
51 TaggedQueue *newPromiseQueue = TaggedQueue::Push(thread, promiseQueue, JSHandle<JSTaggedValue>(pendingJob));
52 jobQueue->SetPromiseJobQueue(thread, JSTaggedValue(newPromiseQueue));
53 LOG_ECMA(VERBOSE) << "EnqueueJob length: " << newPromiseQueue->Size();
54 } else if (queueType == QueueType::QUEUE_SCRIPT) {
55 JSHandle<TaggedQueue> scriptQueue(thread, jobQueue->GetScriptJobQueue());
56 TaggedQueue *newScriptQueue = TaggedQueue::Push(thread, scriptQueue, JSHandle<JSTaggedValue>(pendingJob));
57 jobQueue->SetScriptJobQueue(thread, JSTaggedValue(newScriptQueue));
58 }
59 }
60
ExecutePendingJob(JSThread * thread,JSHandle<MicroJobQueue> jobQueue)61 void MicroJobQueue::ExecutePendingJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)
62 {
63 [[maybe_unused]] EcmaHandleScope handleScope(thread);
64 JSMutableHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
65 JSMutableHandle<PendingJob> pendingJob(thread, JSTaggedValue::Undefined());
66 while (!promiseQueue->Empty()) {
67 LOG_ECMA(VERBOSE) << "ExecutePendingJob length: " << promiseQueue->Size();
68 pendingJob.Update(promiseQueue->Pop(thread));
69 PendingJob::ExecutePendingJob(pendingJob, thread);
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
HasPendingJob(JSThread * thread,JSHandle<MicroJobQueue> jobQueue)91 bool MicroJobQueue::HasPendingJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)
92 {
93 [[maybe_unused]] EcmaHandleScope handleScope(thread);
94 JSHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
95 return !promiseQueue->Empty();
96 }
97 } // namespace panda::ecmascript::job
98