void JSClassWriter::WriteFunctions(String& source)
{
    for (unsigned i = 0; i < klass_->functions_.Size(); i++)
    {
        JSBFunction* function = klass_->functions_.At(i);

        if (function->Skip())
            continue;

        if (function->IsDestructor())
            continue;

        JSFunctionWriter writer(function);
        writer.GenerateSource(source);
    }

}
void JSClassWriter::GenerateNonStaticFunctionsSource(String& source, String& packageName)
{
    source.AppendWithFormat("js_class_get_prototype(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());

    for (unsigned i = 0; i < klass_->functions_.Size(); i++)
    {
        JSBFunction* function = klass_->functions_.At(i);

        if (function->Skip())
            continue;

        if (function->IsConstructor() || function->IsDestructor())
            continue;

        if (function->IsStatic())
            continue;

        if (function->FirstDefaultParameter() != -1)
        {
            source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, DUK_VARARGS);\n", klass_->GetName().CString(), function->GetName().CString());
        }
        else
        {
            source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, %i);\n", klass_->GetName().CString(), function->GetName().CString(), (int)function->GetParameters().Size());
        }

        String scriptName = function->GetName();
        scriptName[0] = tolower(scriptName[0]);
        source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", scriptName.CString());

    }
    source.Append("duk_pop(ctx);\n");
}
void CSClassWriter::WriteNativeFunctions(String& source)
{
    for (unsigned i = 0; i < klass_->functions_.Size(); i++)
    {
        JSBFunction* function = klass_->functions_.At(i);

        if (function->Skip())
            continue;

        if (klass_->IsInterface() && function->IsConstructor())
            continue;

        if (function->IsDestructor())
            continue;

        if (CSTypeHelper::OmitFunction(function))
            continue;

        CSFunctionWriter writer(function);
        writer.GenerateNativeSource(source);
    }

}
    JSBFunction* JSBHaxe::findFunctionInBase(JSBFunction* function) {
        PODVector<JSBClass*>& base = function->GetClass()->GetBaseClasses();
        for (unsigned j = 0; j < base.Size(); j++)
        {
            JSBClass* klass = base[j];

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

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

                JSBFunction* func = functions[j];

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

                if (func->GetName() == function->GetName()) {
                    return func;
                }
            }
        }
        return NULL;
    }
