Skip to content

Commit 4ca3d9c

Browse files
committed
Create interop call adapter call target lazily.
1 parent 91683d9 commit 4ca3d9c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmFunction.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public final class WasmFunction {
5252
@CompilationFinal private int typeEquivalenceClass;
5353
@CompilationFinal private String debugName;
5454
@CompilationFinal private CallTarget callTarget;
55+
/** Interop call adapter for argument and return value validation and conversion. */
56+
@CompilationFinal private volatile CallTarget interopCallAdapter;
5557

5658
/**
5759
* Represents a WebAssembly function.
@@ -166,4 +168,12 @@ void setImportedFunctionCallTarget(CallTarget callTarget) {
166168
assert isImported() : this;
167169
this.callTarget = callTarget;
168170
}
171+
172+
public CallTarget getInteropCallAdapter() {
173+
return interopCallAdapter;
174+
}
175+
176+
public void setInteropCallAdapter(CallTarget interopCallAdapter) {
177+
this.interopCallAdapter = interopCallAdapter;
178+
}
169179
}

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmFunctionInstance.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.graalvm.wasm.nodes.WasmIndirectCallNode;
4747

4848
import com.oracle.truffle.api.CallTarget;
49+
import com.oracle.truffle.api.CompilerDirectives;
4950
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5051
import com.oracle.truffle.api.RootCallTarget;
5152
import com.oracle.truffle.api.TruffleContext;
@@ -71,10 +72,6 @@ public final class WasmFunctionInstance extends EmbedderDataHolder implements Tr
7172
* Initialized during linking.
7273
*/
7374
private Object importedFunction;
74-
/**
75-
* Interop call adapter for exported functions, converting parameter and result values.
76-
*/
77-
private final CallTarget interopCallAdapter;
7875

7976
/**
8077
* Represents a call target that is a WebAssembly function or an imported function.
@@ -89,7 +86,6 @@ public WasmFunctionInstance(WasmContext context, WasmInstance moduleInstance, Wa
8986
this.function = Objects.requireNonNull(function, "function must be non-null");
9087
this.target = Objects.requireNonNull(target, "Call target must be non-null");
9188
this.truffleContext = context.environment().getContext();
92-
this.interopCallAdapter = context.language().interopCallAdapterFor(function.type());
9389
assert ((RootCallTarget) target).getRootNode().getLanguage(WasmLanguage.class) == context.language();
9490
}
9591

@@ -147,11 +143,25 @@ Object execute(Object[] arguments,
147143
TruffleContext c = getTruffleContext();
148144
Object prev = c.enter(self);
149145
try {
150-
CallTarget callAdapter = Objects.requireNonNull(this.interopCallAdapter);
146+
CallTarget callAdapter = this.function.getInteropCallAdapter();
147+
if (callAdapter == null) {
148+
CompilerDirectives.transferToInterpreterAndInvalidate();
149+
callAdapter = createInteropCallAdapter();
150+
}
151151
return callNode.execute(callAdapter, WasmArguments.create(this, arguments));
152152
// throws ArityException, UnsupportedTypeException
153153
} finally {
154154
c.leave(self, prev);
155155
}
156156
}
157+
158+
@TruffleBoundary
159+
private CallTarget createInteropCallAdapter() {
160+
CallTarget callAdapter = this.function.getInteropCallAdapter();
161+
if (callAdapter == null) {
162+
callAdapter = context.language().interopCallAdapterFor(function.type());
163+
this.function.setInteropCallAdapter(callAdapter);
164+
}
165+
return callAdapter;
166+
}
157167
}

0 commit comments

Comments
 (0)