• 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 #ifndef ECMASCRIPT_JOBS_PENDING_JOB_H
17 #define ECMASCRIPT_JOBS_PENDING_JOB_H
18 
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/interpreter/interpreter.h"
21 #include "ecmascript/jobs/hitrace_scope.h"
22 #include "ecmascript/js_function.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/object_factory.h"
25 #include "ecmascript/record.h"
26 #include "ecmascript/tagged_array.h"
27 #include "ecmascript/debugger/js_debugger_manager.h"
28 #include "ecmascript/mem/c_containers.h"
29 
30 namespace panda::ecmascript::job {
31 class PendingJob final : public Record {
32 public:
Cast(TaggedObject * object)33     static PendingJob *Cast(TaggedObject *object)
34     {
35         ASSERT(JSTaggedValue(object).IsPendingJob());
36         return static_cast<PendingJob *>(object);
37     }
38 
ExecutePendingJob(const JSHandle<PendingJob> & pendingJob,JSThread * thread)39     static JSTaggedValue ExecutePendingJob(const JSHandle<PendingJob> &pendingJob, JSThread *thread)
40     {
41         [[maybe_unused]] EcmaHandleScope handleScope(thread);
42         EXECUTE_JOB_HITRACE(pendingJob);
43         EXECUTE_JOB_TRACE(thread, pendingJob);
44 
45         JSHandle<JSTaggedValue> job(thread, pendingJob->GetJob(thread));
46         ASSERT(job->IsCallable());
47         JSHandle<TaggedArray> argv(thread, pendingJob->GetArguments(thread));
48         const uint32_t argsLength = argv->GetLength();
49         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
50         bool stackTrace = thread->GetEcmaVM()->GetJsDebuggerManager()->IsAsyncStackTrace();
51         if (stackTrace && argsLength >= 1) {
52             thread->GetEcmaVM()->GetAsyncStackTrace()->InsertCurrentAsyncTaskStack(argv->Get(thread, 0));
53         }
54         EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, job, undefined, undefined, argsLength);
55         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
56         info->SetCallArg(argsLength, argv);
57         JSTaggedValue result = JSFunction::Call(info);
58         if (stackTrace && argsLength >= 1) {
59             thread->GetEcmaVM()->GetAsyncStackTrace()->RemoveAsyncTaskStack(argv->Get(thread, 0));
60         }
61         return result;
62     }
63 
64     static constexpr size_t JOB_OFFSET = Record::SIZE;
65     ACCESSORS(Job, JOB_OFFSET, ARGUMENT_OFFSET);
66 #if defined(ENABLE_HITRACE)
67     ACCESSORS(Arguments, ARGUMENT_OFFSET, CHAINID_OFFSET);
68     ACCESSORS_PRIMITIVE_FIELD(ChainId, uint64_t, CHAINID_OFFSET, SPANID_OFFSET)
69     ACCESSORS_PRIMITIVE_FIELD(SpanId, uint64_t, SPANID_OFFSET, PARENTSPANID_OFFSET)
70     ACCESSORS_PRIMITIVE_FIELD(ParentSpanId, uint64_t, PARENTSPANID_OFFSET, FLAGS_OFFSET)
71     ACCESSORS_PRIMITIVE_FIELD(Flags, uint32_t, FLAGS_OFFSET, JOBID_OFFSET)
72     ACCESSORS_PRIMITIVE_FIELD(JobId, uint64_t, JOBID_OFFSET, LAST_OFFSET)
73     DEFINE_ALIGN_SIZE(LAST_OFFSET);
74 
75     DECL_VISIT_OBJECT(JOB_OFFSET, CHAINID_OFFSET)
76 #else
77     ACCESSORS(Arguments, ARGUMENT_OFFSET, SIZE);
78 
79     DECL_VISIT_OBJECT(JOB_OFFSET, SIZE)
80 #endif
81 
82     DECL_DUMP()
83 };
84 }  // namespace panda::ecmascript::job
85 #endif  // ECMASCRIPT_JOBS_PENDING_JOB_H
86