Skip to content

Commit 142201c

Browse files
authored
fix: import_type_methods: export wrapper type methods (#95)
Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent a310a55 commit 142201c

File tree

17 files changed

+222
-85
lines changed

17 files changed

+222
-85
lines changed

compiler/compiler.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,24 @@ func (c *PackageCompiler) generateIndexFile(compiledFiles []string) error {
434434
case *ast.FuncDecl:
435435
if d.Recv == nil && d.Name.IsExported() {
436436
valueSymbols = append(valueSymbols, sanitizeIdentifier(d.Name.Name))
437+
} else if d.Recv != nil && len(d.Recv.List) == 1 && d.Name.IsExported() {
438+
recvField := d.Recv.List[0]
439+
recvTypeExpr := recvField.Type
440+
if star, ok := recvTypeExpr.(*ast.StarExpr); ok {
441+
recvTypeExpr = star.X
442+
}
443+
if ident, ok := recvTypeExpr.(*ast.Ident); ok {
444+
typeObj := c.pkg.TypesInfo.ObjectOf(ident)
445+
if typeObj != nil && typeObj.Exported() {
446+
if typeName, ok := typeObj.(*types.TypeName); ok {
447+
underlying := typeName.Type().Underlying()
448+
if _, isStruct := underlying.(*types.Struct); !isStruct {
449+
methodName := sanitizeIdentifier(ident.Name + "_" + d.Name.Name)
450+
valueSymbols = append(valueSymbols, methodName)
451+
}
452+
}
453+
}
454+
}
437455
}
438456
case *ast.GenDecl:
439457
for _, spec := range d.Specs {

compliance/tests/import_type_methods/errlist/errlist.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ package errlist
22

33
type ErrorList []string
44

5-
func (p *ErrorList) Add(msg string) {
6-
*p = append(*p, msg)
7-
}
8-
9-
type AStruct struct {
10-
Msg string
11-
}
12-
13-
func (a *AStruct) Set(msg string) {
14-
a.Msg = msg
5+
func (p ErrorList) Add(msg string) ErrorList {
6+
return append(p, msg)
157
}

compliance/tests/import_type_methods/errlist/errlist.gs.ts

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,8 @@ import * as $ from "@goscript/builtin/index.js";
55

66
export type ErrorList = $.Slice<string>;
77

8-
export function ErrorList_Add(p: ErrorList, msg: string): void {
9-
p!.value = $.append(p!.value, msg)
8+
export function ErrorList_Add(p: ErrorList, msg: string): ErrorList {
9+
return $.append(p, msg)
1010
}
1111

1212

13-
export class AStruct {
14-
public get Msg(): string {
15-
return this._fields.Msg.value
16-
}
17-
public set Msg(value: string) {
18-
this._fields.Msg.value = value
19-
}
20-
21-
public _fields: {
22-
Msg: $.VarRef<string>;
23-
}
24-
25-
constructor(init?: Partial<{Msg?: string}>) {
26-
this._fields = {
27-
Msg: $.varRef(init?.Msg ?? "")
28-
}
29-
}
30-
31-
public clone(): AStruct {
32-
const cloned = new AStruct()
33-
cloned._fields = {
34-
Msg: $.varRef(this._fields.Msg.value)
35-
}
36-
return cloned
37-
}
38-
39-
public Set(msg: string): void {
40-
const a = this
41-
a.Msg = msg
42-
}
43-
44-
// Register this type with the runtime type system
45-
static __typeInfo = $.registerStructType(
46-
'AStruct',
47-
new AStruct(),
48-
[{ name: "Set", args: [{ name: "msg", type: { kind: $.TypeKind.Basic, name: "string" } }], returns: [] }],
49-
AStruct,
50-
{"Msg": { kind: $.TypeKind.Basic, name: "string" }}
51-
);
52-
}
53-
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { AStruct } from "./errlist.gs.js"
1+
export { ErrorList_Add } from "./errlist.gs.js"
22
export type { ErrorList } from "./errlist.gs.js"
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
error
2-
astruct
3-
1+
error

compliance/tests/import_type_methods/import_type_methods.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@ import (
55
)
66

77
type parser struct {
8-
errors errlist.ErrorList
9-
astruct errlist.AStruct
8+
errors errlist.ErrorList
109
}
1110

1211
func main() {
1312
var p parser
14-
// this Add method does not work:
15-
p.errors.Add("error")
13+
p.errors = p.errors.Add("error")
1614
println(p.errors[0])
17-
18-
// but it does work for a struct type:
19-
p.astruct.Set("astruct")
20-
println(p.astruct.Msg)
2115
}

compliance/tests/import_type_methods/import_type_methods.gs.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,20 @@ export class parser {
1313
this._fields.errors.value = value
1414
}
1515

16-
public get astruct(): errlist.AStruct {
17-
return this._fields.astruct.value
18-
}
19-
public set astruct(value: errlist.AStruct) {
20-
this._fields.astruct.value = value
21-
}
22-
2316
public _fields: {
2417
errors: $.VarRef<errlist.ErrorList>;
25-
astruct: $.VarRef<errlist.AStruct>;
2618
}
2719

28-
constructor(init?: Partial<{astruct?: errlist.AStruct, errors?: errlist.ErrorList}>) {
20+
constructor(init?: Partial<{errors?: errlist.ErrorList}>) {
2921
this._fields = {
30-
errors: $.varRef(init?.errors ?? null as errlist.ErrorList),
31-
astruct: $.varRef(init?.astruct?.clone() ?? new errlist.AStruct())
22+
errors: $.varRef(init?.errors ?? null as errlist.ErrorList)
3223
}
3324
}
3425

3526
public clone(): parser {
3627
const cloned = new parser()
3728
cloned._fields = {
38-
errors: $.varRef(this._fields.errors.value),
39-
astruct: $.varRef(this._fields.astruct.value?.clone() ?? null)
29+
errors: $.varRef(this._fields.errors.value)
4030
}
4131
return cloned
4232
}
@@ -47,18 +37,13 @@ export class parser {
4737
new parser(),
4838
[],
4939
parser,
50-
{"errors": "ErrorList", "astruct": "AStruct"}
40+
{"errors": "ErrorList"}
5141
);
5242
}
5343

5444
export async function main(): Promise<void> {
5545
let p: parser = new parser()
56-
// this Add method does not work:
57-
errlist.ErrorList_Add(p.errors, "error")
46+
p.errors = errlist.ErrorList_Add(p.errors, "error")
5847
console.log(p.errors![0])
59-
60-
// but it does work for a struct type:
61-
p.astruct.Set("astruct")
62-
console.log(p.astruct.Msg)
6348
}
6449

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package errlist
2+
3+
type ErrorList []string
4+
5+
func (p *ErrorList) Add(msg string) {
6+
*p = append(*p, msg)
7+
}
8+
9+
type AStruct struct {
10+
Msg string
11+
}
12+
13+
func (a *AStruct) Set(msg string) {
14+
a.Msg = msg
15+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Generated file based on errlist/errlist.go
2+
// Updated when compliance tests are re-run, DO NOT EDIT!
3+
4+
import * as $ from "@goscript/builtin/index.js";
5+
6+
export type ErrorList = $.Slice<string>;
7+
8+
export function ErrorList_Add(p: ErrorList, msg: string): void {
9+
p!.value = $.append(p!.value, msg)
10+
}
11+
12+
13+
export class AStruct {
14+
public get Msg(): string {
15+
return this._fields.Msg.value
16+
}
17+
public set Msg(value: string) {
18+
this._fields.Msg.value = value
19+
}
20+
21+
public _fields: {
22+
Msg: $.VarRef<string>;
23+
}
24+
25+
constructor(init?: Partial<{Msg?: string}>) {
26+
this._fields = {
27+
Msg: $.varRef(init?.Msg ?? "")
28+
}
29+
}
30+
31+
public clone(): AStruct {
32+
const cloned = new AStruct()
33+
cloned._fields = {
34+
Msg: $.varRef(this._fields.Msg.value)
35+
}
36+
return cloned
37+
}
38+
39+
public Set(msg: string): void {
40+
const a = this
41+
a.Msg = msg
42+
}
43+
44+
// Register this type with the runtime type system
45+
static __typeInfo = $.registerStructType(
46+
'AStruct',
47+
new AStruct(),
48+
[{ name: "Set", args: [{ name: "msg", type: { kind: $.TypeKind.Basic, name: "string" } }], returns: [] }],
49+
AStruct,
50+
{"Msg": { kind: $.TypeKind.Basic, name: "string" }}
51+
);
52+
}
53+

0 commit comments

Comments
 (0)