Socket
Socket
Sign inDemoInstall

@webassemblyjs/ast

Package Overview
Dependencies
20
Maintainers
1
Versions
86
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

lib/utils.js

36

lib/index.js

@@ -12,2 +12,3 @@ "use strict";

exports.module = _module;
exports.sectionMetadata = sectionMetadata;
exports.binaryModule = binaryModule;

@@ -50,2 +51,8 @@ exports.quoteModule = quoteModule;

});
Object.defineProperty(exports, "traverseWithHooks", {
enumerable: true,
get: function () {
return _traverse.traverseWithHooks;
}
});
Object.defineProperty(exports, "signatures", {

@@ -57,2 +64,8 @@ enumerable: true,

});
Object.defineProperty(exports, "getSectionMetadata", {
enumerable: true,
get: function () {
return _utils.getSectionMetadata;
}
});

@@ -63,2 +76,4 @@ var _traverse = require("./traverse");

var _utils = require("./utils");
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); }

@@ -130,3 +145,3 @@

function _module(id, fields) {
function _module(id, fields, metadata) {
if (id != null) {

@@ -137,3 +152,3 @@ assert(typeof id === "string");

assert(_typeof(fields) === "object" && typeof fields.length !== "undefined");
return {
var n = {
type: "Module",

@@ -143,4 +158,21 @@ id: id,

};
if (typeof metadata !== "undefined") {
n.metadata = metadata;
}
return n;
}
function sectionMetadata(section, startOffset, size) {
var vectorOfSize = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
return {
type: "SectionMetadata",
section: section,
startOffset: startOffset,
size: size,
vectorOfSize: vectorOfSize
};
}
function binaryModule(id, blob) {

@@ -147,0 +179,0 @@ return {

301

lib/traverse.js

@@ -7,173 +7,232 @@ "use strict";

exports.traverse = traverse;
exports.traverseWithHooks = traverseWithHooks;
function createPath(node) {
return {
node: node
};
}
function removeNodeInBody(node, fromNode) {
switch (fromNode.type) {
case "Module":
fromNode.fields = fromNode.fields.filter(function (n) {
return n !== node;
});
break;
function walk(n, cb) {
if (n.type === "Program") {
cb(n.type, createPath(n)); // $FlowIgnore
case "Program":
case "Func":
fromNode.body = fromNode.body.filter(function (n) {
return n !== node;
});
break;
n.body.forEach(function (x) {
return walk(x, cb);
});
default:
throw new Error("Unsupported operation: removing node of type: " + String(fromNode.type));
}
}
if (n.type === "Module") {
cb(n.type, createPath(n));
if (typeof n.fields !== "undefined") {
// $FlowIgnore
n.fields.forEach(function (x) {
return walk(x, cb);
});
function createPath(node, parentPath) {
function remove() {
if (parentPath == null) {
throw new Error("Can not remove root node");
}
}
if (n.type === "ModuleExport") {
cb(n.type, createPath(n));
}
var parentNode = parentPath.node;
removeNodeInBody(node, parentNode);
node._deleted = true;
} // TODO(sven): do it the good way, changing the node from the parent
if (n.type === "Start") {
cb(n.type, createPath(n)); // $FlowIgnore
walk(n.index, cb);
function replaceWith(newNode) {
// $FlowIgnore
Object.assign(node, newNode);
}
if (n.type === "Data") {
cb(n.type, createPath(n));
}
return {
node: node,
parentPath: parentPath,
replaceWith: replaceWith,
remove: remove
};
}
if (n.type === "Identifier") {
cb(n.type, createPath(n));
function walk(n, cb, parentPath) {
if (n._deleted === true) {
return;
}
if (n.type === "ModuleImport") {
cb(n.type, createPath(n)); // $FlowIgnore
switch (n.type) {
case "Program":
{
var _path = createPath(n, parentPath);
cb(n.descr.type, createPath(n.descr));
}
cb(n.type, _path);
n.body.forEach(function (x) {
return walk(x, cb, _path);
});
break;
}
if (n.type === "Global") {
cb(n.type, createPath(n));
case "SectionMetadata":
case "ModuleExport":
case "Data":
case "Memory":
case "Elem":
case "NumberLiteral":
case "FloatLiteral":
case "BinaryModule":
case "LeadingComment":
case "BlockComment":
case "Identifier":
{
cb(n.type, createPath(n, parentPath));
break;
}
if (n.name != null) {
// $FlowIgnore
walk(n.name, cb);
}
}
case "Module":
{
var _path2 = createPath(n, parentPath);
if (n.type === "Table") {
cb(n.type, createPath(n));
cb(n.type, _path2);
if (n.name != null) {
// $FlowIgnore
walk(n.name, cb);
}
}
if (typeof n.fields !== "undefined") {
n.fields.forEach(function (x) {
return walk(x, cb, _path2);
});
}
if (n.type === "IfInstruction") {
cb(n.type, createPath(n)); // $FlowIgnore
if (typeof n.metadata !== "undefined") {
// $FlowIgnore
n.metadata.sections.forEach(function (x) {
return walk(x, cb, _path2);
});
}
n.test.forEach(function (x) {
return walk(x, cb);
}); // $FlowIgnore
break;
}
walk(n.testLabel, cb); // $FlowIgnore
case "Start":
case "CallInstruction":
{
var _path3 = createPath(n, parentPath); // $FlowIgnore
n.consequent.forEach(function (x) {
return walk(x, cb);
}); // $FlowIgnore
n.alternate.forEach(function (x) {
return walk(x, cb);
});
}
cb(n.type, _path3); // $FlowIgnore
if (n.type === "Memory") {
cb(n.type, createPath(n));
}
walk(n.index, cb, _path3);
break;
}
if (n.type === "Elem") {
cb(n.type, createPath(n));
}
case "ModuleImport":
{
cb(n.type, createPath(n, parentPath));
cb(n.descr.type, createPath(n.descr, parentPath));
break;
}
if (n.type === "Instr") {
cb(n.type, createPath(n)); // $FlowIgnore
case "Table":
case "Global":
{
var _path4 = createPath(n, parentPath);
n.args.forEach(function (x) {
return walk(x, cb);
});
}
cb(n.type, _path4);
if (n.type === "CallInstruction") {
cb(n.type, createPath(n)); // $FlowIgnore
if (n.name != null) {
walk(n.name, cb, _path4);
}
walk(n.index, cb);
}
break;
}
if (n.type === "LoopInstruction") {
cb(n.type, createPath(n));
case "IfInstruction":
{
var _path5 = createPath(n, parentPath); // $FlowIgnore
if (n.label != null) {
// $FlowIgnore
walk(n.label, cb);
} // $FlowIgnore
cb(n.type, _path5); // $FlowIgnore
n.instr.forEach(function (x) {
return walk(x, cb);
});
}
n.test.forEach(function (x) {
return walk(x, cb, _path5);
}); // $FlowIgnore
if (n.type === "BlockInstruction") {
cb(n.type, createPath(n));
n.consequent.forEach(function (x) {
return walk(x, cb, _path5);
}); // $FlowIgnore
if (n.label != null) {
// $FlowIgnore
walk(n.label, cb);
} // $FlowIgnore
n.alternate.forEach(function (x) {
return walk(x, cb, _path5);
}); // $FlowIgnore
walk(n.testLabel, cb, _path5);
break;
}
n.instr.forEach(function (x) {
return walk(x, cb);
});
}
case "Instr":
{
var _path6 = createPath(n, parentPath); // $FlowIgnore
if (n.type === "IfInstruction") {
cb(n.type, createPath(n)); // $FlowIgnore
walk(n.testLabel, cb); // $FlowIgnore
cb(n.type, _path6); // $FlowIgnore
n.consequent.forEach(function (x) {
return walk(x, cb);
}); // $FlowIgnore
n.args.forEach(function (x) {
return walk(x, cb, _path6);
});
break;
}
n.alternate.forEach(function (x) {
return walk(x, cb);
});
}
case "BlockInstruction":
case "LoopInstruction":
{
var _path7 = createPath(n, parentPath); // $FlowIgnore
if (n.type === "Func") {
cb(n.type, createPath(n)); // $FlowIgnore
n.body.forEach(function (x) {
return walk(x, cb);
});
cb(n.type, _path7);
if (n.name != null) {
// $FlowIgnore
walk(n.name, cb);
}
if (n.label != null) {
// $FlowIgnore
walk(n.label, cb, _path7);
} // $FlowIgnore
n.instr.forEach(function (x) {
return walk(x, cb, _path7);
});
break;
}
case "Func":
{
var _path8 = createPath(n, parentPath);
cb(n.type, _path8);
n.body.forEach(function (x) {
return walk(x, cb, _path8);
});
if (n.name != null) {
walk(n.name, cb, _path8);
}
break;
}
default:
throw new Error("Unknown node encounter of type: " + JSON.stringify(n.type));
}
}
function traverse(n, visitor) {
function traverse(n, visitors) {
var parentPath = null;
walk(n, function (type, path) {
if (typeof visitor[type] === "function") {
visitor[type](path);
if (typeof visitors[type] === "function") {
visitors[type](path);
}
});
}, parentPath);
}
function traverseWithHooks(n, visitors, before, after) {
var parentPath = null;
walk(n, function (type, path) {
if (typeof visitors[type] === "function") {
before(type, path);
visitors[type](path);
after(type, path);
}
}, parentPath);
}
{
"name": "@webassemblyjs/ast",
"version": "1.0.0",
"version": "1.1.0",
"description": "AST utils for webassemblyjs",

@@ -14,5 +14,5 @@ "keywords": [

"dependencies": {
"@webassemblyjs/wast-parser": "1.0.0",
"@webassemblyjs/wast-parser": "1.1.0",
"webassembly-floating-point-hex-parser": "0.1.2",
"webassemblyjs": "1.0.0"
"webassemblyjs": "1.1.0"
},

@@ -19,0 +19,0 @@ "repository": {

@@ -64,3 +64,7 @@ // @flow

export function module(id: ?string, fields: ModuleFields): Module {
export function module(
id: ?string,
fields: ModuleFields,
metadata?: ModuleMetadata
): Module {
if (id != null) {

@@ -72,3 +76,3 @@ assert(typeof id === "string");

return {
const n: Module = {
type: "Module",

@@ -78,4 +82,25 @@ id,

};
if (typeof metadata !== "undefined") {
n.metadata = metadata;
}
return n;
}
export function sectionMetadata(
section: SectionName,
startOffset: number,
size: number,
vectorOfSize: number = -1
): SectionMetadata {
return {
type: "SectionMetadata",
section,
startOffset,
size,
vectorOfSize
};
}
export function binaryModule(id: ?string, blob: Array<string>): BinaryModule {

@@ -536,3 +561,4 @@ return {

export { traverse } from "./traverse";
export { traverse, traverseWithHooks } from "./traverse";
export { signatures } from "./signatures";
export { getSectionMetadata } from "./utils";
// @flow
function createPath(node: Node): NodePath<Node> {
return {
node
};
}
type Cb = (type: string, path: NodePath<Node>) => void;
function walk(n: Node, cb: (type: string, path: NodePath<Node>) => void) {
if (n.type === "Program") {
cb(n.type, createPath(n));
function removeNodeInBody(node: Node, fromNode: Node) {
switch (fromNode.type) {
case "Module":
fromNode.fields = fromNode.fields.filter(n => n !== node);
break;
// $FlowIgnore
n.body.forEach(x => walk(x, cb));
case "Program":
case "Func":
fromNode.body = fromNode.body.filter(n => n !== node);
break;
default:
throw new Error(
"Unsupported operation: removing node of type: " + String(fromNode.type)
);
}
}
if (n.type === "Module") {
cb(n.type, createPath(n));
if (typeof n.fields !== "undefined") {
// $FlowIgnore
n.fields.forEach(x => walk(x, cb));
function createPath(node: Node, parentPath: ?NodePath<Node>): NodePath<Node> {
function remove() {
if (parentPath == null) {
throw new Error("Can not remove root node");
}
}
if (n.type === "ModuleExport") {
cb(n.type, createPath(n));
const parentNode = parentPath.node;
removeNodeInBody(node, parentNode);
node._deleted = true;
}
if (n.type === "Start") {
cb(n.type, createPath(n));
// TODO(sven): do it the good way, changing the node from the parent
function replaceWith(newNode: Node) {
// $FlowIgnore
walk(n.index, cb);
Object.assign(node, newNode);
}
if (n.type === "Data") {
cb(n.type, createPath(n));
}
return {
node,
parentPath,
if (n.type === "Identifier") {
cb(n.type, createPath(n));
replaceWith,
remove
};
}
function walk(n: Node, cb: Cb, parentPath: ?NodePath<Node>) {
if (n._deleted === true) {
return;
}
if (n.type === "ModuleImport") {
cb(n.type, createPath(n));
switch (n.type) {
case "Program": {
const path = createPath(n, parentPath);
cb(n.type, path);
// $FlowIgnore
cb(n.descr.type, createPath(n.descr));
}
n.body.forEach(x => walk(x, cb, path));
if (n.type === "Global") {
cb(n.type, createPath(n));
break;
}
if (n.name != null) {
// $FlowIgnore
walk(n.name, cb);
case "SectionMetadata":
case "ModuleExport":
case "Data":
case "Memory":
case "Elem":
case "NumberLiteral":
case "FloatLiteral":
case "BinaryModule":
case "LeadingComment":
case "BlockComment":
case "Identifier": {
cb(n.type, createPath(n, parentPath));
break;
}
}
if (n.type === "Table") {
cb(n.type, createPath(n));
case "Module": {
const path = createPath(n, parentPath);
cb(n.type, path);
if (n.name != null) {
if (typeof n.fields !== "undefined") {
n.fields.forEach(x => walk(x, cb, path));
}
if (typeof n.metadata !== "undefined") {
// $FlowIgnore
n.metadata.sections.forEach(x => walk(x, cb, path));
}
break;
}
case "Start":
case "CallInstruction": {
const path = createPath(n, parentPath);
// $FlowIgnore
walk(n.name, cb);
cb(n.type, path);
// $FlowIgnore
walk(n.index, cb, path);
break;
}
}
if (n.type === "IfInstruction") {
cb(n.type, createPath(n));
case "ModuleImport": {
cb(n.type, createPath(n, parentPath));
// $FlowIgnore
n.test.forEach(x => walk(x, cb));
// $FlowIgnore
walk(n.testLabel, cb);
// $FlowIgnore
n.consequent.forEach(x => walk(x, cb));
// $FlowIgnore
n.alternate.forEach(x => walk(x, cb));
}
cb(n.descr.type, createPath(n.descr, parentPath));
if (n.type === "Memory") {
cb(n.type, createPath(n));
}
break;
}
if (n.type === "Elem") {
cb(n.type, createPath(n));
}
case "Table":
case "Global": {
const path = createPath(n, parentPath);
cb(n.type, path);
if (n.type === "Instr") {
cb(n.type, createPath(n));
if (n.name != null) {
walk(n.name, cb, path);
}
// $FlowIgnore
n.args.forEach(x => walk(x, cb));
}
break;
}
if (n.type === "CallInstruction") {
cb(n.type, createPath(n));
case "IfInstruction": {
const path = createPath(n, parentPath);
// $FlowIgnore
walk(n.index, cb);
}
// $FlowIgnore
cb(n.type, path);
if (n.type === "LoopInstruction") {
cb(n.type, createPath(n));
// $FlowIgnore
n.test.forEach(x => walk(x, cb, path));
// $FlowIgnore
n.consequent.forEach(x => walk(x, cb, path));
// $FlowIgnore
n.alternate.forEach(x => walk(x, cb, path));
if (n.label != null) {
// $FlowIgnore
walk(n.label, cb);
walk(n.testLabel, cb, path);
break;
}
// $FlowIgnore
n.instr.forEach(x => walk(x, cb));
}
case "Instr": {
const path = createPath(n, parentPath);
// $FlowIgnore
cb(n.type, path);
if (n.type === "BlockInstruction") {
cb(n.type, createPath(n));
// $FlowIgnore
n.args.forEach(x => walk(x, cb, path));
if (n.label != null) {
// $FlowIgnore
walk(n.label, cb);
break;
}
// $FlowIgnore
n.instr.forEach(x => walk(x, cb));
}
case "BlockInstruction":
case "LoopInstruction": {
const path = createPath(n, parentPath);
// $FlowIgnore
cb(n.type, path);
if (n.type === "IfInstruction") {
cb(n.type, createPath(n));
if (n.label != null) {
// $FlowIgnore
walk(n.label, cb, path);
}
// $FlowIgnore
walk(n.testLabel, cb);
// $FlowIgnore
n.instr.forEach(x => walk(x, cb, path));
// $FlowIgnore
n.consequent.forEach(x => walk(x, cb));
// $FlowIgnore
n.alternate.forEach(x => walk(x, cb));
}
break;
}
if (n.type === "Func") {
cb(n.type, createPath(n));
case "Func": {
const path = createPath(n, parentPath);
cb(n.type, path);
// $FlowIgnore
n.body.forEach(x => walk(x, cb));
n.body.forEach(x => walk(x, cb, path));
if (n.name != null) {
// $FlowIgnore
walk(n.name, cb);
if (n.name != null) {
walk(n.name, cb, path);
}
break;
}
default:
throw new Error(
"Unknown node encounter of type: " + JSON.stringify(n.type)
);
}
}
export function traverse(n: Node, visitor: Object) {
walk(n, (type: string, path: NodePath<Node>) => {
if (typeof visitor[type] === "function") {
visitor[type](path);
}
});
export function traverse(n: Node, visitors: Object) {
const parentPath = null;
walk(
n,
(type: string, path: NodePath<Node>) => {
if (typeof visitors[type] === "function") {
visitors[type](path);
}
},
parentPath
);
}
export function traverseWithHooks(
n: Node,
visitors: Object,
before: Cb,
after: Cb
) {
const parentPath = null;
walk(
n,
(type: string, path: NodePath<Node>) => {
if (typeof visitors[type] === "function") {
before(type, path);
visitors[type](path);
after(type, path);
}
},
parentPath
);
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc