Skip to content

Commit

Permalink
client: Synthetic module exports cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMrBonnie committed Oct 17, 2023
1 parent d6e7289 commit 832aaa1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 5 additions & 4 deletions client/src/helpers/IModuleHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@

static constexpr const char bytecodeMagic[] = { 'A', 'L', 'T', 'B', 'C' };
static constexpr const char resourceImportPrefix[] = "@resource/";
static std::unordered_map<int, IModuleHandler::PersistentSyntheticModuleExports> syntheticModuleExports;

v8::MaybeLocal<v8::Value> IModuleHandler::SyntheticModuleEvaluateCallback(v8::Local<v8::Context> context, v8::Local<v8::Module> module)
{
CJavaScriptResource* resource = js::IResource::GetFromContext<CJavaScriptResource>(context);

int identityHash = module->GetIdentityHash();
if(!syntheticModuleExports.contains(identityHash)) return v8::MaybeLocal<v8::Value>();
if(!resource->syntheticModuleExports.contains(identityHash)) return v8::MaybeLocal<v8::Value>();

v8::Isolate* isolate = v8::Isolate::GetCurrent();
for(const auto& [key, value] : syntheticModuleExports.at(identityHash)) module->SetSyntheticModuleExport(isolate, js::JSValue(key), value.Get(isolate));
syntheticModuleExports.erase(identityHash);
for(const auto& [key, value] : resource->syntheticModuleExports.at(identityHash)) module->SetSyntheticModuleExport(isolate, js::JSValue(key), value.Get(isolate));
resource->syntheticModuleExports.erase(identityHash);

v8::Local<v8::Promise::Resolver> promise = v8::Promise::Resolver::New(context).ToLocalChecked();
promise->Resolve(context, v8::Undefined(isolate));
Expand Down
2 changes: 2 additions & 0 deletions client/src/helpers/IModuleHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ class IModuleHandler
void Reset()
{
modules.clear();
syntheticModuleExports.clear();
}

std::unordered_map<std::string, Module> modules;
std::unordered_map<int, PersistentSyntheticModuleExports> syntheticModuleExports;

public:
v8::MaybeLocal<v8::Module> CompileModule(const std::string& name, const std::string& source);
Expand Down

0 comments on commit 832aaa1

Please sign in to comment.