|
| 1 | +/* |
| 2 | + * Copyright 2026 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// |
| 18 | +// Embed Wasm binaries into a JS file, or extract embedded Wasm binaries from a |
| 19 | +// JS file. |
| 20 | +// |
| 21 | + |
| 22 | +#include <iostream> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "parsing.h" |
| 27 | +#include "pass.h" |
| 28 | +#include "support/command-line.h" |
| 29 | +#include "support/file.h" |
| 30 | +#include "support/js-embedded-module.h" |
| 31 | +#include "support/utilities.h" |
| 32 | +#include "wasm-io.h" |
| 33 | +#include "wasm-validator.h" |
| 34 | + |
| 35 | +using namespace wasm; |
| 36 | + |
| 37 | +int main(int argc, const char* argv[]) { |
| 38 | + const std::string WasmEmbedOption = "wasm-embed options"; |
| 39 | + |
| 40 | + bool extract = false; |
| 41 | + std::string output; |
| 42 | + std::vector<std::string> positionals; |
| 43 | + |
| 44 | + Options options( |
| 45 | + "wasm-embed", |
| 46 | + "Embed Wasm binaries into a JS file, or extract embedded Wasm binaries " |
| 47 | + "from a JS file"); |
| 48 | + options |
| 49 | + .add("--extract", |
| 50 | + "-e", |
| 51 | + "Extract embedded Wasm modules from the input JS file into " |
| 52 | + "<output>.0.wasm, <output>.1.wasm, ...", |
| 53 | + WasmEmbedOption, |
| 54 | + Options::Arguments::Zero, |
| 55 | + [&](Options* o, const std::string& argument) { extract = true; }) |
| 56 | + .add("--output", |
| 57 | + "-o", |
| 58 | + "Output JS file (or output prefix when --extract is used)", |
| 59 | + WasmEmbedOption, |
| 60 | + Options::Arguments::One, |
| 61 | + [&](Options* o, const std::string& argument) { output = argument; }) |
| 62 | + .add_positional("INFILE.js [WASM_FILES...] [OUTFILE]", |
| 63 | + Options::Arguments::N, |
| 64 | + [&](Options* o, const std::string& argument) { |
| 65 | + positionals.push_back(argument); |
| 66 | + }); |
| 67 | + options.parse(argc, argv); |
| 68 | + |
| 69 | + if (extract) { |
| 70 | + std::string inJsFile; |
| 71 | + if (output.empty()) { |
| 72 | + if (positionals.size() != 2) { |
| 73 | + Fatal() << "Expected input JS file and output prefix"; |
| 74 | + } |
| 75 | + inJsFile = positionals[0]; |
| 76 | + output = positionals[1]; |
| 77 | + } else { |
| 78 | + if (positionals.size() != 1) { |
| 79 | + Fatal() << "Expected a single input JS file when --output is specified"; |
| 80 | + } |
| 81 | + inJsFile = positionals[0]; |
| 82 | + } |
| 83 | + |
| 84 | + auto js = read_file<std::string>(inJsFile, Flags::Text); |
| 85 | + auto modules = findEmbeddedModules(js); |
| 86 | + for (size_t i = 0; i < modules.size(); ++i) { |
| 87 | + write_file(output + "." + std::to_string(i) + ".wasm", modules[i].bytes); |
| 88 | + } |
| 89 | + flush_and_quick_exit(0); |
| 90 | + } |
| 91 | + |
| 92 | + std::string inJsFile; |
| 93 | + std::vector<std::string> wasmFiles; |
| 94 | + if (output.empty()) { |
| 95 | + if (positionals.size() < 2) { |
| 96 | + Fatal() << "Expected input JS file, wasm files, and output JS file"; |
| 97 | + } |
| 98 | + inJsFile = positionals.front(); |
| 99 | + output = positionals.back(); |
| 100 | + wasmFiles.assign(positionals.begin() + 1, positionals.end() - 1); |
| 101 | + } else { |
| 102 | + if (positionals.empty()) { |
| 103 | + Fatal() << "Expected input JS file"; |
| 104 | + } |
| 105 | + inJsFile = positionals.front(); |
| 106 | + wasmFiles.assign(positionals.begin() + 1, positionals.end()); |
| 107 | + } |
| 108 | + |
| 109 | + auto js = read_file<std::string>(inJsFile, Flags::Text); |
| 110 | + auto existing = findEmbeddedModules(js); |
| 111 | + if (existing.size() != wasmFiles.size()) { |
| 112 | + Fatal() << "Number of embedded wasm modules in " << inJsFile << " (" |
| 113 | + << existing.size() |
| 114 | + << ") does not match number of provided wasm files (" |
| 115 | + << wasmFiles.size() << ")"; |
| 116 | + } |
| 117 | + |
| 118 | + for (size_t i = existing.size(); i > 0; --i) { |
| 119 | + const auto& mod = existing[i - 1]; |
| 120 | + const auto& wasmFile = wasmFiles[i - 1]; |
| 121 | + auto bytes = read_file<std::vector<char>>(wasmFile, Flags::Binary); |
| 122 | + bool isBinary = bytes.size() >= 4 && bytes[0] == '\0' && bytes[1] == 'a' && |
| 123 | + bytes[2] == 's' && bytes[3] == 'm'; |
| 124 | + |
| 125 | + Module wasm; |
| 126 | + wasm.features = FeatureSet::All; |
| 127 | + try { |
| 128 | + ModuleReader().readData(bytes, wasm); |
| 129 | + } catch (ParseException& p) { |
| 130 | + p.dump(std::cerr); |
| 131 | + std::cerr << '\n'; |
| 132 | + Fatal() << "error parsing wasm (" << wasmFile << ")"; |
| 133 | + } |
| 134 | + |
| 135 | + if (!WasmValidator().validate(wasm)) { |
| 136 | + Fatal() << "error validating module (" << wasmFile << ")"; |
| 137 | + } |
| 138 | + |
| 139 | + if (!isBinary) { |
| 140 | + PassOptions passOptions; |
| 141 | + ModuleWriter writer(passOptions); |
| 142 | + writer.setBinary(true); |
| 143 | + writer.write(wasm, bytes); |
| 144 | + } |
| 145 | + |
| 146 | + js.replace(mod.start, mod.end - mod.start, formatByteArray(bytes)); |
| 147 | + } |
| 148 | + |
| 149 | + write_file(output, js); |
| 150 | + flush_and_quick_exit(0); |
| 151 | +} |
0 commit comments