@@ -44,14 +44,30 @@ type Decl struct {
44
44
RefExpr string
45
45
// NamedRecvType is method named recv declare.
46
46
NamedRecvType string
47
- // JavaScript code that declares basic information about a symbol. For a type
48
- // it configures basic information about the type and its identity. For a function
49
- // or method it contains its compiled body.
50
- DeclCode []byte
47
+ // JavaScript code that declares a local variable for an imported package.
48
+ ImportCode []byte
49
+ // JavaScript code that declares basic information about a named type symbol.
50
+ // It configures basic information about the type and its identity.
51
+ TypeDeclCode []byte
52
+ // JavaScript code that assigns exposed named types to the package.
53
+ ExportTypeCode []byte
54
+ // JavaScript code that declares basic information about an anonymous types.
55
+ // It configures basic information about the type.
56
+ // This is added to the finish setup phase to has access to all packages.
57
+ AnonTypeDeclCode []byte
58
+ // JavaScript code that declares basic information about a function or
59
+ // method symbol. This contains the function's or method's compiled body.
60
+ // This is added to the finish setup phase to has access to all packages.
61
+ FuncDeclCode []byte
62
+ // JavaScript code that assigns exposed functions to the package.
63
+ // This is added to the finish setup phase to has access to all packages.
64
+ ExportFuncCode []byte
51
65
// JavaScript code that initializes reflection metadata about type's method list.
66
+ // This is added to the finish setup phase to has access to all packages.
52
67
MethodListCode []byte
53
68
// JavaScript code that initializes the rest of reflection metadata about a type
54
69
// (e.g. struct fields, array type sizes, element types, etc.).
70
+ // This is added to the finish setup phase to has access to all packages.
55
71
TypeInitCode []byte
56
72
// JavaScript code that needs to be executed during the package init phase to
57
73
// set the symbol up (e.g. initialize package-level variable value).
@@ -68,7 +84,12 @@ type Decl struct {
68
84
// minify returns a copy of Decl with unnecessary whitespace removed from the
69
85
// JS code.
70
86
func (d Decl ) minify () Decl {
71
- d .DeclCode = removeWhitespace (d .DeclCode , true )
87
+ d .ImportCode = removeWhitespace (d .ImportCode , true )
88
+ d .TypeDeclCode = removeWhitespace (d .TypeDeclCode , true )
89
+ d .ExportTypeCode = removeWhitespace (d .ExportTypeCode , true )
90
+ d .AnonTypeDeclCode = removeWhitespace (d .AnonTypeDeclCode , true )
91
+ d .FuncDeclCode = removeWhitespace (d .FuncDeclCode , true )
92
+ d .ExportFuncCode = removeWhitespace (d .ExportFuncCode , true )
72
93
d .MethodListCode = removeWhitespace (d .MethodListCode , true )
73
94
d .TypeInitCode = removeWhitespace (d .TypeInitCode , true )
74
95
d .InitCode = removeWhitespace (d .InitCode , true )
@@ -164,10 +185,10 @@ func (fc *funcContext) importDecls() (importedPaths []string, importDecls []*Dec
164
185
func (fc * funcContext ) newImportDecl (importedPkg * types.Package ) * Decl {
165
186
pkgVar := fc .importedPkgVar (importedPkg )
166
187
d := & Decl {
167
- FullName : importDeclFullName (importedPkg ),
168
- Vars : []string {pkgVar },
169
- DeclCode : []byte (fmt .Sprintf ("\t %s = $packages[\" %s\" ];\n " , pkgVar , importedPkg .Path ())),
170
- InitCode : fc .CatchOutput (1 , func () { fc .translateStmt (fc .importInitializer (importedPkg .Path ()), nil ) }),
188
+ FullName : importDeclFullName (importedPkg ),
189
+ Vars : []string {pkgVar },
190
+ ImportCode : []byte (fmt .Sprintf ("\t %s = $packages[\" %s\" ];\n " , pkgVar , importedPkg .Path ())),
191
+ InitCode : fc .CatchOutput (1 , func () { fc .translateStmt (fc .importInitializer (importedPkg .Path ()), nil ) }),
171
192
}
172
193
d .Dce ().SetAsAlive ()
173
194
return d
@@ -293,17 +314,19 @@ func (fc *funcContext) funcDecls(functions []*ast.FuncDecl) ([]*Decl, error) {
293
314
// package-level variable by which they all are referenced,
294
315
// e.g. init functions and instances of generic functions.
295
316
objName := fc .objectName (o )
317
+ generic := len (instances ) > 1 || ! instances [0 ].IsTrivial ()
318
+ varName := objName
319
+ if generic {
320
+ varName += ` = []`
321
+ }
296
322
varDecl := & Decl {
297
323
FullName : funcVarDeclFullName (o ),
298
- Vars : []string {objName },
324
+ Vars : []string {varName },
299
325
}
300
326
varDecl .Dce ().SetName (o , nil , nil )
301
- if len (instances ) > 1 || ! instances [0 ].IsTrivial () {
302
- varDecl .DeclCode = fc .CatchOutput (0 , func () {
303
- fc .Printf ("%s = {};" , objName )
304
- if o .Exported () {
305
- fc .Printf ("$pkg.%s = %s;" , encodeIdent (fun .Name .Name ), objName )
306
- }
327
+ if generic && o .Exported () {
328
+ varDecl .ExportFuncCode = fc .CatchOutput (1 , func () {
329
+ fc .Printf ("$pkg.%s = %s;" , encodeIdent (fun .Name .Name ), objName )
307
330
})
308
331
}
309
332
funcDecls = append (funcDecls , varDecl )
@@ -359,7 +382,7 @@ func (fc *funcContext) newFuncDecl(fun *ast.FuncDecl, inst typeparams.Instance)
359
382
}
360
383
361
384
fc .pkgCtx .CollectDCEDeps (d , func () {
362
- d .DeclCode = fc .namedFuncContext (inst ).translateTopLevelFunction (fun )
385
+ d .FuncDeclCode = fc .namedFuncContext (inst ).translateTopLevelFunction (fun )
363
386
})
364
387
return d
365
388
}
@@ -438,18 +461,18 @@ func (fc *funcContext) namedTypeDecls(typeNames typesutil.TypeNames) ([]*Decl, e
438
461
// the type definition directly.
439
462
func (fc * funcContext ) newNamedTypeVarDecl (obj * types.TypeName ) * Decl {
440
463
name := fc .objectName (obj )
464
+ generic := fc .pkgCtx .instanceSet .Pkg (obj .Pkg ()).ObjHasInstances (obj )
465
+ varName := name
466
+ if generic {
467
+ varName += ` = []`
468
+ }
441
469
varDecl := & Decl {
442
470
FullName : typeVarDeclFullName (obj ),
443
- Vars : []string {name },
471
+ Vars : []string {varName },
444
472
}
445
473
varDecl .Dce ().SetName (obj , nil , nil )
446
- if fc .pkgCtx .instanceSet .Pkg (obj .Pkg ()).ObjHasInstances (obj ) {
447
- varDecl .DeclCode = fc .CatchOutput (0 , func () {
448
- fc .Printf ("%s = {};" , name )
449
- })
450
- }
451
474
if isPkgLevel (obj ) {
452
- varDecl .TypeInitCode = fc .CatchOutput (0 , func () {
475
+ varDecl .ExportTypeCode = fc .CatchOutput (0 , func () {
453
476
fc .Printf ("$pkg.%s = %s;" , encodeIdent (obj .Name ()), name )
454
477
})
455
478
}
@@ -483,7 +506,7 @@ func (fc *funcContext) newNamedTypeInstDecl(inst typeparams.Instance) (*Decl, er
483
506
d .Dce ().SetName (inst .Object , inst .TNest , inst .TArgs )
484
507
fc .pkgCtx .CollectDCEDeps (d , func () {
485
508
// Code that declares a JS type (i.e. prototype) for each Go type.
486
- d .DeclCode = fc .CatchOutput (0 , func () {
509
+ d .TypeDeclCode = fc .CatchOutput (0 , func () {
487
510
size := int64 (0 )
488
511
constructor := "null"
489
512
@@ -505,7 +528,7 @@ func (fc *funcContext) newNamedTypeInstDecl(inst typeparams.Instance) (*Decl, er
505
528
})
506
529
507
530
// Reflection metadata about methods the type has.
508
- d .MethodListCode = fc .CatchOutput (0 , func () {
531
+ d .MethodListCode = fc .CatchOutput (1 , func () {
509
532
if _ , ok := underlying .(* types.Interface ); ok {
510
533
return
511
534
}
@@ -531,7 +554,7 @@ func (fc *funcContext) newNamedTypeInstDecl(inst typeparams.Instance) (*Decl, er
531
554
// initialize themselves.
532
555
switch t := underlying .(type ) {
533
556
case * types.Array , * types.Chan , * types.Interface , * types.Map , * types.Pointer , * types.Slice , * types.Signature , * types.Struct :
534
- d .TypeInitCode = fc .CatchOutput (0 , func () {
557
+ d .TypeInitCode = fc .CatchOutput (1 , func () {
535
558
fc .Printf ("%s.init(%s);" , fc .instName (inst ), fc .initArgs (t ))
536
559
})
537
560
}
@@ -541,6 +564,10 @@ func (fc *funcContext) newNamedTypeInstDecl(inst typeparams.Instance) (*Decl, er
541
564
542
565
// structConstructor returns JS constructor function for a struct type.
543
566
func (fc * funcContext ) structConstructor (t * types.Struct ) string {
567
+ if t .NumFields () == 0 {
568
+ return `function() { this.$val = this; }`
569
+ }
570
+
544
571
constructor := & strings.Builder {}
545
572
546
573
ctrArgs := make ([]string , t .NumFields ())
@@ -607,7 +634,7 @@ func (fc *funcContext) anonTypeDecls(anonTypes []*types.TypeName) []*Decl {
607
634
}
608
635
d .Dce ().SetName (t , nil , nil )
609
636
fc .pkgCtx .CollectDCEDeps (d , func () {
610
- d .DeclCode = []byte (fmt .Sprintf ("\t %s = $%sType(%s);\n " , t .Name (), strings .ToLower (typeKind (t .Type ())[5 :]), fc .initArgs (t .Type ())))
637
+ d .AnonTypeDeclCode = []byte (fmt .Sprintf ("\t \t %s = $%sType(%s);\n " , t .Name (), strings .ToLower (typeKind (t .Type ())[5 :]), fc .initArgs (t .Type ())))
611
638
})
612
639
decls = append (decls , d )
613
640
}
0 commit comments