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?
- go code is available in repo shared above
- programm is compiled to wasm using
GOOS=js GOARCH=wasm go build -o test.wasm test.go
- 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);
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.