void JSClassWriter::GenerateStaticFunctionsSource(String& source, String& packageName)
{
    // Store function on both the constructor and prototype
    // so can access static functions from both the class constructor and an instance

    source.AppendWithFormat("js_class_get_constructor(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());
    source.AppendWithFormat("js_class_get_prototype(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());

    for (unsigned i = 0; i < klass_->functions_.Size(); i++)
    {
        JSBFunction* function = klass_->functions_.At(i);

        if (function->Skip(BINDINGLANGUAGE_JAVASCRIPT) || OmitFunction(function))
            continue;

        if (function->IsConstructor() || function->IsDestructor())
            continue;

        if (!function->IsStatic())
            continue;

        if (function->FirstDefaultParameter() != -1)
        {
            source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, DUK_VARARGS);\n", klass_->GetName().CString(), function->GetName().CString());
        }
        else
        {
            source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, %i);\n", klass_->GetName().CString(), function->GetName().CString(), (int)function->GetParameters().Size());
        }

        String scriptName = function->GetName();
        scriptName[0] = tolower(scriptName[0]);

        source.Append("duk_dup(ctx, -1);\n");
        source.AppendWithFormat("duk_put_prop_string(ctx, -3, \"%s\");\n", scriptName.CString());
        source.AppendWithFormat("duk_put_prop_string(ctx, -3, \"%s\");\n", scriptName.CString());
    }

    source.Append("duk_pop_2(ctx);\n");
}
void CSClassWriter::GenerateManagedSource(String& sourceOut)
{
    String source = "";

    if (klass_->IsNumberArray())
        return;

    Indent();

    source += "\n";
    String line;

    if (klass_->GetDocString().Length())
    {
        // monodocer -assembly:NETCore.dll -path:en -pretty
        // mdoc export-html -o htmldocs en
        source += IndentLine("/// <summary>\n");
        if (klass_->GetDocString().Contains('\n'))
            source += IndentLine("/* " + klass_->GetDocString() + "*/\n");
        else
            source += IndentLine("/// " + klass_->GetDocString() + "\n");

        source += IndentLine("/// </summary>\n");
    }

    if (klass_->GetBaseClass())
    {

        String baseString = klass_->GetBaseClass()->GetName();

        const PODVector<JSBClass*>& interfaces = klass_->GetInterfaces();

        if (interfaces.Size())
        {
            StringVector baseStrings;
            baseStrings.Push(baseString);
            for (unsigned i = 0; i < interfaces.Size(); i++)
            {
                baseStrings.Push(interfaces.At(i)->GetName());
            }

            baseString = String::Joined(baseStrings, ",");
        }

        line = ToString("public partial class %s%s : %s\n", klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "", baseString.CString());
    }
    else
    {
        String classString = "class";

        if (klass_->IsInterface())
            classString = "interface";

        line = ToString("public partial %s %s%s\n", classString.CString(), klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "");
    }


    source += IndentLine(line);
    source += IndentLine("{\n");

    Indent();

    WriteManagedProperties(source);

    JSBPackage* package = klass_->GetPackage();

    // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
    // https://github.com/dotnet/coreclr/issues/1605
    // line = "[SuppressUnmanagedCodeSecurity]\n";
    // source += IndentLine(line);

    if (!klass_->IsInterface())
    {
        line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
        source += IndentLine(line);
        line = ToString("public static extern IntPtr csb_%s_%s_GetClassIDStatic();\n", package->GetName().CString(), klass_->GetName().CString());
        source += IndentLine(line);
        source += "\n";
    }

    Dedent();

    // managed functions

    CSFunctionWriter::SetWroteConstructor(false);

    for (unsigned i = 0; i < klass_->functions_.Size(); i++)
    {
        JSBFunction* function = klass_->functions_.At(i);

        if (function->Skip())
            continue;

        if (klass_->IsInterface() && function->IsConstructor())
            continue;

        if (function->IsDestructor())
            continue;

        if (CSTypeHelper::OmitFunction(function))
            continue;

        CSFunctionWriter fwriter(function);
        fwriter.GenerateManagedSource(source);

    }

    // There are some constructors being skipped (like HTTPRequest as it uses a vector of strings in args)
    // Make sure we have at least a IntPtr version
    if (!klass_->IsInterface() && !CSFunctionWriter::GetWroteConstructor() && klass_->GetName() != "RefCounted")
    {
        ATOMIC_LOGINFOF("WARNING: %s class didn't write a constructor, filling in generated native constructor", klass_->GetName().CString());

        line = ToString("public %s (IntPtr native) : base (native)\n", klass_->GetName().CString());
        source += IndentLine(line);
        source += IndentLine("{\n");
        source += IndentLine("}\n\n");
    }

    CSFunctionWriter::SetWroteConstructor(false);

    source += IndentLine("}\n");

    Dedent();

    sourceOut += source;
}
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";

}
bool CSTypeHelper::OmitFunction(JSBFunction* function)
{
    if (!function)
        return false;


    if (function->GetSkipLanguage(BINDINGLANGUAGE_CSHARP))
        return true;

    if (function->IsDestructor())
    {
        function->SetSkipLanguage(BINDINGLANGUAGE_CSHARP);
        return true;
    }

    // We need to rename GetType
    if (function->GetName() == "GetType")
    {
        function->SetSkipLanguage(BINDINGLANGUAGE_CSHARP);
        return true;
    }

    if (function->GetReturnType())
    {
        if (JSBVectorType* vtype = function->GetReturnType()->type_->asVectorType())
        {
            if (!vtype->vectorType_->asClassType() || vtype->vectorType_->asClassType()->class_->IsNumberArray())
            {
                function->SetSkipLanguage(BINDINGLANGUAGE_CSHARP);
                return true;
            }
        }
    }

    const Vector<JSBFunctionType*>& parameters = function->GetParameters();

    for (unsigned i = 0; i < parameters.Size(); i++)
    {
        if (JSBVectorType* vtype = parameters[i]->type_->asVectorType())
        {
            if (!vtype->vectorType_->asClassType() || vtype->vectorType_->asClassType()->class_->IsNumberArray())
            {
                function->SetSkipLanguage(BINDINGLANGUAGE_CSHARP);
                return true;
            }

        }

    }

    // filter overloads which differ in PODVector vs Vector/StringHash vs String, etc

    PODVector<JSBFunction*> allFunctions;
    function->GetClass()->GetAllFunctions(allFunctions);

    for (unsigned i = 0; i < allFunctions.Size(); i++)
    {
        JSBFunction* other = allFunctions[i];

        if (other == function || other->GetSkipLanguage(BINDINGLANGUAGE_CSHARP))
            continue;

        if (other->Match(function))
        {
            if (other->GetClass() == function->GetClass())
                other->SetSkipLanguage(BINDINGLANGUAGE_CSHARP);
        }

    }

    return false;
}