diff options
author | Max Graey <maxgraey@gmail.com> | 2022-08-30 23:01:04 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-30 13:01:04 -0700 |
commit | da24ef6ed7055f64299fce22a051ba0e85284c23 (patch) | |
tree | ca48e2a64c3ee85f41df3640483186e87cb7c661 | |
parent | e50ef7fa62425fb6687932af393fb86589ca5ac0 (diff) | |
download | binaryen-da24ef6ed7055f64299fce22a051ba0e85284c23.tar.gz binaryen-da24ef6ed7055f64299fce22a051ba0e85284c23.tar.bz2 binaryen-da24ef6ed7055f64299fce22a051ba0e85284c23.zip |
[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.
-rw-r--r-- | src/binaryen-c.cpp | 96 | ||||
-rw-r--r-- | src/binaryen-c.h | 4 |
2 files changed, 70 insertions, 30 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 7d11689b0..c6dd1c58b 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -3714,34 +3714,56 @@ 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, @@ -3749,13 +3771,20 @@ void BinaryenAddGlobalImport(BinaryenModuleRef module, 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, @@ -3763,12 +3792,19 @@ void BinaryenAddTagImport(BinaryenModuleRef module, 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 diff --git a/src/binaryen-c.h b/src/binaryen-c.h index a21c0966a..32ffea453 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -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, |