cesanta / v7

Embedded JavaScript engine for C/C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Invalid function body" error on Function constructor call

cancerberoSgx opened this issue · comments

Hello,

It seems that v7 don't support the following function body when using the Function constructor. This code is a simple templating mechanism http://ejohn.org/blog/javascript-micro-templating/ used in several libraries like underscore and lodash and it works in several other javascript implementations but v7, including v8, rhino, spidermonkey, nashorn, JavaScriptCore.Unfortunately I dont have time to further debug and I wanted to track this issue here. I'm getting an "Invalid function body" when executing the following file.

var cache = {};

var tmpl = function tmpl(str, data){

    // Generate a reusable function that will serve as a template
    // generator (and which will be cached).
    var fbody = "var p=[],print=function(){p.push.apply(p,arguments);};" +

        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +

        // Convert the template into pure JavaScript
        str
            .replace(/[\r\t\n]/g, " ")
            .split("<%").join("\t")
            .replace(/((^|%>)[^\t]*)'/g, "$1\r")
            .replace(/\t=(.*?)%>/g, "',$1,'")
            .split("\t").join("');")
            .split("%>").join("p.push('")
            .split("\r").join("\\'")
    + "');}return p.join('');";

    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the res
    var fn = !/\W/.test(str) ?
        cache[str] = cache[str] ||
            tmpl(document.getElementById(str).innerHTML) :
        new Function("obj", fbody);

    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
}

print('before');
var result = tmpl('<p><%= name %> </p>', {name:'seba'})
print('result')

BTW I'm testing v7 and other JavaScript implementations using npm, commons-js and browserify here for if you want to take a look: https://github.com/cancerberoSgx/engify - right know just a playground project.
I will probably find more bugs, in general if something works in all other implementations but in v7 I will report an issue here, unless you think is too much noise...

Hi. Thanks for the interest in v7.

W.r.t. your script: it doesn't work in v7 because v7 doesn't yet implement the with statement. Probably it will be implemented in the future, but I'm afraid it's not going to happen soon, because the usage of with statement is strongly discouraged, and in strict mode it's forbidden. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with

So I'd suggest to refactor the code not to use with statement, and it will work.

W.r.t. reporting of other bugs: surely it would be awesome! If you find some bug, please post it here. Thanks.

thank you very much for the explanation. So the standard is not yet finished, I would put focus on that before supporting more modern language versions like ecma6. just my opinion. Thanks!