goscript 0.0.31 → 0.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/expr-call.go +162 -0
- package/compiler/spec.go +6 -0
- package/package.json +1 -1
package/compiler/expr-call.go
CHANGED
|
@@ -378,8 +378,170 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
378
378
|
c.tsw.WriteLiterally(")")
|
|
379
379
|
return nil // Handled make for named slice type
|
|
380
380
|
}
|
|
381
|
+
|
|
382
|
+
// Handle named types with map underlying types: make(NamedMapType)
|
|
383
|
+
if mapType, isMap := namedType.Underlying().(*types.Map); isMap {
|
|
384
|
+
c.tsw.WriteLiterally("$.makeMap<")
|
|
385
|
+
c.WriteGoType(mapType.Key(), GoTypeContextGeneral) // Write the key type
|
|
386
|
+
c.tsw.WriteLiterally(", ")
|
|
387
|
+
c.WriteGoType(mapType.Elem(), GoTypeContextGeneral) // Write the value type
|
|
388
|
+
c.tsw.WriteLiterally(">()")
|
|
389
|
+
return nil // Handled make for named map type
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Handle named types with channel underlying types: make(NamedChannelType, bufferSize)
|
|
393
|
+
if chanType, isChan := namedType.Underlying().(*types.Chan); isChan {
|
|
394
|
+
c.tsw.WriteLiterally("$.makeChannel<")
|
|
395
|
+
c.WriteGoType(chanType.Elem(), GoTypeContextGeneral)
|
|
396
|
+
c.tsw.WriteLiterally(">(")
|
|
397
|
+
|
|
398
|
+
// If buffer size is provided, add it
|
|
399
|
+
if len(exp.Args) >= 2 {
|
|
400
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil {
|
|
401
|
+
return fmt.Errorf("failed to write buffer size in makeChannel: %w", err)
|
|
402
|
+
}
|
|
403
|
+
} else {
|
|
404
|
+
// Default to 0 (unbuffered channel)
|
|
405
|
+
c.tsw.WriteLiterally("0")
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
c.tsw.WriteLiterally(", ") // Add comma for zero value argument
|
|
409
|
+
|
|
410
|
+
// Write the zero value for the channel's element type
|
|
411
|
+
if chanType.Elem().String() == "struct{}" {
|
|
412
|
+
c.tsw.WriteLiterally("{}")
|
|
413
|
+
} else {
|
|
414
|
+
c.WriteZeroValueForType(chanType.Elem())
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Add direction parameter
|
|
418
|
+
c.tsw.WriteLiterally(", ")
|
|
419
|
+
|
|
420
|
+
// Determine channel direction
|
|
421
|
+
switch chanType.Dir() {
|
|
422
|
+
case types.SendRecv:
|
|
423
|
+
c.tsw.WriteLiterally("'both'")
|
|
424
|
+
case types.SendOnly:
|
|
425
|
+
c.tsw.WriteLiterally("'send'")
|
|
426
|
+
case types.RecvOnly:
|
|
427
|
+
c.tsw.WriteLiterally("'receive'")
|
|
428
|
+
default:
|
|
429
|
+
c.tsw.WriteLiterally("'both'") // Default to bidirectional
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
c.tsw.WriteLiterally(")")
|
|
433
|
+
return nil // Handled make for named channel type
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
// Handle instantiated generic types: make(GenericType[TypeArg], ...)
|
|
441
|
+
// This handles cases like: make(Ints[int64]) where Ints[T] is a generic type
|
|
442
|
+
if indexExpr, ok := exp.Args[0].(*ast.IndexExpr); ok {
|
|
443
|
+
// Get the type information for the instantiated generic type
|
|
444
|
+
if typ := c.pkg.TypesInfo.TypeOf(indexExpr); typ != nil {
|
|
445
|
+
// Check the underlying type of the instantiated generic type
|
|
446
|
+
underlying := typ.Underlying()
|
|
447
|
+
|
|
448
|
+
// Handle instantiated generic map types: make(GenericMap[K, V])
|
|
449
|
+
if mapType, isMap := underlying.(*types.Map); isMap {
|
|
450
|
+
c.tsw.WriteLiterally("$.makeMap<")
|
|
451
|
+
c.WriteGoType(mapType.Key(), GoTypeContextGeneral) // Write the key type
|
|
452
|
+
c.tsw.WriteLiterally(", ")
|
|
453
|
+
c.WriteGoType(mapType.Elem(), GoTypeContextGeneral) // Write the value type
|
|
454
|
+
c.tsw.WriteLiterally(">()")
|
|
455
|
+
return nil // Handled make for instantiated generic map type
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// Handle instantiated generic slice types: make(GenericSlice[T], len, cap)
|
|
459
|
+
if sliceType, isSlice := underlying.(*types.Slice); isSlice {
|
|
460
|
+
goElemType := sliceType.Elem()
|
|
461
|
+
|
|
462
|
+
// Check if it's an instantiated generic type with []byte underlying type
|
|
463
|
+
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
464
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
465
|
+
if len(exp.Args) >= 2 {
|
|
466
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
467
|
+
return err
|
|
468
|
+
}
|
|
469
|
+
// Capacity argument for make([]byte, len, cap) is ignored for new Uint8Array(len)
|
|
470
|
+
} else {
|
|
471
|
+
// If no length is provided, default to 0
|
|
472
|
+
c.tsw.WriteLiterally("0")
|
|
473
|
+
}
|
|
474
|
+
c.tsw.WriteLiterally(")")
|
|
475
|
+
return nil // Handled make for instantiated generic []byte type
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// Handle other instantiated generic slice types
|
|
479
|
+
c.tsw.WriteLiterally("$.makeSlice<")
|
|
480
|
+
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
481
|
+
c.tsw.WriteLiterally(">(")
|
|
482
|
+
|
|
483
|
+
if len(exp.Args) >= 2 {
|
|
484
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
485
|
+
return err
|
|
486
|
+
}
|
|
487
|
+
if len(exp.Args) == 3 {
|
|
488
|
+
c.tsw.WriteLiterally(", ")
|
|
489
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
490
|
+
return err
|
|
491
|
+
}
|
|
492
|
+
} else if len(exp.Args) > 3 {
|
|
493
|
+
return errors.New("makeSlice expects 2 or 3 arguments")
|
|
494
|
+
}
|
|
495
|
+
} else {
|
|
496
|
+
// If no length is provided, default to 0
|
|
497
|
+
c.tsw.WriteLiterally("0")
|
|
498
|
+
}
|
|
499
|
+
c.tsw.WriteLiterally(")")
|
|
500
|
+
return nil // Handled make for instantiated generic slice type
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Handle instantiated generic channel types: make(GenericChannel[T], bufferSize)
|
|
504
|
+
if chanType, isChan := underlying.(*types.Chan); isChan {
|
|
505
|
+
c.tsw.WriteLiterally("$.makeChannel<")
|
|
506
|
+
c.WriteGoType(chanType.Elem(), GoTypeContextGeneral)
|
|
507
|
+
c.tsw.WriteLiterally(">(")
|
|
508
|
+
|
|
509
|
+
// If buffer size is provided, add it
|
|
510
|
+
if len(exp.Args) >= 2 {
|
|
511
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil {
|
|
512
|
+
return fmt.Errorf("failed to write buffer size in makeChannel: %w", err)
|
|
381
513
|
}
|
|
514
|
+
} else {
|
|
515
|
+
// Default to 0 (unbuffered channel)
|
|
516
|
+
c.tsw.WriteLiterally("0")
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
c.tsw.WriteLiterally(", ") // Add comma for zero value argument
|
|
520
|
+
|
|
521
|
+
// Write the zero value for the channel's element type
|
|
522
|
+
if chanType.Elem().String() == "struct{}" {
|
|
523
|
+
c.tsw.WriteLiterally("{}")
|
|
524
|
+
} else {
|
|
525
|
+
c.WriteZeroValueForType(chanType.Elem())
|
|
382
526
|
}
|
|
527
|
+
|
|
528
|
+
// Add direction parameter
|
|
529
|
+
c.tsw.WriteLiterally(", ")
|
|
530
|
+
|
|
531
|
+
// Determine channel direction
|
|
532
|
+
switch chanType.Dir() {
|
|
533
|
+
case types.SendRecv:
|
|
534
|
+
c.tsw.WriteLiterally("'both'")
|
|
535
|
+
case types.SendOnly:
|
|
536
|
+
c.tsw.WriteLiterally("'send'")
|
|
537
|
+
case types.RecvOnly:
|
|
538
|
+
c.tsw.WriteLiterally("'receive'")
|
|
539
|
+
default:
|
|
540
|
+
c.tsw.WriteLiterally("'both'") // Default to bidirectional
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
c.tsw.WriteLiterally(")")
|
|
544
|
+
return nil // Handled make for instantiated generic channel type
|
|
383
545
|
}
|
|
384
546
|
}
|
|
385
547
|
}
|
package/compiler/spec.go
CHANGED
|
@@ -414,6 +414,12 @@ func (c *GoToTSCompiler) WriteTypeSpec(a *ast.TypeSpec) error {
|
|
|
414
414
|
if err := c.WriteValueExpr(a.Name); err != nil {
|
|
415
415
|
return err
|
|
416
416
|
}
|
|
417
|
+
|
|
418
|
+
// Write type parameters if present (for generics)
|
|
419
|
+
if a.TypeParams != nil {
|
|
420
|
+
c.WriteTypeParameters(a.TypeParams)
|
|
421
|
+
}
|
|
422
|
+
|
|
417
423
|
c.tsw.WriteLiterally(" = ")
|
|
418
424
|
c.WriteTypeExpr(a.Type) // The aliased type
|
|
419
425
|
c.tsw.WriteLine(";")
|