mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
5 years ago
9 changed files with 180 additions and 7 deletions
@ -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; |
@ -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; |
@ -0,0 +1,6 @@ |
|||
'use strict'; |
|||
|
|||
const fromXml = require('../extdeps/from-xml'); |
|||
|
|||
const parse = fromXml.fromXML; |
|||
exports.parse = parse; |
@ -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…
Reference in new issue