UNPKG

wgsl-plus

Version:

A WGSL preprocessor, prettifier, minifier, obfuscator, and compiler with C-style macros, conditional compilation, file linking, and multi-format output for WebGPU shaders.

37 lines (36 loc) 2.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = obfuscate; const collect_declared_identifiers_and_entry_points_1 = __importDefault(require("./collect-declared-identifiers-and-entry-points")); const collect_struct_names_1 = __importDefault(require("./collect-struct-names")); const tokenize_wgsl_1 = __importDefault(require("../../tokenization/tokenize-wgsl")); const replace_identifiers_1 = __importDefault(require("./replace-identifiers")); const reconstruct_obfuscated_code_1 = __importDefault(require("./reconstruct-obfuscated-code")); const next_name_1 = require("./next-name"); /** * Obfuscates WGSL code by renaming explicitly declared functions, variables, structs, and function parameters, * as well as struct members, while preserving vector swizzles and entry point names. * @param code The WGSL source code to obfuscate. * @returns The obfuscated and minified code with binding comments. */ function obfuscate(code) { (0, next_name_1.resetNameIndex)(); // Get all the tokens. const tokens = (0, tokenize_wgsl_1.default)(code).filter(token => token.type !== "comment"); // Preliminary pass: Collect struct member names let structMemberMap = (0, collect_struct_names_1.default)(tokens); // First pass: Collect declared identifiers and entry points let { identifierMap, bindingMap } = (0, collect_declared_identifiers_and_entry_points_1.default)(tokens); // Second pass: Replace identifiers with context-aware logic const obfuscatedTokens = (0, replace_identifiers_1.default)(tokens, identifierMap, structMemberMap); // Reconstruct the obfuscated code with corrected spacing let obfuscatedCode = (0, reconstruct_obfuscated_code_1.default)(obfuscatedTokens, tokens); // Generate binding mapping comments const bindingComments = Array.from(bindingMap.entries()) .map(([oldName, newName]) => `//#!binding ${oldName} ${newName}`) .join("\n"); return bindingComments ? `${bindingComments}\n${obfuscatedCode}` : obfuscatedCode; }