Skip to content

Commit

Permalink
A combination of a number of commits to tidy up the PR backlog #203
Browse files Browse the repository at this point in the history
Format go files using gofmt
Reintroduce case insensitive method and property names in the javascript bindings
Use method delarations instead of arrow function in the generated typescript
Support optionalclass return values in the javascript bindings
Don't prepend c++ error messages with "Error: "
  • Loading branch information
edkilday authored Sep 27, 2023
2 parents fbd841d + 67c2744 commit 343f52f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Source/buildbindingccpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("};")
Expand Down
26 changes: 17 additions & 9 deletions Source/buildimplementationjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down Expand Up @@ -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);")
}
}
}
Expand Down Expand Up @@ -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<v8::Object>();",
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<v8::Object>()", param.ParamName, param.ParamName, subComponent.NameSpace, param.ParamName, subComponent.Global.ClassTypeIdMethod)
writer.Writeln("v8::Local<v8::Value> 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(
Expand Down
61 changes: 40 additions & 21 deletions Source/buildimplementationts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import (
"errors"
"log"
"path"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)

type TypeScriptOptions struct {
Expand All @@ -57,7 +58,7 @@ func BuildImplementationTS(
log.Printf("Creating TypeScript Implementation")

options := TypeScriptOptions{
Camelize: false,
Camelize: true,
JsArrays: false,
LineLength: 80,
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 + " "
Expand Down Expand Up @@ -178,33 +179,33 @@ 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),
)
}
if i+1 < len(inParams) {
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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) + "[]"
Expand All @@ -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(
Expand Down Expand Up @@ -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])
Expand All @@ -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])
Expand Down

0 comments on commit 343f52f

Please sign in to comment.