codegen: do not hardcode the View namespace. (#5547)

This commit is contained in:
Philip Rideout
2022-05-12 13:52:54 -07:00
committed by GitHub
parent 2709533fb1
commit bef3c85ebf
3 changed files with 33 additions and 21 deletions
+20 -12
View File
@@ -26,7 +26,15 @@ import (
"text/template"
)
func createCodeGenerator() *template.Template {
func createCodeGenerator(namespace string) *template.Template {
jsPrefix := ""
classPrefix := ""
cppPrefix := ""
if namespace != "" {
jsPrefix = namespace + "$"
classPrefix = namespace + ".prototype."
cppPrefix = namespace + "::"
}
// These template extensions are used to transmogrify C++ symbols and value literals into
// JavaScript. We mostly don't need to do anything since the parser has already done some
// massaging and verification (e.g. it removed the trailing "f" from floating point literals).
@@ -42,7 +50,7 @@ func createCodeGenerator() *template.Template {
"qualifiedvalue": func(name string) string {
count := strings.Count(name, "::")
if count > 0 {
name = "Filament.View$" + name
name = "Filament." + jsPrefix + name
}
name = strings.Replace(name, "::", "$", count-1)
name = strings.Replace(name, "::", ".", 1)
@@ -62,18 +70,19 @@ func createCodeGenerator() *template.Template {
case "LinearColor":
return "float3"
}
// HACK: at this point, assume the type is a View-scoped enum
return "View$" + strings.ReplaceAll(cpptype, "::", "$")
return jsPrefix + strings.ReplaceAll(cpptype, "::", "$")
},
"jsprefix": func() string { return jsPrefix },
"cprefix": func() string { return cppPrefix },
"classprefix": func() string { return classPrefix },
}
codegen := template.New("Settings").Funcs(customExtensions)
return template.Must(codegen.ParseFiles("javascript.template"))
}
func EmitJavaScript(definitions []Scope, outputFolder string) {
codegen := createCodeGenerator()
func EmitJavaScript(definitions []Scope, namespace string, outputFolder string) {
codegen := createCodeGenerator(namespace)
{
path := filepath.Join(outputFolder, "jsbindings_generated.cpp")
file, err := os.Create(path)
@@ -127,8 +136,7 @@ func EmitJavaScript(definitions []Scope, outputFolder string) {
}
}
func EditTypeScript(definitions []Scope, folder string) {
const MarkerCodeline = "The remainder of this file is generated by codegen-options"
func EditTypeScript(definitions []Scope, namespace string, folder string) {
path := filepath.Join(folder, "filament.d.ts")
var codelines []string
{
@@ -141,7 +149,7 @@ func EditTypeScript(definitions []Scope, folder string) {
foundMarker := false
for lineNumber := 1; lineScanner.Scan(); lineNumber++ {
codeline := lineScanner.Text()
if strings.Contains(codeline, MarkerCodeline) {
if strings.Contains(codeline, CodelineMarker) {
foundMarker = true
break
}
@@ -163,9 +171,9 @@ func EditTypeScript(definitions []Scope, folder string) {
file.WriteString(codeline)
file.WriteString("\n")
}
file.WriteString("// " + MarkerCodeline + "\n")
file.WriteString("// " + CodelineMarker + "\n")
codegen := createCodeGenerator()
codegen := createCodeGenerator(namespace)
for _, definition := range definitions {
switch definition.(type) {
case *StructDefinition: