Skip to content

Commit df432fe

Browse files
committed
first step towards reuse of pointers
1 parent d4a8175 commit df432fe

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func WritePkgCode(pkg *Archive, minify bool, w *SourceMapFilter) error {
175175
if _, err := w.Write(removeWhitespace([]byte(fmt.Sprintf("$packages[\"%s\"] = (function() {\n", pkg.ImportPath)), minify)); err != nil {
176176
return err
177177
}
178-
vars := []string{"$pkg = {}"}
178+
vars := []string{"$pkg = {}", "$ptr = {}"}
179179
var filteredDecls []*Decl
180180
for _, d := range pkg.Declarations {
181181
if d.DceObjectFilter == "" && d.DceMethodFilter == "" {

compiler/expressions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
208208
case *ast.CompositeLit:
209209
return c.formatExpr("$newDataPointer(%e, %s)", x, c.typeName(c.p.Types[e].Type))
210210
case *ast.Ident:
211-
if obj, _ := c.p.Uses[x].(*types.Var); c.p.escapingVars[obj] {
211+
obj := c.p.Uses[x].(*types.Var)
212+
if c.p.escapingVars[obj] {
212213
return c.formatExpr("new %s(function() { return this.$target[0]; }, function($v) { this.$target[0] = $v; }, %s)", c.typeName(exprType), c.p.objectVars[obj])
213214
}
214-
return c.formatExpr("new %s(function() { return %e; }, function($v) { %s })", c.typeName(exprType), x, c.translateAssign(x, "$v", exprType, false))
215+
return c.formatExpr("($ptr.%1s || ($ptr.%1s = new %2s(function() { return %1s; }, function($v) { %3s })))", c.p.objectVars[obj], c.typeName(exprType), c.translateAssign(x, "$v", exprType, false))
215216
case *ast.SelectorExpr:
216217
newSel := &ast.SelectorExpr{X: c.newIdent("this.$target", c.p.Types[x.X].Type), Sel: x.Sel}
217218
c.p.Selections[newSel] = c.p.Selections[x]

compiler/package.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ func translateFunction(typ *ast.FuncType, stmts []ast.Stmt, outerContext *funcCo
610610
parent: outerContext,
611611
sig: sig,
612612
allVars: make(map[string]int, len(outerContext.allVars)),
613+
localVars: []string{"$ptr = {}"},
613614
flowDatas: map[*types.Label]*flowData{nil: &flowData{}},
614615
caseCounter: 1,
615616
labelCases: make(map[*types.Label]int),
@@ -634,7 +635,7 @@ func translateFunction(typ *ast.FuncType, stmts []ast.Stmt, outerContext *funcCo
634635
}
635636

636637
if len(c.Flattened) != 0 {
637-
c.localVars = []string{"$this = this"}
638+
c.localVars = append(c.localVars, "$this = this")
638639
}
639640

640641
body := c.CatchOutput(1, func() {

tests/misc_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ func TestPointerEquality(t *testing.T) {
1818
if &a[0] != &a[0] || &a[:][0] != &a[0] || &a[:][0] != &a[:][0] {
1919
t.Fail()
2020
}
21+
22+
b := 1
23+
c := 1
24+
if &b != &b || &b == &c {
25+
t.Fail()
26+
}
27+
m := make(map[*int]int)
28+
m[&b] = 2
29+
m[&c] = 3
30+
if m[&b] != 2 || m[&c] != 3 {
31+
t.Fail()
32+
}
2133
}
2234

2335
type SingleValue struct {

0 commit comments

Comments
 (0)