first commit

This commit is contained in:
monjack
2025-06-20 18:01:48 +08:00
commit 6daa6d65c1
24611 changed files with 2512443 additions and 0 deletions

21
app_vue/node_modules/@webassemblyjs/wasm-edit/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Sven Sauleau <sven@sauleau.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,86 @@
# @webassemblyjs/wasm-edit
> Rewrite a WASM binary
Replace in-place an AST node in the binary.
## Installation
```sh
yarn add @webassemblyjs/wasm-edit
```
## Usage
Update:
```js
import { edit } from "@webassemblyjs/wasm-edit";
const binary = [/*...*/];
const visitors = {
ModuleImport({ node }) {
node.module = "foo";
node.name = "bar";
}
};
const newBinary = edit(binary, visitors);
```
Replace:
```js
import { edit } from "@webassemblyjs/wasm-edit";
const binary = [/*...*/];
const visitors = {
Instr(path) {
const newNode = t.callInstruction(t.indexLiteral(0));
path.replaceWith(newNode);
}
};
const newBinary = edit(binary, visitors);
```
Remove:
```js
import { edit } from "@webassemblyjs/wasm-edit";
const binary = [/*...*/];
const visitors = {
ModuleExport({ node }) {
path.remove()
}
};
const newBinary = edit(binary, visitors);
```
Insert:
```js
import { add } from "@webassemblyjs/wasm-edit";
const binary = [/*...*/];
const newBinary = add(actualBinary, [
t.moduleImport("env", "mem", t.memory(t.limit(1)))
]);
```
## Providing the AST
Providing an AST allows you to handle the decoding yourself, here is the API:
```js
addWithAST(Program, ArrayBuffer, Array<Node>): ArrayBuffer;
editWithAST(Program, ArrayBuffer, visitors): ArrayBuffer;
```
Note that the AST will be updated in-place.

View File

