Преглед изворни кода

Fixed misuse of reflect.SliceHeader. Added go vet. (#365)

Dmitry Panov пре 3 година
родитељ
комит
ddea1f4469
5 измењених фајлова са 31 додато и 81 уклоњено
  1. 3 1
      .github/workflows/main.yml
  2. 1 1
      array_test.go
  3. 0 72
      parser/marshal_test.go
  4. 11 7
      unistring/string.go
  5. 16 0
      unistring/string_test.go

+ 3 - 1
.github/workflows/main.yml

@@ -18,6 +18,8 @@ jobs:
         uses: actions/checkout@v2
       - name: Check formatting
         run: diff -u <(echo -n) <(gofmt -d .)
+      - name: Run go vet
+        run: go vet ./...
       - name: Run staticcheck
         uses: dominikh/[email protected]
         with:
@@ -29,4 +31,4 @@ jobs:
       - name: Run tests
         env:
           GOARCH: ${{ matrix.arch }}
-        run: go test ./...
+        run: go test -vet=off ./...

+ 1 - 1
array_test.go

@@ -122,7 +122,7 @@ func BenchmarkArrayPut(b *testing.B) {
 
 func BenchmarkArraySetEmpty(b *testing.B) {
 	r := New()
-	r.Get("Array").(*Object).Get("prototype").String() // materialise Array.prototype
+	_ = r.Get("Array").(*Object).Get("prototype").String() // materialise Array.prototype
 	a := r.NewArray(0, 0)
 	values := a.self.(*arrayObject).values
 	b.ResetTimer()

+ 0 - 72
parser/marshal_test.go

@@ -886,77 +886,5 @@ func TestParserAST(t *testing.T) {
 ]
             `)
 
-		return
-
-		test(`
-        if (!abc && abc.jkl(def) && abc[0] === +abc[0] && abc.length < ghi) {
-        }
-        ---
-[
-  {
-    "If": {
-      "Consequent": {
-        "BlockStatement": []
-      },
-      "Test": {
-        "BinaryExpression": {
-          "Left": {
-            "BinaryExpression": {
-              "Left": {
-                "BinaryExpression": {
-                  "Left": null,
-                  "Operator": "\u0026\u0026",
-                  "Right": {
-                    "Call": {
-                      "ArgumentList": [
-                        {
-                          "Identifier": "def"
-                        }
-                      ],
-                      "Callee": {
-                        "Dot": {
-                          "Left": {
-                            "Identifier": "abc"
-                          },
-                          "Member": "jkl"
-                        }
-                      }
-                    }
-                  }
-                }
-              },
-              "Operator": "\u0026\u0026",
-              "Right": {
-                "BinaryExpression": {
-                  "Left": null,
-                  "Operator": "===",
-                  "Right": null
-                }
-              }
-            }
-          },
-          "Operator": "\u0026\u0026",
-          "Right": {
-            "BinaryExpression": {
-              "Left": {
-                "Dot": {
-                  "Left": {
-                    "Identifier": "abc"
-                  },
-                  "Member": "length"
-                }
-              },
-              "Operator": "\u003c",
-              "Right": {
-                "Identifier": "ghi"
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-]
-        `)
 	})
 }

+ 11 - 7
unistring/string.go

@@ -106,14 +106,18 @@ func (s String) AsUtf16() []uint16 {
 	if len(s) < 4 || len(s)&1 != 0 {
 		return nil
 	}
-	l := len(s) / 2
+
+	var a []uint16
 	raw := string(s)
-	hdr := (*reflect.StringHeader)(unsafe.Pointer(&raw))
-	a := *(*[]uint16)(unsafe.Pointer(&reflect.SliceHeader{
-		Data: hdr.Data,
-		Len:  l,
-		Cap:  l,
-	}))
+
+	sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&a))
+	sliceHeader.Data = (*reflect.StringHeader)(unsafe.Pointer(&raw)).Data
+
+	l := len(raw) / 2
+
+	sliceHeader.Len = l
+	sliceHeader.Cap = l
+
 	if a[0] == BOM {
 		return a
 	}

+ 16 - 0
unistring/string_test.go

@@ -0,0 +1,16 @@
+package unistring
+
+import "testing"
+
+func TestString_AsUtf16(t *testing.T) {
+	const str = "más"
+	s := NewFromString(str)
+
+	if b := s.AsUtf16(); len(b) != 4 || b[0] != BOM {
+		t.Fatal(b)
+	}
+
+	if s.String() != str {
+		t.Fatal(s)
+	}
+}