Skip to content

Commit

Permalink
wasm gc: support wrapping JS values in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Oct 2, 2024
1 parent 2340760 commit a291eb3
Show file tree
Hide file tree
Showing 30 changed files with 610 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public void opcode(Opcode opcode) {
case IS_NULL:
writer.write("ref.is_null");
break;
case EXTERN_TO_ANY:
writer.write("any.convert_extern");
break;
case ANY_TO_EXTERN:
writer.write("extern.convert_any");
break;
}
writer.eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ public void visit(WasmTest expression) {
expression.getValue().acceptVisitor(this);
}

@Override
public void visit(WasmExternConversion expression) {
expression.getValue().acceptVisitor(this);
}

@Override
public void visit(WasmStructNew expression) {
for (var initializer : expression.getInitializers()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public interface WasmExpressionVisitor {

void visit(WasmCast expression);

void visit(WasmExternConversion expression);

void visit(WasmTest expression);

void visit(WasmStructNew expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,35 @@
*/
package org.teavm.backend.wasm.model.expression;

public class WasmExternConversion {
import java.util.Objects;

public class WasmExternConversion extends WasmExpression {
private WasmExternConversionType type;
private WasmExpression value;

public WasmExternConversion(WasmExternConversionType type, WasmExpression value) {
this.type = Objects.requireNonNull(type);
this.value = Objects.requireNonNull(value);
}

public WasmExternConversionType getType() {
return type;
}

public void setType(WasmExternConversionType type) {
this.type = Objects.requireNonNull(type);
}

public WasmExpression getValue() {
return value;
}

public void setValue(WasmExpression value) {
this.value = Objects.requireNonNull(value);
}

@Override
public void acceptVisitor(WasmExpressionVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
package org.teavm.backend.wasm.model.expression;

public enum WasmExternConversionType {
EXTERN_TO_OBJECT,
OBJECT_TO_EXTERN
EXTERN_TO_ANY,
ANY_TO_EXTERN
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
public class WasmReplacingExpressionVisitor implements WasmExpressionVisitor {
private Function<WasmExpression, WasmExpression> mapper;

public WasmReplacingExpressionVisitor(
Function<WasmExpression, WasmExpression> mapper) {
public WasmReplacingExpressionVisitor(Function<WasmExpression, WasmExpression> mapper) {
this.mapper = mapper;
}

Expand Down Expand Up @@ -332,6 +331,12 @@ public void visit(WasmTest expression) {
expression.setValue(mapper.apply(expression.getValue()));
}

@Override
public void visit(WasmExternConversion expression) {
expression.getValue().acceptVisitor(this);
expression.setValue(mapper.apply(expression.getValue()));
}

@Override
public void visit(WasmStructNew expression) {
replaceExpressions(expression.getInitializers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,13 @@ private boolean parseExtExpr2() {
parseCastBranch(false);
return true;

case 26:
codeListener.opcode(Opcode.EXTERN_TO_ANY);
return true;
case 27:
codeListener.opcode(Opcode.ANY_TO_EXTERN);
return true;

case 28:
codeListener.int31Reference();
return true;
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/org/teavm/backend/wasm/parser/Opcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public enum Opcode {
DROP,
REF_EQ,
ARRAY_LENGTH,
IS_NULL
IS_NULL,
ANY_TO_EXTERN,
EXTERN_TO_ANY
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.teavm.backend.wasm.model.expression.WasmDrop;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmExpressionVisitor;
import org.teavm.backend.wasm.model.expression.WasmExternConversion;
import org.teavm.backend.wasm.model.expression.WasmFill;
import org.teavm.backend.wasm.model.expression.WasmFloat32Constant;
import org.teavm.backend.wasm.model.expression.WasmFloat64Constant;
Expand Down Expand Up @@ -1171,6 +1172,22 @@ public void visit(WasmTest expression) {
popLocation();
}

@Override
public void visit(WasmExternConversion expression) {
pushLocation(expression);
expression.getValue().acceptVisitor(this);
writer.writeByte(0xfb);
switch (expression.getType()) {
case EXTERN_TO_ANY:
writer.writeByte(26);
break;
case ANY_TO_EXTERN:
writer.writeByte(27);
break;
}
popLocation();
}

@Override
public void visit(WasmStructNew expression) {
pushLocation(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.teavm.backend.wasm.model.expression.WasmDrop;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmExpressionVisitor;
import org.teavm.backend.wasm.model.expression.WasmExternConversion;
import org.teavm.backend.wasm.model.expression.WasmFill;
import org.teavm.backend.wasm.model.expression.WasmFloat32Constant;
import org.teavm.backend.wasm.model.expression.WasmFloat64Constant;
Expand Down Expand Up @@ -1197,6 +1198,11 @@ public void visit(WasmTest expression) {
unsupported();
}

@Override
public void visit(WasmExternConversion expression) {
unsupported();
}

@Override
public void visit(WasmStructNew expression) {
unsupported();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.teavm.backend.wasm.model.expression.WasmDrop;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmExpressionVisitor;
import org.teavm.backend.wasm.model.expression.WasmExternConversion;
import org.teavm.backend.wasm.model.expression.WasmFill;
import org.teavm.backend.wasm.model.expression.WasmFloat32Constant;
import org.teavm.backend.wasm.model.expression.WasmFloat64Constant;
Expand Down Expand Up @@ -772,6 +773,21 @@ public void visit(WasmTest expression) {
close();
}

@Override
public void visit(WasmExternConversion expression) {
open();
switch (expression.getType()) {
case EXTERN_TO_ANY:
append("any.convert_extern");
break;
case ANY_TO_EXTERN:
append("extern.convert_any");
break;
}
line(expression.getValue());
close();
}

@Override
public void visit(WasmStructNew expression) {
open().append("struct.new ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.teavm.backend.wasm.model.expression.WasmCopy;
import org.teavm.backend.wasm.model.expression.WasmDrop;
import org.teavm.backend.wasm.model.expression.WasmExpressionVisitor;
import org.teavm.backend.wasm.model.expression.WasmExternConversion;
import org.teavm.backend.wasm.model.expression.WasmFill;
import org.teavm.backend.wasm.model.expression.WasmFloat32Constant;
import org.teavm.backend.wasm.model.expression.WasmFloat64Constant;
Expand Down Expand Up @@ -317,6 +318,18 @@ public void visit(WasmTest expression) {
result = WasmType.INT32;
}

@Override
public void visit(WasmExternConversion expression) {
switch (expression.getType()) {
case EXTERN_TO_ANY:
result = WasmType.Reference.ANY;
break;
case ANY_TO_EXTERN:
result = WasmType.Reference.EXTERN;
break;
}
}

@Override
public void visit(WasmStructNew expression) {
result = expression.getType().getReference();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ public void submitMethod(MethodReference methodRef, Program program) {

public void addDependencyListener(DependencyListener listener) {
listeners.add(listener);
listener.started(agent);
}

public void addClassTransformer(ClassHolderTransformer transformer) {
Expand Down Expand Up @@ -663,6 +662,12 @@ private void processNodeToNodeTransitionQueue() {
}
}

public void initDependencies() {
for (var listener : listeners) {
listener.started(agent);
}
}

public void processDependencies() {
interrupted = false;
processQueue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public BranchingCondition getCondition() {
return condition;
}

public void setCondition(BranchingCondition condition) {
this.condition = condition;
}

public BasicBlock getConsequent() {
return consequent;
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/org/teavm/vm/TeaVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ private void processEntryPoint() {
return;
}

var mainMethod = cls.getMethod(MAIN_METHOD_DESC) != null
? dependencyAnalyzer.linkMethod(new MethodReference(entryPoint,
"main", ValueType.parse(String[].class), ValueType.VOID))
: null;
dependencyAnalyzer.defer(() -> {
var mainMethod = cls.getMethod(MAIN_METHOD_DESC) != null
? dependencyAnalyzer.linkMethod(new MethodReference(entryPoint,
"main", ValueType.parse(String[].class), ValueType.VOID))
: null;
dependencyAnalyzer.linkClass(entryPoint).initClass(null);
if (mainMethod != null) {
mainMethod.getVariable(1).propagate(dependencyAnalyzer.getType("[Ljava/lang/String;"));
Expand Down Expand Up @@ -388,6 +388,7 @@ public void build(BuildTarget buildTarget, String outputName) {
cancelled |= progressListener.progressReached(progress) != TeaVMProgressFeedback.CONTINUE;
return !cancelled;
});
dependencyAnalyzer.initDependencies();
target.contributeDependencies(dependencyAnalyzer);
if (target.needsSystemArrayCopyOptimization()) {
dependencyAnalyzer.addDependencyListener(new StdlibDependencyListener());
Expand Down
Loading

0 comments on commit a291eb3

Please sign in to comment.