@ -0,0 +1,306 @@
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { encodeNode } from "@webassemblyjs/wasm-gen";
import { encodeU32 } from "@webassemblyjs/wasm-gen/lib/encoder";
import { isFunc, isGlobal, assertHasLoc, orderedInsertNode, getSectionMetadata, traverse, getEndOfSection } from "@webassemblyjs/ast";
import { resizeSectionByteSize, resizeSectionVecSize, createEmptySection, removeSections } from "@webassemblyjs/helper-wasm-section";
import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer";
import { getSectionForNode } from "@webassemblyjs/helper-wasm-bytecode";
function shiftLocNodeByDelta(node, delta) {
assertHasLoc(node); // $FlowIgnore: assertHasLoc ensures that
node.loc.start.column += delta; // $FlowIgnore: assertHasLoc ensures that
node.loc.end.column += delta;
}
function applyUpdate(ast, uint8Buffer, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
oldNode = _ref2[0],
newNode = _ref2[1];
var deltaElements = 0;
assertHasLoc(oldNode);
var sectionName = getSectionForNode(newNode);
var replacementByteArray = encodeNode(newNode);
/**
* Replace new node as bytes
*/
uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.end.column, replacementByteArray);
/**
* Update function body size if needed
*/
if (sectionName === "code") {
// Find the parent func
traverse(ast, {
Func: function Func(_ref3) {
var node = _ref3.node;
var funcHasThisIntr = node.body.find(function (n) {
return n === newNode;
}) !== undefined; // Update func's body size if needed
if (funcHasThisIntr === true) {
// These are the old functions locations informations
assertHasLoc(node);
var oldNodeSize = encodeNode(oldNode).length;
var bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
if (bodySizeDeltaBytes !== 0) {
var newValue = node.metadata.bodySize + bodySizeDeltaBytes;
var newByteArray = encodeU32(newValue); // function body size byte
// FIXME(sven): only handles one byte u32
var start = node.loc.start.column;
var end = start + 1;
uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newByteArray);
}
}
}
});
}
/**
* Update section size
*/
var deltaBytes = replacementByteArray.length - (oldNode.loc.end.column - oldNode.loc.start.column); // Init location informations
newNode.loc = {
start: {
line: -1,
column: -1
},
end: {
line: -1,
column: -1
}
}; // Update new node end position
// $FlowIgnore: assertHasLoc ensures that
newNode.loc.start.column = oldNode.loc.start.column; // $FlowIgnore: assertHasLoc ensures that
newNode.loc.end.column = // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.start.column + replacementByteArray.length;
return {
uint8Buffer: uint8Buffer,
deltaBytes: deltaBytes,
deltaElements: deltaElements
};
}
function applyDelete(ast, uint8Buffer, node) {
var deltaElements = -1; // since we removed an element
assertHasLoc(node);
var sectionName = getSectionForNode(node);
if (sectionName === "start") {
var sectionMetadata = getSectionMetadata(ast, "start");
/**
* The start section only contains one element,
* we need to remove the whole section
*/
uint8Buffer = removeSections(ast, uint8Buffer, "start");
var _deltaBytes = -(sectionMetadata.size.value + 1);
/* section id */
return {
uint8Buffer: uint8Buffer,
deltaBytes: _deltaBytes,
deltaElements: deltaElements
};
} // replacement is nothing
var replacement = [];
uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
node.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
node.loc.end.column, replacement);
/**
* Update section
*/
// $FlowIgnore: assertHasLoc ensures that
var deltaBytes = -(node.loc.end.column - node.loc.start.column);
return {
uint8Buffer: uint8Buffer,
deltaBytes: deltaBytes,
deltaElements: deltaElements
};
}
function applyAdd(ast, uint8Buffer, node) {
var deltaElements = +1; // since we added an element
var sectionName = getSectionForNode(node);
var sectionMetadata = getSectionMetadata(ast, sectionName); // Section doesn't exists, we create an empty one
if (typeof sectionMetadata === "undefined") {
var res = createEmptySection(ast, uint8Buffer, sectionName);
uint8Buffer = res.uint8Buffer;
sectionMetadata = res.sectionMetadata;
}
/**
* check that the expressions were ended
*/
if (isFunc(node)) {
// $FlowIgnore
var body = node.body;
if (body.length === 0 || body[body.length - 1].id !== "end") {
throw new Error("expressions must be ended");
}
}
if (isGlobal(node)) {
// $FlowIgnore
var body = node.init;
if (body.length === 0 || body[body.length - 1].id !== "end") {
throw new Error("expressions must be ended");
}
}
/**
* Add nodes
*/
var newByteArray = encodeNode(node); // The size of the section doesn't include the storage of the size itself
// we need to manually add it here
var start = getEndOfSection(sectionMetadata);
var end = start;
/**
* Update section
*/
var deltaBytes = newByteArray.length;
uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newByteArray);
node.loc = {
start: {
line: -1,
column: start
},
end: {
line: -1,
column: start + deltaBytes
}
}; // for func add the additional metadata in the AST
if (node.type === "Func") {
// the size is the first byte
// FIXME(sven): handle LEB128 correctly here
var bodySize = newByteArray[0];
node.metadata = {
bodySize: bodySize
};
}
if (node.type !== "IndexInFuncSection") {
orderedInsertNode(ast.body[0], node);
}
return {
uint8Buffer: uint8Buffer,
deltaBytes: deltaBytes,
deltaElements: deltaElements
};
}
export function applyOperations(ast, uint8Buffer, ops) {
ops.forEach(function (op) {
var state;
var sectionName;
switch (op.kind) {
case "update":
state = applyUpdate(ast, uint8Buffer, [op.oldNode, op.node]);
sectionName = getSectionForNode(op.node);
break;
case "delete":
state = applyDelete(ast, uint8Buffer, op.node);
sectionName = getSectionForNode(op.node);
break;
case "add":
state = applyAdd(ast, uint8Buffer, op.node);
sectionName = getSectionForNode(op.node);
break;
default:
throw new Error("Unknown operation");
}
/**
* Resize section vec size.
* If the length of the LEB-encoded size changes, this can change
* the byte length of the section and the offset for nodes in the
* section. So we do this first before resizing section byte size
* or shifting following operations' nodes.
*/
if (state.deltaElements !== 0 && sectionName !== "start") {
var oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = resizeSectionVecSize(ast, state.uint8Buffer, sectionName, state.deltaElements); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
}
/**
* Resize section byte size.
* If the length of the LEB-encoded size changes, this can change
* the offset for nodes in the section. So we do this before
* shifting following operations' nodes.
*/
if (state.deltaBytes !== 0 && sectionName !== "start") {
var _oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = resizeSectionByteSize(ast, state.uint8Buffer, sectionName, state.deltaBytes); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - _oldBufferLength;
}
/**
* Shift following operation's nodes
*/
if (state.deltaBytes !== 0) {
ops.forEach(function (op) {
// We don't need to handle add ops, they are positioning independent
switch (op.kind) {
case "update":
shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
break;
case "delete":
shiftLocNodeByDelta(op.node, state.deltaBytes);
break;
}
});
}
uint8Buffer = state.uint8Buffer;
});
return uint8Buffer;
}

