Skip to content

Commit

Permalink
wasm gc: add classes that are not directly reachable, but are mention…
Browse files Browse the repository at this point in the history
…ed in method signatures

Rationale: it can be that some function `foo` declares that it takes class C, but class C is never reachable from code (e.g. never get instantiated, and `foo` only takes nulls). In this case it can be that class C has no representation in Wasm module, which can lead to type validation errors.
  • Loading branch information
konsoletyper committed Feb 8, 2025
1 parent 7fdaf3c commit 6a2bd3e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void contribute() {
analyzer.addDependencyListener(new WasmGCReferenceQueueDependency());
analyzer.addDependencyListener(new WasmGCResourceDependency());
analyzer.addDependencyListener(new SystemArrayCopyDependencySupport());
analyzer.addDependencyListener(new WasmGCSignatureDependencyListener());
handleReferences();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2025 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.gc;

import org.teavm.dependency.AbstractDependencyListener;
import org.teavm.dependency.DependencyAgent;
import org.teavm.dependency.FieldDependency;
import org.teavm.dependency.MethodDependency;
import org.teavm.model.ValueType;

public class WasmGCSignatureDependencyListener extends AbstractDependencyListener {
@Override
public void methodReached(DependencyAgent agent, MethodDependency method) {
for (var i = 0; i < method.getMethod().parameterCount(); ++i) {
linkType(agent, method.getMethod().parameterType(i));
}
linkType(agent, method.getMethod().getResultType());
}

@Override
public void fieldReached(DependencyAgent agent, FieldDependency field) {
linkType(agent, field.getField().getType());
}

private void linkType(DependencyAgent agent, ValueType type) {
if (type instanceof ValueType.Object) {
agent.linkClass(((ValueType.Object) type).getClassName());
} else if (type instanceof ValueType.Array) {
linkType(agent, ((ValueType.Array) type).getItemType());
}
}
}

0 comments on commit 6a2bd3e

Please sign in to comment.