Skip to content

Commit da24ef6

Browse files
authored
[C/JS API] Avoid erroring in BinaryenSetMemoryImport etc. if the entity exists (#4991)
If it exists, just turn it into an import. If not, then as before we create it + turn it into an import.
1 parent e50ef7f commit da24ef6

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

src/binaryen-c.cpp

+66-30
Original file line numberDiff line numberDiff line change
@@ -3714,61 +3714,97 @@ void BinaryenAddFunctionImport(BinaryenModuleRef module,
37143714
const char* externalBaseName,
37153715
BinaryenType params,
37163716
BinaryenType results) {
3717-
auto* ret = new Function();
3718-
ret->name = internalName;
3719-
ret->module = externalModuleName;
3720-
ret->base = externalBaseName;
3721-
// TODO: Take a HeapType rather than params and results.
3722-
ret->type = Signature(Type(params), Type(results));
3723-
((Module*)module)->addFunction(ret);
3717+
auto* func = ((Module*)module)->getFunctionOrNull(internalName);
3718+
if (func == nullptr) {
3719+
auto func = make_unique<Function>();
3720+
func->name = internalName;
3721+
func->module = externalModuleName;
3722+
func->base = externalBaseName;
3723+
// TODO: Take a HeapType rather than params and results.
3724+
func->type = Signature(Type(params), Type(results));
3725+
((Module*)module)->addFunction(std::move(func));
3726+
} else {
3727+
// already exists so just set module and base
3728+
func->module = externalModuleName;
3729+
func->base = externalBaseName;
3730+
}
37243731
}
37253732
void BinaryenAddTableImport(BinaryenModuleRef module,
37263733
const char* internalName,
37273734
const char* externalModuleName,
37283735
const char* externalBaseName) {
3729-
auto table = std::make_unique<Table>();
3730-
table->name = internalName;
3731-
table->module = externalModuleName;
3732-
table->base = externalBaseName;
3733-
((Module*)module)->addTable(std::move(table));
3736+
auto* table = ((Module*)module)->getTableOrNull(internalName);
3737+
if (table == nullptr) {
3738+
auto table = make_unique<Table>();
3739+
table->name = internalName;
3740+
table->module = externalModuleName;
3741+
table->base = externalBaseName;
3742+
((Module*)module)->addTable(std::move(table));
3743+
} else {
3744+
// already exists so just set module and base
3745+
table->module = externalModuleName;
3746+
table->base = externalBaseName;
3747+
}
37343748
}
37353749
void BinaryenAddMemoryImport(BinaryenModuleRef module,
37363750
const char* internalName,
37373751
const char* externalModuleName,
37383752
const char* externalBaseName,
37393753
uint8_t shared) {
3740-
auto memory = Builder::makeMemory(internalName);
3741-
memory->module = externalModuleName;
3742-
memory->base = externalBaseName;
3743-
memory->shared = shared;
3744-
((Module*)module)->addMemory(std::move(memory));
3754+
auto* memory = ((Module*)module)->getMemoryOrNull(internalName);
3755+
if (memory == nullptr) {
3756+
auto memory = make_unique<Memory>();
3757+
memory->name = internalName;
3758+
memory->module = externalModuleName;
3759+
memory->base = externalBaseName;
3760+
memory->shared = shared;
3761+
((Module*)module)->addMemory(std::move(memory));
3762+
} else {
3763+
// already exists so just set module and base
3764+
memory->module = externalModuleName;
3765+
memory->base = externalBaseName;
3766+
}
37453767
}
37463768
void BinaryenAddGlobalImport(BinaryenModuleRef module,
37473769
const char* internalName,
37483770
const char* externalModuleName,
37493771
const char* externalBaseName,
37503772
BinaryenType globalType,
37513773
bool mutable_) {
3752-
auto* ret = new Global();
3753-
ret->name = internalName;
3754-
ret->module = externalModuleName;
3755-
ret->base = externalBaseName;
3756-
ret->type = Type(globalType);
3757-
ret->mutable_ = mutable_;
3758-
((Module*)module)->addGlobal(ret);
3774+
auto* glob = ((Module*)module)->getGlobalOrNull(internalName);
3775+
if (glob == nullptr) {
3776+
auto glob = make_unique<Global>();
3777+
glob->name = internalName;
3778+
glob->module = externalModuleName;
3779+
glob->base = externalBaseName;
3780+
glob->type = Type(globalType);
3781+
glob->mutable_ = mutable_;
3782+
((Module*)module)->addGlobal(std::move(glob));
3783+
} else {
3784+
// already exists so just set module and base
3785+
glob->module = externalModuleName;
3786+
glob->base = externalBaseName;
3787+
}
37593788
}
37603789
void BinaryenAddTagImport(BinaryenModuleRef module,
37613790
const char* internalName,
37623791
const char* externalModuleName,
37633792
const char* externalBaseName,
37643793
BinaryenType params,
37653794
BinaryenType results) {
3766-
auto* ret = new Tag();
3767-
ret->name = internalName;
3768-
ret->module = externalModuleName;
3769-
ret->base = externalBaseName;
3770-
ret->sig = Signature(Type(params), Type(results));
3771-
((Module*)module)->addTag(ret);
3795+
auto* tag = ((Module*)module)->getGlobalOrNull(internalName);
3796+
if (tag == nullptr) {
3797+
auto tag = make_unique<Tag>();
3798+
tag->name = internalName;
3799+
tag->module = externalModuleName;
3800+
tag->base = externalBaseName;
3801+
tag->sig = Signature(Type(params), Type(results));
3802+
((Module*)module)->addTag(std::move(tag));
3803+
} else {
3804+
// already exists so just set module and base
3805+
tag->module = externalModuleName;
3806+
tag->base = externalBaseName;
3807+
}
37723808
}
37733809

37743810
// Exports

src/binaryen-c.h

+4
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,10 @@ BinaryenGetFunctionByIndex(BinaryenModuleRef module, BinaryenIndex index);
21852185

21862186
// Imports
21872187

2188+
// These either create a new entity (function/table/memory/etc.) and
2189+
// mark it as an import, or, if an entity already exists with internalName then
2190+
// the existing entity is turned into an import.
2191+
21882192
BINARYEN_API void BinaryenAddFunctionImport(BinaryenModuleRef module,
21892193
const char* internalName,
21902194
const char* externalModuleName,

0 commit comments

Comments
 (0)