View File

@ -0,0 +1,111 @@
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
import { decode } from "@webassemblyjs/wasm-parser";
import { traverse } from "@webassemblyjs/ast";
import { cloneNode } from "@webassemblyjs/ast/lib/clone";
import { shrinkPaddedLEB128 } from "@webassemblyjs/wasm-opt";
import { getSectionForNode } from "@webassemblyjs/helper-wasm-bytecode";
import constants from "@webassemblyjs/helper-wasm-bytecode";
import { applyOperations } from "./apply";
function hashNode(node) {
return JSON.stringify(node);
}
function preprocess(ab) {
var optBin = shrinkPaddedLEB128(new Uint8Array(ab));
return optBin.buffer;
}
function sortBySectionOrder(nodes) {
var originalOrder = new Map();
var _iterator = _createForOfIteratorHelper(nodes),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var node = _step.value;
originalOrder.set(node, originalOrder.size);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
nodes.sort(function (a, b) {
var sectionA = getSectionForNode(a);
var sectionB = getSectionForNode(b);
var aId = constants.sections[sectionA];
var bId = constants.sections[sectionB];
if (typeof aId !== "number" || typeof bId !== "number") {
throw new Error("Section id not found");
}
if (aId === bId) {
// $FlowIgnore originalOrder is filled for all nodes
return originalOrder.get(a) - originalOrder.get(b);
}
return aId - bId;
});
}
export function edit(ab, visitors) {
ab = preprocess(ab);
var ast = decode(ab);
return editWithAST(ast, ab, visitors);
}
export function editWithAST(ast, ab, visitors) {
var operations = [];
var uint8Buffer = new Uint8Array(ab);
var nodeBefore;
function before(type, path) {
nodeBefore = cloneNode(path.node);
}
function after(type, path) {
if (path.node._deleted === true) {
operations.push({
kind: "delete",
node: path.node
}); // $FlowIgnore
} else if (hashNode(nodeBefore) !== hashNode(path.node)) {
operations.push({
kind: "update",
oldNode: nodeBefore,
node: path.node
});
}
}
traverse(ast, visitors, before, after);
uint8Buffer = applyOperations(ast, uint8Buffer, operations);
return uint8Buffer.buffer;
}
export function add(ab, newNodes) {
ab = preprocess(ab);
var ast = decode(ab);
return addWithAST(ast, ab, newNodes);
}
export function addWithAST(ast, ab, newNodes) {
// Sort nodes by insertion order
sortBySectionOrder(newNodes);
var uint8Buffer = new Uint8Array(ab); // Map node into operations
var operations = newNodes.map(function (n) {
return {
kind: "add",
node: n
};
});
uint8Buffer = applyOperations(ast, uint8Buffer, operations);
return uint8Buffer.buffer;
}

