<commit_before>/**
 * Appcelerator Titanium - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2008 Appcelerator, Inc. All Rights Reserved.
 */
#import "objc_bound_object.h"

@implementation ObjcBoundObject

+(id)ValueToID:(Value*)value key:(NSString*)key context:(JSContextRef)context
{
	if (value->IsUndefined())
	{
		return [WebUndefined undefined];
	}
	if (value->IsNull())
	{
		return nil;
	}
	if (value->IsObject())
	{
		return [[ObjcBoundObject alloc] initWithObject:value->ToObject() key:key context:context];
	}
	if (value->IsMethod())
	{
		return [[ObjcBoundObject alloc] initWithObject:value->ToMethod() key:key context:context];
	}
	if (value->IsString())
	{
		return [NSString stringWithCString:value->ToString()];
	}
	if (value->IsInt())
	{
		return [NSNumber numberWithInt:value->ToInt()];
	}
	if (value->IsDouble())
	{
		return [NSNumber numberWithDouble:value->ToDouble()];
	}
	if (value->IsBool())
	{
		return [NSNumber numberWithBool:value->ToBool()];
	}
	if (value->IsList())
	{
		NSMutableArray *result = [[NSMutableArray alloc] init];
		SharedBoundList list = value->ToList();
		for (int c=0;c<list->Size();c++)
		{
			SharedValue v = list->At(c);
			id arg = [ObjcBoundObject ValueToID:v key:[NSString stringWithFormat:@"%@[%d]",key,c] context:context];
			[result addObject:arg];
			[arg release];
		}
		return result;
	}
	return nil;
}

