void CSFunctionWriter::WriteManagedFunction(String& source) { JSBClass* klass = function_->GetClass(); JSBPackage* package = klass->GetPackage(); String sig; String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType()); GenManagedFunctionParameters(sig); String line = "public "; if (function_->IsStatic()) { line += "static "; } bool marked = false; JSBClass* baseClass = klass->GetBaseClass(); if (baseClass) { JSBFunction* override = baseClass->MatchFunction(function_, true); if (override) { marked = true; if (override->IsVirtual()) line += "override "; else line += "new "; } }
String CSTypeHelper::GetNativeFunctionSignature(JSBFunction* function, String& returnType) { if (function->Skip()) return String::EMPTY; if (function->IsDestructor()) return String::EMPTY; if (OmitFunction(function)) return String::EMPTY; JSBClass* klass = function->GetClass(); JSBPackage* package = klass->GetPackage(); String fname = function->IsConstructor() ? "Constructor" : function->GetName(); returnType = "void"; if (function->IsConstructor()) { returnType = "RefCounted*"; } else if (function->GetReturnType()) { if (function->IsConstructor()) { returnType = ToString("%s*", klass->GetNativeName().CString()); } else if (function->GetReturnClass()) { if (!function->GetReturnClass()->IsNumberArray()) { returnType = ToString("const %s*", function->GetReturnClass()->GetNativeName().CString()); } } else if (function->GetReturnType()->type_->asStringHashType()) { returnType = "unsigned"; } else { returnType = ToString("%s", CSTypeHelper::GetNativeTypeString(function->GetReturnType()).CString()); // ScriptVector is handled by a out parameter if (returnType.Contains("ScriptVector")) returnType = "void"; } } String sig; GenNativeFunctionParameterSignature(function, sig); String functionSig = ToString("csb_%s_%s_%s_%u(%s)", package->GetName().CString(), klass->GetName().CString(), fname.CString(), function->GetID(), sig.CString()); return functionSig; }
void CSFunctionWriter::WriteManagedConstructor(String& source) { JSBClass* klass = function_->GetClass(); JSBPackage* package = klass->GetPackage(); if (klass->GetName() == "RefCounted") return; // wrapping constructor String line; line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString()); source += IndentLine(line); source += IndentLine("{\n"); source += IndentLine("}\n\n"); String sig; GenManagedFunctionParameters(sig); line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString()); source += IndentLine(line); source += IndentLine("{\n"); Indent(); WriteDefaultStructParameters(source); line = ToString("if (typeof(%s) == this.GetType()", klass->GetName().CString()); line += ToString(" || (this.GetType().BaseType == typeof(%s) && !NativeCore.GetNativeType(this.GetType())))\n", klass->GetName().CString()); source += IndentLine(line); source += IndentLine("{\n"); Indent(); String callSig; GenPInvokeCallParameters(callSig); line = ToString("nativeInstance = NativeCore.RegisterNative (csb_%s_%s_Constructor(%s), this);\n", package->GetName().CString(), klass->GetName().CString(), callSig.CString()); source += IndentLine(line); Dedent(); source += IndentLine("}\n"); Dedent(); source += IndentLine("}\n"); }
String JSBHaxe::GetScriptType(JSBFunctionType* ftype) { String scriptType = "Dynamic"; if (ftype->type_->asPrimitiveType()) { JSBPrimitiveType* ptype = ftype->type_->asPrimitiveType(); scriptType = GetPrimitiveType(ptype); return scriptType; } if (ftype->type_->asStringHashType() || ftype->type_->asStringType()) scriptType = "String"; if (ftype->type_->asEnumType()) scriptType = ftype->type_->asEnumType()->enum_->GetName(); if (ftype->type_->asClassType()) { JSBClass* klass = ftype->type_->asClassType()->class_; scriptType = klass->GetName(); if (klass->GetPackage()->GetName() != package_->GetName()) { scriptType = klass->GetPackage()->GetName() + "." + klass->GetName(); } } if (ftype->type_->asVectorType()) { JSBVectorType* vectorType = ftype->type_->asVectorType(); scriptType = "Array<String>"; } return scriptType; }
String JSBTypeScript::GetScriptType(JSBFunctionType* ftype) { String scriptType = "number"; if (ftype->type_->asPrimitiveType()) { JSBPrimitiveType* ptype = ftype->type_->asPrimitiveType(); if (ptype->kind_ == JSBPrimitiveType::Bool) scriptType = "boolean"; } if (ftype->type_->asStringHashType() || ftype->type_->asStringType()) scriptType = "string"; if (ftype->type_->asEnumType()) scriptType = ftype->type_->asEnumType()->enum_->GetName(); if (ftype->type_->asClassType()) { JSBClass* klass = ftype->type_->asClassType()->class_; scriptType = klass->GetName(); if (klass->GetPackage()->GetName() != package_->GetName()) { scriptType = klass->GetPackage()->GetName() + "." + klass->GetName(); } } if (ftype->type_->asVectorType()) { scriptType = "string[]"; } return scriptType; }
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 CSFunctionWriter::WriteNativeFunction(String& source) { JSBClass* klass = function_->GetClass(); JSBPackage* package = klass->GetPackage(); String fname = function_->IsConstructor() ? "Constructor" : function_->GetName(); String returnType; String functionSig = CSTypeHelper::GetNativeFunctionSignature(function_, returnType); String line; line = ToString("ATOMIC_EXPORT_API %s %s\n", returnType.CString(), functionSig.CString()); source += IndentLine(line); source += IndentLine("{\n"); Indent(); source += "\n"; bool returnValue = false; bool sharedPtrReturn = false; String returnStatement; if (returnType == "const char*") { returnValue = true; source += IndentLine("static String returnValue;\n"); returnStatement = "returnValue = "; } else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray()) { returnStatement = "*returnValue = "; } else if (function_->GetReturnClass() && function_->GetReturnType()->isSharedPtr_) { returnStatement = ToString("SharedPtr<%s> returnValue = ", function_->GetReturnClass()->GetNativeName().CString()); sharedPtrReturn = true; } else { if (returnType != "void") { returnStatement = "return "; } } String callSig; GenNativeCallParameters(callSig); if (!function_->isConstructor_) { if (function_->IsStatic()) { line = ToString("%s%s::%s(%s);\n", returnStatement.CString(), klass->GetNativeName().CString(), function_->GetName().CString(), callSig.CString()); } else { line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString()); } } else { if (klass->IsAbstract()) { line = "return 0; // Abstract Class\n"; } else if (klass->IsObject()) { if (callSig.Length()) line = ToString("return new %s(NETCore::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString()); else line = ToString("return new %s(NETCore::GetContext());\n", klass->GetNativeName().CString()); } else { line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString()); } } source += IndentLine(line); if (sharedPtrReturn) { source += IndentLine("returnValue->AddRef();\n"); source += IndentLine("return returnValue;\n"); } else if (returnType == "const char*") { source += IndentLine("return returnValue.CString();\n"); } Dedent(); source += IndentLine("}\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"; }
void CSFunctionWriter::WriteManagedConstructor(String& source) { JSBClass* klass = function_->GetClass(); JSBPackage* package = klass->GetPackage(); if (klass->GetName() == "RefCounted") return; // wrapping constructor String line; if (!wroteConstructor_) { line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString()); source += IndentLine(line); source += IndentLine("{\n"); source += IndentLine("}\n\n"); } // don't add wrapping constructor for overloads wroteConstructor_ = true; String sig; GenManagedFunctionParameters(sig); line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString()); source += IndentLine(line); source += IndentLine("{\n"); Indent(); WriteDefaultStructParameters(source); source += IndentLine("if (nativeInstance == IntPtr.Zero)\n"); source += IndentLine("{\n"); Indent(); source += IndentLine(ToString("var classType = typeof(%s);\n", klass->GetName().CString())); source += IndentLine("var thisType = this.GetType();\n"); source += IndentLine("var thisTypeIsNative = NativeCore.IsNativeType(thisType);\n"); source += IndentLine("var nativeAncsestorType = NativeCore.GetNativeAncestorType(thisType);\n"); source += IndentLine("if ( (thisTypeIsNative && (thisType == classType)) || (!thisTypeIsNative && (nativeAncsestorType == classType)))\n"); source += IndentLine("{\n"); Indent(); String callSig; GenPInvokeCallParameters(callSig); line = ToString("nativeInstance = NativeCore.RegisterNative (csb_%s_%s_Constructor_%u(%s), this);\n", package->GetName().CString(), klass->GetName().CString(), function_->GetID(), callSig.CString()); source += IndentLine(line); Dedent(); source += IndentLine("}\n"); Dedent(); source += IndentLine("}\n"); Dedent(); source += IndentLine("}\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"; }
void CSFunctionWriter::WriteNativeFunction(String& source) { JSBClass* klass = function_->GetClass(); JSBPackage* package = klass->GetPackage(); String fname = function_->IsConstructor() ? "Constructor" : function_->GetName(); String returnType; String functionSig = CSTypeHelper::GetNativeFunctionSignature(function_, returnType); String line; line = ToString("ATOMIC_EXPORT_API %s %s\n", returnType.CString(), functionSig.CString()); source += IndentLine(line); source += IndentLine("{\n"); Indent(); source += "\n"; // vector marshal bool hasVectorMarshal = false; const Vector<JSBFunctionType*>& fparams = function_->GetParameters(); for (unsigned i = 0; i < fparams.Size(); i++) { JSBFunctionType* ftype = fparams[i]; // Interface JSBClass* interface = 0; if (ftype->type_->asClassType() && ftype->type_->asClassType()->class_->IsInterface()) { // We need to downcast to the interface // TODO: this assumes Object* is in hierarchy, how do we validate this? interface = ftype->type_->asClassType()->class_; line = ToString("%s = dynamic_cast<%s*>((Object*)%s);\n", ftype->name_.CString(), interface->GetNativeName().CString(), ftype->name_.CString()); source += IndentLine(line); } // Vector JSBVectorType* vtype = ftype->type_->asVectorType(); if (!vtype) continue; JSBClassType* classType = vtype->vectorType_->asClassType(); if (!classType) continue; String className = classType->class_->GetName(); String vectorMarshal; hasVectorMarshal = true; if (vtype->isPODVector_) { const String& pname = ftype->name_; source += IndentLine(ToString("PODVector<%s*> %s__vector;\n", className.CString(), pname.CString())); source += IndentLine(ToString("if (%s) %s->AdaptToVector<%s*>(%s__vector);\n", pname.CString(), pname.CString(), className.CString(), pname.CString())); } else { // vectorMarshal = ToString("PODVector<%s*> %s__vector", className.CString(), ftype->name_.CString()); } if (vectorMarshal.Length()) { source += IndentLine(vectorMarshal); vectorMarshal = String::EMPTY; } } bool returnValue = false; bool sharedPtrReturn = false; String returnStatement; if (returnType == "const char*") { returnValue = true; source += IndentLine("static String returnValue;\n"); returnStatement = "returnValue = "; } else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray()) { returnStatement = "*returnValue = "; } else if (function_->GetReturnClass() && function_->GetReturnType()->isSharedPtr_) { returnStatement = ToString("SharedPtr<%s> returnValue = ", function_->GetReturnClass()->GetNativeName().CString()); sharedPtrReturn = true; } else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType()) { // we have an out parameter JSBVectorType* vtype = function_->GetReturnType()->type_->asVectorType(); if (!vtype->vectorTypeIsSharedPtr_ && !vtype->vectorTypeIsWeakPtr_) { returnStatement = ToString("%sVector<%s*> returnValue__vector = ", vtype->isPODVector_ ? "POD" : "", vtype->vectorType_->asClassType()->class_->GetName().CString()); } else { returnStatement = ToString("%sVector<%s<%s>> returnValue__vector = ", vtype->isPODVector_ ? "POD" : "", vtype->vectorTypeIsSharedPtr_ ? "SharedPtr" : "WeakPtr", vtype->vectorType_->asClassType()->class_->GetName().CString()); } } else { if (returnType != "void" && !hasVectorMarshal) { returnStatement = "return "; } else if (returnType != "void") { returnStatement = ToString("%s returnValue = ", returnType.CString()); } } String callSig; GenNativeCallParameters(callSig); if (!function_->isConstructor_) { if (function_->IsStatic()) { line = ToString("%s%s::%s(%s);\n", returnStatement.CString(), klass->GetNativeName().CString(), function_->GetName().CString(), callSig.CString()); } else { line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString()); } } else { if (klass->IsAbstract()) { line = "return 0; // Abstract Class\n"; } else if (klass->IsObject()) { if (callSig.Length()) line = ToString("return new %s(NETCore::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString()); else line = ToString("return new %s(NETCore::GetContext());\n", klass->GetNativeName().CString()); } else { line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString()); } } source += IndentLine(line); // Vector marshaling for (unsigned i = 0; i < fparams.Size(); i++) { JSBFunctionType* ftype = fparams[i]; JSBVectorType* vtype = ftype->type_->asVectorType(); if (!vtype) continue; JSBClassType* classType = vtype->vectorType_->asClassType(); if (!classType) continue; String className = classType->class_->GetName(); String vectorMarshal; if (vtype->isPODVector_) { const String& pname = ftype->name_; source += IndentLine(ToString("if (%s) %s->AdaptFromVector<%s*>(%s__vector);\n", pname.CString(), pname.CString(), className.CString(), pname.CString())); } else { // vectorMarshal = ToString("PODVector<%s*> %s__vector", className.CString(), ftype->name_.CString()); } if (vectorMarshal.Length()) { source += IndentLine(vectorMarshal); vectorMarshal = String::EMPTY; } } if (sharedPtrReturn) { source += IndentLine("if (returnValue.NotNull()) returnValue->AddRef();\n"); source += IndentLine("return returnValue;\n"); } else if (returnType == "const char*") { source += IndentLine("return returnValue.CString();\n"); } else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType()) { // we have an out parameter JSBVectorType* vtype = function_->GetReturnType()->type_->asVectorType(); source += IndentLine("if (returnValue) returnValue->AdaptFromVector(returnValue__vector);\n"); } else if (returnType != "void" && hasVectorMarshal) { source += IndentLine("return returnValue;\n"); } Dedent(); source += IndentLine("}\n"); source += "\n"; }