Skip to content

@Import does't work #23

Open
Open

Description

@zonghaishang

de.inetsoftware.jwebassembly.api.annotation.Import Does not include the import function definition, is it currently not supported?

demo: https://github.com/zonghaishang/wasm-java/tree/jwebassembly

package com.alipay.wasm;


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

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

public class WasmFunctions {

    @Export
    public static int hello() {
        return 43;
    }

// does't work ??

    @Import
    public static native int max(int a, int b) ;

    public static void main(String[] args) {
        File file = new File("build/test.wasm");
        JWebAssembly wasm = new JWebAssembly();

        Class clazz = WasmFunctions.class;
        URL url = clazz.getResource("/" + clazz.getName().replace(".", "/") + ".class");
        wasm.addFile(url);

        System.out.println(wasm.compileToText());

        wasm.compileToBinary(file);
    }
}
➜  build git:(jwebassembly) ✗ wasm-objdump -x test.wasm -j Import

test.wasm:      file format wasm 0x1

Section Details:

Section not found: Import

➜  build git:(jwebassembly) ✗ wasm-objdump -x test.wasm 

test.wasm:      file format wasm 0x1

Section Details:

Type[1]:
 - type[0] () -> i32
Function[1]:
 - func[0] sig=0 <hello>
Export[1]:
 - func[0] <hello> -> "hello"
Code[1]:
 - func[0] size=5 <hello>
Custom:
 - name: "producers"

➜  build git:(jwebassembly) ✗ wasm-objdump -x test.wasm -j Export

test.wasm:      file format wasm 0x1

Section Details:

Export[1]:
 - func[0] <hello> -> "hello"

Activity

  1. Horcrux7 commented on Apr 13, 2021

    @Horcrux7
    Member

    You have not use the method max. The compiler import only the needed methods.

  2. zonghaishang commented on Apr 14, 2021

    @zonghaishang
    Author

    You have not use the method max. The compiler import only the needed methods.

    @Horcrux7 nice, It is correct to include the import call in export

    public class WasmFunctions {
    
        @Export
        public static int hello() {
            max(1, 2);
            return 43;
        }
    
        @Import
        public static native int max(int a, int b);
    
    1. Is there a switch to compile all @Import functions by default, of course, the corresponding implementation can be guaranteed on the host side
    2. Use JWebassembly to compile into wasm. If it runs in go runtime, does it include gc logic?
  3. Horcrux7 commented on Apr 14, 2021

    @Horcrux7
    Member

    It is correct to include the import call in export
    Yes, why not.

    1.) There is no such switch. It make no sense for me. It would also import all the @import from the runtime. Take a look at: https://github.com/i-net-software/JWebAssembly-API/tree/master/src/de/inetsoftware/jwebassembly/api/java/lang

    Of course the runtime is optional and theoretical you can use different use a different runtime. But practical it will use in the most cases.

    2.) I does not understand the question. What you means with go runtime.

    For the missing GC it use currently a Java script polyfill. With a compiler flag you can enable the usage of the GC code but this is work in progress. In the WASM runtime and in JWebAssembly.

  4. zonghaishang commented on Apr 15, 2021

    @zonghaishang
    Author

    2.) I does not understand the question. What you means with go runtime.

    For the missing GC it use currently a Java script polyfill. With a compiler flag you can enable the usage of the GC code but this is work in progress. In the WASM runtime and in JWebAssembly.

    Please let me briefly introduce. At present, we have supported running wasm module in go , allowing developers to write wasm function extensions, and we expect to do better. We hope that java developers can also contribute wasm plugins, so that the implementation of multi-language extensions can be run in go.

    Currently I find JWebassembly is a great project,at the same time, it is also expected that the compiled wasm can run outside the web environment. The wasm file generated by dump Jwebassembly lacks the content of the Webassembly specification, such as Memory Section...

    So I raised another issue#24 to follow up on this issue

  5. Horcrux7 commented on Apr 15, 2021

    @Horcrux7
    Member

    The memory section will be created if there are data for it. For example you use a string constant. Or you use a instance method call then the vtables are in the memory section.

    But you can not use it for data exchange with go.

  6. zonghaishang commented on Apr 16, 2021

    @zonghaishang
    Author

    The memory section will be created if there are data for it. For example you use a string constant. Or you use a instance method call then the vtables are in the memory section.

    But you can not use it for data exchange with go.

    Can provide some simple examples to demonstrate the compilation and export of the memory segment. At present, data exchange is exchanged through linear memory byte arrays.

  7. Horcrux7 commented on Apr 16, 2021

    @Horcrux7
    Member

    Can provide some simple examples to demonstrate the compilation and export of the memory segment. At present, data exchange is exchanged through linear memory byte arrays.

    No, such sample is not possible. Data exchange via memory segment is not possible. If you want export or import binary data then you need to use a method that use a Java byte[] array like:

    @Export
    public static byte[] getData()
    
    @Export
    public static void setData( byte[] data )
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions