Browse Source

Add JS-based utils

pull/2266/head
Sami Vaarala 5 years ago
parent
commit
ea614153bc
  1. 46
      src-tools/lib/util/bare.js
  2. 15
      src-tools/lib/util/bitencoder.js
  3. 4
      src-tools/lib/util/cquote.js
  4. 42
      src-tools/lib/util/exec.js
  5. 4
      src-tools/lib/util/generate_c.js
  6. 2
      src-tools/lib/util/hex.js
  7. 47
      src-tools/lib/util/sort.js
  8. 6
      src-tools/lib/util/xml.js
  9. 21
      src-tools/lib/util/yaml.js

46
src-tools/lib/util/bare.js

@ -0,0 +1,46 @@
'use strict';
function createBareObject(props) {
var o = Object.create(null);
if (props) {
Object.assign(o, props);
}
return o;
}
exports.createBareObject = createBareObject;
function makeBare(val, doArrays) {
var count = 0;
function process(x) {
if (!(typeof x === 'object' && x !== null)) {
return x;
}
if (Array.isArray(x) === doArrays) {
if (Object.getPrototypeOf(x) !== null) {
Object.setPrototypeOf(x, null);
count++;
}
}
Object.getOwnPropertyNames(x).forEach((k) => {
x[k] = process(x[k]);
});
return x;
}
var res = process(val);
//console.debug('made ' + count + ' objects bare');
void count;
return res;
}
function makeObjectsBareRecursive(x) {
return makeBare(x, false);
}
exports.makeObjectsBareRecursive = makeObjectsBareRecursive;
function makeArraysBareRecursive(x) {
return makeBare(x, true);
}
exports.makeArraysBareRecursive = makeArraysBareRecursive;

15
src-tools/lib/util/bitencoder.js

@ -8,8 +8,8 @@
'use strict';
const { uint8ArrayToBstr } = require('./bstr.js');
const { assert } = require('./assert.js');
const { uint8ArrayToBstr } = require('./bstr');
const { assert } = require('./assert');
function zeroArray(length) {
var res = [];
@ -54,6 +54,17 @@ BitEncoder.prototype.string = function string(x) {
}
};
// Append an Uint8Array.
BitEncoder.prototype.uint8array = function uint8array(x) {
assert(x instanceof Uint8Array);
for (let i = 0; i < x.length; i++) {
let val = x[i];
for (let shift = 7; shift >= 0; shift--) {
this.currBits.push((val >>> shift) & 0x01);
}
}
};
// Encode an unsigned integer as a 'varuint'.
BitEncoder.prototype.varuint = function varuint(x) {
if (typeof x !== 'number' || !(x >= 0)) {

4
src-tools/lib/util/cquote.js

@ -1,7 +1,7 @@
'use strict';
const { assert } = require('./assert.js');
const { assertBstr } = require('./bstr.js');
const { assert } = require('./assert');
const { assertBstr } = require('./bstr');
// Encode an integer into a C constant.
function cIntEncode(x) {

42
src-tools/lib/util/exec.js

@ -0,0 +1,42 @@
'use strict';
const { isNodejs, isDuktape } = require('./engine_detect');
const { readFileUtf8 } = require('../extbindings/fileio'); // Avoid circular dependency on util/fs
function exec(cmdline, opts) {
void opts;
if (isNodejs()) {
const child_process = require('child_process');
var args = cmdline.slice(1);
var cmd = cmdline[0];
var res = child_process.spawnSync(cmd, args, {});
return {
stdout: res.stdout,
stderr: res.stderr,
status: res.status,
signal: res.signal,
error: res.error
};
} else if (isDuktape()) {
if (typeof sysExecute === 'function') {
let tmp = cmdline[0] + ' ' + cmdline.slice(1).map((v) => '"' + v + '"').join(' ') + ' >/tmp/stdout';
//console.log('EXECUTE: ' + tmp);
sysExecute(tmp);
return {
stdout: readFileUtf8('/tmp/stdout')
};
} else {
throw new TypeError('no provider for exec');
}
} else {
throw new TypeError('no provider for exec');
}
}
exports.exec = exec;
function execStdoutUtf8(cmdline, opts) {
return exec(cmdline, opts).stdout.toString('utf-8');
}
exports.execStdoutUtf8 = execStdoutUtf8;

4
src-tools/lib/util/generate_c.js

@ -2,8 +2,8 @@
'use strict';
const { assert } = require('./assert.js');
const { cCommentEscape, cIntEncode } = require('./cquote.js');
const { assert } = require('./assert');
const { cCommentEscape, cIntEncode } = require('./cquote');
function GenerateC() {
this.data = [];

2
src-tools/lib/util/hex.js

@ -1,6 +1,6 @@
'use strict';
const { assert } = require('./assert.js');
const { assert } = require('./assert');
var zeroes = '0'.repeat(256);

47
src-tools/lib/util/sort.js

@ -21,3 +21,50 @@ function numberCompare(a, b) {
return 0;
}
exports.numberCompare = numberCompare;
// Sort object (own) keys (flat).
function sortObjectKeysFlat(obj) {
if (typeof obj === 'object' && obj !== null) {
if (Array.isArray(obj)) {
return obj;
} else {
let res = {};
Object.setPrototypeOf(res, Object.getPrototypeOf(obj));
for (let k of Object.getOwnPropertyNames(obj).sort()) {
res[k] = obj[k];
}
return res;
}
}
return obj;
}
exports.sortObjectKeysFlat = sortObjectKeysFlat;
// Sort object (own) keys recursively.
function sortObjectKeysRecursive(root) {
function process(obj) {
if (typeof obj === 'object' && obj !== null) {
if (Array.isArray(obj)) {
let res = [];
Object.setPrototypeOf(res, Object.getPrototypeOf(obj));
for (let i = 0; i < obj.length; i++) {
res[i] = process(obj[i]);
}
for (let k of Object.getOwnPropertyNames(obj).sort()) {
res[k] = process(obj[k]); // assigns index keys again, but they're already established (and sorted in ES2015+)
}
return res;
} else {
let res = {};
Object.setPrototypeOf(res, Object.getPrototypeOf(obj));
for (let k of Object.getOwnPropertyNames(obj).sort()) {
res[k] = process(obj[k]);
}
return res;
}
}
return obj;
}
return process(root);
}
exports.sortObjectKeysRecursive = sortObjectKeysRecursive;

6
src-tools/lib/util/xml.js

@ -0,0 +1,6 @@
'use strict';
const fromXml = require('../extdeps/from-xml');
const parse = fromXml.fromXML;
exports.parse = parse;

21
src-tools/lib/util/yaml.js

@ -0,0 +1,21 @@
'use strict';
const jsYaml = require('../extdeps/js-yaml');
const { makeObjectsBareRecursive } = require('./bare');
// Parse input as a YAML document. All objects in the returned document are
// made bare (inherit from null) so that it's safe to look up fields without
// risking hitting inherited properties (like 'toString'). Arrays maintain
// inheritance because there's less risk of accidental inheritance. This
// allows array .forEach() etc work naturally.
function parse(input) {
var doc = jsYaml.safeLoad(input);
doc = makeObjectsBareRecursive(doc);
return doc;
}
exports.parse = parse;
function stringify(input) {
return jsYaml.safeDump(input);
}
exports.stringify = stringify;
Loading…
Cancel
Save