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 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"); }
int EBuffer::BlockUnindent() { EPoint B, E; int L; AutoExtend = 0; if (CheckBlock() == 0) return 0; if (RCount <= 0) return 0; B = BB; E = BE; Draw(B.Row, E.Row); if (SetPosR(B.Col, B.Row) == 0) return 0; for (L = B.Row; L <= E.Row; L++) { switch (BlockMode) { case bmStream: case bmLine: if (L < E.Row || E.Col != 0) { int I = LineIndented(L) - 1; if (I >= 0) IndentLine(L, I); } break; case bmColumn: if (L < E.Row) { if (InsText(L, E.Col, 1, 0) == 0) return 0; if (DelText(L, B.Col, 1) == 0) return 0; } break; } } if (SetPosR(B.Col, B.Row) == 0) return 0; return 1; }
void CObjFolder::process(FileEntry *entry) { char szPath[MAX_PATH] = {0}; char szRelPath[MAX_PATH] = {0}; char *pRelPath = szPath; if (strnicmp(entry->name(), ".", 1) == 0 || strnicmp(entry->name(), "..", 2) == 0 ) return; GetCurrentDirectory(MAX_PATH, szPath); strcat(szPath, "\\"); strcat (szPath, entry ->name()); m_nIndent ++; IndentLine(); m_nIndent --; pRelPath += m_nNameOffset; // if (pRelPath[0] == '\\' || pRelPath[0] == '/') { pRelPath ++; } sprintf (szRelPath, ".\\%s", pRelPath); Output ("<File RelativePath=\"%s\"></File>\n", szRelPath/*szPath*/); }
int EBuffer::InsertString(const char *aStr, int aCount) { int P; int C, L; int Y = VToR(CP.Row); if (BFI(this, BFI_InsertKillBlock) == 1) if (CheckBlock() == 1) if (BlockKill() == 0) return 0; if (BFI(this, BFI_Insert) == 0) if (CP.Col < LineLen()) if (KillChar() == 0) return 0; if (InsText(Y, CP.Col, aCount, aStr) == 0) return 0; C = CP.Col; L = VToR(CP.Row); P = CharOffset(RLine(L), C); P += aCount; C = ScreenPos(RLine(L), P); if (SetPos(C, CP.Row) == 0) return 0; if (BFI(this, BFI_Trim) && *aStr != '\t') if (TrimLine(L) == 0) return 0; if (BFI(this, BFI_WordWrap) == 2) { if (DoWrap(0) == 0) return 0; } else if (BFI(this, BFI_WordWrap) == 1) { int P, C = CP.Col; PELine LP; int L; if (C > BFI(this, BFI_RightMargin)) { L = CP.Row; C = BFI(this, BFI_RightMargin); P = CharOffset(LP = RLine(L), C); while ((C > BFI(this, BFI_LeftMargin)) && ((LP->Chars[P] != ' ') && (LP->Chars[P] != 9))) C = ScreenPos(LP, --P); if (P <= BFI(this, BFI_LeftMargin)) { C = BFI(this, BFI_RightMargin); } else C = ScreenPos(LP, P); if (SplitLine(L, C) == 0) return 0; IndentLine(L + 1, BFI(this, BFI_LeftMargin)); if (SetPos(CP.Col - C - 1 + BFI(this, BFI_LeftMargin), CP.Row + 1) == 0) return 0; } } return 1; }
int EBuffer::LineCenter() { if (LineTrim() == 0) return 0; int ind = LineIndented(VToR(CP.Row)); int left = BFI(this, BFI_LeftMargin); int right = BFI(this, BFI_RightMargin); int len = LineLen(); //int chs = len - ind; int newind = left + ((right - left) - (len - ind)) / 2; if (newind < left) newind = left; return IndentLine(VToR(CP.Row), newind); }
void CSFunctionWriter::WriteDefaultStructParameters(String& source) { for (unsigned i = 0; i < defaultStructParameters_.Size(); i++) { const DefaultStructParameter& dparm = defaultStructParameters_[i]; String line = ToString("if (default(%s).Equals(%s)) %s = %s;\n", dparm.type.CString(), dparm.parameterName.CString(), dparm.parameterName.CString(), dparm.assignment.CString()); source += IndentLine(line); } }
static size_t _XML_PackValue2(CONVOPT *opt, unsigned char *p, char *name, ValueStruct *value) { char num[SIZE_NAME + 1]; int i; unsigned char *pp; if (IS_VALUE_NIL(value)) return (0); pp = p; if (value != NULL) { opt->nIndent++; p += IndentLine(opt, p); switch (ValueType(value)) { case GL_TYPE_ARRAY: if (opt->recname != NULL) { p += sprintf(p, "<%s:%s type=\"array\"", opt->recname, name); } else { p += sprintf(p, "<%s type=\"array\"", name); } p += sprintf(p, " count=\"%d\">", (int)ValueArraySize(value)); p += PutCR(opt, p); for (i = 0; i < ValueArraySize(value); i++) { sprintf(num, "%s", name); p += _XML_PackValue2(opt, p, num, ValueArrayItem(value, i)); } p += IndentLine(opt, p); if (opt->recname != NULL) { p += sprintf(p, "</%s:%s>", opt->recname, name); } else { p += sprintf(p, "</%s>", name); } break; case GL_TYPE_ROOT_RECORD: case GL_TYPE_RECORD: if (opt->recname != NULL) { p += sprintf(p, "<%s:%s type=\"record\"", opt->recname, name); } else { p += sprintf(p, "<%s type=\"record\"", name); } p += sprintf(p, " size=\"%d\">", (int)ValueRecordSize(value)); p += PutCR(opt, p); for (i = 0; i < ValueRecordSize(value); i++) { p += _XML_PackValue2(opt, p, ValueRecordName(value, i), ValueRecordItem(value, i)); } p += IndentLine(opt, p); if (opt->recname != NULL) { p += sprintf(p, "</%s:%s>", opt->recname, name); } else { p += sprintf(p, "</%s>", name); } break; case GL_TYPE_ALIAS: if (opt->recname != NULL) { p += sprintf(p, "<%s:%s type=\"alias\">", opt->recname, name); } else { p += sprintf(p, "<%s type=\"alias\">", name); } p += sprintf(p, "%s", ValueAliasName(value)); if (opt->recname != NULL) { p += sprintf(p, "</%s:%s>", opt->recname, name); } else { p += sprintf(p, "</%s>", name); } break; default: if (opt->recname != NULL) { p += sprintf(p, "<%s:%s", opt->recname, name); } else { p += sprintf(p, "<%s", name); } p += sprintf(p, " type="); switch (ValueType(value)) { case GL_TYPE_INT: p += sprintf(p, "\"int\""); break; case GL_TYPE_BOOL: p += sprintf(p, "\"bool\""); break; case GL_TYPE_BYTE: p += sprintf(p, "\"byte\" size=\"%d\"", (int)ValueByteLength(value)); break; case GL_TYPE_BINARY: p += sprintf(p, "\"binary\" size=\"%d\"", (int)ValueByteLength(value)); break; case GL_TYPE_CHAR: p += sprintf(p, "\"char\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_VARCHAR: p += sprintf(p, "\"varchar\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_TEXT: p += sprintf(p, "\"text\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_SYMBOL: p += sprintf(p, "\"symbol\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_DBCODE: p += sprintf(p, "\"dbcode\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_NUMBER: p += sprintf(p, "\"number\" size=\"%d\" ssize=\"%d\"", (int)ValueFixedLength(value), (int)ValueFixedSlen(value)); break; case GL_TYPE_FLOAT: p += sprintf(p, "\"float\""); break; case GL_TYPE_OBJECT: p += sprintf(p, "\"object\""); break; case GL_TYPE_TIMESTAMP: p += sprintf(p, "\"timestamp\""); break; case GL_TYPE_DATE: p += sprintf(p, "\"date\""); break; case GL_TYPE_TIME: p += sprintf(p, "\"time\""); break; default: break; } p += sprintf(p, ">"); if (!IS_VALUE_NIL(value)) { p += XML_Encode(ValueToString(value, ConvCodeset(opt)), p); } if (opt->recname != NULL) { p += sprintf((char *)p, "</%s:%s>", opt->recname, name); } else { p += sprintf((char *)p, "</%s>", name); } break; } p += PutCR(opt, (char *)p); opt->nIndent--; } return (p - pp); }
void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source) { Vector<SharedPtr<JSBEnum>> enums = module_->enums_.Values(); Indent(); for (unsigned i = 0; i < enums.Size(); i++) { JSBEnum* jenum = enums[i]; source += "\n"; String line = "public enum " + jenum->GetName() + "\n"; source += IndentLine(line); source += IndentLine("{\n"); HashMap<String, String>& values = jenum->GetValues(); HashMap<String, String>::ConstIterator itr = values.Begin(); Indent(); while (itr != values.End()) { String name = (*itr).first_; String value = (*itr).second_; if (value.Length()) { line = name + " = " + value; } else { line = name; } itr++; if (itr != values.End()) line += ","; line += "\n"; source += IndentLine(line); } Dedent(); source += IndentLine("}\n"); } // constants HashMap<String, JSBModule::Constant>& constants = module_->GetConstants(); if (constants.Size()) { source += "\n"; String line = "public static partial class Constants\n"; source += IndentLine(line); source += IndentLine("{\n"); const Vector<String>& constantsName = constants.Keys(); Indent(); for (unsigned i = 0; i < constantsName.Size(); i++) { const String& cname = constantsName.At(i); JSBModule::Constant& constant = constants[cname]; String managedType = GetManagedPrimitiveType(constant.type); String value = constant.value; if (!value.Length()) continue; //static const unsigned M_MIN_UNSIGNED = 0x00000000; // /static const unsigned M_MAX_UNSIGNED = 0xffffffff; if (cname == "M_MIN_INT") value = "int.MinValue"; if (cname == "M_INFINITY") value = "float.MaxValue"; if (value == "M_MAX_UNSIGNED") value = "0xffffffff"; // Input stuff if (module_->GetName() == "Input") { if (cname.StartsWith("KEY_")) { if (value.Length() == 1 && (IsAlpha(value[0]) || IsDigit(value[0]))) value = "'" + value + "'"; } // https://raw.githubusercontent.com/flibitijibibo/SDL2-CS/master/src/SDL2.cs if (value.StartsWith("SDL_BUTTON_") || value.StartsWith("SDL_HAT_")) { value = "(int) SDL." + value; } else if (value.StartsWith("SDLK_")) { value = "(int) SDL.SDL_Keycode." + value; } else if (value.StartsWith("SDL_SCANCODE_")) { value = "(int) SDL.SDL_Scancode." + value; } else if (value.StartsWith("SDL_CONTROLLER_BUTTON_")) { value = "(int) SDL.SDL_GameControllerButton." + value; } else if (value.StartsWith("SDL_CONTROLLER_AXIS_")) { value = "(int) SDL.SDL_GameControllerAxis." + value; } } String line = "public const " + managedType + " " + cname + " = " + value; if (managedType == "float" && !line.EndsWith("f") && IsDigit(line[line.Length()-1])) line += "f"; line += ";\n"; source += IndentLine(line); } Dedent(); source += "\n"; line = "}\n"; source += IndentLine(line); } source += "\n"; Dedent(); }
int EBuffer::DoWrap(int WrapAll) { int L, Len, C, P, Ind; PELine LP; int Left = BFI(this, BFI_LeftMargin), Right = BFI(this, BFI_RightMargin); int FirstParaLine; int NoChange = 0, NoChangeX = 0; if (Left >= Right) return 0; L = VToR(CP.Row); FirstParaLine = 0; if (L > 0) if (IsLineBlank(L - 1)) FirstParaLine = L; while (L < RCount) { NoChange = 1; if (VToR(CP.Row) != L || L != FirstParaLine) { if (VToR(CP.Row) == L) if (CP.Col <= LineIndented(L)) if (SetPos(Left, CP.Row) == 0) WFAIL(1); Ind = IndentLine(L, Left); if (VToR(CP.Row) == L) if (SetPos((CP.Col + Ind > 0) ? CP.Col + Ind : 0, CP.Row) == 0) WFAIL(2); NoChange = 0; } Len = LineLen(L); if (IsLineBlank(L)) break; if (Len < Right) { int firstwordbeg = -1; int firstwordend = -1; int X; PELine lp; if (L < RCount - 1) { IndentLine(L + 1, 0); if ((ScreenPos(RLine(L + 1), RLine(L + 1)->Count) == 0) || (RLine(L + 1)->Chars[0] == '>') || (RLine(L + 1)->Chars[0] == '<')) break; } else break; if (L + 1 >= RCount) break; lp = RLine(L + 1); for (X = 0; X < lp->Count; X++) { if (firstwordbeg == -1 && ((lp->Chars[X] != ' ') && (lp->Chars[X] != '\t'))) { firstwordbeg = X; } else if (firstwordend == -1 && ((lp->Chars[X] == ' ' || lp->Chars[X] == '\t'))) { firstwordend = X - 1; } } if (firstwordbeg != -1) if (firstwordend == -1) firstwordend = lp->Count; if (firstwordend == -1) break; if (Right - Len > firstwordend - firstwordbeg) { if (JoinLine(L, Len + 1) == 0) WFAIL(3); NoChange = 0; continue; } else IndentLine(L + 1, Left); } else if (Len > Right) { C = Right; P = CharOffset(LP = RLine(L), C); while ((C > Left) && ((LP->Chars[P] != ' ') && (LP->Chars[P] != 9))) C = ScreenPos(LP, --P); if (P <= Left) { L++; continue; } C = ScreenPos(LP, P); if (SplitLine(L, C) == 0) WFAIL(4); IndentLine(L + 1, Left); if (L < RCount - 2 && LineLen(L + 1) == Left) { if (!IsLineBlank(L + 2)) { if (JoinLine(L + 1, Left) == 0) WFAIL(5); } } if (L == VToR(CP.Row) && CP.Col > C) { if (SetPos(Left + CP.Col - C - 1, CP.Row + 1) == 0) WFAIL(6); } NoChange = 0; L++; continue; } if (WrapAll == 0) if (NoChangeX) { //printf("\n\nBreak OUT = %d\n\x7", L); break; } L++; NoChangeX = NoChange; } if (WrapAll == 1) if (SetPosR(Left, (L < RCount - 2) ? (L + 2) : (L < RCount - 1) ? (L + 1) : (RCount - 1)) == 0) WFAIL(7); return 1; }
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"; }
static size_t _XML_SizeValue2(CONVOPT *opt, char *name, ValueStruct *value, char *buff) { char num[SIZE_NAME + 1]; int i; size_t size; size = 0; if (value != NULL) { if (IS_VALUE_NIL(value)) return (0); opt->nIndent++; size += IndentLine(opt, NULL); switch (ValueType(value)) { case GL_TYPE_ARRAY: if (opt->recname != NULL) { size += sprintf(buff, "<%s:%s type=\"array\" count=\"%d\">", opt->recname, name, (int)ValueArraySize(value)); } else { size += sprintf(buff, "<%s type=\"array\" count=\"%d\">", name, (int)ValueArraySize(value)); } size += PutCR(opt, buff); for (i = 0; i < ValueArraySize(value); i++) { sprintf(num, "%s:%d", name, i); size += _XML_SizeValue2(opt, num, ValueArrayItem(value, i), buff); } size += IndentLine(opt, NULL); if (opt->recname != NULL) { size += sprintf(buff, "</%s:%s>", opt->recname, name); } else { size += sprintf(buff, "</%s>", name); } break; case GL_TYPE_ROOT_RECORD: case GL_TYPE_RECORD: if (opt->recname != NULL) { size += sprintf(buff, "<%s:%s type=\"record\" size=\"%d\">", opt->recname, name, (int)ValueRecordSize(value)); } else { size += sprintf(buff, "<%s type=\"record\" size=\"%d\">", name, (int)ValueRecordSize(value)); } size += PutCR(opt, buff); for (i = 0; i < ValueRecordSize(value); i++) { size += _XML_SizeValue2(opt, ValueRecordName(value, i), ValueRecordItem(value, i), buff); } size += IndentLine(opt, NULL); if (opt->recname != NULL) { size += sprintf(buff, "</%s:%s>", opt->recname, name); } else { size += sprintf(buff, "</%s>", name); } break; default: if (opt->recname != NULL) { size += sprintf(buff, "<%s:%s", opt->recname, name); } else { size += sprintf(buff, "<%s", name); } size += 6; // " type=" switch (ValueType(value)) { case GL_TYPE_INT: size += 5; // "int" break; case GL_TYPE_BOOL: size += 6; // "bool" break; case GL_TYPE_BYTE: size += sprintf(buff, "\"byte\" size=\"%d\"", (int)ValueByteLength(value)); break; case GL_TYPE_BINARY: size += sprintf(buff, "\"binary\" size=\"%d\"", (int)ValueByteLength(value)); break; case GL_TYPE_CHAR: size += sprintf(buff, "\"char\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_VARCHAR: size += sprintf(buff, "\"varchar\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_TEXT: size += sprintf(buff, "\"text\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_SYMBOL: size += sprintf(buff, "\"symbol\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_DBCODE: size += sprintf(buff, "\"dbcode\" size=\"%d\"", (int)ValueStringLength(value)); break; case GL_TYPE_NUMBER: size += sprintf(buff, "\"number\" size=\"%d\" ssize=\"%d\"", (int)ValueFixedLength(value), (int)ValueFixedSlen(value)); break; case GL_TYPE_FLOAT: size += 7; // "float" break; case GL_TYPE_OBJECT: size += 8; // "object" break; case GL_TYPE_TIMESTAMP: size += 9; // "timestamp" break; case GL_TYPE_DATE: size += 4; // "date" break; case GL_TYPE_TIME: size += 8; // "time" break; case GL_TYPE_ALIAS: default: break; } size += 1; // ">" if (!IS_VALUE_NIL(value)) { size += XML_EncodeSize(ValueToString(value, ConvCodeset(opt))); } if (opt->recname != NULL) { size += sprintf(buff, "</%s:%s>", opt->recname, name); } else { size += sprintf(buff, "</%s>", name); } break; } size += PutCR(opt, buff); opt->nIndent--; } return (size); }
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 JSONWriter::BuildJSONString(const Value* const node, int depth, bool escape) { switch(node->GetType()) { case Value::TYPE_NULL: json_string_->append("null"); break; case Value::TYPE_BOOLEAN: { bool value; bool result = node->GetAsBoolean(&value); DCHECK(result); json_string_->append(value ? "true" : "false"); break; } case Value::TYPE_INTEGER: { int value; bool result = node->GetAsInteger(&value); DCHECK(result); StringAppendF(json_string_, "%d", value); break; } case Value::TYPE_DOUBLE: { double value; bool result = node->GetAsDouble(&value); DCHECK(result); std::string real = DoubleToString(value); // 如果没有小数位或者'e', 为数字添加一个.0. 这样可以确保读取JSON的时候 // 被解释为一个real而不是int. if(real.find('.')==std::string::npos && real.find('e')==std::string::npos && real.find('E')==std::string::npos) { real.append(".0"); } // JSON规范规定(-1,1)区间的非整数值在小数位前有一个0. ".52"是非法的, // "0.52"是合法的. if(real[0] == '.') { real.insert(0, "0"); } else if(real.length()>1 && real[0]=='-' && real[1]=='.') { // "-.1"非法 "-0.1"合法 real.insert(1, "0"); } json_string_->append(real); break; } case Value::TYPE_STRING: { std::string value; bool result = node->GetAsString(&value); DCHECK(result); if(escape) { JsonDoubleQuote(UTF8ToUTF16(value), true, json_string_); } else { JsonDoubleQuote(value, true, json_string_); } break; } case Value::TYPE_LIST: { json_string_->append("["); if(pretty_print_) { json_string_->append(" "); } const ListValue* list = static_cast<const ListValue*>(node); for(size_t i=0; i<list->GetSize(); ++i) { if(i != 0) { json_string_->append(","); if(pretty_print_) { json_string_->append(" "); } } Value* value = NULL; bool result = list->Get(i, &value); DCHECK(result); BuildJSONString(value, depth, escape); } if(pretty_print_) { json_string_->append(" "); } json_string_->append("]"); break; } case Value::TYPE_DICTIONARY: { json_string_->append("{"); if(pretty_print_) { json_string_->append(kPrettyPrintLineEnding); } const DictionaryValue* dict = static_cast<const DictionaryValue*>(node); for(DictionaryValue::key_iterator key_itr=dict->begin_keys(); key_itr!=dict->end_keys(); ++key_itr) { if(key_itr != dict->begin_keys()) { json_string_->append(","); if(pretty_print_) { json_string_->append(kPrettyPrintLineEnding); } } Value* value = NULL; bool result = dict->GetWithoutPathExpansion(*key_itr, &value); DCHECK(result); if(pretty_print_) { IndentLine(depth+1); } AppendQuotedString(*key_itr); if(pretty_print_) { json_string_->append(": "); } else { json_string_->append(":"); } BuildJSONString(value, depth+1, escape); } if(pretty_print_) { json_string_->append(kPrettyPrintLineEnding); IndentLine(depth); json_string_->append("}"); } else { json_string_->append("}"); } break; } default: // TODO: 处理TYPE_BINARY NOTREACHED() << "unknown json type"; } }
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 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; }
bool CObjFolder::Recusive(char *lpstrRoot) { char szBuf[MAX_PATH * 3] = {0}; // get the folder name char *p = GetCurrentDirName(lpstrRoot); IndentLine(); // output sprintf(szBuf, "<Filter Name=\"%s\">\n", p); Output (szBuf); ////////////////////////////////////////////////////////////////////////// // enumerates files and folders. // The real guts of Search - carries out the actual search. FileEntry *entry_; // 1st, looks for files. for ( entry_ = new FileEntry("*"); entry_->found(); entry_->next() ) { if (entry_ ->isSubdir()) { continue; } else process(entry_); } delete entry_; // 2nd, looks for sub - folders. for ( entry_ = new FileEntry("*"); entry_->found(); entry_->next() ) { if (entry_ ->isSubdir()) { if (entry_ ->isHidden ()) continue; m_nIndent ++; SetCurrentDirectory(entry_->name()); Recusive((char*)entry_->name()); SetCurrentDirectory(".."); m_nIndent --; } // else // process(entry_); } delete entry_; // output. IndentLine(); Output ("</Filter>\n"); return true; }
void CSClassWriter::WriteManagedProperties(String& sourceOut) { String source; if (klass_->HasProperties()) { Vector<String> pnames; klass_->GetPropertyNames(pnames); for (unsigned j = 0; j < pnames.Size(); j++) { JSBProperty* prop = klass_->GetProperty(pnames[j]); JSBFunctionType* fType = NULL; JSBFunctionType* getType = NULL; JSBFunctionType* setType = NULL; if (CSTypeHelper::OmitFunction(prop->getter_) || CSTypeHelper::OmitFunction(prop->setter_)) continue; if (prop->getter_ && !prop->getter_->Skip()) { fType = getType = prop->getter_->GetReturnType(); } if (prop->setter_ && !prop->setter_->Skip()) { setType = prop->setter_->GetParameters()[0]; if (!fType) fType = setType; else if (fType->type_->ToString() != setType->type_->ToString()) continue; } if (!fType) continue; String line = klass_->IsInterface() ? "" : "public "; JSBClass* baseClass = klass_->GetBaseClass(); if (baseClass) { if (baseClass->MatchProperty(prop, true)) { // always new so we don't have to deal with virtual/override on properties line += "new "; } } String type = CSTypeHelper::GetManagedTypeString(fType, false); line += ToString("%s %s\n", type.CString(), prop->name_.CString()); source += IndentLine(line); source += IndentLine("{\n"); Indent(); if (prop->getter_) { if (klass_->IsInterface()) { source += IndentLine("get;\n"); } else { source += IndentLine("get\n"); source += IndentLine("{\n"); Indent(); source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString())); Dedent(); source += IndentLine("}\n"); } } if (prop->setter_) { if (klass_->IsInterface()) { source += IndentLine("set;\n"); } else { source += IndentLine("set\n"); source += IndentLine("{\n"); Indent(); source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString())); Dedent(); source += IndentLine("}\n"); } } Dedent(); source += IndentLine("}\n\n"); } } sourceOut += source; }
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"; }