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.
26 class ExecutionStateRef;
27 } // namespace Escargot
31 enum class Encoding : uint8_t { kUnknown, kAscii, kLatin1, kUtf16 };
34 void* buffer{nullptr};
36 Encoding encoding{Encoding::kUnknown};
39 FileData(void* buffer_,
42 const std::string& filePath_) {
51 class SourceReaderInterface {
53 virtual FileData read(std::string filename, const Encoding encodingHint) = 0;
56 class SourceReader : public SourceReaderInterface {
58 static SourceReader* getInstance();
59 FileData read(std::string filename, const Encoding encodingHint) override;
62 SourceReader() = default;
67 class ReloadableSourceData {
69 void* preloadedData{nullptr};
71 const char* path() { return path_; }
72 Encoding encoding() { return encoding_; }
73 SourceReaderInterface* sourceReader() { return sourceReader_; }
75 size_t preloadedDataLength() { return preloadedDataLength_; }
76 size_t stringLength() {
77 return isOneByteString() ? preloadedDataLength_
78 : preloadedDataLength_ / 2;
80 bool isOneByteString() {
81 return (encoding_ == Encoding::kAscii) ||
82 (encoding_ == Encoding::kLatin1);
85 static ReloadableSourceData* create(const FileData fileData,
86 SourceReaderInterface* sourceReader);
90 size_t preloadedDataLength_{0};
91 Encoding encoding_{Encoding::kUnknown};
92 ReloadableSourceData() = default;
93 SourceReaderInterface* sourceReader_{nullptr};
96 // should return string buffer
97 typedef void* (*LoadCallback)(void* callbackData);
98 // should free memoryPtr
99 typedef void (*UnloadCallback)(void* memoryPtr, void* callbackData);
101 static FileData createFileDataForReloadableString(
102 std::string filename,
103 std::unique_ptr<void, std::function<void(void*)>> bufferHolder,
105 const Encoding encodingHint);
107 static Escargot::ValueRef* CreateReloadableSourceFromFile(
108 Escargot::ExecutionStateRef* state, std::string fileName);
110 static v8::MaybeLocal<v8::String> NewReloadableString(
111 v8::Isolate* isolate,
112 ReloadableSourceData* data,
113 LoadCallback loadCallback = nullptr,
114 UnloadCallback unloadCallback = nullptr);
117 bool convertUTF8ToUTF16le(char** buffer,
119 const char* utf8Buffer,
120 const size_t utf8BufferSize);
122 void* allocateStringBuffer(size_t size);
123 void freeStringBuffer(void* ptr);
125 } // namespace LWNode