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