• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/global_env.h"
19 #include "ecmascript/jobs/hitrace_scope.h"
20 #include "ecmascript/jobs/pending_job.h"
21 #include "ecmascript/js_arguments.h"
22 #include "ecmascript/js_handle.h"
23 #include "ecmascript/js_tagged_value.h"
24 #include "ecmascript/js_thread.h"
25 #include "ecmascript/object_factory.h"
26 #include "ecmascript/tagged_queue.h"
27 #include "ecmascript/tagged_queue.h"
28 
29 #include "utils/expected.h"
30 
31 namespace panda::ecmascript::job {
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     if (queueType == QueueType::QUEUE_PROMISE) {
45         JSHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
46         TaggedQueue *newPromiseQueue = TaggedQueue::Push(thread, promiseQueue, JSHandle<JSTaggedValue>(pendingJob));
47         jobQueue->SetPromiseJobQueue(thread, JSTaggedValue(newPromiseQueue));
48         LOG_ECMA(VERBOSE) << "EnqueueJob length: " << newPromiseQueue->Size();
49     } else if (queueType == QueueType::QUEUE_SCRIPT) {
50         JSHandle<TaggedQueue> scriptQueue(thread, jobQueue->GetScriptJobQueue());
51         TaggedQueue *newScriptQueue = TaggedQueue::Push(thread, scriptQueue, JSHandle<JSTaggedValue>(pendingJob));
52         jobQueue->SetScriptJobQueue(thread, JSTaggedValue(newScriptQueue));
53     }
54 }
55 
ExecutePendingJob(JSThread * thread,JSHandle<MicroJobQueue> jobQueue)56 void MicroJobQueue::ExecutePendingJob(JSThread *thread, JSHandle<MicroJobQueue> jobQueue)
57 {
58     [[maybe_unused]] EcmaHandleScope handleScope(thread);
59     JSMutableHandle<TaggedQueue> promiseQueue(thread, jobQueue->GetPromiseJobQueue());
60     JSMutableHandle<PendingJob> pendingJob(thread, JSTaggedValue::Undefined());
61     while (!promiseQueue->Empty()) {
62         LOG_ECMA(VERBOSE) << "ExecutePendingJob length: " << promiseQueue->Size();
63         pendingJob.Update(promiseQueue->Pop(thread));
64         PendingJob::ExecutePendingJob(pendingJob, thread);
65         if (thread->HasPendingException()) {
66             return;
67         }
68         promiseQueue.Update(jobQueue->GetPromiseJobQueue());
69     }
70 
71     JSHandle<TaggedQueue> scriptQueue(thread, jobQueue->GetScriptJobQueue());
72     while (!scriptQueue->Empty()) {
73         pendingJob.Update(scriptQueue->Pop(thread));
74         PendingJob::ExecutePendingJob(pendingJob, thread);
75         if (thread->HasPendingException()) {
76             return;
77         }
78     }
79 }
80 }  // namespace panda::ecmascript::job
81