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
30namespace panda::ecmascript::job {
31class PendingJob final : public Record {
32public:
33    static PendingJob *Cast(TaggedObject *object)
34    {
35        ASSERT(JSTaggedValue(object).IsPendingJob());
36        return static_cast<PendingJob *>(object);
37    }
38
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());
46        ASSERT(job->IsCallable());
47        JSHandle<TaggedArray> argv(thread, pendingJob->GetArguments());
48        const uint32_t argsLength = argv->GetLength();
49        JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
50        EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, job, undefined, undefined, argsLength);
51        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
52        info->SetCallArg(argsLength, argv);
53        return JSFunction::Call(info);
54    }
55
56    static constexpr size_t JOB_OFFSET = Record::SIZE;
57    ACCESSORS(Job, JOB_OFFSET, ARGUMENT_OFFSET);
58#if defined(ENABLE_HITRACE)
59    ACCESSORS(Arguments, ARGUMENT_OFFSET, CHAINID_OFFSET);
60    ACCESSORS_PRIMITIVE_FIELD(ChainId, uint64_t, CHAINID_OFFSET, SPANID_OFFSET)
61    ACCESSORS_PRIMITIVE_FIELD(SpanId, uint64_t, SPANID_OFFSET, PARENTSPANID_OFFSET)
62    ACCESSORS_PRIMITIVE_FIELD(ParentSpanId, uint64_t, PARENTSPANID_OFFSET, FLAGS_OFFSET)
63    ACCESSORS_PRIMITIVE_FIELD(Flags, uint32_t, FLAGS_OFFSET, JOBID_OFFSET)
64    ACCESSORS_PRIMITIVE_FIELD(JobId, uint64_t, JOBID_OFFSET, LAST_OFFSET)
65    DEFINE_ALIGN_SIZE(LAST_OFFSET);
66
67    DECL_VISIT_OBJECT(JOB_OFFSET, CHAINID_OFFSET)
68#else
69    ACCESSORS(Arguments, ARGUMENT_OFFSET, SIZE);
70
71    DECL_VISIT_OBJECT(JOB_OFFSET, SIZE)
72#endif
73
74    DECL_DUMP()
75};
76}  // namespace panda::ecmascript::job
77#endif  // ECMASCRIPT_JOBS_PENDING_JOB_H
78