void CSModuleWriter::GenerateManagedModuleClass(String& sourceOut)
{
    Indent();

    String source;
    String line = ToString("public static partial class %sModule\n", module_->GetName().CString());

    source += IndentLine(line);

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

    Indent();

    source += IndentLine("public static void Initialize()\n");

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

    Indent();

    Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();

    for (unsigned i = 0; i < classes.Size(); i++)
    {
        JSBClass* klass = classes.At(i);
        JSBPackage* package = module_->GetPackage();

        if (klass->IsNumberArray() || klass->IsAbstract())
            continue;

        line = ToString("NativeCore.RegisterNativeType(typeof(%s));\n", klass->GetName().CString());

        source += IndentLine(line);

        line = ToString("NativeCore.nativeClassIDToManagedConstructor [ %s.csb_%s_%s_GetClassIDStatic ()] = (IntPtr x) => {\n",
                        klass->GetName().CString(), package->GetName().CString(), klass->GetName().CString());

        source += IndentLine(line);

        Indent();

        source += IndentLine(ToString("return new %s (x);\n", klass->GetName().CString()));

        Dedent();

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

    }

    Dedent();

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

    Dedent();

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

    sourceOut += source;

    Dedent();
}
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 CSFunctionWriter::GenManagedFunctionParameters(String& sig)
{
    // generate args
    const Vector<JSBFunctionType*>& parameters = function_->GetParameters();

    if (parameters.Size())
    {
        for (unsigned int i = 0; i < parameters.Size(); i++)
        {
            bool isStruct = false;
            JSBFunctionType* ptype = parameters.At(i);

            // ignore "Context" parameters
            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();
                JSBClass* klass = classType->class_;

                if (klass->GetName() == "Context")
                {
                    continue;
                }

                // TODO: we should have a better system for struct type in general
                // This number array is really for JS
                if (klass->IsNumberArray())
                {
                    isStruct = true;
                }
            }

            String managedTypeString = CSTypeHelper::GetManagedTypeString(ptype);

            if (!ptype->isConst_ && (ptype->isReference_ && isStruct))
            {
                // pass by reference
                managedTypeString = "ref " + managedTypeString;
            }

            sig += managedTypeString;

            String init = ptype->initializer_;

            if (init.Length())
            {
                init = MapDefaultParameter(ptype);
                if (init.Length())
                    sig += " = " + init;

            }

            if (i + 1 != parameters.Size())
                sig += ", ";
        }
    }
}
void JSBModuleWriter::WriteForwardDeclarations(String& source)
{
    Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();

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

        if (cls->IsNumberArray())
            continue;

        source.AppendWithFormat("static duk_ret_t jsb_constructor_%s(duk_context* ctx);\n", cls->GetName().CString());
        source.AppendWithFormat("static void jsb_class_define_%s(JSVM* vm);\n", cls->GetName().CString());

    }
}
void CSModuleWriter::GenerateManagedClasses(String& source)
{

    Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();

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

        if (klass->IsNumberArray())
            continue;

        CSClassWriter clsWriter(klass);
        clsWriter.GenerateManagedSource(source);

    }

}
void JSBModuleWriter::WriteClassDefine(String& source)
{
    Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();

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

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

        if (klass->IsNumberArray())
            continue;

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

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

}
void CSFunctionWriter::GenNativeCallParameters(String& sig)
{
    JSBClass* klass = function_->GetClass();

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

    Vector<String> args;

    if (parameters.Size())
    {
        for (unsigned int i = 0; i < parameters.Size(); i++)
        {
            JSBFunctionType* ptype = parameters.At(i);

            // ignore "Context" parameters
            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();
                JSBClass* klass = classType->class_;
                if (klass->GetName() == "Context")
                {
                    continue;
                }

                if (klass->IsNumberArray())
                    args.Push(ToString("*%s", ptype->name_.CString()));
                else
                    args.Push(ToString("%s", ptype->name_.CString()));

            }
            else
            {
                args.Push(ToString("%s", ptype->name_.CString()));
            }

        }
    }

    sig.Join(args, ", ");
}
void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
{
    // generate args
    Vector<JSBFunctionType*>& parameters = function_->GetParameters();

    if (parameters.Size())
    {
        for (unsigned int i = 0; i < parameters.Size(); i++)
        {
            JSBFunctionType* ptype = parameters.At(i);

            // ignore "Context" parameters
            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();
                JSBClass* klass = classType->class_;
                if (klass->GetName() == "Context")
                {
                    continue;
                }
            }

            String name = ptype->name_;

            if (name == "object")
                name = "_object";
            else if (name == "readonly")
                name = "readOnly";
            else if (name == "params")
                name = "parameters";

            if (ptype->type_->asClassType())
            {
                JSBClass* pclass = ptype->type_->asClassType()->class_;
                if (pclass->IsNumberArray())
                {
                    sig += "ref " + name;
                }
                else
                {
                    sig += name + " == null ? IntPtr.Zero : " + name + ".nativeInstance";
                }

            }
            else
            {
                sig += name;
            }

            if (i + 1 != parameters.Size())
                sig += ", ";
        }
    }

    // data marshaller
    if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
    {
        if (function_->GetReturnClass()->IsNumberArray())
        {
            if (sig.Length())
                sig += ", ";

            JSBClass* klass = function_->GetClass();
            sig += ToString("ref %s%sReturnValue", klass->GetName().CString(), function_->GetName().CString());
        }
    }


}
void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
{
    source += "\n";

    // 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);

    String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
    source += IndentLine(line);
    JSBClass* klass = function_->GetClass();
    JSBPackage* package = klass->GetPackage();

    String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());

    if (returnType == "string")
        returnType = "IntPtr";

    if (function_->IsConstructor())
        returnType = "IntPtr";

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

    Vector<String> args;

    if (!function_->IsConstructor() && !function_->IsStatic())
    {
        args.Push("IntPtr self");
    }

    if (parameters.Size())
    {
        for (unsigned int i = 0; i < parameters.Size(); i++)
        {
            JSBFunctionType* ptype = parameters.At(i);

            String name = ptype->name_;

            if (name == "object")
                name = "_object";
            else if (name == "readonly")
                name = "readOnly";
            else if (name == "params")
                name = "parameters";

            // ignore "Context" parameters
            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();
                JSBClass* klass = classType->class_;
                if (klass->GetName() == "Context")
                {
                    continue;
                }

                if (klass->IsNumberArray())
                {
                    args.Push("ref " + klass->GetName() + " " + name);
                }
                else
                {
                    args.Push("IntPtr " + name);
                }
            }
            else
            {
                args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
            }

        }
    }

    if (function_->GetReturnClass())
    {
        JSBClass* retClass = function_->GetReturnClass();
        if (retClass->IsNumberArray())
        {
            args.Push("ref " + retClass->GetName() + " retValue");
        }

    }

    String pstring;
    pstring.Join(args, ", ");

    String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
    line = ToString("private static extern %s csb_%s_%s_%s(%s);\n",
                    returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
                    fname.CString(), pstring.CString());

    source += IndentLine(line);

    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 JSFunctionWriter::WriteParameterMarshal(String& source)
{
    // generate args
    const Vector<JSBFunctionType*>& parameters = function_->GetParameters();

    int cparam = 0;
    if (parameters.Size())
    {
        for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
        {
            JSBFunctionType * ptype = parameters.At(i);

            // ignore "Context" parameters
            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();
                JSBClass* klass = classType->class_;
                if (klass->GetName() == "Context")
                {
                    cparam--;
                    continue;
                }

            }

            String pstring = ptype->ToArgString(cparam);
            const String& init = ptype->initializer_;

            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();

                JSBClass* klass = classType->class_;

                if (!klass->IsNumberArray())
                {
                    if (init.Length())
                    {
                        source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? js_to_class_instance<%s>(ctx, %i, 0) : %s;\n",
                                                pstring.CString(), cparam + 1, klass->GetNativeName().CString(), cparam, init.CString());
                    }
                    else
                    {
                        source.AppendWithFormat("%s = js_to_class_instance<%s>(ctx, %i, 0);\n",
                                                pstring.CString(), klass->GetNativeName().CString(), cparam);
                    }
                }
                else
                {
                    int elements = klass->GetNumberArrayElements();
                    String elementType = klass->GetArrayElementType();
                    source.AppendWithFormat("%s arrayData%i[%i];\n", elementType.CString(), cparam, elements);

                    if (init.Length())
                    {
                        source.AppendWithFormat("const %s& defaultArg%i = %s;\n", klass->GetNativeName().CString(), cparam,  init.CString());
                        source.AppendWithFormat("if (duk_get_top(ctx) >= %i) {\n", cparam + 1);
                    }

                    for (int j = 0; j < elements; j++)
                    {
                        source.AppendWithFormat("duk_get_prop_index(ctx, %i, %i);\n", cparam, j);
                        source.AppendWithFormat("arrayData%i[%i] = (%s) duk_to_number(ctx, -1);\n", cparam, j, elementType.CString());
                    }

                    source.AppendWithFormat("duk_pop_n(ctx, %i);\n", elements);

                    if (init.Length())
                    {
                        source.Append("}\n");

                        source.AppendWithFormat("%s __arg%i(duk_get_top(ctx) >= %i ? (const %s *) arrayData%i : defaultArg%i.Data());\n",
                                                klass->GetNativeName().CString(), cparam, cparam + 1, elementType.CString(), cparam, cparam);
                    }
                    else
                    {
                        source.AppendWithFormat("%s __arg%i(arrayData%i);\n", klass->GetNativeName().CString(), cparam, cparam);
                    }

                }

            }
            else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
            {
                if (init.Length())
                {
                    source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? duk_to_string(ctx, %i) : %s;\n", pstring.CString(), cparam + 1, cparam, init.CString());
                }
                else
                {
                    source.AppendWithFormat("%s = duk_to_string(ctx, %i);\n", pstring.CString(),  cparam);

                }
            }
            else if (ptype->type_->asHeapPtrType())
            {
                if (init.Length())
                {
                    source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? duk_get_heapptr(ctx, %i) : %s;\n", pstring.CString(), cparam + 1, cparam, init.CString());
                }
                else
                {
                    source.AppendWithFormat("%s = duk_get_heapptr(ctx, %i);\n", pstring.CString(),  cparam);

                }
            }
            else if (ptype->type_->asPrimitiveType())
            {
                JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();

                if (prtype->kind_ == JSBPrimitiveType::Bool)
                {
                    if (init.Length())
                    {
                        source.AppendWithFormat("bool __arg%i = duk_get_top(ctx) >= %i ? (duk_to_boolean(ctx, %i) ? true : false) : %s;\n",
                                                cparam,  cparam + 1,   cparam, init.CString());
                    }
                    else
                    {
                        source.AppendWithFormat("bool __arg%i = duk_to_boolean(ctx, %i) ? true : false;\n",  cparam,  cparam);
                    }
                }
                else
                {
                    if (init.Length())
                    {
                        source.AppendWithFormat("double __arg%i = duk_get_top(ctx) >= %i ? (duk_to_number(ctx, %i)) : %s;\n",
                                                cparam,  cparam + 1,   cparam, init.CString());

                    }
                    else
                    {
                        source.AppendWithFormat("double __arg%i = duk_to_number(ctx, %i);\n",  cparam,  cparam);
                    }
                }

            }
            else if (ptype->type_->asEnumType())
            {
                JSBEnumType* etype = ptype->type_->asEnumType();

                if (init.Length())
                {
                    source.AppendWithFormat("%s __arg%i = duk_get_top(ctx) >= %i ? ((%s) ((int) duk_to_number(ctx, %i))) : %s;\n", etype->enum_->GetName().CString(),
                                            cparam,  cparam + 1, etype->enum_->GetName().CString(),  cparam, init.CString());

                }
                else
                {
                    source.AppendWithFormat("%s __arg%i = (%s) ((int)duk_to_number(ctx, %i));\n", etype->enum_->GetName().CString(),
                                            cparam, etype->enum_->GetName().CString(),  cparam);

                }

            }
            else if (ptype->type_->asVectorType())
            {
                JSBVectorType* vtype = ptype->type_->asVectorType();

                if (vtype->isVariantVector_)
                {
                    // variant vector arguments
                    source.AppendWithFormat("VariantVector __arg%i;\nScriptVector* __scriptVectorArg%i = js_to_class_instance<ScriptVector>(ctx, %i, 0);\n", cparam, cparam, cparam);
                    if (!function_->HasMutatedReturn())
                        source.AppendWithFormat("__scriptVectorArg%i->AdaptToVector(__arg%i);\n", cparam, cparam);
                }
                else if (ptype->isConst_)
                {                    
                    // JS/TS side needs work for vector parameters, right now we support const (read only)
                    // Vector of String/StringHash

                    source.AppendWithFormat("%s __arg%i;\n", vtype->ToString().CString(), cparam);

                    source.AppendWithFormat("if (duk_get_top(ctx) >= %i)\n{\n", cparam + 1);
                    source.AppendWithFormat("duk_require_object_coercible(ctx, %i);\n", cparam);
                    source.AppendWithFormat("unsigned sz = duk_get_length(ctx, %i);\n", cparam);
                    source.AppendWithFormat("for (unsigned i = 0; i < sz; i++)\n{\n");

                    source.AppendWithFormat("duk_get_prop_index(ctx, 2, i);\n");

                    if (vtype->vectorType_->asStringType() || vtype->vectorType_->asStringHashType() )
                    {
                        source.AppendWithFormat("__arg%i.Push(duk_get_string(ctx, -1));\n", cparam);
                    }

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

                    source.AppendWithFormat("\n}\n");

                    source.AppendWithFormat("\n}\n");

                }
            }
        }
    }
}
void JSFunctionWriter::WriteConstructor(String& source)
{

    // TODO: refactor this

    if (function_->name_ == "RefCounted")
    {
        source.Append("// finalizer may be called more than once\n" \
                      "static int jsb_finalizer_RefCounted(duk_context *ctx)\n" \
                      "{\n" \
                      "JSVM* vm =  JSVM::GetJSVM(ctx);\n" \
                      \
                      "duk_get_prop_index(ctx, 0, JS_INSTANCE_INDEX_FINALIZED);\n" \
                      \
                      "if (!duk_is_boolean(ctx, -1))\n" \
                      "{\n" \
                      "RefCounted* ref = vm->GetObjectPtr(duk_get_heapptr(ctx, 0));\n" \
                      "vm->RemoveObject(ref);\n" \
                      "ref->ReleaseRef();\n" \
                      "duk_push_boolean(ctx, 1);\n" \
                      "duk_put_prop_index(ctx, 0, JS_INSTANCE_INDEX_FINALIZED);\n" \
                      "}\n" \
                      \
                      "return 0;\n" \
                      "}\n");
    }

    JSBClass* klass = function_->class_;
    JSBClass* base = klass->GetBaseClass();


    // Constructor
    source.AppendWithFormat("duk_ret_t jsb_constructor_%s(duk_context* ctx)\n{\n", klass->GetName().CString());

    source.Append( "\nJSVM* vm = JSVM::GetJSVM(ctx);\n" \
                   "duk_push_this(ctx);\n" \
                   "void *ptr = duk_get_heapptr(ctx, -1);\n" \
                   "duk_pop(ctx);\n\n");

    source.Append("   if (!vm->GetObjectPtr(ptr, true))\n   {\n");

    if (!klass->IsAbstract() && !klass->IsNumberArray())
    {

        String marshal;
        WriteParameterMarshal(marshal);

        String sparams;
        int cparam = 0;

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

        for (unsigned i = 0; i < parameters.Size(); i++, cparam++)
        {
            JSBFunctionType * ptype = parameters.At(i);

            String sarg;

            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();
                JSBClass* klass = classType->class_;
                if (klass->GetName() == "Context")
                {
                    sarg = "vm->GetContext()";
                    cparam--;
                }

            }

            if (!sarg.Length())
            {
                sarg.AppendWithFormat("__arg%i", cparam);
            }

            sparams += sarg;

            if (i + 1 < parameters.Size())
                sparams += ", ";

        }

        source.AppendWithFormat("if (!duk_get_top(ctx) || !duk_is_pointer(ctx, 0))\n"\
            "{\n"\
            "%s\n"\
            "%s* native = new %s(%s);\n" \
            "vm->AddObject(ptr, native, INSTANTIATION_JAVASCRIPT);\n"\
            "}\n" \
            "else if (duk_is_pointer(ctx, 0))\n" \
            "{\n" \
            "RefCounted* rc = (RefCounted*) duk_get_pointer(ctx, 0);\n" \
            "vm->AddObject(ptr, rc, rc->GetInstantiationType());\n" \
            "}\n", marshal.CString(), klass->GetNativeName().CString(), klass->GetNativeName().CString(), sparams.CString());
    }
    else
    {
        if (klass->IsAbstract())
            source.Append("assert(0); // abstract class new'd\n");

        if (klass->IsNumberArray())
            source.Append("assert(0); // number array class new'd\n");

    }
    source.Append("   }\n");

    if (base)
    {
        String basePackage = base->GetModule()->GetPackage()->GetName();
        source.AppendWithFormat("   js_constructor_basecall(ctx, \"%s\", \"%s\");\n", basePackage.CString(), base->GetName().CString());
    }

    if (function_->name_ == "RefCounted")
    {
        source.Append("duk_push_this(ctx);\n "\
                      "duk_push_c_function(ctx, jsb_finalizer_RefCounted, 1);\n "\
                      "duk_set_finalizer(ctx, -2);\n "\
                      "duk_pop(ctx);\n");
    }

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

}
void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
{
    JSBClass* klass = function_->GetClass();
    JSBPackage* package = klass->GetPackage();

    if (klass->IsInterface())
        return;

    source += "\n";

    // 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);

    String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
    source += IndentLine(line);

    String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());

    // handled by out parameter
    if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
        returnType = "void";

    if (returnType == "bool")
    {
        // default boolean marshal is 4 byte windows type BOOL and not 1 byte bool
        // https://blogs.msdn.microsoft.com/jaredpar/2008/10/14/pinvoke-and-bool-or-should-i-say-bool/        
        source += IndentLine("[return: MarshalAs(UnmanagedType.I1)]\n");
    }

    if (returnType == "string")
        returnType = "IntPtr";

    if (function_->IsConstructor())
        returnType = "IntPtr";

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

    Vector<String> args;

    if (!function_->IsConstructor() && !function_->IsStatic())
    {
        args.Push("IntPtr self");
    }

    if (parameters.Size())
    {
        for (unsigned int i = 0; i < parameters.Size(); i++)
        {
            JSBFunctionType* ptype = parameters.At(i);

            String name = ptype->name_;

            if (name == "object")
                name = "_object";
            else if (name == "readonly")
                name = "readOnly";
            else if (name == "params")
                name = "parameters";

            // ignore "Context" parameters
            if (ptype->type_->asClassType())
            {
                JSBClassType* classType = ptype->type_->asClassType();
                JSBClass* klass = classType->class_;
                if (klass->GetName() == "Context")
                {
                    continue;
                }

                if (klass->IsNumberArray())
                {
                    args.Push("ref " + klass->GetName() + " " + name);
                }
                else
                {
                    args.Push("IntPtr " + name);
                }
            }
            else
            {
                args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
            }

        }
    }

    if (function_->GetReturnClass())
    {
        JSBClass* retClass = function_->GetReturnClass();

        if (retClass->IsNumberArray())
        {
            args.Push("ref " + retClass->GetName() + " retValue");
        }

    }
    else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
    {
        args.Push("IntPtr returnValue");
    }

    String pstring;
    pstring.Join(args, ", ");

    String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
    line = ToString("private static extern %s csb_%s_%s_%s_%u(%s);\n",
                    returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
                    fname.CString(), function_->GetID(), pstring.CString());

    source += IndentLine(line);

    source += "\n";

}