Handle<Value> TiUIBase::_add(void* userContext, TiObject*, const Arguments& args)
{
    HandleScope handleScope;
    TiUIBase* obj = (TiUIBase*) userContext;
    if ((args.Length() > 0) && (args[0]->IsObject()))
    {
        TiObject* addObj = getTiObjectFromJsObject(args[0]);
        if ((addObj == NULL) || (!addObj->isUIObject()))
        {
            return ThrowException(String::New(Ti::Msg::Invalid_add_argument));
        }
        TiUIBase* uiObj = (TiUIBase*) addObj;
        NativeObject* childNO = uiObj->getNativeObject();
        if (childNO == NULL)
        {
            return ThrowException(String::New(Ti::Msg::Invalid_add_argument));
        }
        NativeObject* parentNO = obj->getNativeObject();
        if (N_SUCCEEDED(parentNO->addChildNativeObject(childNO)))
        {
            ObjectEntry entry = addObj;
            obj->childControls_.push_back(entry);
            obj->jsChildren_->Set(obj->jsChildren_->Length(), addObj->getValue());
        }
        childNO->release();
        parentNO->release();
    }
    else
    {
        // TODO: expand this exception
    }
    return Undefined();
}
Handle<Value> TiObject::setPropHelper(const char* propString, Local<Value> value, SET_VALUE_CALLBACK cb)
{
    TiObject* destObj = onLookupMember(propString);
    TiObject* srcObj = getTiObjectFromJsObject(value);
    if (srcObj == NULL)
    {
        srcObj = new TiObject(propString);
        srcObj->initializeTiObject(NULL);
        srcObj->setValue(value);
        setTiObjectToJsObject(value, srcObj);
    }
    if (destObj == NULL)
    {
        if ((!canAddMembers()) || (!userCanAddMember(propString)))
        {
            srcObj->release();
            return Undefined();
        }
        destObj = srcObj;
    }
    (destObj->*cb)(value);
    addMember(destObj, propString);
    onSetProperty(propString, value);
    srcObj->release();
    return value;
}
Handle<Value> TiTCPSocketObject::_read(void* userContext, TiObject* /*caller*/, const Arguments& args)
{
    if (args.Length() < 1)
    {
        return ThrowException(String::New(Ti::Msg::Missing_argument));
    }

    HandleScope handleScope;
    TiTCPSocketObject* obj = (TiTCPSocketObject*) userContext;
    NativeTCPSocketObject* ntcp = (NativeTCPSocketObject*) obj->getNativeObject();

    NativeBufferObject* nboSource = NULL;
    if (args[0]->IsObject())
    {
        TiBufferObject* objSource = dynamic_cast<TiBufferObject*>(getTiObjectFromJsObject(args[0]));
        if (objSource != NULL)
        {
            nboSource = (NativeBufferObject*) objSource->getNativeObject();
        }
    }

    // Invalid argument passed
    if (nboSource == NULL)
    {
        return ThrowException(String::New(Ti::Msg::Invalid_arguments));
    }

    // Optional arguments provided
    int sourceOffset = -1, sourceLength = -1;
    if (args.Length() > 1)
    {
        // Should provided both offset and length
        if (args.Length() < 3)
        {
            return ThrowException(String::New(Ti::Msg::Missing_argument));
        }

        Handle<Number> sourceOffsetNum = Handle<Number>::Cast(args[1]);
        Handle<Number> sourceLengthNum = Handle<Number>::Cast(args[2]);
        sourceOffset = (int)sourceOffsetNum->Value();
        sourceLength = (int)sourceLengthNum->Value();
    }

    int bytesRead = 0;
    try
    {
        bytesRead = ntcp->read(nboSource, sourceOffset, sourceLength);
    }
    catch (NativeException& ne)
    {
        return ThrowException(String::New(ne.what()));
    }

    Handle<Number> result = Number::New(bytesRead);
    return handleScope.Close(result);
}
Handle<Value> TiObject::_functCallback(const Arguments& args)
{
    HandleScope handleScope;
    TiObject* obj = getTiObjectFromJsObject(args.Holder());
    if (obj == NULL)
    {
        return Undefined();
    }
    return handleScope.Close(obj->onFunctionCall(args));
}
/* Call back for V8 named properties.  This is the entry point for setting
 * properties from js.  We handle the properties we know and let V8 handle
 * all other properties.
 */
