Forums > Javascript

another example of weird javascript crosstalk


ico
July 9, 2007 | 3:57 pm

Hi all,

Here is an addendum to a problem I posted earlier on the maxmsp list.

Symptom 1: multiple instances of the same abstraction (which contains a js
object for the purpose of creation dynamic number of audio outputs) create
objects ok when included in the same file (see poly_test.pat below), their
#0 argument is for some reason cross-polluted. So if we have a "boo" and
"bao" instance, both of them will be for some reason instantiated as "boo"
in this case.

Symptom 2: matters are worse when using this in conjunction with poly~ (see
main.pat below) where sometimes instance 1 fails to create objects. I tried
rigging the code with Tasks to delay their execution within javascript which
for the most part minimizes failure to create objects, but (depending on the
time it takes to instantiate poly object, especially in the case of more
complex objects) its success rate is impossible to assess consistently. I
also tried using setname message which would be responsible for
instantiating object’s name via external message and that does not work
consistently either.

Expected behavior: multiple instances should instantiate as per their #0
vars and create/delete objects as expected.

FWIW, I tried changing js filenames as well as making two different
(identical) abstractions with different filenames in hope to make them more
different, neither of which had any bearing in this case.

Any insight in this matter is most appreciated.

NB: in order for this example to work, please use exact .pat and .js names

//dac2~.pat abstraction
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 70 161 109 9109513 send~ $2;
#P newex 48 46 49 9109513 zl nth $1;
#N comlet signal~;
#P inlet 27 26 15 0;
#P newex 27 132 30 9109513 *~ 0.;
#N comlet amplitude;
#P inlet 48 26 15 0;
#P newex 48 109 44 9109513 line~ 0.;
#P newex 27 161 41 9109513 dac~ $1;
#P flonum 48 68 59 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 48 87 59 9109513 append 10;
#P connect 5 0 2 0;
#P fasten 5 0 8 0 32 155 75 155;
#P connect 0 0 3 0;
#P connect 1 0 0 0;
#P connect 7 0 1 0;
#P connect 4 0 7 0;
#P connect 3 0 5 1;
#P connect 6 0 5 0;
#P window clipboard copycount 9;

//modular_dac~.pat abstraction
#P button 508 25 15 0;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P message 525 25 13 9109513 1;
#N comlet instance number;
#P inlet 540 25 15 0;
#N comlet speaker amplitude list;
#P inlet 287 25 15 0;
#P objectname amp_list;
#P number 626 44 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 583 44 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#N comlet rows;
#P inlet 626 25 15 0;
#N comlet cols;
#P inlet 583 25 15 0;
#P newex 540 64 97 9109513 js dac2.js $1;
#N comlet signal~;
#P inlet 9 25 15 0;
#P objectname sig_in;
#P connect 5 0 1 2;
#P connect 3 0 5 0;
#P connect 4 0 1 1;
#P connect 2 0 4 0;
#P connect 7 0 1 0;
#P connect 9 0 1 0;
#P connect 8 0 1 0;
#P window clipboard copycount 10;

//poly_test.pat (single instance)
#P window setfont "Sans Serif" 10.;
#P window linecount 1;
#N in 2;
#P newobj 143 43 26 9109514 in 2;
#P message 243 95 14 9109514 1;
#P message 299 96 14 9109514 2;
#P newex 285 66 53 9109514 loadbang;
#P newex 254 146 97 9109514 modular_dac~ bao;
#P newex 139 146 97 9109514 modular_dac~ boo;
#P connect 3 0 1 2;
#P connect 3 0 1 3;
#P connect 3 0 1 4;
#P connect 2 0 4 0;
#P connect 2 0 3 0;
#P connect 5 0 0 2;
#P connect 5 0 1 2;
#P connect 4 0 0 4;
#P connect 4 0 0 3;
#P connect 4 0 0 2;
#P window clipboard copycount 6;

