Skip to content

Commit

Permalink
Add class transformers to convert functions to native
Browse files Browse the repository at this point in the history
  • Loading branch information
lax1dude committed Dec 3, 2024
1 parent bfd8b7b commit 530b6a5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/org/teavm/backend/wasm/WasmTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import org.teavm.backend.wasm.render.WasmCRenderer;
import org.teavm.backend.wasm.render.WasmRenderer;
import org.teavm.backend.wasm.runtime.WasmSupport;
import org.teavm.backend.wasm.transformation.BaseClassesTransformation;
import org.teavm.backend.wasm.transformation.IndirectCallTraceTransformation;
import org.teavm.backend.wasm.transformation.MemoryAccessTraceTransformation;
import org.teavm.backend.wasm.transformation.WasiFileSystemProviderTransformer;
Expand Down Expand Up @@ -241,6 +242,7 @@ public VariableCategoryProvider variableCategoryProvider() {
public List<ClassHolderTransformer> getTransformers() {
List<ClassHolderTransformer> transformers = new ArrayList<>();
transformers.add(new ClassPatch());
transformers.add(new BaseClassesTransformation());
transformers.add(new WasmDependencyListener());
if (runtimeType == WasmRuntimeType.WASI) {
transformers.add(new WasiSupportClassTransformer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 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.transformation;

import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassHolderTransformerContext;
import org.teavm.model.ElementModifier;

public class BaseClassesTransformation implements ClassHolderTransformer {

@Override
public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) {
if (cls.getName().equals("java.lang.Integer") || cls.getName().equals("java.lang.Long")) {
for (var method : cls.getMethods()) {
switch (method.getName()) {
case "numberOfLeadingZeros":
case "numberOfTrailingZeros":
case "bitCount":
method.setProgram(null);
method.getModifiers().add(ElementModifier.NATIVE);
break;
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public void transformClass(ClassHolder cls, ClassHolderTransformerContext contex
var clear = cls.getMethod(new MethodDescriptor("clear", void.class));
clear.getModifiers().add(ElementModifier.NATIVE);
clear.setProgram(null);
} else if (cls.getName().equals("java.lang.Integer") || cls.getName().equals("java.lang.Long")) {
for (var method : cls.getMethods()) {
switch (method.getName()) {
case "numberOfLeadingZeros":
case "numberOfTrailingZeros":
case "bitCount":
method.setProgram(null);
method.getModifiers().add(ElementModifier.NATIVE);
break;
}
}
}
}

Expand Down

0 comments on commit 530b6a5

Please sign in to comment.