Home > database >  wasm binary giving wrong output as compared to running binary in terminal
wasm binary giving wrong output as compared to running binary in terminal

Time:06-01

I have a go program (https://github.com/klauspost/cpuid) which I'm running in my terminal and it works perfectly fine. I wanted to make it like a service running in browser. The only way possible I see is to compile it to wasm binary and serve the file statically.

But the problem is, output in terminal and browser are different. Anything I'm missing here?

  1. go code is available in repo shared above
  2. programm is compiled to wasm using GOOS=js GOARCH=wasm go build -o test.wasm test.go
  3. Js code to serve statically
const http = require('http');

const nStatic = require('node-static');

// wasm file is in public folder. 
// wasm_exec.js is used which is available in go installtion
const fileServer = new nStatic.Server('./public');

const PORT = 8000;

http.createServer(function (req, res) {

  fileServer.serve(req, res);

}).listen(PORT);

In browser

In terminal

CodePudding user response:

While in the end both is run on the same the CPU that is not true technically. WASM is bytecode for an Virtual CPU. That bytecode is translated into machine bytecode by most browsers and then executed. But it secured that from this virtual machine nearly no access can be made to the real machine except he accepted API. So whatever functions they will most likely fall or where replaced by dummy functions and so on return error codes or dummy values that where chosen in a way they can be distinguished from real values.

  • Related