//main.pat (poly~ example)
#P window setfont "Sans Serif" 10.;
#P window linecount 1;
#N thispatcher;
#Q end;
#P newobj 313 126 59 9109514 thispatcher;
#P number 101 138 35 10 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 112 162 47 9109514 open $1;
#P message 95 110 44 9109514 target 0;
#P button 252 126 15 0;
#P number 171 75 35 10 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 171 100 52 9109514 voices $1;
#P newex 178 153 88 9109514 poly~ poly_test 1;
#P connect 3 0 0 1;
#P connect 4 0 0 0;
#P connect 1 0 0 0;
#P connect 5 0 0 0;
#P connect 2 0 1 0;
#P connect 6 0 5 0;
#P window clipboard copycount 8;

//dac2.js
inlets=3; //instance/reset (bang), cols, rows
outlets=1;

var cols=0;
var rows=0;
var instance=0;
var output;
var initialized=false;

function msg_int(val)
{
if (inlet==0 && val>0)
{
instance=val;
init();
}
else if (inlet==1 && val>0 && val<17)
{
cols=val;
init();
}
else if (inlet==2 && val>0 && val<17)
{
rows=val;
init();
}
//else post ("dac2.js reports invalid data " + val + "n");
}

function init()
{
name=jsarguments[1];
if (cols && rows && instance && name)
{
if (initialized) destructor();
initialized=true;
tsk=new Task(this.createoutputs);
tsk.repeat(0,1000);
}
}

