/*!
 * a workaround for the problem illustrated by:
 * http: *www.ditchnet.org/wp/2005/10/21/report-a-bug-friday/
 * <br />
 * use a category on WebScriptObject to allow access to the javascript array
 * that it proxies for (if one exists).  access is through the typical NSArray
 * access methods, or you can use NSArray arrayWithArray: to convert the whole
 * thing.
 *
 * @author <a href="mailto:speth@end.com">James G. Speth</a>
 */

#import "WebScriptObject+JSArrayAccessors.h"

@implementation WebScriptObject (JSArrayAccessors)

/*!
 * Returns the number of objects in the Javascript array.
 */
- (unsigned)count
{
    id result = [self valueForKey:@"length"];
    if (result)
    {
        return [result intValue];
    }
    return 0;
}

/*!
 * Returns the object located at <code>index</code>.
 */
- (id)objectAtIndex:(unsigned)index
{
    id value = [self webScriptValueAtIndex:index];
    if (value)
        return [[value retain] autorelease];
    return nil;
}

/*!
 * Copies the objects contained in the receiver that fall within the specified
 * range to <code>aBuffer</code>.
 */
- (void)getObjects:(id *)aBuffer range:(NSRange)aRange
{
    int i;
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:aRange.length];
    for (i = aRange.location; i < aRange.length; i++)
    {
        id obj = [self objectAtIndex:i];
        [items addObject:obj];
    }
    [items getObjects:aBuffer];
	[items autorelease];
}

@end