+(SharedValue)IDToValue:(id)arg context:(JSContextRef)context
{
	/**
	 *  Conversion from WebScriptObject
	 *
	 *  JavaScript              ObjC
	 *  ----------              ----------
	 *   null            =>      nil
	 *   undefined       =>      WebUndefined
	 *   number          =>      NSNumber
	 *   boolean         =>      CFBoolean
	 *   string          =>      NSString
	 *   object          =>      id
	 */
	if (arg == nil)
	{
	  return Value::Null;
	}
	if (arg == [WebUndefined undefined])
	{
	  return Value::Undefined;
	}
	if ([arg isKindOfClass:[NSNumber class]])
	{
	  NSNumber *num = (NSNumber*)arg;
	  NSString *type = [NSString stringWithFormat:@"%s",[num objCType]];
	  if ([type isEqualToString:@"c"])
	  {
	    return new Value((bool)[num boolValue]);
	  }
	  else if ([type isEqualToString:@"d"] || [type isEqualToString:@"f"])
	  {
	    return new Value((double)[num doubleValue]);
	  }
	  else
	  {
	    return new Value((int)[num intValue]);
	  }
	}  
	if ([arg isKindOfClass:[NSString class]])
	{
	  return new Value([(NSString*)arg UTF8String]);
	}
	if ([arg isKindOfClass:[NSArray class]])
	{
		SharedBoundList list = new StaticBoundList;
		NSArray* args = (NSArray*)arg;
		for (int c=0;c<(int)[args count];c++)
		{
			id value = [args objectAtIndex:c];
			Value *v = [ObjcBoundObject IDToValue:value context:context];
			list->Append(v);
		}
		return new Value(list);
	}
	if ([arg isKindOfClass:[ObjcBoundObject class]])
	{
		ObjcBoundObject *b = (ObjcBoundObject*)arg;
		SharedBoundObject bound_object = [b boundObject];
		// attempt to up cast to these other types first
		BoundObject *bound_method = dynamic_cast<BoundMethod*>(bound_object.get());
		if (bound_method!=NULL)
		{
			SharedBoundObject bo = bound_method;
			return new Value(bo);
		}
		BoundList *bound_list = dynamic_cast<BoundList*>(bound_object.get());
		if (bound_list!=NULL)
		{
			SharedBoundList bl = bound_list;
			return new Value(bl);
		}
		return new Value(bound_object);
	}
	if ([arg isKindOfClass:[WebScriptObject class]])
	{
		WebScriptObject *script = (WebScriptObject*)arg;
		if ([script respondsToSelector:@selector(isWrappedBoundObject)])
		{
			NSLog(@"===> Wrapped object = %@",script);
			// // script can be a wrapped JS object or one of our 
			// // bound objects
			// BoundObject *bound_object = reinterpret_cast<BoundObject*>(script);
			// // attempt to up cast to these other types first
			// BoundObject *bound_method = dynamic_cast<BoundMethod*>(bound_object);
			// if (bound_method!=NULL)
			// {
			// 	return new Value(bound_method);
			// }
			// BoundList *bound_list = dynamic_cast<BoundList*>(bound_object);
			// if (bound_list!=NULL)
			// {
			// 	return new Value(bound_list);
			// }
			// return new Value(bound_object);
		}
		//FIXME - BoundList
		
		JSObjectRef js = [script JSObject];
		if (JSObjectIsFunction(context,js))
		{
			SharedBoundMethod method = KJSUtil::ToBoundMethod(context,js);
		  	return new Value(method);
		}
		SharedBoundObject object = KJSUtil::ToBoundObject(context,js);
	  	return new Value(object);
	}  
	return Value::Undefined;
}
-(id)initWithObject:(SharedPtr<BoundObject>)obj key:(NSString*)k context:(JSContextRef)ctx
{
	self = [super init];
	if (self!=nil)
	{
		// in objective-c you can't alloc a C++ when an objective-c class 
		// is instantiated
		object = new SharedPtr<BoundObject>(obj);
		key = k;
		context = ctx;
		[key retain];
	}
	return self;
}
-(void)dealloc
{
	[key release];
	delete object;
	context = nil;
	[super dealloc];
}
-(BOOL)isWrappedBoundObject
{
	return YES;
}
-(SharedPtr<BoundObject>)boundObject
{
	SharedPtr<BoundObject> o = object->get();<commit_msg>more osx fixes<commit_after>/**
 * Appcelerator Titanium - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2008 Appcelerator, Inc. All Rights Reserved.
 */
#import "objc_bound_object.h"

@implementation ObjcBoundObject

+(id)ValueToID:(Value*)value key:(NSString*)key context:(JSContextRef)context
{
	if (value->IsUndefined())
	{
		return [WebUndefined undefined];
	}
	if (value->IsNull())
	{
		return nil;
	}
	if (value->IsObject())
	{
		return [[ObjcBoundObject alloc] initWithObject:value->ToObject() key:key context:context];
	}
	if (value->IsMethod())
	{
		return [[ObjcBoundObject alloc] initWithObject:value->ToMethod() key:key context:context];
	}
	if (value->IsString())
	{
		return [NSString stringWithCString:value->ToString()];
	}
	if (value->IsInt())
	{
		return [NSNumber numberWithInt:value->ToInt()];
	}
	if (value->IsDouble())
	{
		return [NSNumber numberWithDouble:value->ToDouble()];
	}
	if (value->IsBool())
	{
		return [NSNumber numberWithBool:value->ToBool()];
	}
	if (value->IsList())
	{
		NSMutableArray *result = [[NSMutableArray alloc] init];
		SharedBoundList list = value->ToList();
		for (int c=0;c<list->Size();c++)
		{
			SharedValue v = list->At(c);
			id arg = [ObjcBoundObject ValueToID:v key:[NSString stringWithFormat:@"%@[%d]",key,c] context:context];
			[result addObject:arg];
			[arg release];
		}
		return result;
	}
	return nil;
}

+(SharedValue)IDToValue:(id)arg context:(JSContextRef)context
{
	/**
	 *  Conversion from WebScriptObject
	 *
	 *  JavaScript              ObjC
	 *  ----------              ----------
	 *   null            =>      nil
	 *   undefined       =>      WebUndefined
	 *   number          =>      NSNumber
	 *   boolean         =>      CFBoolean
	 *   string          =>      NSString
	 *   object          =>      id
	 */
	if (arg == nil)
	{
	  return Value::Null;
	}
	if (arg == [WebUndefined undefined])
	{
	  return Value::Undefined;
	}
	if ([arg isKindOfClass:[NSNumber class]])
	{
	  NSNumber *num = (NSNumber*)arg;
	  NSString *type = [NSString stringWithFormat:@"%s",[num objCType]];
	  if ([type isEqualToString:@"c"])
	  {
	    return Value::NewBool((bool)[num boolValue]);
	  }
	  else if ([type isEqualToString:@"d"] || [type isEqualToString:@"f"])
	  {
	    return Value::NewDouble((double)[num doubleValue]);
	  }
	  else
	  {
	    return Value::NewInt((int)[num intValue]);
	  }
	}  
	if ([arg isKindOfClass:[NSString class]])
	{
	  return Value::NewString([(NSString*)arg UTF8String]);
	}
	if ([arg isKindOfClass:[NSArray class]])
	{
		SharedBoundList list = new StaticBoundList;
		NSArray* args = (NSArray*)arg;
		for (int c=0;c<(int)[args count];c++)
		{
			id value = [args objectAtIndex:c];
			SharedValue v = [ObjcBoundObject IDToValue:value context:context];
			list->Append(v);
		}
		return Value::NewList(list);
	}
	if ([arg isKindOfClass:[ObjcBoundObject class]])
	{
		ObjcBoundObject *b = (ObjcBoundObject*)arg;
		SharedBoundObject bound_object = [b boundObject];
		// attempt to up cast to these other types first
		BoundMethod *bound_method = dynamic_cast<BoundMethod*>(bound_object.get());
		if (bound_method!=NULL)
		{
			SharedBoundMethod sbm = bound_method;
			return Value::NewMethod(sbm);
		}
		BoundList *bound_list = dynamic_cast<BoundList*>(bound_object.get());
		if (bound_list!=NULL)
		{
			SharedBoundList sbl = bound_list;
			return Value::NewList(sbl);
		}
		return Value::NewObject(bound_object);
	}
	if ([arg isKindOfClass:[WebScriptObject class]])
	{
		WebScriptObject *script = (WebScriptObject*)arg;
		if ([script respondsToSelector:@selector(isWrappedBoundObject)])
		{
			NSLog(@"===> Wrapped object = %@",script);
			// // script can be a wrapped JS object or one of our 
			// // bound objects
			// BoundObject *bound_object = reinterpret_cast<BoundObject*>(script);
			// // attempt to up cast to these other types first
			// BoundObject *bound_method = dynamic_cast<BoundMethod*>(bound_object);
			// if (bound_method!=NULL)
			// {
			// 	return new Value(bound_method);
			// }
			// BoundList *bound_list = dynamic_cast<BoundList*>(bound_object);
			// if (bound_list!=NULL)
			// {
			// 	return new Value(bound_list);
			// }
			// return new Value(bound_object);
		}
		//FIXME - BoundList
		
		JSObjectRef js = [script JSObject];
		if (JSObjectIsFunction(context,js))
		{
			SharedBoundMethod method = KJSUtil::ToBoundMethod(context,js);
		  	return Value::NewMethod(method);
		}
		SharedBoundObject object = KJSUtil::ToBoundObject(context,js);
	  	return Value::NewObject(object);
	}  
	return Value::Undefined;
}
-(id)initWithObject:(SharedPtr<BoundObject>)obj key:(NSString*)k context:(JSContextRef)ctx
{
	self = [super init];
	if (self!=nil)
	{
		// in objective-c you can't alloc a C++ when an objective-c class 
		// is instantiated
		object = new SharedPtr<BoundObject>(obj);
		key = k;
		context = ctx;
		[key retain];
	}
	return self;
}
-(void)dealloc
{
	[key release];
	delete object;
	context = nil;
	[super dealloc];
}
-(BOOL)isWrappedBoundObject
{
	return YES;
}
-(SharedPtr<BoundObject>)boundObject
{
	SharedPtr<BoundObject> o = object->get();
{'fraction_non_alphanumeric': 0.1515269782417797, 'fraction_numerical': 0.0015611279149185286, 'mean_word_length': 3.6569741026805995, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5819866', 'n_tokens_mistral': 3791, 'n_tokens_neox': 3395, 'n_words': 761}