function createoutputs()
{
output=new Array(rows*cols);
//make new ones and connect them
for (i=0; i
{

output[i]=this.patcher.newdefault(100*(i%cols)+20,30*Math.floor(i/cols)+120,
"dac2~", (i+1), name + "_" + instance + "_" + (i+1));
output[i].varname=(i+1);
}
tsk=new Task(this.connectall);
tsk.repeat(0,500);
}

function connectall()
{
sig=this.patcher.getnamed("sig_in");
amp=this.patcher.getnamed("amp_list");
for (i=0; i
{
this.patcher.connect(sig,0,output[i],0);
this.patcher.connect(amp,0,output[i],1);
}
}

function bang()
{
if (inlet==0) destructor();
}

function destructor()
{
//a kind of destructor: remove old outputs
i=1;
while(tmp=this.patcher.getnamed(i))
{
this.patcher.remove(tmp);
//post(i + "n");
i++;
}
initialized=false;
}
//end dac2.js

Ivica Ico Bukvic, D.M.A.
Composition, Music Technology, CCTAD, CHCI
Virginia Tech
Dept. of Music – 0240
Blacksburg, VA 24061
(540) 231-1137
(540) 231-5034 (fax)
ico@vt.edu
http://www.music.vt.edu/people/faculty/bukvic/

http://ico.bukvic.net


July 9, 2007 | 4:06 pm

I haven’t looked closely at your files, but I have a hunch that you
have global variables messing things up. In js, variables are global
unless declared with the var keyword where global means global to all
js objects in max.

example:

var x = 10;

function test()
{
y = 100;
}

In the above code, x is not global but y is.

wes

On 7/9/07, Ivica Ico Bukvic wrote:
> Hi all,
>
> Here is an addendum to a problem I posted earlier on the maxmsp list.
>
> Symptom 1: multiple instances of the same abstraction (which contains a js
> object for the purpose of creation dynamic number of audio outputs) create
> objects ok when included in the same file (see poly_test.pat below), their
> #0 argument is for some reason cross-polluted. So if we have a "boo" and
> "bao" instance, both of them will be for some reason instantiated as "boo"
> in this case.
>
> Symptom 2: matters are worse when using this in conjunction with poly~ (see
> main.pat below) where sometimes instance 1 fails to create objects. I tried
> rigging the code with Tasks to delay their execution within javascript which
> for the most part minimizes failure to create objects, but (depending on the
> time it takes to instantiate poly object, especially in the case of more
> complex objects) its success rate is impossible to assess consistently. I
> also tried using setname message which would be responsible for
> instantiating object’s name via external message and that does not work
> consistently either.
>
> Expected behavior: multiple instances should instantiate as per their #0
> vars and create/delete objects as expected.
>
> FWIW, I tried changing js filenames as well as making two different
> (identical) abstractions with different filenames in hope to make them more
> different, neither of which had any bearing in this case.
>
> Any insight in this matter is most appreciated.
>
> NB: in order for this example to work, please use exact .pat and .js names
>
> //dac2~.pat abstraction
> #P window setfont "Sans Serif" 9.;
> #P window linecount 1;
> #P newex 70 161 109 9109513 send~ $2;
> #P newex 48 46 49 9109513 zl nth $1;
> #N comlet signal~;
> #P inlet 27 26 15 0;
> #P newex 27 132 30 9109513 *~ 0.;
> #N comlet amplitude;
> #P inlet 48 26 15 0;
> #P newex 48 109 44 9109513 line~ 0.;
> #P newex 27 161 41 9109513 dac~ $1;
> #P flonum 48 68 59 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 48 87 59 9109513 append 10;
> #P connect 5 0 2 0;
> #P fasten 5 0 8 0 32 155 75 155;
> #P connect 0 0 3 0;
> #P connect 1 0 0 0;
> #P connect 7 0 1 0;
> #P connect 4 0 7 0;
> #P connect 3 0 5 1;
> #P connect 6 0 5 0;
> #P window clipboard copycount 9;
>
> //modular_dac~.pat abstraction
> #P button 508 25 15 0;
> #P window setfont "Sans Serif" 9.;
> #P window linecount 1;
> #P message 525 25 13 9109513 1;
> #N comlet instance number;
> #P inlet 540 25 15 0;
> #N comlet speaker amplitude list;
> #P inlet 287 25 15 0;
> #P objectname amp_list;
> #P number 626 44 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P number 583 44 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #N comlet rows;
> #P inlet 626 25 15 0;
> #N comlet cols;
> #P inlet 583 25 15 0;
> #P newex 540 64 97 9109513 js dac2.js $1;
> #N comlet signal~;
> #P inlet 9 25 15 0;
> #P objectname sig_in;
> #P connect 5 0 1 2;
> #P connect 3 0 5 0;
> #P connect 4 0 1 1;
> #P connect 2 0 4 0;
> #P connect 7 0 1 0;
> #P connect 9 0 1 0;
> #P connect 8 0 1 0;
> #P window clipboard copycount 10;
>
> //poly_test.pat (single instance)
> #P window setfont "Sans Serif" 10.;
> #P window linecount 1;
> #N in 2;
> #P newobj 143 43 26 9109514 in 2;
> #P message 243 95 14 9109514 1;
> #P message 299 96 14 9109514 2;
> #P newex 285 66 53 9109514 loadbang;
> #P newex 254 146 97 9109514 modular_dac~ bao;
> #P newex 139 146 97 9109514 modular_dac~ boo;
> #P connect 3 0 1 2;
> #P connect 3 0 1 3;
> #P connect 3 0 1 4;
> #P connect 2 0 4 0;
> #P connect 2 0 3 0;
> #P connect 5 0 0 2;
> #P connect 5 0 1 2;
> #P connect 4 0 0 4;
> #P connect 4 0 0 3;
> #P connect 4 0 0 2;
> #P window clipboard copycount 6;
>
> //main.pat (poly~ example)
> #P window setfont "Sans Serif" 10.;
> #P window linecount 1;
> #N thispatcher;
> #Q end;
> #P newobj 313 126 59 9109514 thispatcher;
> #P number 101 138 35 10 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P message 112 162 47 9109514 open $1;
> #P message 95 110 44 9109514 target 0;
> #P button 252 126 15 0;
> #P number 171 75 35 10 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P message 171 100 52 9109514 voices $1;
> #P newex 178 153 88 9109514 poly~ poly_test 1;
> #P connect 3 0 0 1;
> #P connect 4 0 0 0;
> #P connect 1 0 0 0;
> #P connect 5 0 0 0;
> #P connect 2 0 1 0;
> #P connect 6 0 5 0;
> #P window clipboard copycount 8;
>
> //dac2.js
> inlets=3; //instance/reset (bang), cols, rows
> outlets=1;
>
> var cols=0;
> var rows=0;
> var instance=0;
> var output;
> var initialized=false;
>
> function msg_int(val)
> {
> if (inlet==0 && val>0)
> {
> instance=val;
> init();
> }
> else if (inlet==1 && val>0 && val<17)
> {
> cols=val;
> init();
> }
> else if (inlet==2 && val>0 && val<17)
> {
> rows=val;
> init();
> }
> //else post ("dac2.js reports invalid data " + val + "n");
> }
>
> function init()
> {
> name=jsarguments[1];
> if (cols && rows && instance && name)
> {
> if (initialized) destructor();
> initialized=true;
> tsk=new Task(this.createoutputs);
> tsk.repeat(0,1000);
> }
> }
>
> function createoutputs()
> {
> output=new Array(rows*cols);
> //make new ones and connect them
> for (i=0; i
> {
>
> output[i]=this.patcher.newdefault(100*(i%cols)+20,30*Math.floor(i/cols)+120,
> "dac2~", (i+1), name + "_" + instance + "_" + (i+1));
> output[i].varname=(i+1);
> }
> tsk=new Task(this.connectall);
> tsk.repeat(0,500);
> }
>
> function connectall()
> {
> sig=this.patcher.getnamed("sig_in");
> amp=this.patcher.getnamed("amp_list");
> for (i=0; i
> {
> this.patcher.connect(sig,0,output[i],0);
> this.patcher.connect(amp,0,output[i],1);
> }
> }
>
> function bang()
> {
> if (inlet==0) destructor();
> }
>
> function destructor()
> {
> //a kind of destructor: remove old outputs
> i=1;
> while(tmp=this.patcher.getnamed(i))
> {
> this.patcher.remove(tmp);
> //post(i + "n");
> i++;
> }
> initialized=false;
> }
> //end dac2.js
>
> Ivica Ico Bukvic, D.M.A.
> Composition, Music Technology, CCTAD, CHCI
> Virginia Tech
> Dept. of Music – 0240
> Blacksburg, VA 24061
> (540) 231-1137
> (540) 231-5034 (fax)
> ico@vt.edu
> http://www.music.vt.edu/people/faculty/bukvic/
> http://ico.bukvic.net
>
>
>
>


July 9, 2007 | 4:17 pm

On 9 juil. 07, at 17:57, Ivica Ico Bukvic wrote:

> Hi all,
>
> Here is an addendum to a problem I posted earlier on the maxmsp list.
>
> Symptom 1: multiple instances of the same abstraction (which
> contains a js
> object for the purpose of creation dynamic number of audio outputs)
> create
> objects ok when included in the same file (see poly_test.pat
> below), their
> #0 argument is for some reason cross-polluted. So if we have a
> "boo" and
> "bao" instance, both of them will be for some reason instantiated
> as "boo"
> in this case.

The problem is that your name variable is not declared in your global
code. So it becomes global to every instances of your js code. That
is usually the problem when you see when 2 instances of the same js
seems to share things.

> Symptom 2: matters are worse when using this in conjunction with
> poly~ (see
> main.pat below) where sometimes instance 1 fails to create objects.
> I tried
> rigging the code with Tasks to delay their execution within
> javascript which
> for the most part minimizes failure to create objects, but
> (depending on the
> time it takes to instantiate poly object, especially in the case of
> more
> complex objects) its success rate is impossible to assess
> consistently. I
> also tried using setname message which would be responsible for
> instantiating object’s name via external message and that does not
> work
> consistently either.

I couldn’t reproduce that one. Next time if you can put an archive
somewhere, that would be awesome ;-)

Cheers,
ej



ico
July 9, 2007 | 4:40 pm

That’s it! Many thanks for keeping me sane! For some reason I thought that
variable declarations within functions were local. Would function.local=1;
have helped in this case, or would that simply prevent external calls of
such functions but have no bearing whatsoever on shared data?

Again, many thanks for the tip!

Best wishes,

Ico


July 9, 2007 | 4:46 pm


Viewing 5 posts - 1 through 5 (of 5 total)