From f8e1e6b1abf58dc78608f521b6b863fd49f5420c Mon Sep 17 00:00:00 2001 From: Ed Date: Wed, 6 Sep 2023 23:55:35 +0100 Subject: [PATCH] optionalclass --- Source/buildbindingccpp.go | 12 ++++-------- Source/buildimplementationjs.go | 23 ++++++++++++++--------- Source/buildimplementationts.go | 11 +++++------ Source/componentdefinition.go | 1 - 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/Source/buildbindingccpp.go b/Source/buildbindingccpp.go index 13f4c3d3..70be8194 100644 --- a/Source/buildbindingccpp.go +++ b/Source/buildbindingccpp.go @@ -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 diff --git a/Source/buildimplementationjs.go b/Source/buildimplementationjs.go index a3d4798d..8b4a8e6c 100644 --- a/Source/buildimplementationjs.go +++ b/Source/buildimplementationjs.go @@ -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);") } } } @@ -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()", 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();", + 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 d33b4744..f5ce2786 100644 --- a/Source/buildimplementationts.go +++ b/Source/buildimplementationts.go @@ -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" || diff --git a/Source/componentdefinition.go b/Source/componentdefinition.go index cd6e6de7..38b21d2a 100644 --- a/Source/componentdefinition.go +++ b/Source/componentdefinition.go @@ -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