e8675906ce66e0033dc99df21028e4039bff67f1
[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 <string>
21
22 namespace Escargot {
23 class ValueRef;
24 class ExecutionStateRef;
25 }  // namespace Escargot
26
27 namespace LWNode {
28
29 enum Encoding { UNKNOWN, ONE_BYTE, ONE_BYTE_LATIN1, TWO_BYTE };
30
31 struct FileData {
32   void* buffer{nullptr};
33   long int size{0};
34   Encoding encoding{ONE_BYTE};
35
36   FileData(void* buffer_, long int size_, Encoding encoding_) {
37     buffer = buffer_;
38     size = size_;
39     encoding = encoding_;
40   }
41   FileData() = default;
42 };
43
44 class Loader {
45  public:
46   using U8String = std::basic_string<uint8_t, std::char_traits<uint8_t>>;
47
48   static FileData readFile(std::string filename, const Encoding fileEncoding);
49
50   static void tryConvertUTF8ToLatin1(U8String& latin1String,
51                                      Encoding& encoding,
52                                      const uint8_t* buffer,
53                                      const size_t bufferSize,
54                                      const Encoding encodingHint);
55
56   // should return string buffer
57   typedef void* (*LoadCallback)(void* callbackData);
58   // should free memoryPtr
59   typedef void (*UnloadCallback)(void* memoryPtr, void* callbackData);
60
61   class ReloadableSourceData {
62    public:
63     void* preloadedData{nullptr};
64
65     const char* path() { return path_; }
66     size_t preloadedDataLength() { return preloadedDataLength_; }
67     size_t stringLength() {
68       return isOneByteString() ? preloadedDataLength_
69                                : preloadedDataLength_ / 2;
70     }
71     bool isOneByteString() {
72       return (encoding_ == ONE_BYTE) || (encoding_ == ONE_BYTE_LATIN1);
73     }
74     Encoding encoding() { return encoding_; }
75
76     static ReloadableSourceData* create(std::string sourcePath,
77                                         void* preloadedData,
78                                         size_t preloadedDataLength,
79                                         Encoding encoding);
80
81    private:
82     char* path_{nullptr};
83     size_t preloadedDataLength_{0};
84     Encoding encoding_{UNKNOWN};
85     ReloadableSourceData() = default;
86   };
87
88   static Escargot::ValueRef* CreateReloadableSourceFromFile(
89       Escargot::ExecutionStateRef* state, std::string fileName);
90
91   static v8::MaybeLocal<v8::String> NewReloadableString(
92       v8::Isolate* isolate,
93       ReloadableSourceData* data,
94       LoadCallback loadCallback,
95       UnloadCallback unloadCallback);
96 };
97
98 bool convertUTF8ToUTF16le(char** buffer,
99                           size_t* bufferSize,
100                           const char* utf8Buffer,
101                           const size_t utf8BufferSize);
102
103 void* allocateStringBuffer(size_t size);
104 void freeStringBuffer(void* ptr);
105
106 }  // namespace LWNode