diff --git a/tools/codegen-options/javascript.go b/tools/codegen-options/javascript.go index 5464685e46..f1e87d57d5 100644 --- a/tools/codegen-options/javascript.go +++ b/tools/codegen-options/javascript.go @@ -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: diff --git a/tools/codegen-options/javascript.template b/tools/codegen-options/javascript.template index abed023cce..8e01f48d08 100644 --- a/tools/codegen-options/javascript.template +++ b/tools/codegen-options/javascript.template @@ -13,12 +13,12 @@ EMSCRIPTEN_BINDINGS(jsbindings_generated) { {{define "JsBindingsStruct"}} {{- $struct_name := .QualifiedName }} -value_object("View${{ qualifiedtype .QualifiedName }}") +value_object<{{ cprefix }}{{ $struct_name }}>("{{ jsprefix }}{{ qualifiedtype .QualifiedName }}") {{- range $index, $field := .Fields}} {{- if $field.SkipJavaScript}} // JavaScript binding for {{$field.Name}} is not yet supported, must use default value. {{- else }} - .field("{{ $field.Name }}", &View::{{ $struct_name }}::{{ $field.Name }}) + .field("{{ $field.Name }}", &{{ cprefix }}{{ $struct_name }}::{{ $field.Name }}) {{- end }} {{- end }} ; @@ -45,9 +45,9 @@ EMSCRIPTEN_BINDINGS(jsenums_generated) { {{define "JsEnum"}} {{- $enum_name := .QualifiedName }} -enum_("View${{ qualifiedtype .QualifiedName }}") +enum_<{{ cprefix }}{{ $enum_name }}>("{{ jsprefix }}{{ qualifiedtype .QualifiedName }}") {{- range .Values}} - .value("{{ . }}", View::{{ $enum_name }}::{{ . }}) + .value("{{ . }}", {{ cprefix }}{{ $enum_name }}::{{ . }}) {{- end }} ; {{end}} @@ -64,7 +64,7 @@ Filament.loadGeneratedExtensions = function() { {{end}} {{define "JsExtension"}} - Filament.View.prototype.set{{ .BaseName }}Defaults = function(overrides) { + Filament.{{ classprefix}}set{{ .BaseName }}Defaults = function(overrides) { const options = { {{- range $index, $field := .Fields}} {{- if $field.SkipJavaScript}} @@ -86,7 +86,7 @@ Filament.loadGeneratedExtensions = function() { {{define "TsStruct"}} {{- $struct_name := .QualifiedName }} -export interface View${{ qualifiedtype .QualifiedName }} { +export interface {{ jsprefix }}{{ qualifiedtype .QualifiedName }} { {{- range $index, $field := .Fields}} {{- if $field.SkipJavaScript}} // JavaScript binding for {{$field.Name}} is not yet supported, must use default value. @@ -99,7 +99,7 @@ export interface View${{ qualifiedtype .QualifiedName }} { {{end}} {{define "TsEnum"}} -export enum View${{ qualifiedtype .QualifiedName }} { +export enum {{ jsprefix }}{{ qualifiedtype .QualifiedName }} { {{- range .Values}} {{ . }}, {{- end }} diff --git a/tools/codegen-options/main.go b/tools/codegen-options/main.go index af06ff52cf..4384d919a2 100644 --- a/tools/codegen-options/main.go +++ b/tools/codegen-options/main.go @@ -24,6 +24,8 @@ import ( "runtime" ) +const CodelineMarker = "The remainder of this file is generated by codegen-options" + func findFilamentRoot() string { var ( _, b, _, _ = runtime.Caller(0) @@ -61,8 +63,10 @@ func main() { } EmitSerializer(definitions, filepath.Join(root, "libs", "viewer", "src")) - EmitJavaScript(definitions, filepath.Join(root, "web", "filament-js")) - EditTypeScript(definitions, filepath.Join(root, "web", "filament-js")) + + jsfolder := filepath.Join(root, "web", "filament-js") + EmitJavaScript(definitions, "View", jsfolder) + EditTypeScript(definitions, "View", jsfolder) fmt.Print(` Note that this tool does not generate bindings for setter methods on