Skip to content

Commit

Permalink
optionalclass
Browse files Browse the repository at this point in the history
  • Loading branch information
edkilday committed Sep 6, 2023
1 parent 4c1b528 commit f8e1e6b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
12 changes: 4 additions & 8 deletions Source/buildbindingccpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,15 +827,11 @@ func writeDynamicCPPMethod(method ComponentDefinitionMethod, w LanguageWriter, N
returnCodeLines = append(returnCodeLines, fmt.Sprintf(" return nullptr;"))
returnCodeLines = append(returnCodeLines, fmt.Sprintf("}"))
} else {
if param.ParamNullable != "true" {
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("}"))
} else {
returnCodeLines = append(returnCodeLines, fmt.Sprintf("if (!h%s) return nullptr;", param.ParamName))
}
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("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))

case "basicarray":
requiresInitCall = true
Expand Down
23 changes: 14 additions & 9 deletions Source/buildimplementationjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,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 @@ -764,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("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) // Very bad
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
11 changes: 5 additions & 6 deletions Source/buildimplementationts.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,25 +303,24 @@ func getType(
param ComponentDefinitionParam,
options TypeScriptOptions,
) string {
return getTypeString(param.ParamType, param.ParamClass, param.ParamNullable, options)
return getTypeString(param.ParamType, param.ParamClass, options)
}

func getTypeString(
paramType string,
paramClass string,
paramNullable 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 paramNullable == "true" {
return paramClass + "|undefined"
if paramType == "optionalclass" {
return paramClass + "|null"
}
return paramClass
} else if paramType == "basicarray" {
return getTypeString(paramClass, "", "false", options) + "[]"
return getTypeString(paramClass, "", options) + "[]"
} else if paramType == "double" ||
paramType == "int16" ||
paramType == "int32" ||
Expand Down
1 change: 0 additions & 1 deletion Source/componentdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ type ComponentDefinitionParam struct {
ParamDefaultValue string `xml:"defaultvalue,attr"`
ParamOptional string `xml:"optional,attr"`
ParamDescription string `xml:"description,attr"`
ParamNullable string `xml:"nullable,attr"`
}

// ComponentDefinitionMethod definition of a method provided by the component's API
Expand Down

0 comments on commit f8e1e6b

Please sign in to comment.