Build webassembly sorting functions from C++ and Java source code

257 Views Asked by At

I have a problem that I was going to work on arrays in WebAssembly and I wanted to use Java and C ++, and trying to do this, I ran into the following problems and I would like to ask for help:

Java: I'm using JWebAssembly

And we have a class that works on tables

import de.inetsoftware.jwebassembly.api.annotation.Export;

public class Add {
    @Export
    public static int[] add( int a[], int b ) {

        for(int i = 0;i<b-1;i++){
            a[i] += b ;
        }
        return a;
    }
}

we convert it to wasm

import de.inetsoftware.jwebassembly.JWebAssembly;

import java.io.File;
import java.net.URL;

public class Wasm {
    public static void main(String[] args) {
        File wasmFile = new File("testTable.wasm");
        JWebAssembly wasm = new JWebAssembly();

        Class clazz = Add.class;

        URL url = clazz.getResource(
                '/' +
                        clazz.getName().replace('.', '/') +
                        ".class");
        wasm.addFile(url);
        String txt = wasm.compileToText();
        System.out.println(txt);
        wasm.compileToBinary(wasmFile);
    }
}

and such an error comes out Exception in thread "main" de.inetsoftware.jwebassembly.WasmException: Unimplemented Java byte code operation: 42 at Add.java:11

https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions

And I don't understand why because in this guy's presentation https://www.youtube.com/watch?v=93z9SaLQVVw (40 min+) you can see that it works and compiles

Now C++

I use emscripten, I wanted to do a bubble sort but for the sake of simplicity an example showing the problem

 #include <emscripten.h>
 using namespace std;
 
EMSCRIPTEN_KEEPALIVE
int* mainFunction(int table[], int length)
{
    
    int* outTable = new int[length];

    for(int i = 0;i<length; i++){
        outTable[i] = table[i];
    }

    return table;
}

By running it with this command test.cpp -s WASM=1 -o test.html

after compiling it, files appear to me from which I extract the appropriate data and in javascript I set and import everything

let wasmExports = null;

let asmLibraryArg = {
    abort: () => {},
    emscripten_resize_heap: () => {},
};

let info = {
    env: asmLibraryArg,
    wasi_snapshot_preview1: asmLibraryArg,
};

async function loadWasm() {
    let response = await fetch("test.wasm");
    let bytes = await response.arrayBuffer();
    let wasmObj = await WebAssembly.instantiate(bytes, info);
    wasmExports = wasmObj.instance.exports;
}

loadWasm();

and later in the code I use

console.log(wasmExports);
console.log(wasmExports._Z12mainFunctionPii(table, table.length));

results

and when I throw in some array of integers it only throws me the number 0 and I have no idea how to get out of it. Or maybe someone knows another language in which it is possible to compile for wasm and then run it on the website?

1

There are 1 best solutions below

0
Horcrux7 On

de.inetsoftware.jwebassembly.WasmException: Unimplemented Java byte code operation: 42

It appears that you are using an outdated version of the compiler. The operation 42 (hexadecimal value 2a) has been implemented since 2018.