@webassemblyjs/ast
Advanced tools
Comparing version 1.3.3 to 1.4.0
@@ -73,2 +73,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "isInstruction", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.isInstruction; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getSectionMetadata", { | ||
@@ -86,2 +92,32 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(exports, "orderedInsertNode", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.orderedInsertNode; | ||
} | ||
}); | ||
Object.defineProperty(exports, "assertHasLoc", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.assertHasLoc; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getEndOfSection", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.getEndOfSection; | ||
} | ||
}); | ||
Object.defineProperty(exports, "shiftSection", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.shiftSection; | ||
} | ||
}); | ||
Object.defineProperty(exports, "shiftLoc", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.shiftLoc; | ||
} | ||
}); | ||
Object.defineProperty(exports, "cloneNode", { | ||
@@ -259,3 +295,3 @@ enumerable: true, | ||
function moduleExport(name, type, id) { | ||
function moduleExport(name, exportType, id) { | ||
return { | ||
@@ -265,3 +301,4 @@ type: "ModuleExport", | ||
descr: { | ||
type: type, | ||
type: "ModuleExportDescr", | ||
exportType: exportType, | ||
id: id | ||
@@ -268,0 +305,0 @@ } |
@@ -9,34 +9,4 @@ "use strict"; | ||
function _typeof(obj) { 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); } | ||
var debug = require("debug")("ast:traverse"); | ||
var debug = require("debug")("wasm:traverse"); | ||
function _shift(node, delta) { | ||
if (node.type === "SectionMetadata") { | ||
node.startOffset += delta; | ||
if (_typeof(node.size.loc) === "object") { | ||
// $FlowIgnore | ||
node.size.loc.start.column += delta; // $FlowIgnore | ||
node.size.loc.end.column += delta; | ||
} | ||
if (_typeof(node.vectorOfSize.loc) === "object") { | ||
// $FlowIgnore | ||
node.vectorOfSize.loc.start.column += delta; // $FlowIgnore | ||
node.vectorOfSize.loc.end.column += delta; | ||
} | ||
debug("shifted %s startOffset=%d", node.type, node.startOffset); | ||
} else { | ||
// // $FlowIgnore | ||
// node.loc.start.column += delta; | ||
// // $FlowIgnore | ||
// node.loc.end.column += delta; | ||
throw new Error("Can not shift node " + JSON.stringify(node.type)); | ||
} | ||
} | ||
function removeNodeInBody(node, fromNode) { | ||
@@ -95,256 +65,30 @@ switch (fromNode.type) { | ||
replaceWith: replaceWith, | ||
remove: remove, | ||
shift: function shift(delta) { | ||
return _shift(node, delta); | ||
} | ||
remove: remove | ||
}; | ||
} | ||
} // recursively walks the AST starting at the given node. The callback is invoked for | ||
// and object that has a 'type' property. | ||
function walk(n, cb, parentPath) { | ||
if (n._deleted === true) { | ||
function walk(node, callback, parentPath) { | ||
if (node._deleted === true) { | ||
return; | ||
} | ||
switch (n.type) { | ||
case "Program": | ||
{ | ||
var _path = createPath(n, parentPath); | ||
var path = createPath(node, parentPath); // $FlowIgnore | ||
cb(n.type, _path); | ||
n.body.forEach(function (x) { | ||
return walk(x, cb, _path); | ||
}); | ||
break; | ||
} | ||
callback(node.type, path); | ||
Object.keys(node).forEach(function (prop) { | ||
var value = node[prop]; | ||
case "SectionMetadata": | ||
case "FunctionNameMetadata": | ||
case "ModuleNameMetadata": | ||
case "LocalNameMetadata": | ||
case "Data": | ||
case "Memory": | ||
case "Elem": | ||
case "FuncImportDescr": | ||
case "GlobalType": | ||
case "NumberLiteral": | ||
case "ValtypeLiteral": | ||
case "FloatLiteral": | ||
case "StringLiteral": | ||
case "QuoteModule": | ||
case "LongNumberLiteral": | ||
case "BinaryModule": | ||
case "LeadingComment": | ||
case "BlockComment": | ||
case "Identifier": | ||
{ | ||
cb(n.type, createPath(n, parentPath)); | ||
break; | ||
} | ||
if (value === null || value === undefined) { | ||
return; | ||
} | ||
case "ModuleExport": | ||
{ | ||
var _path2 = createPath(n, parentPath); | ||
cb(n.type, _path2); | ||
walk(n.descr.id, cb, _path2); | ||
break; | ||
var valueAsArray = Array.isArray(value) ? value : [value]; | ||
valueAsArray.forEach(function (v) { | ||
if (typeof v.type === "string") { | ||
walk(v, callback, path); | ||
} | ||
case "ModuleMetadata": | ||
{ | ||
var _path3 = createPath(n, parentPath); | ||
cb(n.type, _path3); | ||
n.sections.forEach(function (x) { | ||
return walk(x, cb, _path3); | ||
}); | ||
if (typeof n.functionNames !== "undefined") { | ||
// $FlowIgnore | ||
n.functionNames.forEach(function (x) { | ||
return walk(x, cb, _path3); | ||
}); | ||
} | ||
if (typeof n.localNames !== "undefined") { | ||
// $FlowIgnore | ||
n.localNames.forEach(function (x) { | ||
return walk(x, cb, _path3); | ||
}); | ||
} | ||
break; | ||
} | ||
case "Module": | ||
{ | ||
var _path4 = createPath(n, parentPath); | ||
cb(n.type, _path4); | ||
if (typeof n.fields !== "undefined") { | ||
n.fields.forEach(function (x) { | ||
return walk(x, cb, _path4); | ||
}); | ||
} | ||
if (typeof n.metadata !== "undefined") { | ||
// $FlowIgnore | ||
walk(n.metadata, cb, _path4); | ||
} | ||
break; | ||
} | ||
case "Start": | ||
case "CallInstruction": | ||
{ | ||
var _path5 = createPath(n, parentPath); // $FlowIgnore | ||
cb(n.type, _path5); // $FlowIgnore | ||
walk(n.index, cb, _path5); | ||
break; | ||
} | ||
case "CallIndirectInstruction": | ||
{ | ||
var _path6 = createPath(n, parentPath); // $FlowIgnore | ||
cb(n.type, _path6); | ||
if (n.index != null) { | ||
// $FlowIgnore | ||
walk(n.index, cb, _path6); | ||
} | ||
break; | ||
} | ||
case "ModuleImport": | ||
{ | ||
cb(n.type, createPath(n, parentPath)); | ||
if (n.descr != null) { | ||
// $FlowIgnore | ||
walk(n.descr, cb, createPath(n, parentPath)); | ||
} | ||
break; | ||
} | ||
case "Table": | ||
case "Global": | ||
{ | ||
var _path7 = createPath(n, parentPath); | ||
cb(n.type, _path7); | ||
if (n.name != null) { | ||
walk(n.name, cb, _path7); | ||
} | ||
if (n.init != null) { | ||
// $FlowIgnore | ||
n.init.forEach(function (x) { | ||
return walk(x, cb, _path7); | ||
}); | ||
} | ||
break; | ||
} | ||
case "TypeInstruction": | ||
{ | ||
var _path8 = createPath(n, parentPath); | ||
cb(n.type, _path8); | ||
if (n.id != null) { | ||
walk(n.id, cb, _path8); | ||
} | ||
break; | ||
} | ||
case "IfInstruction": | ||
{ | ||
var _path9 = createPath(n, parentPath); // $FlowIgnore | ||
cb(n.type, _path9); // $FlowIgnore | ||
n.test.forEach(function (x) { | ||
return walk(x, cb, _path9); | ||
}); // $FlowIgnore | ||
n.consequent.forEach(function (x) { | ||
return walk(x, cb, _path9); | ||
}); // $FlowIgnore | ||
n.alternate.forEach(function (x) { | ||
return walk(x, cb, _path9); | ||
}); // $FlowIgnore | ||
walk(n.testLabel, cb, _path9); | ||
break; | ||
} | ||
case "Instr": | ||
{ | ||
var _path10 = createPath(n, parentPath); // $FlowIgnore | ||
cb(n.type, _path10); // $FlowIgnore | ||
if (_typeof(n.args) === "object") { | ||
n.args.forEach(function (x) { | ||
return walk(x, cb, _path10); | ||
}); | ||
} | ||
break; | ||
} | ||
case "BlockInstruction": | ||
case "LoopInstruction": | ||
{ | ||
var _path11 = createPath(n, parentPath); // $FlowIgnore | ||
cb(n.type, _path11); | ||
if (n.label != null) { | ||
// $FlowIgnore | ||
walk(n.label, cb, _path11); | ||
} // $FlowIgnore | ||
n.instr.forEach(function (x) { | ||
return walk(x, cb, _path11); | ||
}); | ||
break; | ||
} | ||
case "Func": | ||
{ | ||
var _path12 = createPath(n, parentPath); | ||
cb(n.type, _path12); | ||
n.body.forEach(function (x) { | ||
return walk(x, cb, _path12); | ||
}); | ||
if (n.name != null) { | ||
walk(n.name, cb, _path12); | ||
} | ||
break; | ||
} | ||
default: | ||
throw new Error("Unknown node encounter of type: " + JSON.stringify(n.type)); | ||
} | ||
}); | ||
}); | ||
} | ||
@@ -351,0 +95,0 @@ |
@@ -8,2 +8,8 @@ "use strict"; | ||
exports.sortSectionMetadata = sortSectionMetadata; | ||
exports.isInstruction = isInstruction; | ||
exports.orderedInsertNode = orderedInsertNode; | ||
exports.assertHasLoc = assertHasLoc; | ||
exports.getEndOfSection = getEndOfSection; | ||
exports.shiftLoc = shiftLoc; | ||
exports.shiftSection = shiftSection; | ||
@@ -16,2 +22,6 @@ var _traverse = require("./traverse"); | ||
function _typeof(obj) { 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); } | ||
var debug = require("debug")("ast:utils"); | ||
function getSectionMetadata(ast, name) { | ||
@@ -58,2 +68,88 @@ var section; | ||
}); | ||
} | ||
function isInstruction(n) { | ||
return n.type === "Instr" || n.type === "CallInstruction" || n.type === "CallIndirectInstruction" || n.type === "BlockInstruction" || n.type === "LoopInstruction" || n.type === "IfInstruction"; | ||
} | ||
function orderedInsertNode(m, n) { | ||
assertHasLoc(n); | ||
var didInsert = false; | ||
if (n.type === "ModuleExport") { | ||
m.fields.push(n); | ||
return; | ||
} | ||
m.fields = m.fields.reduce(function (acc, field) { | ||
var fieldEndCol = Infinity; | ||
if (field.loc != null) { | ||
// $FlowIgnore | ||
fieldEndCol = field.loc.end.column; | ||
} // $FlowIgnore: assertHasLoc ensures that | ||
if (didInsert === false && n.loc.start.column < fieldEndCol) { | ||
didInsert = true; | ||
acc.push(n); | ||
} | ||
acc.push(field); | ||
return acc; | ||
}, []); // Handles empty modules or n is the last element | ||
if (didInsert === false) { | ||
m.fields.push(n); | ||
} | ||
} | ||
function assertHasLoc(n) { | ||
if (n.loc == null || n.loc.start == null || n.loc.end == null) { | ||
throw new Error("Internal failure: node (".concat(JSON.stringify(n.type), ") has no location information")); | ||
} | ||
} | ||
function getEndOfSection(s) { | ||
assertHasLoc(s.size); | ||
return s.startOffset + s.size.value + ( // $FlowIgnore | ||
s.size.loc.end.column - s.size.loc.start.column); | ||
} | ||
function shiftLoc(node, delta) { | ||
// $FlowIgnore | ||
node.loc.start.column += delta; // $FlowIgnore | ||
node.loc.end.column += delta; | ||
} | ||
function shiftSection(ast, node, delta) { | ||
if (node.type !== "SectionMetadata") { | ||
throw new Error("Can not shift node " + JSON.stringify(node.type)); | ||
} | ||
node.startOffset += delta; | ||
if (_typeof(node.size.loc) === "object") { | ||
shiftLoc(node.size, delta); | ||
} | ||
if (_typeof(node.vectorOfSize.loc) === "object") { | ||
shiftLoc(node.vectorOfSize, delta); | ||
} | ||
debug("shifted %s startOffset=%d", node.type, node.startOffset); | ||
var sectionName = node.section; // shift node locations within that section | ||
(0, _traverse.traverse)(ast, { | ||
Node: function Node(_ref2) { | ||
var node = _ref2.node; | ||
var section = _helperWasmBytecode.default.getSectionForNode(node); | ||
if (section === sectionName && _typeof(node.loc) === "object") { | ||
shiftLoc(node, delta); | ||
} | ||
} | ||
}); | ||
} |
{ | ||
"name": "@webassemblyjs/ast", | ||
"version": "1.3.3", | ||
"version": "1.4.0", | ||
"description": "AST utils for webassemblyjs", | ||
@@ -14,5 +14,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@webassemblyjs/helper-wasm-bytecode": "1.3.3", | ||
"@webassemblyjs/wast-parser": "1.3.3", | ||
"webassemblyjs": "1.3.3" | ||
"@webassemblyjs/helper-wasm-bytecode": "1.4.0", | ||
"@webassemblyjs/wast-parser": "1.4.0", | ||
"webassemblyjs": "1.4.0" | ||
}, | ||
@@ -19,0 +19,0 @@ "repository": { |
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
40870
1279
+ Added@webassemblyjs/floating-point-hex-parser@1.4.0(transitive)
+ Added@webassemblyjs/helper-code-frame@1.4.0(transitive)
+ Added@webassemblyjs/helper-fsm@1.4.0(transitive)
+ Added@webassemblyjs/helper-wasm-bytecode@1.4.0(transitive)
+ Added@webassemblyjs/leb128@1.4.0(transitive)
+ Added@webassemblyjs/validation@1.4.0(transitive)
+ Added@webassemblyjs/wasm-parser@1.4.0(transitive)
+ Added@webassemblyjs/wast-parser@1.4.0(transitive)
+ Added@webassemblyjs/wast-printer@1.4.0(transitive)
+ Addedwebassemblyjs@1.4.0(transitive)
- Removed@webassemblyjs/floating-point-hex-parser@1.3.3(transitive)
- Removed@webassemblyjs/helper-code-frame@1.3.3(transitive)
- Removed@webassemblyjs/helper-fsm@1.3.3(transitive)
- Removed@webassemblyjs/helper-wasm-bytecode@1.3.3(transitive)
- Removed@webassemblyjs/leb128@1.3.3(transitive)
- Removed@webassemblyjs/validation@1.3.3(transitive)
- Removed@webassemblyjs/wasm-parser@1.3.3(transitive)
- Removed@webassemblyjs/wast-parser@1.3.3(transitive)
- Removed@webassemblyjs/wast-printer@1.3.3(transitive)
- Removedwebassemblyjs@1.3.3(transitive)
Updatedwebassemblyjs@1.4.0