1/*
2 * Copyright (c) 2022 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_REQUIRE_CJS_MODULE_H
17#define ECMASCRIPT_REQUIRE_CJS_MODULE_H
18
19#include "ecmascript/ecma_macros.h"
20#include "ecmascript/js_object.h"
21#include "ecmascript/js_tagged_value.h"
22#include "ecmascript/ecma_runtime_call_info.h"
23
24namespace panda::ecmascript {
25enum class CjsModuleStatus : uint8_t { UNLOAD = 0x01, LOADED};
26class CjsModule final : public JSObject {
27public:
28    CAST_CHECK(CjsModule, IsCjsModule);
29
30    // Instantiate member
31    static constexpr size_t JS_CJS_MODULE_OFFSET = JSObject::SIZE;
32    ACCESSORS(Id, JS_CJS_MODULE_OFFSET, ID_OFFSET)
33    ACCESSORS(Path, ID_OFFSET, PATH_OFFSET)
34    ACCESSORS(Exports, PATH_OFFSET, EXPORTS_OFFSET)
35    ACCESSORS(Filename, EXPORTS_OFFSET, BIT_FIELD_OFFSET)
36    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_SIZE) // define BitField
37    DEFINE_ALIGN_SIZE(LAST_SIZE);
38
39    static constexpr size_t STATUS_BITS = 2;
40    FIRST_BIT_FIELD(BitField, Status, CjsModuleStatus, STATUS_BITS)
41
42    DECL_DUMP()
43    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, JS_CJS_MODULE_OFFSET, BIT_FIELD_OFFSET)
44
45    static void InitializeModule(JSThread *thread, JSHandle<CjsModule> &module,
46                                              JSHandle<JSTaggedValue> &filename, JSHandle<JSTaggedValue> &dirname);
47
48    static JSHandle<JSTaggedValue> SearchFromModuleCache(JSThread *thread, JSHandle<JSTaggedValue> &filename);
49
50    static void PutIntoCache(JSThread *thread, JSHandle<CjsModule> &module, JSHandle<JSTaggedValue> &filename);
51
52    static JSHandle<JSTaggedValue> Load(JSThread *thread, JSHandle<EcmaString> &request);
53
54    static JSTaggedValue Require(JSThread *thread, JSHandle<EcmaString> &request, JSHandle<CjsModule> &parent,
55                                 bool isMain);
56
57    static void RequireExecution(JSThread *thread, const CString &mergedFilename, const CString &requestEntryPoint);
58};
59}  // namespace panda::ecmascript
60#endif  // ECMASCRIPT_REQUIRE_JS_CJS_MODULE_H
61