emscripten-core / emscripten-core/emscripten

C compiled WASM functions - Pointers logic do not work as expected

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

Description

I compiled a c program having pointers and imported it in js using WebAssembly. The c function seems to be available in js but the pointer concept is not working as expected. I am totally confused, as javascript does not use pointer concept. Thus there must be something I must do there.

Below is my c program, I store the fibonacci numbers in an array that is taken as pointer. Also changed a character value, modified it.
```
#include

EMSCRIPTEN_KEEPALIVE
int fibonacci_pointer(int *res, int n, char *ch) {
res[0] = 0;
if(n == 0) { return n; }
res[1] = 1;
if(n == 1) { return n; }
for(int i = 2; i <= n; i++) {
res[i] = res[i-1] + res[i-2];
}
ch = "Bye";
return n;
}
```
I compiled the file using below emcc command,
```
emcc \
fibonacci.c \
-Os \
-o fibonacci.wasm \
--no-entry
```
I served this WASM file via a local node server. Finally in my Angular 8 application in app.component.ts. I added the below code.
```
import { AfterViewInit, Component, OnInit } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {
title = 'FrontEnd';

constructor() {}

ngOnInit() {
WebAssembly.instantiateStreaming(window.fetch('http://localhost:3000/fibonacci.wasm'), {
imports: {
imported_func: function(arg) {
console.log(arg);
},
wasi_unstable: () => {}
}
})
.then(async (obj) => {
// Declare variable
// tslint:disable-next-line: prefer-const
let array = []; let n = 8; let str = 'Hi';

// Print to validate of function present
console.log(obj.instance.exports);

// Call the function
const fun: any = obj.instance.exports;
console.log(fun.fibonacci_pointer(array, n, str));

// Print to check if values modified
console.log('Array: ', array, ', N: ', n, ', str: ', str);
// Expected: Array: [0,1,1,....], n: 8, str: Bye
// Got: Array: [], n: 8, str: Hi

})
.catch(err => {
console.log('Unable to get WASM files. Please try again later: ', err);
alert('Unable to get WASM files. Please try again later !');
/* window.history.back(); */
});
}

}
```
Below is my console output image. enter image description here
![image](https://user-images.githubusercontent.com/8725690/98535451-22e3b600-22ac-11eb-8137-593ab56ce7fa.png)

As you can see, the c function is available and works fine since it returns the n value correctly. But the pointer logic is not working because the array is still empty.

Am i wrong? I have searched quite a lot in the documentation. They say pointers are supported. but it does not work this way. I know javascript does not have pointers. How can this work ?

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.