Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Reefbarman

Hi All,

Just wondering if someone has come across a similar error to what I'm seeing! I have a game engine I have built that has its own javascript file include mechanism which involves loading a js file via a XMLHttpRequest then evaling the response (this is done to scope each file and to provide namescaping). Anyway certain files are getting ReferenceErrors that seem very strange! For example here is the error below then the code causing it!

ReferenceError: Can't find variable: Color at line 9 in undefined

function Color(r, g, b, a)
{
    this.r = r || 0;
    this.g = g || 0;
    this.b = b || 0;
    this.a = isset(a) ? a : 1;
}

Color.prototype.toString = function(sFormat){
    sFormat = sFormat || "rgba";
    
    var sColor = "";
    
    switch (sFormat)
    {
        case "rgb":
            sColor = "rgb(" + this.r + "," + this.g + "," + this.b + ")";
            break;
        default:
            sColor = "rgba(" + this.r + "," + this.g + "," + this.b + "," + this.a + ")";
            break;
    }
    
    return sColor;
};

with the error happening on the line with Color.prototype.toString for some reason the function Color is found?

Anyway any help is appreciated as I would like to avoid having to refactor things to use ejecta.include

1 decade ago by Reefbarman

I have found a workaround but it does mean a bit of refactoring and for aesthetic reasons I don't like it and I find it very strange the previous way doesn't work! Here is the work around

var Color = function(r, g, b, a){
    this.r = r || 0;
    this.g = g || 0;
    this.b = b || 0;
    this.a = isset(a) ? a : 1;
};

Color.prototype.toString = function(sFormat){
    sFormat = sFormat || "rgba";
    
    var sColor = "";
    
    switch (sFormat)
    {
        case "rgb":
            sColor = "rgb(" + this.r + "," + this.g + "," + this.b + ")";
            break;
        default:
            sColor = "rgba(" + this.r + "," + this.g + "," + this.b + "," + this.a + ")";
            break;
    }
    
    return sColor;
};

Basically storing th function in a variable called Color version defining a function called Color

If anyone knows why the first way doesn't work and can suggest a way to get it working that will be great!

1 decade ago by Reefbarman

Seems this is just the start of the problems with eval(ed) code! I seem to be having scope issue all over the place I'm getting errors like so now:

TypeError: undefined is not a constructor (evaluating 'new Vector(cVec1.x + cVec2.x, cVec1.y + cVec2.y)') at line 100 in undefined

where its not finding Vector where its defined at the top of the file like

var Vector = function(){
    var x = 0;
    var y = 0;
    
    switch(arguments.length)
    {
        case 0:
            break;
        case 1:
            var cVec = arguments[0];
            
            x = cVec.x;
            y = cVec.y;
            break;
        case 2:
            x = arguments[0];
            y = arguments[1];
            break;
        default:
            throw new Error("Wrong number of arguments passed!");
            break;
    }
    
    this.x = x;
    this.y = y;
};

just seems that eval is being handled completely differently to any browser I have tested with

1 decade ago by Reefbarman

Sorry to bump this but would really like some help? It works on CocoonJS but would rather use ejecta if I can

1 decade ago by stillen

Could this issue be due to the different JS environments? I know ejecta uses the javascriptcore were cacoon uses a web view and it's js accelerator?

Not sure if this helps or is completely relevant, but this could help. I'm at work, so I can really do any debugging here.

http://reality.hk/2014/01/10/debugging-javascriptcore-using-safari-web-inspector/

Page 1 of 1
« first « previous next › last »