View File

@ -0,0 +1,318 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.applyOperations = applyOperations;
var _wasmGen = require("@webassemblyjs/wasm-gen");
var _encoder = require("@webassemblyjs/wasm-gen/lib/encoder");
var _ast = require("@webassemblyjs/ast");
var _helperWasmSection = require("@webassemblyjs/helper-wasm-section");
var _helperBuffer = require("@webassemblyjs/helper-buffer");
var _helperWasmBytecode = require("@webassemblyjs/helper-wasm-bytecode");
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function shiftLocNodeByDelta(node, delta) {
(0, _ast.assertHasLoc)(node); // $FlowIgnore: assertHasLoc ensures that
node.loc.start.column += delta; // $FlowIgnore: assertHasLoc ensures that
node.loc.end.column += delta;
}
function applyUpdate(ast, uint8Buffer, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
oldNode = _ref2[0],
newNode = _ref2[1];
var deltaElements = 0;
(0, _ast.assertHasLoc)(oldNode);
var sectionName = (0, _helperWasmBytecode.getSectionForNode)(newNode);
var replacementByteArray = (0, _wasmGen.encodeNode)(newNode);
/**
* Replace new node as bytes
*/
uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.end.column, replacementByteArray);
/**
* Update function body size if needed
*/
if (sectionName === "code") {
// Find the parent func
(0, _ast.traverse)(ast, {
Func: function Func(_ref3) {
var node = _ref3.node;
var funcHasThisIntr = node.body.find(function (n) {
return n === newNode;
}) !== undefined; // Update func's body size if needed
if (funcHasThisIntr === true) {
// These are the old functions locations informations
(0, _ast.assertHasLoc)(node);
var oldNodeSize = (0, _wasmGen.encodeNode)(oldNode).length;
var bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
if (bodySizeDeltaBytes !== 0) {
var newValue = node.metadata.bodySize + bodySizeDeltaBytes;
var newByteArray = (0, _encoder.encodeU32)(newValue); // function body size byte
// FIXME(sven): only handles one byte u32
var start = node.loc.start.column;
var end = start + 1;
uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newByteArray);
}
}
}
});
}
/**
* Update section size
*/
var deltaBytes = replacementByteArray.length - (oldNode.loc.end.column - oldNode.loc.start.column); // Init location informations
newNode.loc = {
start: {
line: -1,
column: -1
},
end: {
line: -1,
column: -1
}
}; // Update new node end position
// $FlowIgnore: assertHasLoc ensures that
newNode.loc.start.column = oldNode.loc.start.column; // $FlowIgnore: assertHasLoc ensures that
newNode.loc.end.column = // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.start.column + replacementByteArray.length;
return {
uint8Buffer: uint8Buffer,
deltaBytes: deltaBytes,
deltaElements: deltaElements
};
}
function applyDelete(ast, uint8Buffer, node) {
var deltaElements = -1; // since we removed an element
(0, _ast.assertHasLoc)(node);
var sectionName = (0, _helperWasmBytecode.getSectionForNode)(node);
if (sectionName === "start") {
var sectionMetadata = (0, _ast.getSectionMetadata)(ast, "start");
/**
* The start section only contains one element,
* we need to remove the whole section
*/
uint8Buffer = (0, _helperWasmSection.removeSections)(ast, uint8Buffer, "start");
var _deltaBytes = -(sectionMetadata.size.value + 1);
/* section id */
return {
uint8Buffer: uint8Buffer,
deltaBytes: _deltaBytes,
deltaElements: deltaElements
};
} // replacement is nothing
var replacement = [];
uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
node.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
node.loc.end.column, replacement);
/**
* Update section
*/
// $FlowIgnore: assertHasLoc ensures that
var deltaBytes = -(node.loc.end.column - node.loc.start.column);
return {
uint8Buffer: uint8Buffer,
deltaBytes: deltaBytes,
deltaElements: deltaElements
};
}
function applyAdd(ast, uint8Buffer, node) {
var deltaElements = +1; // since we added an element
var sectionName = (0, _helperWasmBytecode.getSectionForNode)(node);
var sectionMetadata = (0, _ast.getSectionMetadata)(ast, sectionName); // Section doesn't exists, we create an empty one
if (typeof sectionMetadata === "undefined") {
var res = (0, _helperWasmSection.createEmptySection)(ast, uint8Buffer, sectionName);
uint8Buffer = res.uint8Buffer;
sectionMetadata = res.sectionMetadata;
}
/**
* check that the expressions were ended
*/
if ((0, _ast.isFunc)(node)) {
// $FlowIgnore
var body = node.body;
if (body.length === 0 || body[body.length - 1].id !== "end") {
throw new Error("expressions must be ended");
}
}
if ((0, _ast.isGlobal)(node)) {
// $FlowIgnore
var body = node.init;
if (body.length === 0 || body[body.length - 1].id !== "end") {
throw new Error("expressions must be ended");
}
}
/**
* Add nodes
*/
var newByteArray = (0, _wasmGen.encodeNode)(node); // The size of the section doesn't include the storage of the size itself
// we need to manually add it here
var start = (0, _ast.getEndOfSection)(sectionMetadata);
var end = start;
/**
* Update section
*/
var deltaBytes = newByteArray.length;
uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newByteArray);
node.loc = {
start: {
line: -1,
column: start
},
end: {
line: -1,
column: start + deltaBytes
}
}; // for func add the additional metadata in the AST
if (node.type === "Func") {
// the size is the first byte
// FIXME(sven): handle LEB128 correctly here
var bodySize = newByteArray[0];
node.metadata = {
bodySize: bodySize
};
}
if (node.type !== "IndexInFuncSection") {
(0, _ast.orderedInsertNode)(ast.body[0], node);
}
return {
uint8Buffer: uint8Buffer,
deltaBytes: deltaBytes,
deltaElements: deltaElements
};
}
function applyOperations(ast, uint8Buffer, ops) {
ops.forEach(function (op) {
var state;
var sectionName;
switch (op.kind) {
case "update":
state = applyUpdate(ast, uint8Buffer, [op.oldNode, op.node]);
sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
break;
case "delete":
state = applyDelete(ast, uint8Buffer, op.node);
sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
break;
case "add":
state = applyAdd(ast, uint8Buffer, op.node);
sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
break;
default:
throw new Error("Unknown operation");
}
/**
* Resize section vec size.
* If the length of the LEB-encoded size changes, this can change
* the byte length of the section and the offset for nodes in the
* section. So we do this first before resizing section byte size
* or shifting following operations' nodes.
*/
if (state.deltaElements !== 0 && sectionName !== "start") {
var oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = (0, _helperWasmSection.resizeSectionVecSize)(ast, state.uint8Buffer, sectionName, state.deltaElements); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
}
/**
* Resize section byte size.
* If the length of the LEB-encoded size changes, this can change
* the offset for nodes in the section. So we do this before
* shifting following operations' nodes.
*/
if (state.deltaBytes !== 0 && sectionName !== "start") {
var _oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = (0, _helperWasmSection.resizeSectionByteSize)(ast, state.uint8Buffer, sectionName, state.deltaBytes); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - _oldBufferLength;
}
/**
* Shift following operation's nodes
*/
if (state.deltaBytes !== 0) {
ops.forEach(function (op) {
// We don't need to handle add ops, they are positioning independent
switch (op.kind) {
case "update":
shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
break;
case "delete":
shiftLocNodeByDelta(op.node, state.deltaBytes);
break;
}
});
}
uint8Buffer = state.uint8Buffer;
});
return uint8Buffer;
}

