Skip to content

Import function with struct/class parameters #21043

@xzmqaq0307

Description

@xzmqaq0307

When using the import and export function, I encountered a problem where the structure is used as a parameter for the export function. However, when this function is imported into another wasm and used, the final output result is incorrect

utils.h

#ifndef UTILS_H
#define UTILS_H

#define WASM_EXPORT(name) __attribute__((export_name(name)))
#define WASM_IMPORT(mod, name) __attribute__((import_module(mod), import_name(name)))

struct MyObject {
	int val1 = 5;
	int val2 = 5;
	int val3 = 5;
	void resetAll() {
		val1 = 5;
		val2 = 5;
		val3 = 5;
	}
};
#endif

share.cpp (define export function)

#include "../inc/utils.h"
#include <stdint.h>

MyObject obj;

WASM_IMPORT("env", "print") extern void print(int num);
WASM_EXPORT("set_obj") void set_obj(MyObject val) {
	obj = val;
}

main.cpp (import functions which is exported by share.cpp)

#include "../inc/utils.h"
#include <stdint.h>

WASM_IMPORT("env", "print") extern void print(int num);
WASM_IMPORT("share_ctx", "set_obj") extern void set_obj(MyObject obj);

WASM_EXPORT("main_set_obj") void main_set_obj() {
	MyObject obj;
	obj.val1 = 1;
	obj.val2 = 2;
	obj.val3 = 3;
	set_obj(obj);
}

Can I pass the MyObject value and set it through the import 'set_obj' function ? the result is that all value is 0, which is not expected

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