Skip to content

Commit 6b101f6

Browse files
committed
Moved slice methods to toplevel and got rid of Go$copyFields.
1 parent 2d2b586 commit 6b101f6

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

src/angularjs/angularjs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package angularjs
33
func NewModule(name string, requires []string, configFn func()) *Module { return nil }
44

55
const js_NewModule = `
6-
requires = requires ? requires.Go$toArray() : [];
6+
requires = requires ? Go$sliceToArray(requires) : [];
77
return new Module(angular.module(name, requires, configFn));
88
`
99

@@ -66,7 +66,7 @@ const js_Scope_Set = `
6666
this.native[key] = Go$externalizeString(value.v);
6767
break;
6868
case Go$Slice:
69-
this.native[key] = value.Go$toArray();
69+
this.native[key] = Go$sliceToArray(value);
7070
break;
7171
default:
7272
this.native[key] = value.v !== undefined ? value.v : value;

src/gopherjs/translator/expressions.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -433,22 +433,24 @@ func (c *PkgContext) translateExpr(expr ast.Expr) string {
433433
case *ast.SliceExpr:
434434
b, isBasic := c.info.Types[e.X].(*types.Basic)
435435
isString := isBasic && b.Info()&types.IsString != 0
436-
method := "Go$subslice"
437-
if isString {
438-
method = "substring"
439-
}
440436
slice := c.translateExprToType(e.X, exprType)
441437
if e.High == nil {
442438
if e.Low == nil {
443439
return slice
444440
}
445-
return fmt.Sprintf("%s.%s(%s)", slice, method, c.translateExpr(e.Low))
441+
if isString {
442+
return fmt.Sprintf("%s.substring(%s)", slice, c.translateExpr(e.Low))
443+
}
444+
return fmt.Sprintf("Go$subslice(%s, %s)", slice, c.translateExpr(e.Low))
446445
}
447446
low := "0"
448447
if e.Low != nil {
449448
low = c.translateExpr(e.Low)
450449
}
451-
return fmt.Sprintf("%s.%s(%s, %s)", slice, method, low, c.translateExpr(e.High))
450+
if isString {
451+
return fmt.Sprintf("%s.substring(%s, %s)", slice, low, c.translateExpr(e.High))
452+
}
453+
return fmt.Sprintf("Go$subslice(%s, %s, %s)", slice, low, c.translateExpr(e.High))
452454

453455
case *ast.SelectorExpr:
454456
sel := c.info.Selections[e]
@@ -498,7 +500,7 @@ func (c *PkgContext) translateExpr(expr ast.Expr) string {
498500
switch t2 := c.info.Types[e.Args[0]].Underlying().(type) {
499501
case *types.Slice:
500502
if len(e.Args) == 3 {
501-
return fmt.Sprintf("new %s(Go$clear(new %s(%s))).Go$subslice(0, %s)", c.typeName(c.info.Types[e.Args[0]]), toArrayType(t2.Elem()), c.translateExpr(e.Args[2]), c.translateExpr(e.Args[1]))
503+
return fmt.Sprintf("Go$subslice(new %s(Go$clear(new %s(%s))), 0, %s)", c.typeName(c.info.Types[e.Args[0]]), toArrayType(t2.Elem()), c.translateExpr(e.Args[2]), c.translateExpr(e.Args[1]))
502504
}
503505
return fmt.Sprintf("new %s(Go$clear(new %s(%s)))", c.typeName(c.info.Types[e.Args[0]]), toArrayType(t2.Elem()), c.translateExpr(e.Args[1]))
504506
default:
@@ -749,7 +751,7 @@ func (c *PkgContext) translateExprToType(expr ast.Expr, desiredType types.Type)
749751
case t.Kind() == types.UnsafePointer:
750752
if unary, isUnary := expr.(*ast.UnaryExpr); isUnary && unary.Op == token.AND {
751753
if indexExpr, isIndexExpr := unary.X.(*ast.IndexExpr); isIndexExpr {
752-
return fmt.Sprintf("%s.Go$toArray()", c.translateExprToType(indexExpr.X, types.NewSlice(nil)))
754+
return fmt.Sprintf("Go$sliceToArray(%s)", c.translateExprToType(indexExpr.X, types.NewSlice(nil)))
753755
}
754756
if ident, isIdent := unary.X.(*ast.Ident); isIdent && ident.Name == "_zero" {
755757
return "new Uint8Array(0)"
@@ -783,7 +785,7 @@ func (c *PkgContext) translateExprToType(expr ast.Expr, desiredType types.Type)
783785
}
784786
_, desiredIsNamed := desiredType.(*types.Named)
785787
if desiredIsNamed && !types.IsIdentical(exprType, desiredType) {
786-
return fmt.Sprintf("(Go$obj = %s, (new %s(Go$obj.array)).Go$subslice(Go$obj.offset, Go$obj.offset + Go$obj.length))", c.translateExpr(expr), c.typeName(desiredType))
788+
return fmt.Sprintf("(Go$obj = %s, Go$subslice(new %s(Go$obj.array), Go$obj.offset, Go$obj.offset + Go$obj.length))", c.translateExpr(expr), c.typeName(desiredType))
787789
}
788790
return c.translateExpr(expr)
789791

src/gopherjs/translator/natives.go

+11-17
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ var Go$keys = Object.keys;
1010
var Go$min = Math.min;
1111
var Go$charToString = String.fromCharCode;
1212
13-
var Go$copyFields = function(from, to) {
14-
var keys = Object.keys(from);
15-
for (var i = 0; i < keys.length; i++) {
16-
var key = keys[i];
17-
to[key] = from[key];
18-
}
19-
};
20-
2113
var Go$Uint8 = function(v) { this.v = v; };
2214
Go$Uint8.prototype.Go$key = function() { return "Uint8$" + this.v; };
2315
var Go$Uint16 = function(v) { this.v = v; };
@@ -185,23 +177,25 @@ var Go$Slice = function(array) {
185177
this.length = array && array.length;
186178
};
187179
Go$Slice.Go$nil = new Go$Slice({ isNil: true, length: 0 });
188-
Go$Slice.prototype.Go$subslice = function(begin, end) {
189-
var s = new this.constructor(this.array);
190-
s.offset = this.offset + begin;
191-
s.length = this.length - begin;
180+
181+
var Go$subslice = function(slice, begin, end) {
182+
var s = new slice.constructor(slice.array);
183+
s.offset = slice.offset + begin;
184+
s.length = slice.length - begin;
192185
if (end !== undefined) {
193186
s.length = end - begin;
194187
}
195188
return s;
196189
};
197-
Go$Slice.prototype.Go$toArray = function() {
198-
if (this.length === 0) {
190+
191+
var Go$sliceToArray = function(slice) {
192+
if (slice.length === 0) {
199193
return [];
200194
}
201-
if (this.array.constructor !== Array) {
202-
return this.array.subarray(this.offset, this.offset + this.length);
195+
if (slice.array.constructor !== Array) {
196+
return slice.array.subarray(slice.offset, slice.offset + slice.length);
203197
}
204-
return this.array.slice(this.offset, this.offset + this.length);
198+
return slice.array.slice(slice.offset, slice.offset + slice.length);
205199
};
206200
207201
var Go$decodeRune = function(str, pos) {

src/gopherjs/translator/package.go

-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ func (c *PkgContext) translateSpec(spec ast.Spec) {
399399
default:
400400
underlyingTypeName := c.typeName(t)
401401
c.Printf("%s = function() { %s.apply(this, arguments); };", typeName, underlyingTypeName)
402-
c.Printf("Go$copyFields(%s.prototype, %s.prototype);", underlyingTypeName, typeName)
403402
c.Printf("%s.Go$Pointer = function(getter, setter) { this.Go$get = getter; this.Go$set = setter; };", typeName)
404403
if _, isSlice := t.(*types.Slice); isSlice {
405404
c.Printf("%s.Go$nil = new %s({ isNil: true, length: 0 });", typeName, typeName)

0 commit comments

Comments
 (0)