Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: add syntax check before wrap origin script #1516

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 8 additions & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const NativeModule = require('native_module');
const util = require('util');
const runInThisContext = require('vm').runInThisContext;
const vm = require('vm');
const assert = require('assert').ok;
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -405,10 +405,16 @@ Module.prototype._compile = function(content, filename) {

var dirname = path.dirname(filename);

// first, create the Script object to check the syntax
vm.createScript(content, {
filename: filename,
displayErrors: false
});

// create wrapper function
var wrapper = Module.wrap(content);

var compiledWrapper = runInThisContext(wrapper, { filename: filename });
var compiledWrapper = vm.runInThisContext(wrapper, { filename: filename });
if (global.v8debug) {
if (!resolvedArgv) {
// we enter the repl if we're not given a filename argument.
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/module-syntax-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.log("func 1");
})();

(function() {
console.log("func 2");
11 changes: 11 additions & 0 deletions test/parallel/test-module-syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var common = require('../common');
var assert = require('assert');

try {
require('../fixtures/module-syntax-error.js');
} catch (e) {
assert.equal(e.name, 'SyntaxError');
assert.equal(e.message, 'Unexpected token }');
return;
}
assert.ok(false, 'should not ok for code with syntax error');