Handle<Value> TiObject::_propSetter(Local<String> prop, Local<Value> value, const AccessorInfo& info)
{
    HandleScope handleScope;
    Handle<Object> result;
    TiObject* obj = getTiObjectFromJsObject(info.Holder());
    if (obj == NULL)
    {
        // We are not tracking this object so let V8 handle it.
        info.Holder()->ForceSet(prop, value);
        return value;
    }
    return obj->setPropHelper(*String::Utf8Value(prop), value, &TiObject::setValue);
}
/* Call back for V8 named properties.  This is the entry point for accessing
 * properties from js.  We handle the properties we know and let V8 handle
 * all other properties.
 */
Handle<Value> TiObject::_propGetter(Local<String> prop, const AccessorInfo& info)
{
    HandleScope handleScope;
    Handle<Object> result;
    TiObject* obj = getTiObjectFromJsObject(info.Holder());
    if (obj == NULL)
    {
        // Returns "empty". This will cause V8 to go back to default lookup.
        return result;
    }
    Handle<ObjectTemplate> global = getObjectTemplateFromJsObject(info.Holder());
    String::Utf8Value propName(prop);
    const char* propString = (const char*)(*propName);
    TiObject* propObject = obj->onLookupMember(propString);
    if (propObject == NULL)
    {
        // TODO: Fix the following block of commented out code. Currently it breaks
        // Titanium runtime.
        /*
        if(obj->canAddMembers())
        {
            // If we're allowed to add members, return an "empty" result
            // so V8 will handle it. V8 will set the value internally so
            // we can ignore non-Titanium objects.
            return result;
        }
        */
        return Handle<Value>();
    }
    Handle<Value> ret = propObject->getValue();
    if (!ret.IsEmpty())
    {
        return handleScope.Close(ret);
    }
    if ((propObject->hasMembers()) || (propObject->isFunction()))
    {
        result = global->NewInstance();
        propObject->setValue(result);
        setTiObjectToJsObject(result, propObject);
    }
    else
    {
        propObject->release();
        return handleScope.Close(propObject->getValue());
    }
    propObject->release();
    return handleScope.Close(result);
}
void TiProxy::setParametersFromObject(void* userContext, Local<Object> obj)
{
    HandleScope handleScope;
    Handle<Value> value;
    Handle<Value> controlValue = getValue();
    if (!controlValue->IsObject())
    {
        return;
    }
    Handle<Array> propNames = obj->GetPropertyNames();
    uint32_t props = propNames->Length();
    Local<Value> propValue;
    for (uint32_t i = 0; i < props; i++)
    {
        Handle<String> propString = Handle<String>::Cast(propNames->Get(Integer::New(i)));
        String::Utf8Value propNameUTF(propString);
        Local<Value> propValue = obj->Get(propString);
        TiObject* foundProp = onLookupMember(*propNameUTF);
        if (foundProp != NULL)
        {
            TiObject* addObj = getTiObjectFromJsObject(propValue);
            if (addObj)
            {
                TiProxy* obj = (TiProxy*) userContext;
                TiProxy* uiObj = (TiProxy*) addObj;
                NativeObject* childNO = uiObj->getNativeObject();
                NativeObject* parentNO = obj->getNativeObject();
                parentNO->addChildNativeObject(childNO);
                parentNO->release();
            }
            else
            {
                foundProp->setValue(propValue);
            }
        }
        else
        {
            // Set any custom properties onto the JS object.
            getValue()->ToObject()->ForceSet(propString, propValue);
        }
    }
}
Handle<Value> TiUITab::_open(void* userContext, TiObject*, const Arguments& args)
{
    HandleScope handleScope;
    if ((args.Length() > 0) && (args[0]->IsObject()))
    {
        TiObject* addObj = getTiObjectFromJsObject(args[0]);
        if ((addObj == NULL) || (!addObj->isUIObject()))
        {
            return Undefined();
        }
        TiUIBase* uiObj = (TiUIBase*) addObj;
        NativeObject* win = uiObj->getNativeObject();
        TiUITab* obj = (TiUITab*) userContext;
        NativeObject* tab = obj->getNativeObject();
        tab->openWindowOnTab(win);
        tab->release();
        win->release();
    }
    else
    {
        return ThrowException(String::New(Native::Msg::Expected_argument_of_type_object_or_external));
    }
    return Undefined();
}