Skip to content
This repository was archived by the owner on Oct 9, 2018. It is now read-only.

Fix: instrumentation breaks new(require('foo'))(args) #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions instrument-js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ static void instrument_function(JSParseNode * node, Stream * f, int indent, enum
Stream_write_char(f, '}');
}

static void instrument_function_call(JSParseNode * node, Stream * f) {
static void instrument_function_call(JSParseNode * node, Stream * f, bool parenthesize_for_new) {
JSParseNode * function_node = node->pn_head;
if (function_node->pn_type == TOK_FUNCTION) {
JSObject * object = function_node->pn_funpob->object;
Expand All @@ -455,7 +455,14 @@ static void instrument_function_call(JSParseNode * node, Stream * f) {
return;
}
}
bool parenthesize_callee = parenthesize_for_new && (function_node->pn_type == TOK_LP); // e.g. new (require('foo'))()
if (parenthesize_callee) {
Stream_write_char(f, '(');
}
output_expression(function_node, f, false);
if (parenthesize_callee) {
Stream_write_char(f, ')');
}
Stream_write_char(f, '(');
for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
if (p != node->pn_head->pn_next) {
Expand Down Expand Up @@ -636,7 +643,7 @@ static void output_expression(JSParseNode * node, Stream * f, bool parenthesize_
break;
case TOK_NEW:
Stream_write_string(f, "new ");
instrument_function_call(node, f);
instrument_function_call(node, f, true);
break;
case TOK_DELETE:
Stream_write_string(f, "delete ");
Expand Down Expand Up @@ -693,7 +700,7 @@ static void output_expression(JSParseNode * node, Stream * f, bool parenthesize_
Stream_write_char(f, ']');
break;
case TOK_LP:
instrument_function_call(node, f);
instrument_function_call(node, f, false);
break;
case TOK_RB:
Stream_write_char(f, '[');
Expand Down
25 changes: 25 additions & 0 deletions tests/javascript.expected/javascript-new-indirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* automatically generated by JSCoverage - do not edit */
if (typeof _$jscoverage === 'undefined') _$jscoverage = {};
if (! _$jscoverage['javascript-new-indirect.js']) {
_$jscoverage['javascript-new-indirect.js'] = [];
_$jscoverage['javascript-new-indirect.js'][1] = 0;
_$jscoverage['javascript-new-indirect.js'][2] = 0;
_$jscoverage['javascript-new-indirect.js'][3] = 0;
_$jscoverage['javascript-new-indirect.js'][4] = 0;
_$jscoverage['javascript-new-indirect.js'][5] = 0;
}
_$jscoverage['javascript-new-indirect.js'][1]++;
function X() {
}
_$jscoverage['javascript-new-indirect.js'][2]++;
function y() {
_$jscoverage['javascript-new-indirect.js'][2]++;
return X;
}
_$jscoverage['javascript-new-indirect.js'][3]++;
x = new (y())();
_$jscoverage['javascript-new-indirect.js'][4]++;
x = new (y())(1);
_$jscoverage['javascript-new-indirect.js'][5]++;
x = new (y())(1, 2);
_$jscoverage['javascript-new-indirect.js'].source = ["function X() {}","function y() { return X; }","x = new (y())();","x = new (y())(1);","x = new (y())(1, 2);"];
5 changes: 5 additions & 0 deletions tests/javascript/javascript-new-indirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function X() {}
function y() { return X; }
x = new (y())();
x = new (y())(1);
x = new (y())(1, 2);