Repository: tetratelabs/wazero
Pull request #1134 (2023-02-16 23:58:32)

Removes unnecessary Engine.CreateElementInstance
the less code the better


Patch:
diff --git a/internal/engine/compiler/engine.go b/internal/engine/compiler/engine.go
index c6af0730..f4148d54 100644
--- a/internal/engine/compiler/engine.go
+++ b/internal/engine/compiler/engine.go
@@ -588,20 +588,6 @@ func (e *moduleEngine) FunctionInstanceReference(funcIndex wasm.Index) wasm.Refe
 	return uintptr(unsafe.Pointer(&e.functions[funcIndex]))
 }
 
-// CreateFuncElementInstance implements the same method as documented on wasm.ModuleEngine.
-func (e *moduleEngine) CreateFuncElementInstance(indexes []*wasm.Index) *wasm.ElementInstance {
-	refs := make([]wasm.Reference, len(indexes))
-	for i, index := range indexes {
-		if index != nil {
-			refs[i] = uintptr(unsafe.Pointer(&e.functions[*index]))
-		}
-	}
-	return &wasm.ElementInstance{
-		References: refs,
-		Type:       wasm.RefTypeFuncref,
-	}
-}
-
 func (e *moduleEngine) NewCallEngine(_ *wasm.CallContext, f *wasm.FunctionInstance) (ce wasm.CallEngine, err error) {
 	// Note: The input parameters are pre-validated, so a compiled function is only absent on close. Updates to
 	// code on close aren't locked, neither is this read.
diff --git a/internal/engine/interpreter/interpreter.go b/internal/engine/interpreter/interpreter.go
index 9635e88f..c2cea3d0 100644
--- a/internal/engine/interpreter/interpreter.go
+++ b/internal/engine/interpreter/interpreter.go
@@ -737,20 +737,6 @@ func (e *moduleEngine) Name() string {
 	return e.name
 }
 
-// CreateFuncElementInstance implements the same method as documented on wasm.ModuleEngine.
-func (e *moduleEngine) CreateFuncElementInstance(indexes []*wasm.Index) *wasm.ElementInstance {
-	refs := make([]wasm.Reference, len(indexes))
-	for i, index := range indexes {
-		if index != nil {
-			refs[i] = uintptr(unsafe.Pointer(&e.functions[*index]))
-		}
-	}
-	return &wasm.ElementInstance{
-		References: refs,
-		Type:       wasm.RefTypeFuncref,
-	}
-}
-
 // FunctionInstanceReference implements the same method as documented on wasm.ModuleEngine.
 func (e *moduleEngine) FunctionInstanceReference(funcIndex wasm.Index) wasm.Reference {
 	return uintptr(unsafe.Pointer(&e.functions[funcIndex]))
diff --git a/internal/wasm/engine.go b/internal/wasm/engine.go
index a0e9907a..c536922a 100644
--- a/internal/wasm/engine.go
+++ b/internal/wasm/engine.go
@@ -45,10 +45,6 @@ type ModuleEngine interface {
 	// LookupFunction returns the index of the function in the function table.
 	LookupFunction(t *TableInstance, typeId FunctionTypeID, tableOffset Index) (Index, error)
 
-	// CreateFuncElementInstance creates an ElementInstance whose references are engine-specific function pointers
-	// corresponding to the given `indexes`.
-	CreateFuncElementInstance(indexes []*Index) *ElementInstance
-
 	// FunctionInstanceReference returns Reference for the given Index for a FunctionInstance. The returned values are used by
 	// the initialization via ElementSegment.
 	FunctionInstanceReference(funcIndex Index) Reference
diff --git a/internal/wasm/store.go b/internal/wasm/store.go
index 5a12f339..1f559647 100644
--- a/internal/wasm/store.go
+++ b/internal/wasm/store.go
@@ -162,7 +162,16 @@ func (m *ModuleInstance) buildElementInstances(elements []*ElementSegment) {
 		if elm.Type == RefTypeFuncref && elm.Mode == ElementModePassive {
 			// Only passive elements can be access as element instances.
 			// See https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/syntax/modules.html#element-segments
-			m.ElementInstances[i] = *m.Engine.CreateFuncElementInstance(elm.Init)
+			inits := elm.Init
+			elemInst := &m.ElementInstances[i]
+			elemInst.References = make([]Reference, len(inits))
+			elemInst.Type = RefTypeFuncref
+			for j, idxPtr := range inits {
+				if idxPtr != nil {
+					idx := *idxPtr
+					elemInst.References[j] = m.Engine.FunctionInstanceReference(idx)
+				}
+			}
 		}
 	}
 }


Test patch:
diff --git a/internal/wasm/store_test.go b/internal/wasm/store_test.go
index 4b781b3d..5643bc1f 100644
--- a/internal/wasm/store_test.go
+++ b/internal/wasm/store_test.go
@@ -445,11 +445,6 @@ func (e *mockModuleEngine) NewCallEngine(callCtx *CallContext, f *FunctionInstan
 	return &mockCallEngine{f: f, callFailIndex: e.callFailIndex}, nil
 }
 
-// CreateFuncElementInstance implements the same method as documented on wasm.ModuleEngine.
-func (e *mockModuleEngine) CreateFuncElementInstance([]*Index) *ElementInstance {
-	return nil
-}
-
 // InitializeFuncrefGlobals implements the same method as documented on wasm.ModuleEngine.
 func (e *mockModuleEngine) InitializeFuncrefGlobals(globals []*GlobalInstance) {}