emscripten-core / emscripten-core/emscripten

Import function with struct/class parameters

Open
#21,043 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

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**
```cpp
#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)**
```cpp
#include "../inc/utils.h"
#include

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)
```cpp
#include "../inc/utils.h"
#include

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.