aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/webassembly/validate/index.html
blob: 7d44cc216bf0bcd1c040c793f27602c8a6f35675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: WebAssembly.validate()
slug: Web/JavaScript/Reference/Global_Objects/WebAssembly/validate
translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly/validate
---
<div>{{JSRef}}</div>

<p><strong><code>WebAssembly.validate()</code></strong> 方法用于验证包含 WebAssembly 二进制码的一个 <a href="/en-US/docs/Web/JavaScript/Typed_arrays">typed array</a> 是否合法,返回 <code>true</code> 如果这些字节能构成一个合法的 wasm 模块,否则返回 <code>false</code></p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">WebAssembly.validate(bufferSource);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>bufferSource</code></dt>
 <dd>一个包含 WebAssembly 二进制码的 <a href="/en-US/docs/Web/JavaScript/Typed_arrays">typed array</a> 或 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a></dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>一个布尔值,用来表示给定的 <code>bufferSource</code> 是合法 wasm 代码(<code>true</code>)或者不是(<code>false</code>)。</p>

<h3 id="异常">异常</h3>

<p>如果给定的 <code>bufferSource</code> 不是 <a href="/en-US/docs/Web/JavaScript/Typed_arrays">typed array</a> 或 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a> 类型,将会抛出 {{jsxref("TypeError")}} 异常。</p>

<h2 id="例子">例子</h2>

<p>下面的例子(查看 validate.html <a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/validate.html">源代码</a>,或者<a href="https://mdn.github.io/webassembly-examples/js-api-examples/validate.html">在线预览</a>)通过 <code>fetch</code> 获取了一个 .wasm 模块并将其转换为一个 typed array。接下来用 <code>validate()</code> 方法来验证这个模块是否合法。</p>

<pre class="brush: js">fetch('simple.wasm').then(response =&gt;
  response.arrayBuffer()
).then(function(bytes) {
  var valid = WebAssembly.validate(bytes);
  console.log("The given bytes are "
    + (valid ? "" : "not ") + "a valid wasm module");
});
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('WebAssembly JS', '#webassemblyvalidate', 'validate()')}}</td>
   <td>{{Spec2('WebAssembly JS')}}</td>
   <td>Initial draft definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>

<div>


<p>{{Compat("javascript.builtins.WebAssembly.validate")}}</p>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/WebAssembly">WebAssembly</a> overview page</li>
 <li><a href="/en-US/docs/WebAssembly/Concepts">WebAssembly concepts</a></li>
 <li><a href="/en-US/docs/WebAssembly/Using_the_JavaScript_API">Using the WebAssembly JavaScript API</a></li>
</ul>