Skip to content

Commit

Permalink
Use generated function instead of eval to get async func ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed May 5, 2018
1 parent 9c9f44c commit 40bb0ea
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ Template.prototype = {
var prepended = '';
var appended = '';
var escapeFn = opts.escapeFunction;
var constructor;
var asyncCtor;

if (!this.source) {
this.generateSource();
Expand Down Expand Up @@ -598,9 +598,10 @@ Template.prototype = {

try {
if (opts.async) {
// Have to use eval for this, since in envs without support, it breaks in parsing
// Have to use generated function for this, since in envs without support,
// it breaks in parsing
try {
constructor = eval('(async function(){})').constructor;
asyncCtor = (new Function('return (async function(){}).constructor;'))();
}
catch(e) {
if (e instanceof SyntaxError) {
Expand All @@ -612,9 +613,9 @@ Template.prototype = {
}
}
else {
constructor = Function;
asyncCtor = Function;
}
fn = new constructor(opts.localsName + ', escapeFn, include, rethrow', src);
fn = new asyncCtor(opts.localsName + ', escapeFn, include, rethrow', src);
}
catch(e) {
// istanbul ignore else
Expand Down

0 comments on commit 40bb0ea

Please sign in to comment.