Error with code in jsextensions

Jul 26, 2013 at 4:49pm

Error with code in jsextensions

I moved some js code into a library and put it in jsextensions so that I can reuse it. But when I access one of the functions in this library I get following error…

js: processinglib.js: Javascript ReferenceError: outlet is not defined, line 115

Why would outlet not be defined? Is it not a global method?

#257265
Jul 28, 2013 at 2:06pm

Are you calling it when the object is instantiated? The outlets might not have had time to be created – try deferring the function call, moving it from your global code, or setting it up in a loadbang() function. It’s hard to tell if the cause is anything else without seeing your code.

#257382
Jul 29, 2013 at 7:25am

Yes, I am calling the function after the patcher and objects are instantiated. I am triggering it manually. Are you saying that when Max starts up, it loads the files in jsextensions and outlet is undefined? According to Max documentation, outlet is a method within the jsthis object. Does the jsthis object not exist at startup? Why would it not find it by the time I call the function?

#257452
Jul 29, 2013 at 3:47pm

I meant that you couldn’t have something like

outlet(0,"hello")

by itself in your code if it’s not part of a function. Try loading just that line in a [js] object and re-open the file – you’ll get an error message because the outlet doesn’t exist in the global code. Do you have a snippet of your code that replicates the issue so I can take a closer look?

#257546
Jul 29, 2013 at 4:03pm

This is the function that is causing the error. It is loaded in a file that is in jsextensions.

function drawProcessingScene()
{
var theImage = null;

//jsthis.draw();

mgfx.identity_matrix();
theImage = new Image(mgfx.pop_group());
mgfx.image_surface_draw(theImage);

theImage.tonamedmatrix(outmatrix.name);
outlet(0, “jit_matrix”, outmatrix.name);

frameCount++;

gc();

}

#257548
Jul 30, 2013 at 3:04pm

Okay I simplified it even more. If you put this function in a file in jsextensions, it will error out.

function outletRelay(msg)
{
outlet(0, msg);
}

Could this be some problem with the jsthis object? Perhaps it does not exist when source in the jsextensions directory is loaded?

#257633
Jul 30, 2013 at 3:38pm

How and when are you calling it from your main patch? Could you ZIP up all the files and attach them here so I can take a run exactly what you’re running?

#257636
Jul 30, 2013 at 4:25pm

Okay, here you go.

Attachments:
  1. jstest.zip
#257641
Jul 30, 2013 at 5:33pm

Hmmmm, I tried passing the scope across with no luck (perhaps someone knows more about this than me), otherwise my suggestion would be including your patcher-interfacing code in the main js and any utility functions in the jesextensions, for example:

function bang() {
    outlet(0,outletRelay("test message"));
}

and

function outletRelay(msg) {
    return msg + "123";
}
#257646
Aug 5, 2013 at 7:41am

I really feel this is an issue that needs clarification from someone at Cycling. What is the expectation of what I can access from code that is loaded in jsextentions? Is the jsthis object instantiated at that time? Why can I not access the function “outlet”?

#258063
Aug 5, 2013 at 12:43pm

Your functions in jsextensions are compiled as global code outside the concept of the js “this” object. When you call outlet() an an ordinary js instance it is calling this.outlet(), but in the jsextensions code there is no similar this. You will need to change your functions to something like the following where you pass in your js “this” object to the function in order to be able to call the outlet functions of that object.

function doit(myObject)
{
myObject.outlet(0,”bang”);
}

Hope this helps.

#258100
Aug 5, 2013 at 4:10pm

Thanks Joshua for your insight. It is working much better now.

#258121

You must be logged in to reply to this topic.