Skip to content

Commit

Permalink
Return WasmModule directly.
Browse files Browse the repository at this point in the history
(cherry picked from commit 40012c6)
  • Loading branch information
woess committed Jan 14, 2025
1 parent cfe34b1 commit 5c43744
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
29 changes: 11 additions & 18 deletions wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@
import org.graalvm.options.OptionDescriptors;
import org.graalvm.options.OptionValues;
import org.graalvm.wasm.api.JsConstants;
import org.graalvm.wasm.api.WasmModuleWithSource;
import org.graalvm.wasm.api.WebAssembly;
import org.graalvm.wasm.exception.WasmJsApiException;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.predefined.BuiltinModule;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.ContextThreadLocal;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.TruffleLanguage;
Expand Down Expand Up @@ -141,6 +139,16 @@ private ParsedWasmModuleRootNode(WasmLanguage language, WasmModule module, Sourc
this.source = source;
}

/**
* The CallTarget returned by {@code parse} supports two calling conventions:
*
* <ol>
* <li>(default) zero arguments provided: on the first call, instantiates the decoded module
* and puts it in the context's module instance map; then returns the {@link WasmInstance}.
* <li>first argument is {@code "module_decode"}: returns the decoded {@link WasmModule}
* (i.e. behaves like {@link WebAssembly#moduleDecode module_decode}). Used by the JS API.
* </ol>
*/
@Override
public Object execute(VirtualFrame frame) {
if (frame.getArguments().length == 0) {
Expand All @@ -153,7 +161,7 @@ public Object execute(VirtualFrame frame) {
} else {
if (frame.getArguments()[0] instanceof String mode) {
if (mode.equals(MODULE_DECODE)) {
return moduleDecode();
return module;
} else {
throw WasmJsApiException.format(WasmJsApiException.Kind.TypeError, "Unsupported first argument: '%s'", mode);
}
Expand All @@ -163,21 +171,6 @@ public Object execute(VirtualFrame frame) {
}
}

@TruffleBoundary
private Object moduleDecode() {
/*
* Get the source used for parsing on the JS side and associate it with the returned
* WasmModule, so that it will be kept alive with the JS WebAssembly.Module object.
*/
Source sourceHandle = getParsingSource();
return new WasmModuleWithSource(module, sourceHandle);
}

@TruffleBoundary
private Source getParsingSource() {
return Source.newBuilder(source).build();
}

@Override
public SourceSection getSourceSection() {
return source.createUnavailableSection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ private static WasmJsApiException cannotConvertToBytesError(Throwable cause) {
return (cause == null) ? new WasmJsApiException(kind, message) : new WasmJsApiException(kind, message, cause);
}

/**
* Extract a {@link WasmModule} from argument 0. The argument may be a {@link WasmModule} or a
* {@link WasmModuleWithSource} as produced by the CallTarget returned from Env.parse or
* {@link #moduleDecode}, respectively.
*/
private static WasmModule toModule(Object[] args) {
checkArgumentCount(args, 1);
Object arg0 = args[0];
Expand Down

0 comments on commit 5c43744

Please sign in to comment.