View File

@ -0,0 +1,134 @@
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.edit = edit;
exports.editWithAST = editWithAST;
exports.add = add;
exports.addWithAST = addWithAST;
var _wasmParser = require("@webassemblyjs/wasm-parser");
var _ast = require("@webassemblyjs/ast");
var _clone = require("@webassemblyjs/ast/lib/clone");
var _wasmOpt = require("@webassemblyjs/wasm-opt");
var _helperWasmBytecode = _interopRequireWildcard(require("@webassemblyjs/helper-wasm-bytecode"));
var _apply = require("./apply");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function hashNode(node) {
return JSON.stringify(node);
}
function preprocess(ab) {
var optBin = (0, _wasmOpt.shrinkPaddedLEB128)(new Uint8Array(ab));
return optBin.buffer;
}
function sortBySectionOrder(nodes) {
var originalOrder = new Map();
var _iterator = _createForOfIteratorHelper(nodes),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var node = _step.value;
originalOrder.set(node, originalOrder.size);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
nodes.sort(function (a, b) {
var sectionA = (0, _helperWasmBytecode.getSectionForNode)(a);
var sectionB = (0, _helperWasmBytecode.getSectionForNode)(b);
var aId = _helperWasmBytecode["default"].sections[sectionA];
var bId = _helperWasmBytecode["default"].sections[sectionB];
if (typeof aId !== "number" || typeof bId !== "number") {
throw new Error("Section id not found");
}
if (aId === bId) {
// $FlowIgnore originalOrder is filled for all nodes
return originalOrder.get(a) - originalOrder.get(b);
}
return aId - bId;
});
}
function edit(ab, visitors) {
ab = preprocess(ab);
var ast = (0, _wasmParser.decode)(ab);
return editWithAST(ast, ab, visitors);
}
function editWithAST(ast, ab, visitors) {
var operations = [];
var uint8Buffer = new Uint8Array(ab);
var nodeBefore;
function before(type, path) {
nodeBefore = (0, _clone.cloneNode)(path.node);
}
function after(type, path) {
if (path.node._deleted === true) {
operations.push({
kind: "delete",
node: path.node
}); // $FlowIgnore
} else if (hashNode(nodeBefore) !== hashNode(path.node)) {
operations.push({
kind: "update",
oldNode: nodeBefore,
node: path.node
});
}
}
(0, _ast.traverse)(ast, visitors, before, after);
uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
return uint8Buffer.buffer;
}
function add(ab, newNodes) {
ab = preprocess(ab);
var ast = (0, _wasmParser.decode)(ab);
return addWithAST(ast, ab, newNodes);
}
function addWithAST(ast, ab, newNodes) {
// Sort nodes by insertion order
sortBySectionOrder(newNodes);
var uint8Buffer = new Uint8Array(ab); // Map node into operations
var operations = newNodes.map(function (n) {
return {
kind: "add",
node: n
};
});
uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
return uint8Buffer.buffer;
}

View File

@ -0,0 +1,33 @@
{
"name": "@webassemblyjs/wasm-edit",
"version": "1.14.1",
"description": "",
"main": "lib/index.js",
"module": "esm/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/xtuc/webassemblyjs.git"
},
"publishConfig": {
"access": "public"
},
"author": "Sven Sauleau",
"license": "MIT",
"dependencies": {
"@webassemblyjs/ast": "1.14.1",
"@webassemblyjs/helper-buffer": "1.14.1",
"@webassemblyjs/helper-wasm-bytecode": "1.13.2",
"@webassemblyjs/helper-wasm-section": "1.14.1",
"@webassemblyjs/wasm-gen": "1.14.1",
"@webassemblyjs/wasm-opt": "1.14.1",
"@webassemblyjs/wasm-parser": "1.14.1",
"@webassemblyjs/wast-printer": "1.14.1"
},
"devDependencies": {
"@webassemblyjs/helper-test-framework": "1.14.1"
},
"gitHead": "25d52b1296e151ac56244a7c3886661e6b4a69ea"
}