diff --git a/Source/buildbindingccpp.go b/Source/buildbindingccpp.go index 21de07b6..7a8d6ab1 100644 --- a/Source/buildbindingccpp.go +++ b/Source/buildbindingccpp.go @@ -830,8 +830,8 @@ func writeDynamicCPPMethod(method ComponentDefinitionMethod, w LanguageWriter, N returnCodeLines = append(returnCodeLines, fmt.Sprintf("if (!h%s) {", param.ParamName)) returnCodeLines = append(returnCodeLines, fmt.Sprintf(" %s%s_ERROR_INVALIDPARAM%s;", checkErrorCodeBegin, strings.ToUpper(NameSpace), checkErrorCodeEnd)) returnCodeLines = append(returnCodeLines, fmt.Sprintf("}")) - returnCodeLines = append(returnCodeLines, fmt.Sprintf("return std::shared_ptr<%s>(dynamic_cast<%s*>(%s->polymorphicFactory(h%s)));", CPPClass, CPPClass, makeSharedParameter, param.ParamName)) } + returnCodeLines = append(returnCodeLines, fmt.Sprintf("return std::shared_ptr<%s>(dynamic_cast<%s*>(%s->polymorphicFactory(h%s)));", CPPClass, CPPClass, makeSharedParameter, param.ParamName)) case "basicarray": requiresInitCall = true @@ -1321,7 +1321,7 @@ func writeExceptionClass(w LanguageWriter, NameSpace string, errors ComponentDef w.Writeln(" if (msg.empty()) {") w.Writeln(" msg = getErrorDescription();") w.Writeln(" }") - w.Writeln(" return std::string(\"Error: \") + getErrorName() + \": \" + msg;") + w.Writeln(" return std::string(getErrorName()) + \": \" + msg;") w.Writeln(" }") w.Writeln("};") diff --git a/Source/buildimplementationjs.go b/Source/buildimplementationjs.go index ed980d85..8b4a8e6c 100644 --- a/Source/buildimplementationjs.go +++ b/Source/buildimplementationjs.go @@ -477,11 +477,14 @@ func buildJSInjectionClass(component ComponentDefinition, subComponent Component } if readOnly { cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s);", getter.PropertyGet, class.ClassName, getter.MethodName) + cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s);", camelize(getter.PropertyGet), class.ClassName, getter.MethodName) } else { cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s, Cv8%s::v8%s);", setter.PropertySet, class.ClassName, getter.MethodName, class.ClassName, setter.MethodName) + cppw.Writeln(" Cv8toolsUtils::Set_proto_accessor(localClassTemplate, \"%s\", Cv8%s::v8%s, Cv8%s::v8%s);", camelize(setter.PropertySet), class.ClassName, getter.MethodName, class.ClassName, setter.MethodName) } } else { cppw.Writeln(" Cv8toolsUtils::Set_proto_method(localClassTemplate, \"%s\", Cv8%s::v8%s);", method.MethodName, class.ClassName, method.MethodName) + cppw.Writeln(" Cv8toolsUtils::Set_proto_method(localClassTemplate, \"%s\", Cv8%s::v8%s);", camelize(method.MethodName), class.ClassName, method.MethodName) } } cppw.Writeln("") @@ -617,8 +620,8 @@ func buildJSInjectionClass(component ComponentDefinition, subComponent Component param := method.Params[k] if param.ParamPass == "return" { if method.PropertySet == "" { - returnValueArg := generateReturnValue(param, subComponent) - cppw.Writeln("setReturnValue(isolate, args, %s);", returnValueArg) + writeReturnValue(cppw, param, subComponent) + cppw.Writeln("setReturnValue(isolate, args, retVal);") } } } @@ -761,18 +764,23 @@ func v8TypeString(paramType string) string { return v8type } -func generateReturnValue(param ComponentDefinitionParam, subComponent ComponentDefinition) string { - returnValue := "" +func writeReturnValue(writer LanguageWriter, param ComponentDefinitionParam, subComponent ComponentDefinition) { if param.ParamType == "class" { - returnValue = fmt.Sprintf("createV8Instance(objectCreator, param%s, %s_MapClassIdToInjectionClassType(param%s->%s()))", param.ParamName, subComponent.NameSpace, param.ParamName, subComponent.Global.ClassTypeIdMethod) + writer.Writeln( + "auto retVal = param%s?createV8Instance(objectCreator, param%s, %s_MapClassIdToInjectionClassType(param%s->%s())):v8::Local();", + param.ParamName, param.ParamName, subComponent.NameSpace, param.ParamName, subComponent.Global.ClassTypeIdMethod) } else if param.ParamType == "optionalclass" { - returnValue = fmt.Sprintf("param%s?createV8Instance(objectCreator, param%s, %s_MapClassIdToInjectionClassType(param%s->%s())):v8::Local()", param.ParamName, param.ParamName, subComponent.NameSpace, param.ParamName, subComponent.Global.ClassTypeIdMethod) + writer.Writeln("v8::Local retVal = v8::Null(isolate);") + writer.Writeln("if (param%s) {", param.ParamName) + writer.Writeln( + " retVal = createV8Instance(objectCreator, param%s, %s_MapClassIdToInjectionClassType(param%s->%s()));", + param.ParamName, subComponent.NameSpace, param.ParamName, subComponent.Global.ClassTypeIdMethod) + writer.Writeln("}") } else if param.ParamType == "enum" { - returnValue = "(uint32_t) param" + param.ParamName + writer.Writeln("auto retVal = (uint32_t) param%s;", param.ParamName) } else { - returnValue = "param" + param.ParamName + writer.Writeln("auto retVal = param%s;", param.ParamName) } - return returnValue } func cppParamType( diff --git a/Source/buildimplementationts.go b/Source/buildimplementationts.go index 4dfb7810..f5ce2786 100644 --- a/Source/buildimplementationts.go +++ b/Source/buildimplementationts.go @@ -37,8 +37,9 @@ import ( "errors" "log" "path" + "regexp" "strings" - "unicode" + "unicode/utf8" ) type TypeScriptOptions struct { @@ -57,7 +58,7 @@ func BuildImplementationTS( log.Printf("Creating TypeScript Implementation") options := TypeScriptOptions{ - Camelize: false, + Camelize: true, JsArrays: false, LineLength: 80, } @@ -106,11 +107,11 @@ func writeTypescriptEnum( writer LanguageWriter, options TypeScriptOptions, ) error { - writer.Writeln("const enum %s {", getId(enum.Name, options)) + writer.Writeln("const enum %s {", getTypeId(enum.Name, options)) writer.Indentation++ for _, option := range enum.Options { writeCommentEnumOption(option, writer, options) - identifier := getId(option.Name, options) + identifier := getTypeId(option.Name, options) value := option.Value writer.Writeln("%s = %d,", identifier, value) } @@ -140,7 +141,7 @@ func writeTypescriptInterface( options TypeScriptOptions, ) error { writeCommentClass(class, writer, options) - identifier := getId(class.ClassName, options) + identifier := getTypeId(class.ClassName, options) extends := "" if class.ParentClass != "" { extends = "extends " + class.ParentClass + " " @@ -178,18 +179,18 @@ func writeTypescriptMethod( writer.Writeln("") writeCommentMethod(class, method, writer, options) writer.BeginLine() - writer.Printf("%s: (", getId(method.MethodName, options)) + writer.Printf("%s(", getName(method.MethodName, options)) for i, param := range inParams { if param.ParamOptional == "true" { writer.Printf( "%s?: %s", - getId(param.ParamName, options), + getName(param.ParamName, options), getType(param, options), ) } else { writer.Printf( "%s: %s", - getId(param.ParamName, options), + getName(param.ParamName, options), getType(param, options), ) } @@ -197,14 +198,14 @@ func writeTypescriptMethod( writer.Printf(", ") } } - writer.Printf(") => ") + writer.Printf("): ") if len(outParams) > 0 { writer.Printf("[") for i, param := range outParams { writer.Printf( "%s: %s", - getId(param.ParamName, options), + getName(param.ParamName, options), getType(param, options), ) if i+1 < len(outParams) { @@ -258,7 +259,7 @@ func writeTypescriptProperty( writer.Writeln( "%s%s: %s;", readOnly, - getId(getter.PropertyGet, options), + getName(getter.PropertyGet, options), getType(returnParams[0], options), ) return nil @@ -287,7 +288,11 @@ func filterOptional(params []ComponentDefinitionParam) []ComponentDefinitionPara return result } -func getId(identifier string, options TypeScriptOptions) string { +func getTypeId(identifier string, options TypeScriptOptions) string { + return identifier +} + +func getName(identifier string, options TypeScriptOptions) string { if options.Camelize { return camelize(identifier) } @@ -306,10 +311,13 @@ func getTypeString( paramClass string, options TypeScriptOptions, ) string { - if paramType == "class" || paramType == "enum" { + if paramType == "class" || paramType == "optionalclass" || paramType == "enum" { if options.JsArrays && strings.HasSuffix(paramClass, "Vector") { return strings.TrimSuffix(paramClass, "Vector") + "[]" } + if paramType == "optionalclass" { + return paramClass + "|null" + } return paramClass } else if paramType == "basicarray" { return getTypeString(paramClass, "", options) + "[]" @@ -327,13 +335,24 @@ func getTypeString( return paramType } -func camelize(identifier string) string { - if len(identifier) == 0 { - return identifier +func camelize(str string) string { + r, _ := regexp.Compile("^([A-Z]+)([A-Z])(.*)") + result := r.FindStringSubmatch(str) + + if len(result) > 0 { + if len(result) > 1 { + if len(result) == 4 && result[3] == "" { + str = strings.ToLower(str) + } else { + str = strings.ToLower(result[1]) + strings.Join(result[2:], "") + } + } + } else { + r, _ := utf8.DecodeRuneInString(str) + str = strings.ToLower(string(r)) + str[len(string(r)):] } - result := []rune(identifier) - result[0] = unicode.ToLower(result[0]) - return string(result) + + return str } func writeCommentEnumOption( @@ -407,7 +426,7 @@ func writeCommentInParams( ) { for _, param := range params { prefix := " * @param {" + getType(param, options) + "} " + - getId(param.ParamName, options) + " " + getName(param.ParamName, options) + " " lines := getCommentLines(prefix, param.ParamDescription, writer, options) if len(lines) > 0 { writer.Writeln(prefix + lines[0]) @@ -427,7 +446,7 @@ func writeCommentOutParams( ) { for _, param := range params { prefix := " * @returns {" + getType(param, options) + "} " - prefix2 := prefix + getId(param.ParamName, options) + " " + prefix2 := prefix + getName(param.ParamName, options) + " " lines := getCommentLines(prefix2, param.ParamDescription, writer, options) if len(lines) > 0 { writer.Writeln(prefix2 + lines[0])