LWNode_Release_211109_610b330
[platform/framework/web/lwnode.git] / lwnode / code / escargotshim / include / lwnode / lwnode-loader.h
1 /*
2  * Copyright (c) 2021-present Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <v8.h>
20 #include <functional>
21 #include <memory>
22 #include <string>
23
24 namespace Escargot {
25 class ValueRef;
26 class ExecutionStateRef;
27 }  // namespace Escargot
28
29 namespace LWNode {
30
31 enum class Encoding : uint8_t { kUnknown, kAscii, kLatin1, kUtf16 };
32
33 struct FileData {
34   void* buffer{nullptr};
35   long int size{0};
36   Encoding encoding{Encoding::kUnknown};
37   std::string filePath;
38
39   FileData(void* buffer_,
40            long int size_,
41            Encoding encoding_,
42            const std::string& filePath_) {
43     buffer = buffer_;
44     size = size_;
45     encoding = encoding_;
46     filePath = filePath_;
47   }
48   FileData() = default;
49 };
50
51 class SourceReaderInterface {
52  public:
53   virtual FileData read(std::string filename, const Encoding encodingHint) = 0;
54 };
55
56 class SourceReader : public SourceReaderInterface {
57  public:
58   static SourceReader* getInstance();
59   FileData read(std::string filename, const Encoding encodingHint) override;
60
61  private:
62   SourceReader() = default;
63 };
64
65 class Loader {
66  public:
67   class ReloadableSourceData {
68    public:
69     void* preloadedData{nullptr};
70
71     const char* path() { return path_; }
72     Encoding encoding() { return encoding_; }
73     SourceReaderInterface* sourceReader() { return sourceReader_; }
74
75     size_t preloadedDataLength() { return preloadedDataLength_; }
76     size_t stringLength() {
77       return isOneByteString() ? preloadedDataLength_
78                                : preloadedDataLength_ / 2;
79     }
80     bool isOneByteString() {
81       return (encoding_ == Encoding::kAscii) ||
82              (encoding_ == Encoding::kLatin1);
83     }
84
85     static ReloadableSourceData* create(const FileData fileData,
86                                         SourceReaderInterface* sourceReader);
87
88    private:
89     char* path_{nullptr};
90     size_t preloadedDataLength_{0};
91     Encoding encoding_{Encoding::kUnknown};
92     ReloadableSourceData() = default;
93     SourceReaderInterface* sourceReader_{nullptr};
94   };
95
96   // should return string buffer
97   typedef void* (*LoadCallback)(void* callbackData);
98   // should free memoryPtr
99   typedef void (*UnloadCallback)(void* memoryPtr, void* callbackData);
100
101   static FileData createFileDataForReloadableString(
102       std::string filename,
103       std::unique_ptr<void, std::function<void(void*)>> bufferHolder,
104       size_t bufferSize,
105       const Encoding encodingHint);
106
107   static Escargot::ValueRef* CreateReloadableSourceFromFile(
108       Escargot::ExecutionStateRef* state, std::string fileName);
109
110   static v8::MaybeLocal<v8::String> NewReloadableString(
111       v8::Isolate* isolate,
112       ReloadableSourceData* data,
113       LoadCallback loadCallback = nullptr,
114       UnloadCallback unloadCallback = nullptr);
115 };
116
117 bool convertUTF8ToUTF16le(char** buffer,
118                           size_t* bufferSize,
119                           const char* utf8Buffer,
120                           const size_t utf8BufferSize);
121
122 void* allocateStringBuffer(size_t size);
123 void freeStringBuffer(void* ptr);
124
125 }  // namespace LWNode