Skip to content

[C-/JS-Api] Avoid add imports for existing entities in BinaryenSetMemoryImport and etc #4991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 66 additions & 30 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3714,61 +3714,97 @@ void BinaryenAddFunctionImport(BinaryenModuleRef module,
const char* externalBaseName,
BinaryenType params,
BinaryenType results) {
auto* ret = new Function();
ret->name = internalName;
ret->module = externalModuleName;
ret->base = externalBaseName;
// TODO: Take a HeapType rather than params and results.
ret->type = Signature(Type(params), Type(results));
((Module*)module)->addFunction(ret);
auto* func = ((Module*)module)->getFunctionOrNull(internalName);
if (func == nullptr) {
auto func = make_unique<Function>();
func->name = internalName;
func->module = externalModuleName;
func->base = externalBaseName;
// TODO: Take a HeapType rather than params and results.
func->type = Signature(Type(params), Type(results));
((Module*)module)->addFunction(std::move(func));
} else {
// already exists so just set module and base
func->module = externalModuleName;
func->base = externalBaseName;
}
}
void BinaryenAddTableImport(BinaryenModuleRef module,
const char* internalName,
const char* externalModuleName,
const char* externalBaseName) {
auto table = std::make_unique<Table>();
table->name = internalName;
table->module = externalModuleName;
table->base = externalBaseName;
((Module*)module)->addTable(std::move(table));
auto* table = ((Module*)module)->getTableOrNull(internalName);
if (table == nullptr) {
auto table = make_unique<Table>();
table->name = internalName;
table->module = externalModuleName;
table->base = externalBaseName;
((Module*)module)->addTable(std::move(table));
} else {
// already exists so just set module and base
table->module = externalModuleName;
table->base = externalBaseName;
}
}
void BinaryenAddMemoryImport(BinaryenModuleRef module,
const char* internalName,
const char* externalModuleName,
const char* externalBaseName,
uint8_t shared) {
auto memory = Builder::makeMemory(internalName);
memory->module = externalModuleName;
memory->base = externalBaseName;
memory->shared = shared;
((Module*)module)->addMemory(std::move(memory));
auto* memory = ((Module*)module)->getMemoryOrNull(internalName);
if (memory == nullptr) {
auto memory = make_unique<Memory>();
memory->name = internalName;
memory->module = externalModuleName;
memory->base = externalBaseName;
memory->shared = shared;
((Module*)module)->addMemory(std::move(memory));
} else {
// already exists so just set module and base
memory->module = externalModuleName;
memory->base = externalBaseName;
}
}
void BinaryenAddGlobalImport(BinaryenModuleRef module,
const char* internalName,
const char* externalModuleName,
const char* externalBaseName,
BinaryenType globalType,
bool mutable_) {
auto* ret = new Global();
ret->name = internalName;
ret->module = externalModuleName;
ret->base = externalBaseName;
ret->type = Type(globalType);
ret->mutable_ = mutable_;
((Module*)module)->addGlobal(ret);
auto* glob = ((Module*)module)->getGlobalOrNull(internalName);
if (glob == nullptr) {
auto glob = make_unique<Global>();
glob->name = internalName;
glob->module = externalModuleName;
glob->base = externalBaseName;
glob->type = Type(globalType);
glob->mutable_ = mutable_;
((Module*)module)->addGlobal(std::move(glob));
} else {
// already exists so just set module and base
glob->module = externalModuleName;
glob->base = externalBaseName;
}
}
void BinaryenAddTagImport(BinaryenModuleRef module,
const char* internalName,
const char* externalModuleName,
const char* externalBaseName,
BinaryenType params,
BinaryenType results) {
auto* ret = new Tag();
ret->name = internalName;
ret->module = externalModuleName;
ret->base = externalBaseName;
ret->sig = Signature(Type(params), Type(results));
((Module*)module)->addTag(ret);
auto* tag = ((Module*)module)->getGlobalOrNull(internalName);
if (tag == nullptr) {
auto tag = make_unique<Tag>();
tag->name = internalName;
tag->module = externalModuleName;
tag->base = externalBaseName;
tag->sig = Signature(Type(params), Type(results));
((Module*)module)->addTag(std::move(tag));
} else {
// already exists so just set module and base
tag->module = externalModuleName;
tag->base = externalBaseName;
}
}

// Exports
Expand Down
4 changes: 4 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,10 @@ BinaryenGetFunctionByIndex(BinaryenModuleRef module, BinaryenIndex index);

// Imports

// These either create a new entity (function/table/memory/etc.) and
// mark it as an import, or, if an entity already exists with internalName then
// the existing entity is turned into an import.

BINARYEN_API void BinaryenAddFunctionImport(BinaryenModuleRef module,
const char* internalName,
const char* externalModuleName,
Expand Down