admin管理员组文章数量:1391937
I'm new to WebAssembly and trying to get a basic example working, but loading the .wasm
file in Google Chrome returns:
CompileError: WebAssemblypile(): expected magic word 00 61 73 6d, found 30 30 36 31 @+0
This is the C++ code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
And this is the WASM file:
Code that I'm executing in Chrome, based on documentation:
function instantiate(bytes, imports) {
return WebAssemblypile(bytes).then(function(m) {
return new WebAssembly.Instance(m, imports)
});
}
fetch('simple.wasm').then(function(response) {
return response.arrayBuffer()
})
.then(function(bytes) {
var importObject = {
imports: {
i: function(arg) {
console.log(arg)
}
}
};
return instantiate(bytes, importObject)
})
.then(function(instance) {
return instance.exports.e()
})
I'm new to WebAssembly and trying to get a basic example working, but loading the .wasm
file in Google Chrome returns:
CompileError: WebAssembly.pile(): expected magic word 00 61 73 6d, found 30 30 36 31 @+0
This is the C++ code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
And this is the WASM file: https://drive.google./open?id=1nvoxoeZ6TA9OVc4JFHSwGIVxuDHFnIU1
Code that I'm executing in Chrome, based on documentation:
function instantiate(bytes, imports) {
return WebAssembly.pile(bytes).then(function(m) {
return new WebAssembly.Instance(m, imports)
});
}
fetch('simple.wasm').then(function(response) {
return response.arrayBuffer()
})
.then(function(bytes) {
var importObject = {
imports: {
i: function(arg) {
console.log(arg)
}
}
};
return instantiate(bytes, importObject)
})
.then(function(instance) {
return instance.exports.e()
})
Share
Improve this question
asked Mar 3, 2020 at 12:11
ValipValip
4,65023 gold badges91 silver badges157 bronze badges
1 Answer
Reset to default 5The problem is that your .wasm
file is in hex format instead of binary.
You should probably check your pile flags, but you can also convert it back from hex with something like this:
fetch('simple.wasm').then(function(response) {
return response.arrayBuffer()
})
-.then(function(bytes) {
+.then(function(buffer) {
+ const bytes = fromHex(buffer);
var importObject = {
imports: {
i: function(arg) {
function fromHex(hexBuffer) {
const hexBytes = new Uint8Array(hexBuffer);
if (hexBytes.length % 2 !== 0)
throw new Error("Hex buffer must have an even length.");
const result = new Uint8Array(hexBytes.length / 2);
function fromAscii(char) {
if (char < 0x30 || (char > 0x39 && char < 0x41) || (char > 0x46 && char < 0x61) || char > 0x66)
throw new Error("Invalid hex character: " + String.fromCharCode(char));
/* '0'..'9' -> 0..9
'A'..'F', 'a'..'f' -> 1..6 */
const result = char & 0xF;
/* 'A'..'F', 'a'..'f' -> 10..15 */
return char & 0x40 ? result + 9 : result;
}
for (let i = 0, j = 0; i < result.length; i++, j+=2) {
result[i] = (fromAscii(hexBytes[j]) << 4) |
fromAscii(hexBytes[j + 1]);
}
return result;
}
本文标签: javascriptCompileError WebAssemblycompile() expected magic wordStack Overflow
版权声明:本文标题:javascript - CompileError: WebAssembly.compile(): expected magic word - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744753916a2623345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论