xref: /aosp_15_r20/external/angle/third_party/spirv-tools/src/test/wasm/test.js (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1// Copyright (c) 2020 The Khronos Group Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15const spirvTools = require("../../out/web/spirv-tools");
16const fs = require("fs");
17const util = require("util");
18const readFile = util.promisify(fs.readFile);
19
20const SPV_PATH = "./test/fuzzers/corpora/spv/simple.spv";
21
22const test = async () => {
23  const spv = await spirvTools();
24
25  // disassemble from file
26  const buffer = await readFile(SPV_PATH);
27  const disFileResult = spv.dis(
28    buffer,
29    spv.SPV_ENV_UNIVERSAL_1_3,
30    spv.SPV_BINARY_TO_TEXT_OPTION_INDENT |
31      spv.SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES |
32      spv.SPV_BINARY_TO_TEXT_OPTION_COLOR
33  );
34  console.log("dis from file:\n", disFileResult);
35
36  // assemble
37  const source = `
38             OpCapability Linkage
39             OpCapability Shader
40             OpMemoryModel Logical GLSL450
41             OpSource GLSL 450
42             OpDecorate %spec SpecId 1
43      %int = OpTypeInt 32 1
44     %spec = OpSpecConstant %int 0
45    %const = OpConstant %int 42`;
46  const asResult = spv.as(
47    source,
48    spv.SPV_ENV_UNIVERSAL_1_3,
49    spv.SPV_TEXT_TO_BINARY_OPTION_NONE
50  );
51  console.log(`as returned ${asResult.byteLength} bytes`);
52
53  // re-disassemble
54  const disResult = spv.dis(
55    asResult,
56    spv.SPV_ENV_UNIVERSAL_1_3,
57    spv.SPV_BINARY_TO_TEXT_OPTION_INDENT |
58      spv.SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES |
59      spv.SPV_BINARY_TO_TEXT_OPTION_COLOR
60  );
61  console.log("dis:\n", disResult);
62};
63
64test();
65