Skip to content

Commit

Permalink
Works
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Sep 29, 2024
1 parent 2e18412 commit 0f1b0ee
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
34 changes: 34 additions & 0 deletions Source/JavaScriptCore/runtime/CachedTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,12 @@ class CacheEntry : public GenericCacheEntry {
return true;
}

bool decode(Decoder& decoder, SourceCodeKey& key) const
{
m_key.decode(decoder, key);
return true;
}

CachedSourceCodeKey m_key;
CachedPtr<CachedCodeBlockType<UnlinkedCodeBlockType>> m_codeBlock;
};
Expand All @@ -2551,6 +2557,24 @@ bool GenericCacheEntry::decode(Decoder& decoder, std::pair<SourceCodeKey, Unlink
return false;
}

bool GenericCacheEntry::decode(Decoder& decoder, SourceCodeKey& key) const
{
if (!isUpToDate(decoder))
return false;

switch (m_tag) {
case CachedProgramCodeBlockTag:
return bitwise_cast<const CacheEntry<UnlinkedProgramCodeBlock>*>(this)->decode(decoder, key);
case CachedModuleCodeBlockTag:
return bitwise_cast<const CacheEntry<UnlinkedModuleProgramCodeBlock>*>(this)->decode(decoder, key);
case CachedEvalCodeBlockTag:
// We do not cache eval code blocks
return false;
}

return false;
}

