1{{/* 2 Copyright 2021 The Dawn Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/}} 16 17{{- /* 18-------------------------------------------------------------------------------- 19Template file for use with src/dawn_node/tools/cmd/idlgen/main.go to generate 20the WebGPU.h header file. 21 22See: 23* https://github.com/ben-clayton/webidlparser/blob/main/ast/ast.go for the AST 24 types used by this template 25* src/dawn_node/tools/cmd/idlgen/main.go for additional structures and functions 26 used by this template 27* https://golang.org/pkg/text/template/ for documentation on the template syntax 28-------------------------------------------------------------------------------- 29*/ -}} 30 31{{- Include "WebGPUCommon.tmpl" -}} 32 33#ifndef DAWN_NODE_GEN_INTEROP_WEBGPU_H_ 34#define DAWN_NODE_GEN_INTEROP_WEBGPU_H_ 35 36#include "src/dawn_node/interop/Core.h" 37 38namespace wgpu { 39namespace interop { 40 41// Initialize() registers the WebGPU types with the Napi environment. 42void Initialize(Napi::Env env); 43 44{{ range $ := .Declarations}} 45{{- if IsDictionary $}}{{template "Dictionary" $}} 46{{- else if IsNamespace $}}{{template "Namespace" $}} 47{{- else if IsInterface $}}{{template "Interface" $}} 48{{- else if IsEnum $}}{{template "Enum" $}} 49{{- else if IsTypedef $}}{{template "Typedef" $}} 50{{- end}} 51{{- end}} 52 53} // namespace interop 54} // namespace wgpu 55 56#endif // DAWN_NODE_GEN_INTEROP_WEBGPU_H_ 57 58 59{{- /* 60-------------------------------------------------------------------------------- 61-- Dictionary emits the C++ header declaration that defines the interop type for 62-- the given ast.Dictionary 63-------------------------------------------------------------------------------- 64*/ -}} 65{{- define "Dictionary"}} 66// dictionary {{$.Name}} 67class {{$.Name}} {{- if $.Inherits }} : public {{$.Inherits}}{{end}} { 68public: 69{{ range $m := $.Members}} 70{{- if IsConstructor $m}} {{$.Name}}(); 71{{ else if IsMember $m}} {{template "DictionaryMember" $m}} 72{{ end}} 73{{- end -}} 74}; 75 76template<> 77class Converter<{{$.Name}}> { 78public: 79 static Result FromJS(Napi::Env, Napi::Value, {{$.Name}}&); 80 static Napi::Value ToJS(Napi::Env, {{$.Name}}); 81}; 82 83std::ostream& operator<<(std::ostream& o, const {{$.Name}}& desc); 84{{end}} 85 86 87{{- /* 88-------------------------------------------------------------------------------- 89-- Namespace emits the C++ header declaration that defines the interop type for 90-- the given ast.Namespace 91-------------------------------------------------------------------------------- 92*/ -}} 93{{- define "Namespace"}} 94// namespace {{$.Name}} 95class {{$.Name}} { 96public: 97 virtual ~{{$.Name}}(); 98 {{$.Name}}(); 99{{- range $c := ConstantsOf $}} 100{{- template "Constant" $c}} 101{{- end}} 102}; 103{{end}} 104 105 106{{- /* 107-------------------------------------------------------------------------------- 108-- Interface emits the C++ header declaration that defines the interop type for 109-- the given ast.Interface 110-------------------------------------------------------------------------------- 111*/ -}} 112{{- define "Interface"}} 113// interface {{$.Name}} 114class {{$.Name}} {{- if $.Inherits }} : public {{$.Inherits}}{{end}} { 115public: 116 static Interface<{{$.Name}}> Bind(Napi::Env, std::unique_ptr<{{$.Name}}>&&); 117 static {{$.Name}}* Unwrap(Napi::Object); 118 119 template<typename T, typename ... ARGS> 120 static inline Interface<{{$.Name}}> Create(Napi::Env env, ARGS&& ... args) { 121 return Bind(env, std::make_unique<T>(std::forward<ARGS>(args)...)); 122 } 123 124 virtual ~{{$.Name}}(); 125 {{$.Name}}(); 126{{- if $s := SetlikeOf $}} 127{{- template "InterfaceSetlike" $s}} 128{{- end}} 129{{- range $m := MethodsOf $}} 130{{- template "InterfaceMethod" $m}} 131{{- end}} 132{{- range $a := AttributesOf $}} 133{{- template "InterfaceAttribute" $a}} 134{{- end}} 135{{- range $c := ConstantsOf $}} 136{{- template "Constant" $c}} 137{{- end}} 138}; 139{{end}} 140 141 142{{- /* 143-------------------------------------------------------------------------------- 144-- Typedef emits the C++ header declaration that defines the interop type for 145-- the given ast.Interface 146-------------------------------------------------------------------------------- 147*/ -}} 148{{- define "Typedef"}} 149using {{$.Name}} = {{template "Type" $.Type}}; 150{{end}} 151 152 153{{- /* 154-------------------------------------------------------------------------------- 155-- Enum emits the C++ header declaration that defines the interop type for 156-- the given ast.Enum 157-------------------------------------------------------------------------------- 158*/ -}} 159{{- define "Enum"}} 160enum class {{$.Name}} { 161{{- range $ := $.Values}} 162 {{EnumEntryName $.Value}}, 163{{- end}} 164}; 165 166template<> 167class Converter<{{$.Name}}> { 168public: 169 static Result FromJS(Napi::Env, Napi::Value, {{$.Name}}&); 170 static Napi::Value ToJS(Napi::Env, {{$.Name}}); 171 static bool FromString(std::string, {{$.Name}}&); 172 static const char* ToString({{$.Name}}); 173}; 174 175std::ostream& operator<<(std::ostream& o, {{$.Name}}); 176{{end}} 177 178 179{{- /* 180-------------------------------------------------------------------------------- 181-- DictionaryMember emits the C++ declaration for a single dictionary ast.Member 182-------------------------------------------------------------------------------- 183*/ -}} 184{{- define "DictionaryMember"}} 185{{- if $.Attribute}}{{template "AttributeType" $}} {{$.Name}} 186{{- if $.Init}} = {{Eval "Literal" "Value" $.Init "Type" $.Type}}{{end}}; 187{{- else }}{{template "Type" $.Type}} {{$.Name}}({{template "Parameters" $.Parameters}}); 188{{- end }} 189{{- end }} 190 191 192{{- /* 193-------------------------------------------------------------------------------- 194-- InterfaceSetlike emits the C++ methods for a setlike interface 195-------------------------------------------------------------------------------- 196*/ -}} 197{{- define "InterfaceSetlike"}} 198 virtual bool has(Napi::Env, {{template "Type" $.Elem}}) = 0; 199 virtual std::vector<{{template "Type" $.Elem}}> keys(Napi::Env) = 0; 200{{- /* TODO(crbug.com/dawn/1143): 201 entries, forEach, size, values 202 read-write: add, clear, or delete 203*/}} 204{{- end }} 205 206 207{{- /* 208-------------------------------------------------------------------------------- 209-- InterfaceMethod emits the C++ declaration for a single interface ast.Member 210-- method 211-------------------------------------------------------------------------------- 212*/ -}} 213{{- define "InterfaceMethod"}} 214{{- range $o := $.Overloads}} 215 virtual {{template "Type" $o.Type}} {{$.Name}}(Napi::Env{{template "ParametersWithLeadingComma" $o.Parameters}}) = 0; 216{{- end }} 217{{- end }} 218 219 220{{- /* 221-------------------------------------------------------------------------------- 222-- InterfaceAttribute emits the C++ declaration for a single interface 223-- ast.Member attribute 224-------------------------------------------------------------------------------- 225*/ -}} 226{{- define "InterfaceAttribute"}} 227 virtual {{template "Type" $.Type}} get{{Title $.Name}}(Napi::Env) = 0; 228{{- if not $.Readonly}} 229 virtual void set{{Title $.Name}}(Napi::Env, {{template "Type" $.Type}} value) = 0; 230{{- end }} 231{{- end }} 232 233 234{{- /* 235-------------------------------------------------------------------------------- 236-- Constant emits the C++ declaration for a single ast.Member constant 237-------------------------------------------------------------------------------- 238*/ -}} 239{{- define "Constant"}} 240 static constexpr {{template "Type" $.Type}} {{$.Name}} = {{Eval "Literal" "Value" $.Init "Type" $.Type}}; 241{{- end }} 242 243 244{{- /* 245-------------------------------------------------------------------------------- 246-- Parameters emits the C++ comma separated list of parameter declarations for 247-- the given []ast.Parameter 248-------------------------------------------------------------------------------- 249*/ -}} 250{{- define "Parameters"}} 251{{- range $i, $param := $ }} 252{{- if $i }}, {{end}} 253{{- template "Parameter" $param}} 254{{- end }} 255{{- end }} 256 257 258{{- /* 259-------------------------------------------------------------------------------- 260-- ParametersWithLeadingComma emits the C++ comma separated list of parameter 261-- declarations for the given []ast.Parameter, starting with a leading comma 262-- for the first parameter 263-------------------------------------------------------------------------------- 264*/ -}} 265{{- define "ParametersWithLeadingComma"}} 266{{- range $i, $param := $ }}, {{/* */}} 267{{- template "Parameter" $param}} 268{{- end }} 269{{- end }} 270 271 272{{- /* 273-------------------------------------------------------------------------------- 274-- Parameter emits the C++ parameter type and name for the given ast.Parameter 275-------------------------------------------------------------------------------- 276*/ -}} 277{{- define "Parameter" -}} 278{{- if $.Init }}{{template "Type" $.Type}} {{$.Name}} 279{{- else if $.Optional}}std::optional<{{template "Type" $.Type}}> {{$.Name}} 280{{- else }}{{template "Type" $.Type}} {{$.Name}} 281{{- end }} 282{{- end}} 283