wasm-json-toolkit
Advanced tools
Comparing version 0.2.4 to 0.2.5
27
index.js
@@ -28,27 +28,2 @@ /** | ||
exports.text2json = require('./text2json') | ||
/** | ||
* iterates thourgh an array of sections returning a subset of sections | ||
* @param {Array} | ||
* @param {Sections} | ||
*/ | ||
exports.findSections = function * (array, sections) { | ||
let section = array[0] | ||
let index = 0 | ||
const wantedSections = new Set(sections) | ||
let nextSection = sections.shift() | ||
while (section) { | ||
if (!wantedSections.has(section.sectionName || section.name)) { | ||
section = array[++index] | ||
} else { | ||
if ((section.sectionName || section.name) === nextSection) { | ||
yield section | ||
section = array[++index] | ||
} else { | ||
yield | ||
} | ||
nextSection = sections.shift() | ||
} | ||
} | ||
} | ||
exports.Iterator = require('./iterator') |
250
json2wasm.js
@@ -311,194 +311,110 @@ const Buffer = require('safe-buffer').Buffer | ||
const sectionGenerators = { | ||
'custom': (json, stream = new Stream()) => { | ||
stream.write([0]) | ||
const payload = new Stream() | ||
leb.unsigned.write(json.sectionName.length, payload) | ||
payload.write(json.sectionName) | ||
payload.write(json.payload) | ||
// write the size of the payload | ||
leb.unsigned.write(payload.bytesWrote, stream) | ||
stream.write(payload.buffer) | ||
return stream | ||
}, | ||
'type': (json, stream = new Stream()) => { | ||
stream.write([SECTION_IDS['type']]) | ||
let binEntries = new Stream() | ||
const entryGenerators = { | ||
'type': (entry, stream = new Stream()) => { | ||
// a single type entry binary encoded | ||
stream.write([LANGUAGE_TYPES[entry.form]]) // the form | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
for (let entry of json.entries) { | ||
// a single type entry binary encoded | ||
binEntries.write([LANGUAGE_TYPES[entry.form]]) // the form | ||
const len = entry.params.length // number of parameters | ||
leb.unsigned.write(len, binEntries) | ||
if (len !== 0) { | ||
binEntries.write(entry.params.map(type => LANGUAGE_TYPES[type])) // the paramter types | ||
} | ||
binEntries.write([entry.return_type ? 1 : 0]) // number of return types | ||
if (entry.return_type) { | ||
binEntries.write([LANGUAGE_TYPES[entry.return_type]]) | ||
} | ||
const len = entry.params.length // number of parameters | ||
leb.unsigned.write(len, stream) | ||
if (len !== 0) { | ||
stream.write(entry.params.map(type => LANGUAGE_TYPES[type])) // the paramter types | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) // write the size | ||
stream.write(binEntries.buffer) | ||
stream.write([entry.return_type ? 1 : 0]) // number of return types | ||
return stream | ||
}, | ||
'import': (json, stream) => { | ||
stream.write([SECTION_IDS['import']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) // write the number of entries | ||
for (let entry of json.entries) { | ||
// write the module string | ||
leb.unsigned.write(entry.moduleStr.length, binEntries) | ||
binEntries.write(entry.moduleStr) | ||
// write the field string | ||
leb.unsigned.write(entry.fieldStr.length, binEntries) | ||
binEntries.write(entry.fieldStr) | ||
binEntries.write([EXTERNAL_KIND[entry.kind]]) | ||
_exports.typeGenerators[entry.kind](entry.type, binEntries) | ||
if (entry.return_type) { | ||
stream.write([LANGUAGE_TYPES[entry.return_type]]) | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) // write the size | ||
stream.write(binEntries.buffer) | ||
return stream | ||
return stream.buffer | ||
}, | ||
'function': (json, stream = new Stream()) => { | ||
stream.write([SECTION_IDS['function']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
for (let index of json.entries) { | ||
leb.unsigned.write(index, binEntries) | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
return stream | ||
'import': (entry, stream = new Stream()) => { | ||
// write the module string | ||
leb.unsigned.write(entry.moduleStr.length, stream) | ||
stream.write(entry.moduleStr) | ||
// write the field string | ||
leb.unsigned.write(entry.fieldStr.length, stream) | ||
stream.write(entry.fieldStr) | ||
stream.write([EXTERNAL_KIND[entry.kind]]) | ||
_exports.typeGenerators[entry.kind](entry.type, stream) | ||
}, | ||
'table': (json, stream) => { | ||
stream.write([SECTION_IDS['table']]) | ||
let binEntries = new Stream() | ||
// write table_type | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
for (let entry of json.entries) { | ||
_exports.typeGenerators.table(entry, binEntries) | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
'function': (entry, stream = new Stream()) => { | ||
leb.unsigned.write(entry, stream) | ||
return stream.buffer | ||
}, | ||
'table': _exports.typeGenerators.table, | ||
'global': (entry, stream = new Stream()) => { | ||
_exports.typeGenerators.global(entry.type, stream) | ||
_exports.typeGenerators.initExpr(entry.init, stream) | ||
return stream | ||
}, | ||
'memory': (json, stream) => { | ||
stream.write([SECTION_IDS['memory']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
for (let entry of json.entries) { | ||
_exports.typeGenerators.memory(entry, binEntries) | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
'memory': _exports.typeGenerators.memory, | ||
'export': (entry, stream = new Stream()) => { | ||
const fieldStr = Buffer.from(entry.field_str) | ||
const strLen = fieldStr.length | ||
leb.unsigned.write(strLen, stream) | ||
stream.write(fieldStr) | ||
stream.write([EXTERNAL_KIND[entry.kind]]) | ||
leb.unsigned.write(entry.index, stream) | ||
return stream | ||
}, | ||
'global': (json, stream) => { | ||
stream.write([SECTION_IDS['global']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
for (let entry of json.entries) { | ||
_exports.typeGenerators.global(entry.type, binEntries) | ||
_exports.typeGenerators.initExpr(entry.init, binEntries) | ||
'element': (entry, stream = new Stream()) => { | ||
leb.unsigned.write(entry.index, stream) | ||
_exports.typeGenerators.initExpr(entry.offset, stream) | ||
leb.unsigned.write(entry.elements.length, stream) | ||
for (let elem of entry.elements) { | ||
leb.unsigned.write(elem, stream) | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
return stream | ||
}, | ||
'export': (json, stream = new Stream()) => { | ||
stream.write([SECTION_IDS['export']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
for (let entry of json.entries) { | ||
const fieldStr = Buffer.from(entry.field_str) | ||
const strLen = fieldStr.length | ||
leb.unsigned.write(strLen, binEntries) | ||
binEntries.write(fieldStr) | ||
binEntries.write([EXTERNAL_KIND[entry.kind]]) | ||
leb.unsigned.write(entry.index, binEntries) | ||
'code': (entry, stream = new Stream()) => { | ||
let codeStream = new Stream() | ||
// write the locals | ||
leb.unsigned.write(entry.locals.length, codeStream) | ||
for (let local of entry.locals) { | ||
leb.unsigned.write(local.count, codeStream) | ||
codeStream.write([LANGUAGE_TYPES[local.type]]) | ||
} | ||
// write opcode | ||
for (let op of entry.code) { | ||
_exports.generateOp(op, codeStream) | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
leb.unsigned.write(codeStream.bytesWrote, stream) | ||
stream.write(codeStream.buffer) | ||
return stream | ||
}, | ||
'start': (json, stream) => { | ||
stream.write([SECTION_IDS['start']]) | ||
const index = new Stream() | ||
leb.unsigned.write(json.index, index) | ||
leb.unsigned.write(index.bytesWrote, stream) | ||
stream.write(index.buffer) | ||
'data': (entry, stream = new Stream()) => { | ||
leb.unsigned.write(entry.index, stream) | ||
_exports.typeGenerators.initExpr(entry.offset, stream) | ||
leb.unsigned.write(entry.data.length, stream) | ||
stream.write(entry.data) | ||
return stream | ||
}, | ||
'element': (json, stream) => { | ||
stream.write([SECTION_IDS['element']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
} | ||
} | ||
for (let entry of json.entries) { | ||
leb.unsigned.write(entry.index, binEntries) | ||
_exports.typeGenerators.initExpr(entry.offset, binEntries) | ||
leb.unsigned.write(entry.elements.length, binEntries) | ||
for (let elem of entry.elements) { | ||
leb.unsigned.write(elem, binEntries) | ||
} | ||
} | ||
_exports.entryGenerators = entryGenerators | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
return stream | ||
}, | ||
'code': (json, stream = new Stream()) => { | ||
stream.write([SECTION_IDS['code']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
_exports.generateSection = function (json, stream = new Stream()) { | ||
const name = json.name | ||
const payload = new Stream() | ||
stream.write([SECTION_IDS[name]]) | ||
if (name === 'custom') { | ||
leb.unsigned.write(json.sectionName.length, payload) | ||
payload.write(json.sectionName) | ||
payload.write(json.payload) | ||
} else if (name === 'start') { | ||
leb.unsigned.write(json.index, payload) | ||
} else { | ||
leb.unsigned.write(json.entries.length, payload) | ||
for (let entry of json.entries) { | ||
let codeStream = new Stream() | ||
// write the locals | ||
leb.unsigned.write(entry.locals.length, codeStream) | ||
for (let local of entry.locals) { | ||
leb.unsigned.write(local.count, codeStream) | ||
codeStream.write([LANGUAGE_TYPES[local.type]]) | ||
} | ||
// write opcode | ||
for (let op of entry.code) { | ||
_exports.generateOp(op, codeStream) | ||
} | ||
leb.unsigned.write(codeStream.bytesWrote, binEntries) | ||
binEntries.write(codeStream.buffer) | ||
entryGenerators[name](entry, payload) | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
return stream | ||
}, | ||
'data': (json, stream) => { | ||
stream.write([SECTION_IDS['data']]) | ||
let binEntries = new Stream() | ||
leb.unsigned.write(json.entries.length, binEntries) | ||
for (let entry of json.entries) { | ||
leb.unsigned.write(entry.index, binEntries) | ||
_exports.typeGenerators.initExpr(entry.offset, binEntries) | ||
leb.unsigned.write(entry.data.length, binEntries) | ||
binEntries.write(entry.data) | ||
} | ||
} | ||
leb.unsigned.write(binEntries.bytesWrote, stream) | ||
stream.write(binEntries.buffer) | ||
return stream | ||
} | ||
// write the size of the payload | ||
leb.unsigned.write(payload.bytesWrote, stream) | ||
stream.write(payload.buffer) | ||
return stream | ||
} | ||
@@ -510,3 +426,3 @@ | ||
for (let item of rest) { | ||
sectionGenerators[item.name](item, stream) | ||
_exports.generateSection(item, stream) | ||
} | ||
@@ -513,0 +429,0 @@ |
{ | ||
"name": "wasm-json-toolkit", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "this convertes wasm binaries to json and json to wasm binaries", | ||
@@ -19,6 +19,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"bn.js": "^4.11.8", | ||
"buffer-pipe": "0.0.2", | ||
"buffer-pipe": "0.0.3", | ||
"leb128": "0.0.4", | ||
"safe-buffer": "^5.1.1" | ||
"safe-buffer": "^5.1.2" | ||
}, | ||
@@ -30,4 +29,4 @@ "bin": { | ||
"devDependencies": { | ||
"coveralls": "^3.0.0", | ||
"nyc": "11.6.0", | ||
"coveralls": "^3.0.1", | ||
"nyc": "12.0.2", | ||
"standard": "^11.0.1", | ||
@@ -34,0 +33,0 @@ "tape": "^4.9.0" |
@@ -73,2 +73,61 @@ # SYNOPSIS | ||
# iterator | ||
[iterator.js:12-58](https://github.com/ewasm/wasm-json-toolkit/blob/e9fdd9498451b39b84c1167e78dc4aad03b055bd/iterator.js#L12-L58 "Source code on GitHub") | ||
The Module Iterator allows for iteration over a webassembly module's sections. | ||
A section is wrapped in a section class. A section class instance allows you | ||
append entries to a given section | ||
**Examples** | ||
```javascript | ||
const it = new Iterator(wasm) | ||
for (const section of it) { | ||
console.log(section.type) | ||
const json = section.toJSON() | ||
} | ||
``` | ||
## wasm | ||
[iterator.js:26-32](https://github.com/ewasm/wasm-json-toolkit/blob/e9fdd9498451b39b84c1167e78dc4aad03b055bd/iterator.js#L26-L32 "Source code on GitHub") | ||
if the orignal wasm module was modified then this will return the modified | ||
wasm module | ||
## iterator | ||
[iterator.js:38-52](https://github.com/ewasm/wasm-json-toolkit/blob/e9fdd9498451b39b84c1167e78dc4aad03b055bd/iterator.js#L38-L52 "Source code on GitHub") | ||
Iterates through the module's sections | ||
return {Iterator.<Section>} | ||
# Section | ||
[iterator.js:64-110](https://github.com/ewasm/wasm-json-toolkit/blob/e9fdd9498451b39b84c1167e78dc4aad03b055bd/iterator.js#L64-L110 "Source code on GitHub") | ||
The section class is always internal created by the Module class. And return | ||
through the Module's iternator | ||
## toJSON | ||
[iterator.js:83-85](https://github.com/ewasm/wasm-json-toolkit/blob/e9fdd9498451b39b84c1167e78dc4aad03b055bd/iterator.js#L83-L85 "Source code on GitHub") | ||
Parses the section and return the JSON repesentation of it | ||
returns {Object} | ||
## appendEntries | ||
[iterator.js:92-109](https://github.com/ewasm/wasm-json-toolkit/blob/e9fdd9498451b39b84c1167e78dc4aad03b055bd/iterator.js#L92-L109 "Source code on GitHub") | ||
Appends an array of entries to this section. NOTE: this will modify the | ||
parent wasm module. | ||
**Parameters** | ||
- `entries` **Arrayy<[Buffer](https://nodejs.org/api/buffer.html)>** | ||
## exammple json output | ||
@@ -75,0 +134,0 @@ |
@@ -609,14 +609,7 @@ const leb = require('leb128') | ||
_exports.parse = (stream, filter) => { | ||
const json = [] | ||
const preramble = _exports.parsePreramble(stream) | ||
if (!filter || filter.has('preramble')) { | ||
json.push(preramble) | ||
} | ||
const json = [preramble] | ||
while (!stream.end) { | ||
const header = _exports.parseSectionHeader(stream) | ||
if (filter && !filter.has(header.name)) { | ||
stream.read(header.size) | ||
continue | ||
} | ||
json.push(sectionParsers[header.name](stream, header)) | ||
@@ -623,0 +616,0 @@ } |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 373 instances in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
352253
3
7645
213
3
1
19
+ Addedbuffer-pipe@0.0.3(transitive)
- Removedbn.js@^4.11.8
- Removedbuffer-pipe@0.0.2(transitive)
Updatedbuffer-pipe@0.0.3
Updatedsafe-buffer@^5.1.2