Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Merge nodejs/master
Browse files Browse the repository at this point in the history
Merge 2dc09f6 as of 2017-08-04.
This is an automatically created merge. For any problems please
contact @kunalspathak.
  • Loading branch information
chakrabot committed Aug 4, 2017
2 parents 357fdf6 + 2dc09f6 commit 745709f
Show file tree
Hide file tree
Showing 22 changed files with 378 additions and 348 deletions.
8 changes: 8 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ corresponding argument. Supported placeholders are:
* `%f` - Floating point value.
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
contains circular references.
* `%o` - Object. A string representation of an object
with generic JavaScript object formatting.
Similar to `util.inspect()` with options `{ showHidden: true, depth: 4, showProxy: true }`.
This will show the full object including non-enumerable symbols and properties.
* `%O` - Object. A string representation of an object
with generic JavaScript object formatting.
Similar to `util.inspect()` without options.
This will show the full object not including non-enumerable symbols and properties.
* `%%` - single percent sign (`'%'`). This does not consume an argument.

If the placeholder does not have a corresponding argument, the placeholder is
Expand Down
11 changes: 11 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ function format(f) {
str += f.slice(lastPos, i);
str += String(arguments[a++]);
break;
case 79: // 'O'
if (lastPos < i)
str += f.slice(lastPos, i);
str += inspect(arguments[a++]);
break;
case 111: // 'o'
if (lastPos < i)
str += f.slice(lastPos, i);
str += inspect(arguments[a++],
{ showHidden: true, depth: 4, showProxy: true });
break;
case 37: // '%'
if (lastPos < i)
str += f.slice(lastPos, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ if (!binding.ensureAllocation(2 * kStringMaxLength))
common.skip(skipMessage);

const maxString = buf.toString('utf16le');
assert.strictEqual(maxString.length, (kStringMaxLength + 2) / 2);
assert.strictEqual(maxString.length, Math.floor((kStringMaxLength + 2) / 2));
4 changes: 3 additions & 1 deletion test/inspector/inspector-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function sendEnd(socket) {
}

function parseWSFrame(buffer, handler) {
// Protocol described in https://tools.ietf.org/html/rfc6455#section-5
if (buffer.length < 2)
return 0;
if (buffer[0] === 0x88 && buffer[1] === 0x00) {
Expand All @@ -68,7 +69,8 @@ function parseWSFrame(buffer, handler) {
dataLen = buffer.readUInt16BE(2);
bodyOffset = 4;
} else if (dataLen === 127) {
dataLen = buffer.readUInt32BE(2);
assert(buffer[2] === 0 && buffer[3] === 0, 'Inspector message too big');
dataLen = buffer.readUIntBE(4, 6);
bodyOffset = 10;
}
if (buffer.length < bodyOffset + dataLen)
Expand Down
82 changes: 82 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,84 @@ assert.strictEqual(util.format('%j', '42'), '"42"');
assert.strictEqual(util.format('%j %j', 42, 43), '42 43');
assert.strictEqual(util.format('%j %j', 42), '42 %j');

// Object format specifier
const obj = {
foo: 'bar',
foobar: 1,
func: function() {}
};
const nestedObj = {
foo: 'bar',
foobar: {
foo: 'bar',
func: function() {}
}
};
assert.strictEqual(util.format('%o'), '%o');
assert.strictEqual(util.format('%o', 42), '42');
assert.strictEqual(util.format('%o', 'foo'), '\'foo\'');
assert.strictEqual(
util.format('%o', obj),
'{ foo: \'bar\',\n' +
' foobar: 1,\n' +
' func: \n' +
' { [Function: func]\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular] } } }');
assert.strictEqual(
util.format('%o', nestedObj),
'{ foo: \'bar\',\n' +
' foobar: \n' +
' { foo: \'bar\',\n' +
' func: \n' +
' { [Function: func]\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular] } } } }');
assert.strictEqual(
util.format('%o %o', obj, obj),
'{ foo: \'bar\',\n' +
' foobar: 1,\n' +
' func: \n' +
' { [Function: func]\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular] } } }' +
' { foo: \'bar\',\n' +
' foobar: 1,\n' +
' func: \n' +
' { [Function: func]\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular] } } }');
assert.strictEqual(
util.format('%o %o', obj),
'{ foo: \'bar\',\n' +
' foobar: 1,\n' +
' func: \n' +
' { [Function: func]\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular] } } } %o');

assert.strictEqual(util.format('%O'), '%O');
assert.strictEqual(util.format('%O', 42), '42');
assert.strictEqual(util.format('%O', 'foo'), '\'foo\'');
assert.strictEqual(
util.format('%O', obj),
'{ foo: \'bar\', foobar: 1, func: [Function: func] }');
assert.strictEqual(
util.format('%O', nestedObj),
'{ foo: \'bar\', foobar: { foo: \'bar\', func: [Function: func] } }');
assert.strictEqual(
util.format('%O %O', obj, obj),
'{ foo: \'bar\', foobar: 1, func: [Function: func] } ' +
'{ foo: \'bar\', foobar: 1, func: [Function: func] }');
assert.strictEqual(
util.format('%O %O', obj),
'{ foo: \'bar\', foobar: 1, func: [Function: func] } %O');

// Various format specifiers
assert.strictEqual(util.format('%%s%s', 'foo'), '%sfoo');
assert.strictEqual(util.format('%s:%s'), '%s:%s');
Expand All @@ -129,6 +207,10 @@ assert.strictEqual(util.format('%f:%f'), '%f:%f');
assert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []');
assert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j');
assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
assert.strictEqual(util.format('o: %o, a: %O', {}, []), 'o: {}, a: []');
assert.strictEqual(util.format('o: %o, a: %o', {}), 'o: {}, a: %o');
assert.strictEqual(util.format('o: %O, a: %O'), 'o: %O, a: %O');


// Invalid format specifiers
assert.strictEqual(util.format('a% b', 'x'), 'a% b x');
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-vm-context-async-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const vm = require('vm');

const sandbox = { setTimeout: setTimeout };
const sandbox = { setTimeout };

const ctx = vm.createContext(sandbox);

vm.runInContext('setTimeout(function() { x = 3; }, 0);', ctx);
setTimeout(function() {
setTimeout(common.mustCall(() => {
assert.strictEqual(sandbox.x, 3);
assert.strictEqual(ctx.x, 3);
}, 1);
}), 1);
16 changes: 8 additions & 8 deletions test/parallel/test-vm-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ const vm = require('vm');
const Script = vm.Script;
let script = new Script('"passed";');

console.error('run in a new empty context');
// Run in a new empty context
let context = vm.createContext();
let result = script.runInContext(context);
assert.strictEqual('passed', result);

console.error('create a new pre-populated context');
// Create a new pre-populated context
context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
assert.strictEqual('bar', context.foo);
assert.strictEqual('lala', context.thing);

console.error('test updating context');
// Test updating context
script = new Script('foo = 3;');
result = script.runInContext(context);
assert.strictEqual(3, context.foo);
assert.strictEqual('lala', context.thing);

// Issue GH-227:
assert.throws(function() {
assert.throws(() => {
vm.runInNewContext('', null, 'some.js');
}, /^TypeError: sandbox must be an object$/);

// Issue GH-1140:
console.error('test runInContext signature');
// Test runInContext signature
let gh1140Exception;
try {
vm.runInContext('throw new Error()', context, 'expected-filename.js');
Expand Down Expand Up @@ -77,7 +77,7 @@ const contextifiedSandboxErrorMsg =
});

// Issue GH-693:
console.error('test RegExp as argument to assert.throws');
// Test RegExp as argument to assert.throws
script = vm.createScript('const assert = require(\'assert\'); assert.throws(' +
'function() { throw "hello world"; }, /hello/);',
'some.js');
Expand All @@ -92,13 +92,13 @@ assert.strictEqual(script.runInContext(ctx), false);

// Error on the first line of a module should
// have the correct line and column number
assert.throws(function() {
assert.throws(() => {
vm.runInContext('throw new Error()', context, {
filename: 'expected-filename.js',
lineOffset: 32,
columnOffset: 123
});
}, function(err) {
}, (err) => {
return /expected-filename\.js:33:130/.test(err.stack);
}, 'Expected appearance of proper offset in Error stack');

Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-vm-create-and-run-in-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ const assert = require('assert');

const vm = require('vm');

console.error('run in a new empty context');
// Run in a new empty context
let context = vm.createContext();
let result = vm.runInContext('"passed";', context);
assert.strictEqual('passed', result);

console.error('create a new pre-populated context');
// Create a new pre-populated context
context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
assert.strictEqual('bar', context.foo);
assert.strictEqual('lala', context.thing);

console.error('test updating context');
// Test updating context
result = vm.runInContext('var foo = 3;', context);
assert.strictEqual(3, context.foo);
assert.strictEqual('lala', context.thing);

// https://github.com/nodejs/node/issues/5768
console.error('run in contextified sandbox without referencing the context');
// Run in contextified sandbox without referencing the context
const sandbox = { x: 1 };
vm.createContext(sandbox);
global.gc();
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-vm-function-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ assert.strictEqual(res.name, 'b', 'res should be named b');
assert.strictEqual(typeof o.a, 'function', 'a should be function');
assert.strictEqual(typeof o.b, 'function', 'b should be function');
assert.strictEqual(res, o.b, 'result should be global b function');

console.log('ok');
8 changes: 4 additions & 4 deletions test/parallel/test-vm-new-script-new-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ const Script = require('vm').Script;

{
const script = new Script('throw new Error(\'test\');');
assert.throws(function() {
assert.throws(() => {
script.runInNewContext();
}, /^Error: test$/);
}

{
const script = new Script('foo.bar = 5;');
assert.throws(function() {
assert.throws(() => {
script.runInNewContext();
}, common.engineSpecificMessage({
v8: /^ReferenceError: foo is not defined$/,
Expand Down Expand Up @@ -97,7 +97,7 @@ const Script = require('vm').Script;
script.runInNewContext({ f: f });
assert.strictEqual(f.a, 2);

assert.throws(function() {
assert.throws(() => {
script.runInNewContext();
}, common.engineSpecificMessage({
v8: /^ReferenceError: f is not defined$/,
Expand All @@ -107,7 +107,7 @@ const Script = require('vm').Script;

{
const script = new Script('');
assert.throws(function() {
assert.throws(() => {
script.runInNewContext.call('\'hello\';');
}, common.engineSpecificMessage({
v8: /^TypeError: this\.runInContext is not a function$/,
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-vm-new-script-this-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const Script = require('vm').Script;

common.globalCheck = false;

console.error('run a string');
// Run a string
let script = new Script('\'passed\';');
const result = script.runInThisContext(script);
assert.strictEqual('passed', result);

console.error('thrown error');
// Thrown error
script = new Script('throw new Error(\'test\');');
assert.throws(function() {
assert.throws(() => {
script.runInThisContext(script);
}, /^Error: test$/);

Expand All @@ -43,7 +43,7 @@ script.runInThisContext(script);
assert.strictEqual(2, global.hello);


console.error('pass values');
// Pass values
global.code = 'foo = 1;' +
'bar = 2;' +
'if (typeof baz !== "undefined") throw new Error("test fail");';
Expand All @@ -55,7 +55,7 @@ assert.strictEqual(0, global.obj.foo);
assert.strictEqual(2, global.bar);
assert.strictEqual(1, global.foo);

console.error('call a function');
// Call a function
global.f = function() { global.foo = 100; };
script = new Script('f()');
script.runInThisContext(script);
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-vm-run-in-new-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ assert.strictEqual(typeof global.gc, 'function',

common.globalCheck = false;

console.error('run a string');
// Run a string
const result = vm.runInNewContext('\'passed\';');
assert.strictEqual('passed', result);

console.error('thrown error');
assert.throws(function() {
// Thrown error
assert.throws(() => {
vm.runInNewContext('throw new Error(\'test\');');
}, /^Error: test$/);

Expand All @@ -45,7 +45,7 @@ vm.runInNewContext('hello = 2');
assert.strictEqual(5, global.hello);


console.error('pass values in and out');
// Pass values in and out
global.code = 'foo = 1;' +
'bar = 2;' +
'if (baz !== 3) throw new Error(\'test fail\');';
Expand All @@ -58,17 +58,17 @@ assert.strictEqual(1, global.obj.foo);
assert.strictEqual(2, global.obj.bar);
assert.strictEqual(2, global.foo);

console.error('call a function by reference');
// Call a function by reference
function changeFoo() { global.foo = 100; }
vm.runInNewContext('f()', { f: changeFoo });
assert.strictEqual(global.foo, 100);

console.error('modify an object by reference');
// Modify an object by reference
const f = { a: 1 };
vm.runInNewContext('f.a = 2', { f: f });
assert.strictEqual(f.a, 2);

console.error('use function in context without referencing context');
// Use function in context without referencing context
const fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} });
global.gc();
fn();
Expand Down
Loading

0 comments on commit 745709f

Please sign in to comment.