bool JSBHaxe::checkV(JSBClass* clazz, const String name, const String type) {
        PODVector<JSBClass*>& base = clazz->GetBaseClasses();
        for (unsigned j = 0; j < base.Size(); j++)
        {
            JSBClass* baseClass = base[j];

            Vector<String> propertyNames;

            baseClass->GetPropertyNames(propertyNames);

            for (unsigned i = 0; i < propertyNames.Size(); i++)
            {
                JSBProperty* prop = baseClass->GetProperty(propertyNames[i]);
                JSBFunctionType* ftype = NULL;

                if (prop->getter_ && !prop->getter_->Skip())
                {
                    ftype = prop->getter_->GetReturnType();
                }
                else if (prop->setter_ && !prop->setter_->Skip())
                    ftype = prop->setter_->GetParameters()[0];

                if (!ftype)
                    continue;

                String scriptName = prop->GetCasePropertyName();
                if (scriptName == name)
                {
                    return true;
                }
            }
        }
        return false;
    }
void JSBModuleWriter::WriteClassDeclaration(String& source)
{
    Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();

    source += "static void jsb_declare_classes(JSVM* vm)\n{\n";

    source += "duk_context* ctx = vm->GetJSContext();\n";

    String packageName = module_->GetPackage()->GetName();

    for (unsigned i = 0; i < classes.Size(); i++)
    {
        JSBClass* klass = classes.At(i);

        if (klass->IsNumberArray())
            continue;

        source.AppendWithFormat("   js_class_declare<%s>(vm, \"%s\", \"%s\", jsb_constructor_%s);\n", klass->GetNativeName().CString(), packageName.CString(), klass->GetName().CString(), klass->GetName().CString());

        if (klass->HasProperties())
        {
            source.AppendWithFormat("js_class_push_propertyobject(vm, \"%s\", \"%s\");\n", packageName.CString(), klass->GetName().CString());

            Vector<String> pnames;
            klass->GetPropertyNames(pnames);

            for (unsigned j = 0; j < pnames.Size(); j++)
            {
                JSBProperty* prop = klass->GetProperty(pnames[j]);

                source.Append("duk_push_object(ctx);\n");

                if (prop->getter_ && !prop->getter_->Skip())
                {
                    source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 0);\n",
                                            klass->GetName().CString(), prop->getter_->GetName().CString());
                    source.Append("duk_put_prop_string(ctx, -2, \"get\");\n");
                }
                if (prop->setter_ && !prop->setter_->Skip())
                {
                    source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 1);\n",
                                            klass->GetName().CString(), prop->setter_->GetName().CString());
                    source.Append("duk_put_prop_string(ctx, -2, \"set\");\n");
                }

                String propertyName = prop->GetCasePropertyName();
                source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", propertyName.CString());

            }

            source.Append("duk_pop(ctx);\n");

        }
    }

    source += "\n}\n\n";

}
void JSBModule::WriteClassDeclaration(String& source)
{

    source += "static void jsb_declare_classes(JSVM* vm)\n{\n";

    source += "duk_context* ctx = vm->GetJSContext();\n";

    for (unsigned i = 0; i < classes_.Size(); i++)
    {
        JSBClass* klass = classes_.At(i);

        if (klass->isNumberArray())
            continue;

        source.AppendWithFormat("   js_class_declare(vm, \"%s\", jsb_constructor_%s);\n", klass->GetName().CString(), klass->GetName().CString());

        if (klass->hasProperties())
        {
            source.AppendWithFormat("js_class_push_propertyobject(vm, \"%s\");\n", klass->GetName().CString());


            Vector<String> pnames;
            klass->GetPropertyNames(pnames);

            for (unsigned j = 0; j < pnames.Size(); j++)
            {
                JSBProperty* prop = klass->GetProperty(pnames[j]);

                source.Append("duk_push_object(ctx);\n");

                if (prop->getter_ && !prop->getter_->Skip())
                {
                    source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 0);\n",
                                            klass->GetName().CString(), prop->getter_->name_.CString());
                    source.Append("duk_put_prop_string(ctx, -2, \"get\");\n");
                }
                if (prop->setter_ && !prop->setter_->Skip())
                {
                    source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 1);\n",
                                            klass->GetName().CString(), prop->setter_->name_.CString());
                    source.Append("duk_put_prop_string(ctx, -2, \"set\");\n");
                }

                pnames[j][0] = tolower(pnames[j][0]);
                source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", pnames[j].CString());

            }

            source.Append("duk_pop(ctx);\n");

        }
    }

    source += "\n}\n\n";

}
void JSBTypeScript::ExportModuleClasses(const String& moduleName)
{
    JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);

    if (!module->classes_.Size())
        return;

    source_ += "\n";

    for (unsigned i = 0; i < module->classes_.Size(); i++)
    {
        JSBClass* klass = module->classes_.At(i);

        source_ += "   export class " + klass->GetName();
        if (klass->GetBaseClass())
            source_ += " extends " + klass->GetBaseClass()->GetName();

        source_ += " {\n\n";

        Vector<String> propertyNames;

        klass->GetPropertyNames(propertyNames);

        for (unsigned j = 0; j < propertyNames.Size(); j++)
        {

            JSBProperty* prop = klass->GetProperty(propertyNames[j]);

            JSBFunctionType* ftype = NULL;

            if (prop->getter_ && !prop->getter_->Skip())
            {
                ftype = prop->getter_->returnType_;
            }
            else if (prop->setter_ && !prop->setter_->Skip())
                ftype = prop->setter_->parameters_[0];

            if (!ftype)
                continue;

            String scriptType = GetScriptType(ftype);

            String scriptName =  propertyNames[j];
            scriptName[0] = tolower(scriptName[0]);

            source_ += "      " + scriptName + ": " + scriptType + ";\n";

        }

        if (propertyNames.Size())
            source_ += "\n";

        JSBFunction* constructor = klass->GetConstructor();
        if (constructor)
        {
            ExportFunction(constructor);
            source_ += "\n";
        }

        for (unsigned j = 0; j < klass->GetFunctionCount(); j++)
        {

            JSBFunction* func = klass->GetFunction(j);

            if (func->isConstructor_ || func->isDestructor_ || func->Skip())
                continue;

            ExportFunction(func);


        }



        source_ += "\n   }\n\n";

    }

    source_ += "\n";

}
    void JSBHaxe::ExportModuleClasses(JSBModule* module)
    {
        Vector<SharedPtr<JSBClass>> classes = module->GetClasses();

        if (!classes.Size())
            return;

        source_ += "\n";

        for (unsigned i = 0; i < classes.Size(); i++)
        {
            JSBClass* klass = classes.At(i);

            if (klass->IsNumberArray()) {
                source_ += "typedef " + klass->GetName() + " = Array<Float>;\n";
                continue;
            }

            source_ += "@:native(\"Atomic." + klass->GetName() + "\")\n";

            source_ += "extern class " + klass->GetName();

            JSBClass* base = klass->GetBaseClass();

            if (base)
            {
                    source_ += " extends " + base->GetName();
            }

            source_ += " {\n\n";

            Vector<String> propertyNames;

            klass->GetPropertyNames(propertyNames);

            for (unsigned j = 0; j < propertyNames.Size(); j++)
            {

                JSBProperty* prop = klass->GetProperty(propertyNames[j]);

                JSBFunctionType* ftype = NULL;

                if (prop->getter_ && !prop->getter_->Skip())
                {
                    ftype = prop->getter_->GetReturnType();
                }
                else if (prop->setter_ && !prop->setter_->Skip())
                    ftype = prop->setter_->GetParameters()[0];

                if (!ftype)
                    continue;

                String scriptType = GetScriptType(ftype);
                String scriptName = prop->GetCasePropertyName();

                if (!checkV(klass, scriptName, scriptType)) {
                    //rename haxe reserved words
                    if (scriptName == "override") {
                        scriptName = "overide";
                    }
                    if (scriptName == "dynamic") {
                        scriptName = "dynamik";
                    }
                    source_ += "    var " + scriptName + ": " + scriptType + ";\n";
                }

            }

            if (propertyNames.Size())
                source_ += "\n";

            JSBFunction* constructor = klass->GetConstructor();
            if (constructor)
            {
                ExportFunction(constructor);
                source_ += "\n";
            }

            PODVector<JSBFunction*>& functions = klass->GetFunctions();

            for (unsigned j = 0; j < functions.Size(); j++)
            {

                JSBFunction* func = functions[j];

                if (func->IsConstructor() || func->IsDestructor() || func->Skip())
                    continue;

                ExportFunction(func);
            }

            for (unsigned j = 0; j < klass->GetNumHaxeDecl(); j++)
            {
                source_ += "      " + klass->GetHaxeDecl(j) + "\n";
            }

            source_ += "\n}\n\n";

        }

        source_ += "\n";

    }
