1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include "root.h"
#include "helpers.h"
#include "ZigSourceProvider.h"
#include "JavaScriptCore/BytecodeCacheError.h"
#include "JavaScriptCore/CodeCache.h"
#include "JavaScriptCore/Completion.h"
#include "wtf/Scope.h"
#include "wtf/text/StringHash.h"
#include <sys/stat.h>
extern "C" void RefString__free(void*, void*, unsigned);
namespace Zig {
using Base = JSC::SourceProvider;
using BytecodeCacheGenerator = JSC::BytecodeCacheGenerator;
using UnlinkedFunctionExecutable = JSC::UnlinkedFunctionExecutable;
using CachedBytecode = JSC::CachedBytecode;
using UnlinkedFunctionCodeBlock = JSC::UnlinkedFunctionCodeBlock;
using SourceCode = JSC::SourceCode;
using CodeSpecializationKind = JSC::CodeSpecializationKind;
using SourceOrigin = JSC::SourceOrigin;
using String = WTF::String;
using SourceProviderSourceType = JSC::SourceProviderSourceType;
static char* wasmSourceName = "[WebAssembly Source]";
static size_t wasmSourceName_len = 20;
Ref<SourceProvider> SourceProvider::create(ResolvedSource resolvedSource)
{
void* allocator = resolvedSource.allocator;
JSC::SourceProviderSourceType sourceType = JSC::SourceProviderSourceType::Module;
if (allocator) {
Ref<WTF::ExternalStringImpl> stringImpl_ = WTF::ExternalStringImpl::create(
resolvedSource.source_code.ptr, resolvedSource.source_code.len,
allocator,
RefString__free);
return adoptRef(*new SourceProvider(
resolvedSource, stringImpl_,
JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))),
toStringNotConst(resolvedSource.source_url), TextPosition(),
sourceType));
} else {
Ref<WTF::ExternalStringImpl> stringImpl_ = WTF::ExternalStringImpl::createStatic(
resolvedSource.source_code.ptr, resolvedSource.source_code.len);
return adoptRef(*new SourceProvider(
resolvedSource, stringImpl_,
JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))),
toStringNotConst(resolvedSource.source_url), TextPosition(),
sourceType));
}
}
unsigned SourceProvider::getHash()
{
if (m_hash) {
return m_hash;
}
m_hash = WTF::StringHash::hash(m_source.get());
return m_hash;
}
void SourceProvider::freeSourceCode()
{
if (did_free_source_code) {
return;
}
did_free_source_code = true;
if (m_resolvedSource.allocator != 0) { // // WTF::ExternalStringImpl::destroy(m_source.ptr());
this->m_source = WTF::StringImpl::empty()->isolatedCopy();
this->m_hash = 0;
m_resolvedSource.allocator = 0;
}
// if (m_resolvedSource.allocator != 0) {
// ZigString__free(m_resolvedSource.source_code.ptr, m_resolvedSource.source_code.len,
// m_resolvedSource.allocator);
// }
}
void SourceProvider::updateCache(const UnlinkedFunctionExecutable* executable, const SourceCode&,
CodeSpecializationKind kind,
const UnlinkedFunctionCodeBlock* codeBlock)
{
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode)
return;
JSC::BytecodeCacheError error;
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeFunctionCodeBlock(executable->vm(), codeBlock, error);
if (cachedBytecode && !error.isValid())
m_cachedBytecode->addFunctionUpdate(executable, kind, *cachedBytecode);
}
void SourceProvider::cacheBytecode(const BytecodeCacheGenerator& generator)
{
// if (!m_resolvedSource.bytecodecache_fd)
return;
if (!m_cachedBytecode)
m_cachedBytecode = JSC::CachedBytecode::create();
auto update = generator();
if (update)
m_cachedBytecode->addGlobalUpdate(*update);
}
void SourceProvider::commitCachedBytecode()
{
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode || !m_cachedBytecode->hasUpdates())
return;
// auto clearBytecode = WTF::makeScopeExit([&] { m_cachedBytecode = nullptr; });
// const auto fd = m_resolvedSource.bytecodecache_fd;
// auto fileSize = FileSystem::fileSize(fd);
// if (!fileSize)
// return;
// size_t cacheFileSize;
// if (!WTF::convertSafely(*fileSize, cacheFileSize) || cacheFileSize != m_cachedBytecode->size()) {
// // The bytecode cache has already been updated
// return;
// }
// if (!FileSystem::truncateFile(fd, m_cachedBytecode->sizeForUpdate()))
// return;
// m_cachedBytecode->commitUpdates([&](off_t offset, const void* data, size_t size) {
// long long result = FileSystem::seekFile(fd, offset, FileSystem::FileSeekOrigin::Beginning);
// ASSERT_UNUSED(result, result != -1);
// size_t bytesWritten = static_cast<size_t>(FileSystem::writeToFile(fd, data, size));
// ASSERT_UNUSED(bytesWritten, bytesWritten == size);
// });
}
bool SourceProvider::isBytecodeCacheEnabled() const
{
// return m_resolvedSource.bytecodecache_fd > 0;
return false;
}
void SourceProvider::readOrGenerateByteCodeCache(JSC::VM& vm, const JSC::SourceCode& sourceCode)
{
// auto status = this->readCache(vm, sourceCode);
// switch (status) {
// case -1: {
// m_resolvedSource.bytecodecache_fd = 0;
// break;
// }
// case 0: {
// JSC::BytecodeCacheError err;
// m_cachedBytecode = JSC::generateModuleBytecode(vm, sourceCode, m_resolvedSource.bytecodecache_fd, err);
// if (err.isValid()) {
// m_resolvedSource.bytecodecache_fd = 0;
// m_cachedBytecode = JSC::CachedBytecode::create();
// }
// }
// // TODO: read the bytecode into a JSC::SourceCode object here
// case 1: {
// }
// }
}
int SourceProvider::readCache(JSC::VM& vm, const JSC::SourceCode& sourceCode)
{
return -1;
// if (m_resolvedSource.bytecodecache_fd == 0)
// return -1;
// if (!FileSystem::isHandleValid(m_resolvedSource.bytecodecache_fd))
// return -1;
// const auto fd = m_resolvedSource.bytecodecache_fd;
// bool success;
// FileSystem::MappedFileData mappedFile(fd, FileSystem::MappedFileMode::Shared, success);
// if (!success)
// return -1;
// const uint8_t* fileData = reinterpret_cast<const uint8_t*>(mappedFile.data());
// unsigned fileTotalSize = mappedFile.size();
// if (fileTotalSize == 0)
// return 0;
// Ref<JSC::CachedBytecode> cachedBytecode = JSC::CachedBytecode::create(WTFMove(mappedFile));
// // auto key = JSC::sourceCodeKeyForSerializedModule(vm, sourceCode);
// // if (isCachedBytecodeStillValid(vm, cachedBytecode.copyRef(), key,
// // JSC::SourceCodeType::ModuleType)) {
// m_cachedBytecode = WTFMove(cachedBytecode);
// return 1;
// } else {
// FileSystem::truncateFile(fd, 0);
// return 0;
// }
}
}; // namespace Zig
|