bool GenericCacheEntry::isStillValid(Decoder& decoder, const SourceCodeKey& key, CachedCodeBlockTag tag) const
{
if (!isUpToDate(decoder))
Expand Down Expand Up @@ -2604,6 +2628,16 @@ RefPtr<CachedBytecode> encodeFunctionCodeBlock(VM& vm, const UnlinkedFunctionCod
return encoder.release(error);
}

std::optional<SourceCodeKey> decodeSourceCodeKey(VM& vm, Ref<CachedBytecode> cachedBytecode)
{
const auto* cachedEntry = bitwise_cast<const GenericCacheEntry*>(cachedBytecode->span().data());
Ref<Decoder> decoder = Decoder::create(vm, WTFMove(cachedBytecode));

SourceCodeKey key;
if (!cachedEntry->decode(decoder.get(), key))
return std::nullopt;
return key;
}
UnlinkedCodeBlock* decodeCodeBlockImpl(VM& vm, const SourceCodeKey& key, Ref<CachedBytecode> cachedBytecode)
{
const auto* cachedEntry = bitwise_cast<const GenericCacheEntry*>(cachedBytecode->span().data());
Expand Down
2 changes: 2 additions & 0 deletions Source/JavaScriptCore/runtime/CachedTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ UnlinkedCodeBlockType* decodeCodeBlock(VM& vm, const SourceCodeKey& key, Ref<Cac
return jsCast<UnlinkedCodeBlockType*>(decodeCodeBlockImpl(vm, key, WTFMove(cachedBytecode)));
}

std::optional<SourceCodeKey> decodeSourceCodeKey(VM& vm, Ref<CachedBytecode> cachedBytecode);

JS_EXPORT_PRIVATE RefPtr<CachedBytecode> encodeFunctionCodeBlock(VM&, const UnlinkedFunctionCodeBlock*, BytecodeCacheError&);

JS_EXPORT_PRIVATE void decodeFunctionCodeBlock(Decoder&, int32_t cachedFunctionCodeBlockOffset, WriteBarrier<UnlinkedFunctionCodeBlock>&, const JSCell*);
Expand Down
37 changes: 26 additions & 11 deletions Source/JavaScriptCore/runtime/CodeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,28 @@ static void generateUnlinkedCodeBlockForFunctions(VM& vm, UnlinkedCodeBlock* unl

// FIXME: We should also generate CodeBlocks for CodeForConstruct
// https://bugs.webkit.org/show_bug.cgi?id=193823
for (unsigned i = 0; i < unlinkedCodeBlock->numberOfFunctionDecls(); i++)
generate(unlinkedCodeBlock->functionDecl(i), CodeForCall);
for (unsigned i = 0; i < unlinkedCodeBlock->numberOfFunctionExprs(); i++)
generate(unlinkedCodeBlock->functionExpr(i), CodeForCall);
//
// NOTE: We changed this in Bun. We check if the function is a constructor
// and if it is, we generate a CodeForConstruct block.
for (unsigned i = 0; i < unlinkedCodeBlock->numberOfFunctionDecls(); i++) {
auto* functionDecl = unlinkedCodeBlock->functionDecl(i);
if (functionDecl->isConstructor()) {
generate(functionDecl, CodeForConstruct);
} else {
generate(functionDecl, CodeForCall);
}
}
for (unsigned i = 0; i < unlinkedCodeBlock->numberOfFunctionExprs(); i++) {
auto* functionExpr = unlinkedCodeBlock->functionExpr(i);
if (functionExpr->isConstructor()) {
generate(functionExpr, CodeForConstruct);
} else {
generate(functionExpr, CodeForCall);
}
}
}

template <class UnlinkedCodeBlockType, class ExecutableType = ScriptExecutable>
template<class UnlinkedCodeBlockType, class ExecutableType = ScriptExecutable>
UnlinkedCodeBlockType* generateUnlinkedCodeBlockImpl(VM& vm, const SourceCode& source, LexicallyScopedFeatures lexicallyScopedFeatures, JSParserScriptMode scriptMode, OptionSet<CodeGenerationMode> codeGenerationMode, ParserError& error, EvalContextType evalContextType, DerivedContextType derivedContextType, bool isArrowFunctionContext, const TDZEnvironment* variablesUnderTDZ = nullptr, const PrivateNameEnvironment* privateNameEnvironment = nullptr, ExecutableType* executable = nullptr)
{
typedef typename CacheTypes<UnlinkedCodeBlockType>::RootNode RootNode;
Expand Down Expand Up @@ -119,7 +134,7 @@ UnlinkedCodeBlockType* generateUnlinkedCodeBlockImpl(VM& vm, const SourceCode& s
return unlinkedCodeBlock;
}

template <class UnlinkedCodeBlockType, class ExecutableType>
template<class UnlinkedCodeBlockType, class ExecutableType>
UnlinkedCodeBlockType* generateUnlinkedCodeBlock(VM& vm, ExecutableType* executable, const SourceCode& source, JSParserScriptMode scriptMode, OptionSet<CodeGenerationMode> codeGenerationMode, ParserError& error, EvalContextType evalContextType, const TDZEnvironment* variablesUnderTDZ = nullptr, const PrivateNameEnvironment* privateNameEnvironment = nullptr)
{
return generateUnlinkedCodeBlockImpl<UnlinkedCodeBlockType, ExecutableType>(vm, source, executable->lexicallyScopedFeatures(), scriptMode, codeGenerationMode, error, evalContextType, executable->derivedContextType(), executable->isArrowFunctionContext(), variablesUnderTDZ, privateNameEnvironment, executable);
Expand All @@ -130,7 +145,7 @@ UnlinkedEvalCodeBlock* generateUnlinkedCodeBlockForDirectEval(VM& vm, DirectEval
return generateUnlinkedCodeBlock<UnlinkedEvalCodeBlock>(vm, executable, source, scriptMode, codeGenerationMode, error, evalContextType, variablesUnderTDZ, privateNameEnvironment);
}

template <class UnlinkedCodeBlockType>
template<class UnlinkedCodeBlockType>
std::enable_if_t<!std::is_same<UnlinkedCodeBlockType, UnlinkedEvalCodeBlock>::value, UnlinkedCodeBlockType*>
recursivelyGenerateUnlinkedCodeBlock(VM& vm, const SourceCode& source, LexicallyScopedFeatures lexicallyScopedFeatures, JSParserScriptMode scriptMode, OptionSet<CodeGenerationMode> codeGenerationMode, ParserError& error, EvalContextType evalContextType)
{
Expand All @@ -153,7 +168,7 @@ UnlinkedModuleProgramCodeBlock* recursivelyGenerateUnlinkedCodeBlockForModulePro
return recursivelyGenerateUnlinkedCodeBlock<UnlinkedModuleProgramCodeBlock>(vm, source, lexicallyScopedFeatures, scriptMode, codeGenerationMode, error, evalContextType);
}

template <class UnlinkedCodeBlockType, class ExecutableType>
template<class UnlinkedCodeBlockType, class ExecutableType>
UnlinkedCodeBlockType* CodeCache::getUnlinkedGlobalCodeBlock(VM& vm, ExecutableType* executable, const SourceCode& source, JSParserScriptMode scriptMode, OptionSet<CodeGenerationMode> codeGenerationMode, ParserError& error, EvalContextType evalContextType)
{
DerivedContextType derivedContextType = executable->derivedContextType();
Expand Down Expand Up @@ -245,7 +260,7 @@ UnlinkedFunctionExecutable* CodeCache::getUnlinkedGlobalFunctionExecutable(VM& v
ASSERT(metadata);
if (!metadata)
return nullptr;

metadata->overrideName(name);
metadata->setEndPosition(positionBeforeLastNewline);
// The Function constructor only has access to global variables, so no variables will be under TDZ unless they're
Expand Down Expand Up @@ -294,13 +309,13 @@ static SourceCodeKey sourceCodeKeyForSerializedBytecode(VM&, const SourceCode& s
SourceCodeKey sourceCodeKeyForSerializedProgram(VM& vm, const SourceCode& sourceCode)
{
JSParserScriptMode scriptMode = JSParserScriptMode::Classic;
return sourceCodeKeyForSerializedBytecode(vm, sourceCode, SourceCodeType::ProgramType, NoLexicallyScopedFeatures, scriptMode, { });
return sourceCodeKeyForSerializedBytecode(vm, sourceCode, SourceCodeType::ProgramType, NoLexicallyScopedFeatures, scriptMode, {});
}

SourceCodeKey sourceCodeKeyForSerializedModule(VM& vm, const SourceCode& sourceCode)
{
JSParserScriptMode scriptMode = JSParserScriptMode::Module;
return sourceCodeKeyForSerializedBytecode(vm, sourceCode, SourceCodeType::ModuleType, StrictModeLexicallyScopedFeature, scriptMode, { });
return sourceCodeKeyForSerializedBytecode(vm, sourceCode, SourceCodeType::ModuleType, StrictModeLexicallyScopedFeature, scriptMode, {});
}

RefPtr<CachedBytecode> serializeBytecode(VM& vm, UnlinkedCodeBlock* codeBlock, const SourceCode& source, SourceCodeType codeType, LexicallyScopedFeatures lexicallyScopedFeatures, JSParserScriptMode scriptMode, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error, OptionSet<CodeGenerationMode> codeGenerationMode)
Expand Down
2 changes: 0 additions & 2 deletions Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,6 @@ JSC_DEFINE_HOST_FUNCTION(globalFuncImportModule, (JSGlobalObject* globalObject,
promise->resolve(globalObject, internalPromise);
}

auto* promise = JSPromise::create(vm, globalObject->promiseStructure());
promise->resolve(globalObject, internalPromise);
return JSValue::encode(promise);
}

Expand Down

0 comments on commit 0f1b0ee

Please sign in to comment.