2 * Copyright (c) 2021-present Samsung Electronics Co., Ltd
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 class ExecutionStateRef;
25 } // namespace Escargot
29 enum Encoding { UNKNOWN, ONE_BYTE, ONE_BYTE_LATIN1, TWO_BYTE };
32 void* buffer{nullptr};
34 Encoding encoding{ONE_BYTE};
36 FileData(void* buffer_, long int size_, Encoding encoding_) {
46 using U8String = std::basic_string<uint8_t, std::char_traits<uint8_t>>;
48 static FileData readFile(std::string filename, const Encoding fileEncoding);
50 static void tryConvertUTF8ToLatin1(U8String& latin1String,
52 const uint8_t* buffer,
53 const size_t bufferSize,
54 const Encoding encodingHint);
56 // should return string buffer
57 typedef void* (*LoadCallback)(void* callbackData);
58 // should free memoryPtr
59 typedef void (*UnloadCallback)(void* memoryPtr, void* callbackData);
61 class ReloadableSourceData {
63 void* preloadedData{nullptr};
65 const char* path() { return path_; }
66 size_t preloadedDataLength() { return preloadedDataLength_; }
67 size_t stringLength() {
68 return isOneByteString() ? preloadedDataLength_
69 : preloadedDataLength_ / 2;
71 bool isOneByteString() {
72 return (encoding_ == ONE_BYTE) || (encoding_ == ONE_BYTE_LATIN1);
74 Encoding encoding() { return encoding_; }
76 static ReloadableSourceData* create(std::string sourcePath,
78 size_t preloadedDataLength,
83 size_t preloadedDataLength_{0};
84 Encoding encoding_{UNKNOWN};
85 ReloadableSourceData() = default;
88 static Escargot::ValueRef* CreateReloadableSourceFromFile(
89 Escargot::ExecutionStateRef* state, std::string fileName);
91 static v8::MaybeLocal<v8::String> NewReloadableString(
93 ReloadableSourceData* data,
94 LoadCallback loadCallback,
95 UnloadCallback unloadCallback);
98 bool convertUTF8ToUTF16le(char** buffer,
100 const char* utf8Buffer,
101 const size_t utf8BufferSize);
103 void* allocateStringBuffer(size_t size);
104 void freeStringBuffer(void* ptr);
106 } // namespace LWNode