mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2025-01-08 16:04:10 -08:00
Add support of strings (breaks compilation for some reason)
This commit is contained in:
parent
278b926c04
commit
fc2c6b9e07
|
@ -50,6 +50,12 @@ public final class Example {
|
|||
for (byte bt : bytes) {
|
||||
WasmRuntime.print(bt);
|
||||
}
|
||||
|
||||
String str = "foobar";
|
||||
WasmRuntime.print(str.length());
|
||||
for (int i = 0; i < str.length(); ++i) {
|
||||
WasmRuntime.print(str.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
private static Base instance(int index) {
|
||||
|
|
|
@ -62,6 +62,7 @@ import org.teavm.wasm.generate.WasmClassGenerator;
|
|||
import org.teavm.wasm.generate.WasmGenerationContext;
|
||||
import org.teavm.wasm.generate.WasmGenerator;
|
||||
import org.teavm.wasm.generate.WasmMangling;
|
||||
import org.teavm.wasm.generate.WasmStringPool;
|
||||
import org.teavm.wasm.intrinsics.WasmAddressIntrinsic;
|
||||
import org.teavm.wasm.intrinsics.WasmRuntimeIntrinsic;
|
||||
import org.teavm.wasm.intrinsics.WasmStructureIntrinsic;
|
||||
|
@ -136,7 +137,8 @@ public class WasmTarget implements TeaVMTarget {
|
|||
|
||||
Decompiler decompiler = new Decompiler(classes, controller.getClassLoader(), new HashSet<>(),
|
||||
new HashSet<>());
|
||||
WasmGenerationContext context = new WasmGenerationContext(classes, vtableProvider, tagRegistry);
|
||||
WasmStringPool stringPool = new WasmStringPool(classGenerator, binaryWriter);
|
||||
WasmGenerationContext context = new WasmGenerationContext(classes, vtableProvider, tagRegistry, stringPool);
|
||||
|
||||
context.addIntrinsic(new WasmAddressIntrinsic());
|
||||
context.addIntrinsic(new WasmStructureIntrinsic(classGenerator));
|
||||
|
|
|
@ -31,21 +31,24 @@ import org.teavm.model.MethodReference;
|
|||
import org.teavm.model.ValueType;
|
||||
import org.teavm.model.classes.TagRegistry;
|
||||
import org.teavm.model.classes.VirtualTableProvider;
|
||||
import org.teavm.wasm.binary.BinaryWriter;
|
||||
import org.teavm.wasm.intrinsics.WasmIntrinsic;
|
||||
|
||||
public class WasmGenerationContext {
|
||||
private ClassReaderSource classSource;
|
||||
private VirtualTableProvider vtableProvider;
|
||||
private TagRegistry tagRegistry;
|
||||
private WasmStringPool stringPool;
|
||||
private Map<MethodReference, ImportedMethod> importedMethods = new HashMap<>();
|
||||
private List<WasmIntrinsic> intrinsics = new ArrayList<>();
|
||||
private Map<MethodReference, WasmIntrinsic> intrinsicCache = new HashMap<>();
|
||||
|
||||
public WasmGenerationContext(ClassReaderSource classSource, VirtualTableProvider vtableProvider,
|
||||
TagRegistry tagRegistry) {
|
||||
TagRegistry tagRegistry, WasmStringPool stringPool) {
|
||||
this.classSource = classSource;
|
||||
this.vtableProvider = vtableProvider;
|
||||
this.tagRegistry = tagRegistry;
|
||||
this.stringPool = stringPool;
|
||||
}
|
||||
|
||||
public void addIntrinsic(WasmIntrinsic intrinsic) {
|
||||
|
@ -101,6 +104,10 @@ public class WasmGenerationContext {
|
|||
return tagRegistry;
|
||||
}
|
||||
|
||||
public WasmStringPool getStringPool() {
|
||||
return stringPool;
|
||||
}
|
||||
|
||||
public class ImportedMethod {
|
||||
public final String name;
|
||||
public final String module;
|
||||
|
|
|
@ -511,6 +511,9 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
|
|||
result = new WasmFloat32Constant((Float) expr.getValue());
|
||||
} else if (expr.getValue() instanceof Double) {
|
||||
result = new WasmFloat64Constant((Double) expr.getValue());
|
||||
} else if (expr.getValue() instanceof String) {
|
||||
String str = (String) expr.getValue();
|
||||
result = new WasmInt32Constant(context.getStringPool().getStringPointer(str));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Constant unsupported: " + expr.getValue());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright 2016 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.wasm.generate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.teavm.model.ValueType;
|
||||
import org.teavm.wasm.binary.BinaryWriter;
|
||||
import org.teavm.wasm.binary.DataArray;
|
||||
import org.teavm.wasm.binary.DataPrimitives;
|
||||
import org.teavm.wasm.binary.DataStructure;
|
||||
import org.teavm.wasm.binary.DataValue;
|
||||
|
||||
public class WasmStringPool {
|
||||
private WasmClassGenerator classGenerator;
|
||||
private BinaryWriter binaryWriter;
|
||||
private Map<String, Integer> stringMap = new HashMap<>();
|
||||
private DataStructure arrayHeaderType = new DataStructure((byte) 0, DataPrimitives.INT, DataPrimitives.INT);
|
||||
private DataStructure stringType = new DataStructure((byte) 0,
|
||||
DataPrimitives.INT, /* class pointer */
|
||||
DataPrimitives.ADDRESS, /* monitor */
|
||||
DataPrimitives.ADDRESS, /* characters */
|
||||
DataPrimitives.INT /* hash code */);
|
||||
|
||||
public WasmStringPool(WasmClassGenerator classGenerator, BinaryWriter binaryWriter) {
|
||||
this.classGenerator = classGenerator;
|
||||
this.binaryWriter = binaryWriter;
|
||||
}
|
||||
|
||||
public int getStringPointer(String value) {
|
||||
return stringMap.computeIfAbsent(value, str -> {
|
||||
DataArray charactersType = new DataArray(DataPrimitives.SHORT, str.length());
|
||||
DataStructure wrapperType = new DataStructure((byte) 0, arrayHeaderType, charactersType);
|
||||
DataValue wrapper = wrapperType.createValue();
|
||||
DataValue header = wrapper.getValue(0);
|
||||
DataValue characters = wrapper.getValue(1);
|
||||
|
||||
header.setInt(0, classGenerator.getClassPointer(ValueType.arrayOf(ValueType.CHARACTER)));
|
||||
header.setInt(1, str.length());
|
||||
for (int i = 0; i < str.length(); ++i) {
|
||||
characters.setShort(i, (short) str.charAt(i));
|
||||
}
|
||||
|
||||
DataValue stringObject = stringType.createValue();
|
||||
int stringPointer = binaryWriter.append(stringObject);
|
||||
stringObject.setInt(0, classGenerator.getClassPointer(ValueType.object(String.class.getName())));
|
||||
stringObject.setAddress(2, binaryWriter.append(wrapper));
|
||||
|
||||
return stringPointer;
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user