Skip to content

Commit

Permalink
module: add syntax check before wrap origin script
Browse files Browse the repository at this point in the history
Add syntax check for origin script code.

fix nodejs/node-v0.x-archive#8183
  • Loading branch information
JacksonTian committed Apr 24, 2015
1 parent 3d3083b commit cef8bd1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
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');

0 comments on commit cef8bd1

Please sign in to comment.