There is no way to access the internal parameters of the actual glyphs, in particular the em box relevant for font size and spacing calculations, so you need to have some sort of reference in your formula like e.g. the exact widths of each letter of the typeface you use at a given size in an array or list or such from which you then can parste the string, calculate the lengths at the reference size and adjust the font size that will actually be used, including reformatting the text if necessary to wrap extra words around. Similarly, you could have an iterative algorithm that adjusts your text based on actually visible pixels checked with sampleImage() and sourceRectAtTime() and other functions, but in any case it's going to be quite complicated. The more fancy you want to go, the more code you have to add to check specific things.
Something like this should be close (assumes your comp is selected and layer 1 is your text layer):
var myPercent = 90; // fill 90% of comp width
var myComp = app.project.activeItem;
var myTextLayer = myComp.layer(1);
var mySourceText = myTextLayer.property("ADBE Text Properties").property("ADBE Text Document");
var myTextDoc = mySourceText.value;
var myFontSize = myTextDoc.fontSize;
var myTextWidth = myTextLayer.sourceRectAtTime(0,false).width;
var newFontSize = ((myComp.width*myPercent/100)/myTextWidth)*myFontSize;
myTextDoc.fontSize = newFontSize;
Yeah, sorry--I didn't read the question close enough. I can't think of any easy way to do it. I thought maybe you could use paragraph text and have the script set the box size to match the comp size, and then increase the font size until until text starts to vanish and then back it off, but I couldn't come up with a reliable algorithm to do that. That would be a good function to have handy--maximize text size to fit in a given rectangle, but I don't have anything like that on hand.