void JSBTypeScript::ExportModuleClasses(JSBModule* module)
{
    Vector<SharedPtr<JSBClass>> classes = module->GetClasses();

    if (!classes.Size())
        return;

    source_ += "\n";

    for (unsigned i = 0; i < classes.Size(); i++)
    {
        JSBClass* klass = classes.At(i);

        source_ += "   export class " + klass->GetName();

        JSBClass* base = klass->GetBaseClass();

        if (base)
        {
            if (klass->GetPackage() != base->GetPackage())
            {
                source_ += " extends " + base->GetPackage()->GetName() + "." + base->GetName();
            }
            else
            {
                source_ += " extends " + base->GetName();
            }

        }

        source_ += " {\n\n";

        Vector<String> propertyNames;

        klass->GetPropertyNames(propertyNames);

        for (unsigned j = 0; j < propertyNames.Size(); j++)
        {

            JSBProperty* prop = klass->GetProperty(propertyNames[j]);

            JSBFunctionType* ftype = NULL;

            if (prop->getter_ && !prop->getter_->Skip())
            {
                ftype = prop->getter_->GetReturnType();
            }
            else if (prop->setter_ && !prop->setter_->Skip())
                ftype = prop->setter_->GetParameters()[0];

            if (!ftype)
                continue;

            String scriptType = GetScriptType(ftype);
            String scriptName = prop->GetCasePropertyName();

            source_ += "      " + scriptName + ": " + scriptType + ";\n";

        }

        if (propertyNames.Size())
            source_ += "\n";

        JSBFunction* constructor = klass->GetConstructor();
        if (constructor)
        {
            ExportFunction(constructor);
            source_ += "\n";
        }

        PODVector<JSBFunction*>& functions = klass->GetFunctions();

        for (unsigned j = 0; j < functions.Size(); j++)
        {

            JSBFunction* func = functions[j];

            if (func->IsConstructor() || func->IsDestructor() || func->Skip())
                continue;

            ExportFunction(func);


        }

        for (unsigned j = 0; j < klass->GetNumTypeScriptDecl(); j++)
        {
            source_ += "      " + klass->GetTypeScriptDecl(j) + "\n";
        }



        source_ += "\n   }\n\n";

    }

    source_ += "\n";

}