@webassemblyjs/helper-wasm-section
Advanced tools
Comparing version 1.2.7 to 1.2.8
@@ -20,3 +20,3 @@ "use strict"; | ||
var debug = require("debug")("wasm"); | ||
var debug = require("debug")("wasm:createsection"); | ||
@@ -58,14 +58,31 @@ function findLastSection(ast, forSection) { | ||
} else { | ||
start = lastSection.startOffset + lastSection.size + 1; | ||
start = lastSection.startOffset + lastSection.size.value + 1; | ||
end = start; | ||
debug("create empty section=%s after=%s start=%d end=%d", section, lastSection.section, start, end); | ||
} | ||
} // section id | ||
var size = 1; // empty vector | ||
var vectorOfSize = 0; | ||
var sectionMetadata = t.sectionMetadata(section, start + 1, // ignore the section id from the AST | ||
size, vectorOfSize); | ||
start += 1; | ||
var sizeStartLoc = { | ||
line: -1, | ||
column: start | ||
}; | ||
var sizeEndLoc = { | ||
line: -1, | ||
column: start + 1 | ||
}; // 1 byte for the empty vector | ||
var size = t.withLoc(t.numberLiteral(1), sizeEndLoc, sizeStartLoc); | ||
var vectorOfSizeStartLoc = { | ||
line: -1, | ||
column: sizeEndLoc.column | ||
}; | ||
var vectorOfSizeEndLoc = { | ||
line: -1, | ||
column: sizeEndLoc.column + 1 | ||
}; | ||
var vectorOfSize = t.withLoc(t.numberLiteral(0), vectorOfSizeEndLoc, vectorOfSizeStartLoc); | ||
var sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize); | ||
var sectionBytes = (0, _wasmGen.encodeNode)(sectionMetadata); | ||
uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, sectionBytes); // Add section into the AST for later lookups | ||
uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start - 1, end, sectionBytes); // Add section into the AST for later lookups | ||
@@ -75,4 +92,25 @@ if (_typeof(ast.body[0].metadata) === "object") { | ||
ast.body[0].metadata.sections.push(sectionMetadata); | ||
t.sortSectionMetadata(ast.body[0]); | ||
} | ||
/** | ||
* Update AST | ||
*/ | ||
// Once we hit our section every that is after needs to be shifted by the delta | ||
var deltaBytes = +sectionBytes.length; | ||
var encounteredSection = false; | ||
t.traverse(ast, { | ||
SectionMetadata: function SectionMetadata(path) { | ||
if (path.node.section === section) { | ||
encounteredSection = true; | ||
return; | ||
} | ||
if (encounteredSection === true) { | ||
path.shift(deltaBytes); | ||
debug("shift section section=%s detla=%d", path.node.section, deltaBytes); | ||
} | ||
} | ||
}); | ||
return { | ||
@@ -79,0 +117,0 @@ uint8Buffer: uint8Buffer, |
@@ -46,2 +46,3 @@ "use strict"; | ||
case "Func": | ||
case "Instr": | ||
return "code"; | ||
@@ -48,0 +49,0 @@ |
@@ -12,2 +12,4 @@ "use strict"; | ||
var debug = require("debug")("wasm:removesection"); | ||
function removeSection(ast, uint8Buffer, section) { | ||
@@ -23,4 +25,24 @@ var sectionMetadata = (0, _ast.getSectionMetadata)(ast, section); | ||
var startsIncludingId = sectionMetadata.startOffset - 1; | ||
var ends = sectionMetadata.startOffset + sectionMetadata.size + 1; | ||
var ends = sectionMetadata.startOffset + sectionMetadata.size.value + 1; | ||
var delta = -(ends - startsIncludingId); | ||
/** | ||
* update AST | ||
*/ | ||
// Once we hit our section every that is after needs to be shifted by the delta | ||
var encounteredSection = false; | ||
(0, _ast.traverse)(ast, { | ||
SectionMetadata: function SectionMetadata(path) { | ||
if (path.node.section === section) { | ||
encounteredSection = true; | ||
return path.remove(); | ||
} | ||
if (encounteredSection === true) { | ||
path.shift(delta); | ||
debug("shift section section=%s detla=%d", section, delta); | ||
} | ||
} | ||
}); | ||
return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, startsIncludingId, ends, replacement); | ||
} |
@@ -15,2 +15,4 @@ "use strict"; | ||
var debug = require("debug")("wasm:resizesection"); | ||
function resizeSectionByteSize(ast, uint8Buffer, section, deltaBytes) { | ||
@@ -21,10 +23,47 @@ var sectionMetadata = (0, _ast.getSectionMetadata)(ast, section); | ||
throw new Error("Section metadata not found"); | ||
} // Encode the current value to know the number of bytes to override | ||
} | ||
if (typeof sectionMetadata.size.loc === "undefined") { | ||
throw new Error("SectionMetadata " + section + " has no loc"); | ||
} // keep old node location to be overriden | ||
var oldSizeInBytes = (0, _wasmGen.encodeU32)(sectionMetadata.size); | ||
var newSectionSize = sectionMetadata.size + deltaBytes; // Update AST | ||
sectionMetadata.size = newSectionSize; | ||
return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, sectionMetadata.startOffset, sectionMetadata.startOffset + oldSizeInBytes.length, (0, _wasmGen.encodeU32)(newSectionSize)); | ||
var start = sectionMetadata.size.loc.start.column; | ||
var end = sectionMetadata.size.loc.end.column; | ||
var newSectionSize = sectionMetadata.size.value + deltaBytes; | ||
var newBytes = (0, _wasmGen.encodeU32)(newSectionSize); | ||
/** | ||
* update AST | ||
*/ | ||
sectionMetadata.size.value = newSectionSize; | ||
var oldu32EncodedLen = end - start; | ||
var newu32EncodedLen = newBytes.length; // the new u32 has a different encoded length | ||
if (newu32EncodedLen !== oldu32EncodedLen) { | ||
var deltaInSizeEncoding = newu32EncodedLen - oldu32EncodedLen; | ||
sectionMetadata.size.loc.end.column = start + newu32EncodedLen; | ||
deltaBytes += deltaInSizeEncoding; // move the vec size pointer size the section size is now smaller | ||
sectionMetadata.vectorOfSize.loc.start.column += deltaInSizeEncoding; | ||
sectionMetadata.vectorOfSize.loc.end.column += deltaInSizeEncoding; | ||
} // Once we hit our section every that is after needs to be shifted by the delta | ||
var encounteredSection = false; | ||
(0, _ast.traverse)(ast, { | ||
SectionMetadata: function SectionMetadata(path) { | ||
if (path.node.section === section) { | ||
encounteredSection = true; | ||
return; | ||
} | ||
if (encounteredSection === true) { | ||
path.shift(deltaBytes); | ||
debug("shift section section=%s detla=%d", path.node.section, deltaBytes); | ||
} | ||
} | ||
}); | ||
debug("resize byte size section=%s newValue=%s start=%d end=%d", section, newSectionSize, start, end); | ||
return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newBytes); | ||
} | ||
@@ -37,17 +76,23 @@ | ||
throw new Error("Section metadata not found"); | ||
} | ||
if (typeof sectionMetadata.vectorOfSize.loc === "undefined") { | ||
throw new Error("SectionMetadata " + section + " has no loc"); | ||
} // Section has no vector | ||
if (sectionMetadata.vectorOfSize === -1) { | ||
if (sectionMetadata.vectorOfSize.value === -1) { | ||
return uint8Buffer; | ||
} // Encode the current value to know the number of bytes to override | ||
} // keep old node location to be overriden | ||
var oldSizeInBytes = (0, _wasmGen.encodeU32)(sectionMetadata.vectorOfSize); | ||
var newValue = sectionMetadata.vectorOfSize + deltaElements; | ||
var newBytes = (0, _wasmGen.encodeU32)(newValue); | ||
var start = sectionMetadata.startOffset + 1; // Update AST | ||
var start = sectionMetadata.vectorOfSize.loc.start.column; | ||
var end = sectionMetadata.vectorOfSize.loc.end.column; | ||
var newValue = sectionMetadata.vectorOfSize.value + deltaElements; | ||
var newBytes = (0, _wasmGen.encodeU32)(newValue); // Update AST | ||
sectionMetadata.vectorOfSize = newValue; | ||
return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, start + oldSizeInBytes.length, newBytes); | ||
sectionMetadata.vectorOfSize.value = newValue; | ||
sectionMetadata.vectorOfSize.loc.end.column = start + newBytes.length; | ||
debug("resize vec size section=%s detla=%d newValue=%s", section, deltaElements, newValue); | ||
return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newBytes); | ||
} |
{ | ||
"name": "@webassemblyjs/helper-wasm-section", | ||
"version": "1.2.7", | ||
"version": "1.2.8", | ||
"description": "", | ||
@@ -19,10 +19,10 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@webassemblyjs/ast": "1.2.7", | ||
"@webassemblyjs/helper-buffer": "1.2.7", | ||
"@webassemblyjs/helper-wasm-bytecode": "1.2.7", | ||
"@webassemblyjs/wasm-gen": "1.2.7" | ||
"@webassemblyjs/ast": "1.2.8", | ||
"@webassemblyjs/helper-buffer": "1.2.8", | ||
"@webassemblyjs/helper-wasm-bytecode": "1.2.8", | ||
"@webassemblyjs/wasm-gen": "1.2.8" | ||
}, | ||
"devDependencies": { | ||
"@webassemblyjs/wasm-parser": "1.2.7" | ||
"@webassemblyjs/wasm-parser": "1.2.8" | ||
} | ||
} |
@@ -8,3 +8,3 @@ // @flow | ||
const t = require("@webassemblyjs/ast"); | ||
const debug = require("debug")("wasm"); | ||
const debug = require("debug")("wasm:createsection"); | ||
@@ -57,3 +57,3 @@ type Res = { uint8Buffer: Uint8Array, sectionMetadata: SectionMetadata }; | ||
} else { | ||
start = lastSection.startOffset + lastSection.size + 1; | ||
start = lastSection.startOffset + lastSection.size.value + 1; | ||
end = start; | ||
@@ -70,15 +70,30 @@ | ||
const size = 1; // empty vector | ||
const vectorOfSize = 0; | ||
// section id | ||
start += 1; | ||
const sectionMetadata = t.sectionMetadata( | ||
section, | ||
start + 1, // ignore the section id from the AST | ||
size, | ||
vectorOfSize | ||
const sizeStartLoc = { line: -1, column: start }; | ||
const sizeEndLoc = { line: -1, column: start + 1 }; | ||
// 1 byte for the empty vector | ||
const size = t.withLoc(t.numberLiteral(1), sizeEndLoc, sizeStartLoc); | ||
const vectorOfSizeStartLoc = { line: -1, column: sizeEndLoc.column }; | ||
const vectorOfSizeEndLoc = { line: -1, column: sizeEndLoc.column + 1 }; | ||
const vectorOfSize = t.withLoc( | ||
t.numberLiteral(0), | ||
vectorOfSizeEndLoc, | ||
vectorOfSizeStartLoc | ||
); | ||
const sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize); | ||
const sectionBytes = encodeNode(sectionMetadata); | ||
uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, sectionBytes); | ||
uint8Buffer = overrideBytesInBuffer( | ||
uint8Buffer, | ||
start - 1, | ||
end, | ||
sectionBytes | ||
); | ||
@@ -89,5 +104,33 @@ // Add section into the AST for later lookups | ||
ast.body[0].metadata.sections.push(sectionMetadata); | ||
t.sortSectionMetadata(ast.body[0]); | ||
} | ||
/** | ||
* Update AST | ||
*/ | ||
// Once we hit our section every that is after needs to be shifted by the delta | ||
const deltaBytes = +sectionBytes.length; | ||
let encounteredSection = false; | ||
t.traverse(ast, { | ||
SectionMetadata(path) { | ||
if (path.node.section === section) { | ||
encounteredSection = true; | ||
return; | ||
} | ||
if (encounteredSection === true) { | ||
path.shift(deltaBytes); | ||
debug( | ||
"shift section section=%s detla=%d", | ||
path.node.section, | ||
deltaBytes | ||
); | ||
} | ||
} | ||
}); | ||
return { uint8Buffer, sectionMetadata }; | ||
} |
@@ -15,2 +15,3 @@ // @flow | ||
case "Func": | ||
case "Instr": | ||
return "code"; | ||
@@ -17,0 +18,0 @@ |
// @flow | ||
import { getSectionMetadata } from "@webassemblyjs/ast"; | ||
import { traverse, getSectionMetadata } from "@webassemblyjs/ast"; | ||
import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer"; | ||
const debug = require("debug")("wasm:removesection"); | ||
export function removeSection( | ||
@@ -21,4 +23,28 @@ ast: Program, | ||
const startsIncludingId = sectionMetadata.startOffset - 1; | ||
const ends = sectionMetadata.startOffset + sectionMetadata.size + 1; | ||
const ends = sectionMetadata.startOffset + sectionMetadata.size.value + 1; | ||
const delta = -(ends - startsIncludingId); | ||
/** | ||
* update AST | ||
*/ | ||
// Once we hit our section every that is after needs to be shifted by the delta | ||
let encounteredSection = false; | ||
traverse(ast, { | ||
SectionMetadata(path) { | ||
if (path.node.section === section) { | ||
encounteredSection = true; | ||
return path.remove(); | ||
} | ||
if (encounteredSection === true) { | ||
path.shift(delta); | ||
debug("shift section section=%s detla=%d", section, delta); | ||
} | ||
} | ||
}); | ||
return overrideBytesInBuffer( | ||
@@ -25,0 +51,0 @@ uint8Buffer, |
// @flow | ||
import { encodeU32 } from "@webassemblyjs/wasm-gen"; | ||
import { getSectionMetadata } from "@webassemblyjs/ast"; | ||
import { getSectionMetadata, traverse } from "@webassemblyjs/ast"; | ||
import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer"; | ||
const debug = require("debug")("wasm:resizesection"); | ||
export function resizeSectionByteSize( | ||
@@ -19,16 +21,65 @@ ast: Program, | ||
// Encode the current value to know the number of bytes to override | ||
const oldSizeInBytes = encodeU32(sectionMetadata.size); | ||
if (typeof sectionMetadata.size.loc === "undefined") { | ||
throw new Error("SectionMetadata " + section + " has no loc"); | ||
} | ||
const newSectionSize = sectionMetadata.size + deltaBytes; | ||
// keep old node location to be overriden | ||
const start = sectionMetadata.size.loc.start.column; | ||
const end = sectionMetadata.size.loc.end.column; | ||
// Update AST | ||
sectionMetadata.size = newSectionSize; | ||
const newSectionSize = sectionMetadata.size.value + deltaBytes; | ||
const newBytes = encodeU32(newSectionSize); | ||
return overrideBytesInBuffer( | ||
uint8Buffer, | ||
sectionMetadata.startOffset, | ||
sectionMetadata.startOffset + oldSizeInBytes.length, | ||
encodeU32(newSectionSize) | ||
/** | ||
* update AST | ||
*/ | ||
sectionMetadata.size.value = newSectionSize; | ||
const oldu32EncodedLen = end - start; | ||
const newu32EncodedLen = newBytes.length; | ||
// the new u32 has a different encoded length | ||
if (newu32EncodedLen !== oldu32EncodedLen) { | ||
const deltaInSizeEncoding = newu32EncodedLen - oldu32EncodedLen; | ||
sectionMetadata.size.loc.end.column = start + newu32EncodedLen; | ||
deltaBytes += deltaInSizeEncoding; | ||
// move the vec size pointer size the section size is now smaller | ||
sectionMetadata.vectorOfSize.loc.start.column += deltaInSizeEncoding; | ||
sectionMetadata.vectorOfSize.loc.end.column += deltaInSizeEncoding; | ||
} | ||
// Once we hit our section every that is after needs to be shifted by the delta | ||
let encounteredSection = false; | ||
traverse(ast, { | ||
SectionMetadata(path) { | ||
if (path.node.section === section) { | ||
encounteredSection = true; | ||
return; | ||
} | ||
if (encounteredSection === true) { | ||
path.shift(deltaBytes); | ||
debug( | ||
"shift section section=%s detla=%d", | ||
path.node.section, | ||
deltaBytes | ||
); | ||
} | ||
} | ||
}); | ||
debug( | ||
"resize byte size section=%s newValue=%s start=%d end=%d", | ||
section, | ||
newSectionSize, | ||
start, | ||
end | ||
); | ||
return overrideBytesInBuffer(uint8Buffer, start, end, newBytes); | ||
} | ||
@@ -48,24 +99,30 @@ | ||
if (typeof sectionMetadata.vectorOfSize.loc === "undefined") { | ||
throw new Error("SectionMetadata " + section + " has no loc"); | ||
} | ||
// Section has no vector | ||
if (sectionMetadata.vectorOfSize === -1) { | ||
if (sectionMetadata.vectorOfSize.value === -1) { | ||
return uint8Buffer; | ||
} | ||
// Encode the current value to know the number of bytes to override | ||
const oldSizeInBytes = encodeU32(sectionMetadata.vectorOfSize); | ||
// keep old node location to be overriden | ||
const start = sectionMetadata.vectorOfSize.loc.start.column; | ||
const end = sectionMetadata.vectorOfSize.loc.end.column; | ||
const newValue = sectionMetadata.vectorOfSize + deltaElements; | ||
const newValue = sectionMetadata.vectorOfSize.value + deltaElements; | ||
const newBytes = encodeU32(newValue); | ||
const start = sectionMetadata.startOffset + 1; | ||
// Update AST | ||
sectionMetadata.vectorOfSize = newValue; | ||
sectionMetadata.vectorOfSize.value = newValue; | ||
sectionMetadata.vectorOfSize.loc.end.column = start + newBytes.length; | ||
return overrideBytesInBuffer( | ||
uint8Buffer, | ||
start, | ||
start + oldSizeInBytes.length, | ||
newBytes | ||
debug( | ||
"resize vec size section=%s detla=%d newValue=%s", | ||
section, | ||
deltaElements, | ||
newValue | ||
); | ||
return overrideBytesInBuffer(uint8Buffer, start, end, newBytes); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19331
526
+ Added@webassemblyjs/ast@1.2.8(transitive)
+ Added@webassemblyjs/floating-point-hex-parser@1.2.8(transitive)
+ Added@webassemblyjs/helper-buffer@1.2.8(transitive)
+ Added@webassemblyjs/helper-code-frame@1.2.8(transitive)
+ Added@webassemblyjs/helper-fsm@1.2.8(transitive)
+ Added@webassemblyjs/helper-wasm-bytecode@1.2.8(transitive)
+ Added@webassemblyjs/leb128@1.2.8(transitive)
+ Added@webassemblyjs/validation@1.2.8(transitive)
+ Added@webassemblyjs/wasm-gen@1.2.8(transitive)
+ Added@webassemblyjs/wasm-parser@1.2.8(transitive)
+ Added@webassemblyjs/wast-parser@1.2.8(transitive)
+ Added@webassemblyjs/wast-printer@1.2.8(transitive)
+ Addedwebassemblyjs@1.2.8(transitive)
- Removed@webassemblyjs/ast@1.2.7(transitive)
- Removed@webassemblyjs/floating-point-hex-parser@1.2.7(transitive)
- Removed@webassemblyjs/helper-buffer@1.2.7(transitive)
- Removed@webassemblyjs/helper-code-frame@1.2.7(transitive)
- Removed@webassemblyjs/helper-fsm@1.2.7(transitive)
- Removed@webassemblyjs/helper-wasm-bytecode@1.2.7(transitive)
- Removed@webassemblyjs/leb128@1.2.7(transitive)
- Removed@webassemblyjs/validation@1.2.7(transitive)
- Removed@webassemblyjs/wasm-gen@1.2.7(transitive)
- Removed@webassemblyjs/wasm-parser@1.2.7(transitive)
- Removed@webassemblyjs/wast-parser@1.2.7(transitive)
- Removed@webassemblyjs/wast-printer@1.2.7(transitive)
- Removedwebassemblyjs@1.2.7(transitive)
Updated@webassemblyjs/ast@1.2.8