JavaScript: Getting "jsobject 148795" instead of integer

Learn about building and using Max for Live devices.
Post Reply
Jeff Brown
Posts: 85
Joined: Wed Nov 07, 2007 5:18 am

JavaScript: Getting "jsobject 148795" instead of integer

Post by Jeff Brown » Thu Jun 24, 2010 1:57 am

Hi,

I'm using an object called Report that accumulates information about the live set using LiveAPI objects and then posts it all at once. For some reason, when pushed onto the Report's list of data, certain integers turn into the string "jsobject" followed by a long meaningless-to-me number.

In the short example below, I've created a function f1() that tries to create a Report indicating the track position and folding state of every group track. Track numbers appear correctly, but folding states generate the error described above.

The function f2() is almost identical, but instead of accumulating the data into a Report, it prints the data immediately. f2() does not generate the same problem.

I'm totally confused. Many thanks for your help.
Jeff


function countTracks() {
// Counts the number of tracks in the Live set.
return ( new LiveAPI( this.patcher, "live_set") )
.get( "tracks" ).length / 2;
}

function Report() {
// A class to accumulate and then post reports
this.data = new Array();
this.clear = function() {
this.data = new Array();
}
this.push = function() {
// adds its arguments, however many they were, to this.data
var args = Array.prototype.slice.call(arguments);
for (var i in args) this.data.push( args );
}
this.post = function() {
post( 0, this.data, "\n" );
this.clear();
}
}

function f1() {
// Should post a message, "Groups track N1 state S1 track N2 state S2 ", where N = position on screen, and S = fold state (0 if folded). It alsmost does, except each S, rather than being a 0 or a 1, instead is a "jsobject" string followed by a long meaningless number.
post( "Exceuting f1():\n" );
var track;
var report = new Report();
report.push( "Groups" );
for (var i=0; i<countTracks(); i++) {
track = new LiveAPI( this.patcher, "live_set tracks " + i);
if ( track.get( "is_foldable" ) > 0 ) {
report.push( "track", i );
report.push( "state", track.get("fold_state") );
}
}
report.post();
}

function f2() {
// And yet, as this function demonstrates, track.get("fold_state") behaves like an ordinary number if I post it immediately, rather than first pushing it onto a Report.
post( "Exceuting f2():\n" );
for (var i=0; i<countTracks(); i++) {
track = new LiveAPI( this.patcher, "live_set tracks " + i);
if ( track.get( "is_foldable" ) > 0 ) {
post( "track", i, "\n" );
post( "fold_state", track.get("fold_state"), "\n" );
}
}
}

darth_fader
Posts: 36
Joined: Thu Jun 18, 2009 11:29 am
Location: Germany
Contact:

Re: JavaScript: Getting "jsobject 148795" instead of integer

Post by darth_fader » Thu Jun 24, 2010 7:06 am

I'm not sure about the implementation of the push function:

var args = Array.prototype.slice.call(arguments);
for (var i in args) this.data.push( args );

The JavaScript documentation states that "arguments" is not really an array so there might be some unexpected type conversion involved by calling "slice". Why not simply do:

for (var i = 0; i < arguments.length; i++)
this.data.push (arguments);

What I did not understand is why your countTracks() function divides the length by 2. Notice that the result of the division is a float value.

amounra93
Posts: 432
Joined: Sat Jan 24, 2009 8:16 pm
Location: Arcata, CA
Contact:

Re: JavaScript: Getting "jsobject 148795" instead of integer

Post by amounra93 » Thu Jun 24, 2010 8:05 am

Slice is a function of a string, I believe. I've ran into this before and its confusing. For some reason it seems like js in Max behaves oddly with normal type conversions that js is supposed to make. Since I've only worked with js inside Max, I can only assume this is not ordinary in comparison to what I've read about how it supposed to work.

Parse a string from it when calling its value, or simply +"" to the variable you are calling.

Some of the other stuff is above my head, and I've had a beer or two so I may be missing something, but I think that will solve the issue, at least. The reason why it happens (as js is supposed to automatically type convert with things like that) is beyond me.

There is also arrayfromargs[messagename, arguments], but I don't know that it applies in this instance. Under js docs, "Universally Available Methods" or something.
http://www.aumhaa.com for Monomod and other m4l goodies.

Post Reply