Moh*_*ail 3 google-chrome webassembly
我正在尝试在WebAssembly中执行计算阶乘的基本C代码,并且在Google Chrome(57.0.2987.98)中加载WASM文件时得到
CompileError:WebAssembly.compile():
Wasm解码失败结果=预期的魔术字00 61 73 6d,
找到30 30 36 31 @ + 0`
C代码:
double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}
Run Code Online (Sandbox Code Playgroud)
浪费:
(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "_Z4facti" (func $_Z4facti))
(func $_Z4facti (param $0 i32) (result f64)
(local $1 i64)
(local $2 i64)
(block $label$0
(br_if $label$0
(i32.lt_s
(get_local $0)
(i32.const 1)
)
)
(set_local $1
(i64.add
(i64.extend_s/i32
(get_local $0)
)
(i64.const 1)
)
)
(set_local $2
(i64.const 1)
)
(loop $label$1
(set_local $2
(i64.mul
(get_local $2)
(tee_local $1
(i64.add
(get_local $1)
(i64.const -1)
)
)
)
)
(br_if $label$1
(i64.gt_s
(get_local $1)
(i64.const 1)
)
)
)
(return
(f64.convert_s/i64
(get_local $2)
)
)
)
(f64.const 1)
)
)
Run Code Online (Sandbox Code Playgroud)
WASM编译代码:
0061 736d 0100 0000 0186 8080 8000 0160
017f 017c 0382 8080 8000 0100 0484 8080
8000 0170 0000 0583 8080 8000 0100 0106
8180 8080 0000 0795 8080 8000 0206 6d65
6d6f 7279 0200 085f 5a34 6661 6374 6900
000a c380 8080 0001 bd80 8080 0001 027e
0240 2000 4101 480d 0020 00ac 4201 7c21
0142 0121 0203 4020 0220 0142 7f7c 2201
7e21 0220 0142 0155 0d00 0b20 02b9 0f0b
4400 0000 0000 00f0 3f0b
`
Run Code Online (Sandbox Code Playgroud)
在Chrome中执行的代码:
async function load(){
let binary = await fetch('https://flinkhub.com/t.wasm');
let bytes = await binary.arrayBuffer();
console.log(bytes);
let module = await WebAssembly.compile(bytes);
let instance = await WebAssembly.Instance(module);
}
load().then(instance => console.log(instance.exports.fact(3)));
Run Code Online (Sandbox Code Playgroud)
谁能帮帮我,我已经整天呆在这里,无法理解出了什么问题。我使用WebAssembly Explorer来获取WAST和WASM代码。
使用您引用的WebAssembly Explorer的下载功能,我得到以下文件(如hexdump所示):
0000000 00 61 73 6d 01 00 00 00 01 86 80 80 80 00 01 60
0000010 01 7f 01 7c 03 82 80 80 80 00 01 00 04 84 80 80
0000020 80 00 01 70 00 00 05 83 80 80 80 00 01 00 01 06
0000030 81 80 80 80 00 00 07 95 80 80 80 00 02 06 6d 65
0000040 6d 6f 72 79 02 00 08 5f 5a 34 66 61 63 74 69 00
0000050 00 0a c3 80 80 80 00 01 bd 80 80 80 00 01 02 7e
0000060 02 40 20 00 41 01 48 0d 00 20 00 ac 42 01 7c 21
0000070 01 42 01 21 02 03 40 20 02 20 01 42 7f 7c 22 01
0000080 7e 21 02 20 01 42 01 55 0d 00 0b 20 02 b9 0f 0b
0000090 44 00 00 00 00 00 00 f0 3f 0b
000009a
Run Code Online (Sandbox Code Playgroud)
这是一个有效的.wasm二进制数,以00 61 73 6daka 开头\0asm。根据收到的错误消息,您的文件开头30 30 36 31是无效的。
仔细检查.wasm您拥有的文件。
解码30 30 36 31为ASCII可能会0061出现问题:您正在加载十六进制文件的文本版本。当然,您提供的URL(https://flinkhub.com/t.wasm)包含以下内容(我没有将其十六进制转储!它是ASCII):
0061 736d 0100 0000 0186 8080 8000 0160
017f 017c 0382 8080 8000 0100 0484 8080
8000 0170 0000 0583 8080 8000 0100 0106
8180 8080 0000 0795 8080 8000 0206 6d65
6d6f 7279 0200 085f 5a34 6661 6374 6900
000a c380 8080 0001 bd80 8080 0001 027e
0240 2000 4101 480d 0020 00ac 4201 7c21
0142 0121 0203 4020 0220 0142 7f7c 2201
7e21 0220 0142 0155 0d00 0b20 02b9 0f0b
4400 0000 0000 00f0 3f0b
Run Code Online (Sandbox Code Playgroud)
我猜您覆盖了从资源管理器保存的文件。
| 归档时间: |
|
| 查看次数: |
4147 次 |
| 最近记录: |