diff --git a/build/build.go b/build/build.go index f2513ba5b..9170f2ce8 100644 --- a/build/build.go +++ b/build/build.go @@ -139,6 +139,9 @@ func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build if installSuffix != "" { bctx.InstallSuffix += "_" + installSuffix } + case "syscall/js": + // There are no buildable files in this package, but we need to use files in the virtual directory. + mode |= build.FindOnly case "math/big": // Use pure Go version of math/big; we don't want non-Go assembly versions. bctx.BuildTags = append(bctx.BuildTags, "math_big_pure_go") diff --git a/compiler/gopherjspkg/fs_vfsdata.go b/compiler/gopherjspkg/fs_vfsdata.go index 8931d724b..b21d6e838 100644 --- a/compiler/gopherjspkg/fs_vfsdata.go +++ b/compiler/gopherjspkg/fs_vfsdata.go @@ -21,7 +21,7 @@ var FS = func() http.FileSystem { fs := vfsgen۰FS{ "/": &vfsgen۰DirInfo{ name: "/", - modTime: time.Date(2019, 4, 6, 11, 9, 42, 660657937, time.UTC), + modTime: time.Date(2019, 4, 25, 16, 19, 34, 225618757, time.UTC), }, "/js": &vfsgen۰DirInfo{ name: "js", diff --git a/compiler/natives/fs_vfsdata.go b/compiler/natives/fs_vfsdata.go index 749606e01..ab727bf90 100644 --- a/compiler/natives/fs_vfsdata.go +++ b/compiler/natives/fs_vfsdata.go @@ -21,7 +21,7 @@ var FS = func() http.FileSystem { fs := vfsgen۰FS{ "/": &vfsgen۰DirInfo{ name: "/", - modTime: time.Date(2019, 4, 10, 21, 27, 44, 478007813, time.UTC), + modTime: time.Date(2019, 4, 24, 16, 42, 24, 967092015, time.UTC), }, "/src": &vfsgen۰DirInfo{ name: "src", @@ -131,11 +131,11 @@ var FS = func() http.FileSystem { }, "/src/encoding/gob": &vfsgen۰DirInfo{ name: "gob", - modTime: time.Date(2019, 4, 6, 11, 3, 15, 126398833, time.UTC), + modTime: time.Date(2019, 4, 25, 15, 24, 30, 246543308, time.UTC), }, "/src/encoding/gob/gob_test.go": &vfsgen۰CompressedFileInfo{ name: "gob_test.go", - modTime: time.Date(2019, 4, 6, 11, 3, 15, 126473498, time.UTC), + modTime: time.Date(2019, 4, 25, 15, 24, 30, 246620225, time.UTC), uncompressedSize: 2598, compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x56\x51\x6f\xdb\x3e\x0e\x7f\xb6\x3e\x05\x67\xdc\x0a\xa7\xe7\x39\x95\x93\xae\x9d\x81\x3e\xac\x5b\x77\xd8\x43\x3b\x60\x33\x70\xdb\x8a\x62\x70\x6c\x26\xd1\xea\x48\x3e\x49\x6e\x1a\x04\xf9\xee\x07\x4a\x76\x9c\xae\xff\x0d\x2b\xd0\x56\xa4\x7e\xfc\x91\xa2\x48\xca\xe3\x31\xfc\x7b\xd6\x8a\xba\x82\x9f\x86\xb1\xa6\x28\xef\x8b\x05\xc2\x42\xcd\x18\x13\xab\x46\x69\x0b\x11\x0b\xc2\xd9\xc6\xa2\x09\x59\x10\x6a\x9c\xd7\x58\x5a\x5a\x5a\x34\x56\xc8\x45\xc8\x46\x8c\x8d\xc7\x90\x7f\x7a\xff\x29\x83\x1c\x8d\xbd\x92\x55\xae\xae\x64\x05\xea\x01\xb5\x16\x15\x42\x59\x48\x98\x21\x68\x5c\xa9\x07\xac\x40\xc9\x12\xc1\x2e\x11\x66\xed\x02\xd6\xc2\x2e\xe1\xba\xd0\x1a\xe6\x02\xeb\x0a\x84\x81\xb9\x78\xc4\x2a\x61\xf3\x56\x96\x4f\x08\x23\x0b\xc7\x9d\xd7\x24\x1f\xc1\x96\x05\x76\xd3\x20\xe4\x29\x18\xab\xdb\xd2\x92\x26\xc8\x49\x10\x72\xc1\x82\x5d\xbf\x3f\x39\xdc\xff\x0a\xf3\x5a\x15\xf6\xf5\x94\x05\xc1\x77\x38\x16\xd2\x1e\x20\xf9\x21\xf2\x6d\x0c\x97\x31\xbc\x03\x70\x98\xe0\x1a\xba\x9f\x55\xd1\xdc\x7a\x1f\x77\xc7\x03\xd7\x75\x7a\xb0\x2d\xa4\xbd\xcb\x27\xa4\xf5\xc0\x27\x46\x7d\x7c\xc1\xb5\x90\xb6\xb1\x7a\x30\x39\xee\x3c\x95\x6a\xd5\xf4\x54\xb4\xae\xf1\x91\xa7\xe7\x77\xc3\x92\x40\x94\xb2\x1e\x74\x9b\x76\xac\x77\xb7\xe9\x61\x50\x57\xab\xc6\x6e\xae\x8b\xe6\xd0\xbd\x90\x16\xc6\x63\xb0\x0a\xca\x25\x96\xf7\x60\x97\x85\x85\x35\xdd\x4e\x89\xe2\x01\xa1\x00\xa9\xe4\x2b\x29\x6a\x32\x4a\x58\x10\xdc\xf4\x07\x3f\xbe\x9d\xdc\x0d\xdc\x5f\xac\x36\x9d\x3a\x1d\xce\xf4\x51\xda\xd7\x53\xe3\xb4\xe4\xc9\x21\x3f\x7f\xec\x08\xba\x03\x78\xf3\x9e\x75\x6f\xfa\xad\xd7\xdc\xde\x51\xbd\xb9\xbb\xec\x3d\xe7\xa9\xbb\xa5\x46\x40\x76\x01\x93\x84\x4f\xf9\xe9\x1b\x16\x20\x49\x69\x72\xc6\xcf\x29\x25\x76\xad\xbc\x7c\xc2\x82\x15\x16\x92\xf2\x9e\x5d\xc0\x34\x65\xc1\x5c\xc8\x05\x6a\x43\xe2\x29\x0b\x0c\xa7\x45\xe8\x1d\xf3\x90\x05\x26\x3d\x50\xa4\x21\x0b\x1e\x0a\xed\x82\xe5\x30\xe4\x1c\x2e\x7a\x21\xe2\xc9\x49\x0c\x3c\x39\x19\x0d\xc8\xf4\xaf\x90\x85\xd6\x1c\x0e\xd2\x45\xf2\xed\xc9\x1d\x5c\x80\xe1\x9d\xc4\x9d\x94\xee\xf1\xe9\x2f\xf8\xb4\xc3\xa7\x9d\xc4\x7b\x6b\xc2\xbb\xdb\x79\xdb\x39\x19\xea\x60\xaf\xf6\xb6\x47\x8d\x38\xd4\x39\x86\x23\x7c\xca\x90\xfe\x33\x43\xe7\x9d\xd0\x83\xca\x13\xd8\xb5\x62\x81\x75\xa9\x3d\xca\xb9\x6b\xa0\xac\xbb\x3e\x7e\x16\xb3\x20\xb8\xdc\x8b\xe7\x24\xbe\xeb\xc5\x57\xa7\x24\x5e\x67\xbf\x6f\xaf\x6d\xd8\x88\x30\xa3\xb8\x63\x08\x91\x56\xb8\x73\x36\x69\xf6\x6b\xcf\x6d\xa7\x19\xe4\x93\xed\xd7\x0c\x08\xfc\x3d\x83\xa3\xae\x14\x76\x31\xf0\x93\x7e\x0f\xfd\x56\x57\x16\x3b\x4f\xe6\x9d\x66\xcf\x5b\xb5\x73\x1f\x52\xdd\x85\x5d\x04\x21\x95\x5d\xe8\x0d\x7d\x1b\x67\x4f\xda\x78\xdb\xb9\x1d\xbc\xc4\xd0\x2d\x0e\x63\xea\xbb\x3d\xfb\x53\xb7\x6f\x5d\x29\x66\xbe\xce\x62\xff\xcf\x4b\xdc\x31\xec\xa7\xef\x07\xf1\x08\x76\x29\x0c\x34\x5a\xcd\x6a\x5c\x65\x7e\x33\xc8\x37\x0d\x5e\x69\xad\x74\x06\x95\xb1\xc9\xbf\x0c\x5a\x9a\xb3\x52\x59\x28\x80\xc6\xac\x15\x4a\x76\x58\x4a\x67\x61\x81\xe6\x61\xb5\xc2\x15\x4d\x6c\x88\xc6\x0b\x61\x97\xed\x2c\x29\xd5\x6a\xbc\x50\xcd\x12\xf5\x4f\x33\x2c\xba\x47\x21\x59\xa8\x6c\x7a\x7e\x96\x4d\x46\x8e\x8a\x06\x54\xf6\xe7\x09\xb5\xa5\x8a\xcf\x86\xaa\x8d\x5d\xc1\x0f\x8a\xd4\x1d\xaf\x1f\x62\x94\xe0\x7b\x8c\x9e\x8e\xb2\x11\x21\x6e\xfa\xda\x81\xa3\x61\x44\x6d\x79\x72\x1a\x43\x4a\x7f\x26\xc9\xa9\x63\xa2\x91\x95\x75\xb8\x3e\x9e\xad\xe1\x31\x18\xef\xc9\x0f\xaf\xcc\xed\xfb\xe9\xb5\x3d\x3b\x8b\xe1\xfc\x4d\x0c\x3c\x9d\x4c\xe9\x37\xe5\x93\xa9\xc3\x7e\xfe\x38\x54\x37\xbc\x82\x74\x22\x9c\x87\x7d\x24\xe1\x8d\x5a\x53\x92\xe9\x9d\xb3\x62\x85\x21\x6d\x7f\xcb\x9e\xce\xb8\x28\x5c\x62\x5d\xab\x18\x4c\x21\x6a\xa5\x43\x77\x9a\x7c\x38\x4d\x9e\x6e\x43\x77\xa1\xc2\x40\x9e\xba\x72\xdb\xb1\x60\x46\x3d\x26\x71\x1d\xb9\x67\x39\xb9\x6c\xe7\x73\xd4\x23\x16\xa0\xd6\xb4\x73\x83\xeb\x2b\x59\xaa\x0a\x75\x34\x1b\x25\x7e\x19\x59\x3e\x62\x81\x98\x03\x61\x5e\x5c\x00\x8d\x77\x6a\x51\x9b\xb8\xba\x88\x42\x74\xb0\x2c\x8c\x09\x31\x72\x6e\x68\x1c\xfc\xb0\x1c\x72\xee\xa9\x1d\xf3\x7b\xdc\x33\xfb\x65\x74\xf4\xe3\xb7\xdc\x1f\x0a\x5b\xd4\x51\x58\xe1\x33\x6e\x31\x87\x17\x7d\xd9\xbc\x47\x6c\xae\xfe\xd7\x16\x75\x64\x79\x0c\x8e\xee\x30\xb6\x79\x1f\x1c\xe0\x63\x83\xa5\xc5\x0a\x5e\x3e\xc0\x42\x59\x78\xf9\x10\xc6\x70\x4c\x46\x3e\x84\x1d\xa3\x0a\xbe\x44\x28\x66\x46\xd5\xad\xc5\x7a\x03\xa6\xd5\xfe\x5b\xa3\x7b\xde\x2a\xaa\x46\x5f\xfc\xee\x91\x4b\x5c\x2c\x96\x27\xfb\xa7\xf2\xe2\x59\x76\xe6\x51\xd8\x3d\x87\x60\x50\xda\x70\x7f\x84\x1f\x7f\x6d\xd7\x7b\xf7\xb6\x3b\x36\x7c\xdc\x50\x6f\x7e\x2e\x4a\x7c\xfe\x71\x33\x1e\x83\x3b\xb8\x90\x8b\xf1\x42\xcd\xa0\x6c\xb5\x46\x69\xeb\x0d\xb4\x06\xe9\x00\x66\x23\xcb\x04\x72\xaa\x0f\xb2\xf4\x6a\xa7\xfc\x6f\x21\xec\x7f\xb4\x6a\x1b\x28\x64\xe5\x98\xca\x42\x52\xbb\x9b\xb6\x2c\x11\x2b\x58\x2f\x51\x76\x0c\x94\x8c\xd6\xd0\x07\x57\x60\x93\x2f\xf7\xa2\x89\xc2\xd6\xd0\xdb\xe9\xb7\xc3\x11\xdb\xb1\xff\x07\x00\x00\xff\xff\x9b\x7c\x41\xd0\x26\x0a\x00\x00"), @@ -544,7 +544,18 @@ var FS = func() http.FileSystem { }, "/src/syscall": &vfsgen۰DirInfo{ name: "syscall", - modTime: time.Date(2019, 4, 10, 21, 27, 44, 476613264, time.UTC), + modTime: time.Date(2019, 4, 11, 0, 32, 11, 442033261, time.UTC), + }, + "/src/syscall/js": &vfsgen۰DirInfo{ + name: "js", + modTime: time.Date(2019, 4, 25, 16, 20, 36, 810751444, time.UTC), + }, + "/src/syscall/js/js.go": &vfsgen۰CompressedFileInfo{ + name: "js.go", + modTime: time.Date(2019, 4, 25, 16, 20, 36, 810208460, time.UTC), + uncompressedSize: 5659, + + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x58\xed\x6f\xdb\xbc\x11\xff\x6c\xfd\x15\x57\x7d\xe8\x23\x35\x9a\xfc\xbc\x74\x59\xe1\xc2\x1f\xba\x61\x2d\x5a\xac\xcd\xb0\x74\xdb\x87\x20\x58\x68\x99\xb2\xe9\xc8\x94\x41\x51\x8a\xbd\xc0\xff\xfb\x70\x47\x8a\xa2\x14\x39\x71\xb1\xa7\x40\x1d\xfa\xf8\xbb\x17\x1e\xef\xcd\x9c\x4e\xe1\x62\x51\x8b\x62\x09\x9b\x2a\x08\x76\x2c\xbb\x67\x2b\x4e\x6b\xb1\xdd\x95\x4a\x43\x14\x4c\x42\xc5\xf3\x82\x67\x3a\x0c\x26\x61\x2d\x2b\x96\xf3\x30\x08\x26\xe1\x4a\xe8\x75\xbd\x48\xb3\x72\x3b\x5d\x95\xbb\x35\x57\x9b\xaa\x5b\x6c\xaa\x30\x88\x83\x40\x1f\x76\x1c\xbe\xe3\x87\x90\x3a\x08\xb2\x52\x56\x24\x12\x49\xff\x94\x4b\x9e\x0b\xc9\x97\x06\x30\x07\x51\x6a\x66\xb6\xbe\xd5\x45\x61\x56\x7f\x2e\xcb\x82\x33\xd9\x92\xb7\x0b\xae\xcc\xfa\x5a\x2b\x21\x57\x76\x7d\xd8\x2e\x4a\xcb\x70\xb5\xd8\xf0\x4c\x9b\xf5\xc7\x5a\x66\x5a\x94\x12\x2d\xc9\x6b\x99\x41\xa4\x49\x57\x0c\x86\x3b\x8a\xa1\xa2\x05\x3c\x06\x93\xea\x41\xe8\x6c\x0d\x1a\xd7\x19\xab\x8c\xd9\xce\xc6\x59\x30\x99\x28\xae\x6b\x25\x21\xac\x5b\x62\xe8\x21\xd1\x64\x1f\x24\xeb\xa2\xf0\xf7\xed\x41\x7c\xc8\xc2\x90\xfa\x52\xf0\x84\x7d\x39\x48\xf1\x31\xc6\x76\x1f\x63\x0e\xd1\xc3\x90\x47\x7a\x18\xa2\xf8\x18\xe3\x29\x1f\x53\x12\xc5\xc7\xb4\x1e\xf4\x51\xb9\xa5\x85\xc1\x64\xc9\x73\x56\x17\x24\x63\xc7\xa4\xc8\xa2\x70\xc1\x96\x80\x97\x1e\xc6\xc1\xe4\x18\x1c\xad\xdf\x3f\x15\xe5\x82\x15\x51\x0c\xff\x62\x45\xcd\xd1\xc3\x56\x98\xd1\xf8\xbd\x24\x7a\xb4\xa9\x52\x83\x8c\x1d\x27\xba\xf5\x45\x3e\x29\x3c\x0e\x77\x65\xe7\xa8\x73\x60\xe2\xa7\x68\xc5\x23\x63\x58\xd4\x19\x85\x02\x41\x9d\xf0\x28\xa7\xfd\x18\xfe\xc1\x0b\xce\x2a\x1e\xc5\x88\xc9\x53\xa3\x68\x6e\xcd\x75\x70\xc4\x5e\xe5\x51\x2e\x01\xbf\x46\x7a\x2d\x2a\x63\x53\x02\x4c\xad\x2a\xb8\xb9\xa5\x6f\x31\x66\x07\x57\x39\xcb\xf8\xe3\x31\x36\x16\x74\x46\xe3\xd7\xc7\x60\x62\x2c\x99\x3d\x3d\xc3\x57\x76\x4f\xf7\x14\x75\x3a\xde\x6c\xaa\xd4\x5c\xaf\x53\xd4\x91\x7a\xda\x50\xcf\x64\xd2\x10\x68\x36\x87\x2d\xbb\xe7\x91\xb5\x2a\x81\x82\xcb\x08\x77\xe2\x18\x41\x79\xa9\x40\x24\xc0\x10\xa7\x98\x5c\x71\x23\x9a\x04\x18\x09\x37\xe2\x16\xe6\x03\x03\x19\xf1\x1e\xf1\xc3\x9e\x27\x97\x51\x1f\x82\x26\xc7\x09\x90\x08\x44\x1f\xe3\x38\xb1\xd1\x43\x37\xf2\x57\xa5\x4a\x75\xfa\x4a\x2c\x20\x36\x7f\x7a\x39\xdd\x86\xec\x17\xd6\xb0\xeb\x4c\x89\x9d\x06\x8e\xa0\x19\x84\x70\x01\x3c\xfd\xc4\x75\x14\x6e\x79\x55\xb1\x15\x0f\xe3\xb4\xad\x0a\x4e\xb3\xb9\xd6\x4e\x73\xe3\x79\x36\x08\x26\xd3\x29\x08\x29\x34\x5f\x82\xe2\x3b\xc5\x2b\x2e\x75\x05\x0f\x6b\xae\xd7\x5c\x59\x5e\x51\x81\x2c\xe5\x1f\xfe\xcb\x55\x09\x0d\x52\x52\xd0\xaa\xe6\x3e\x83\x5e\x73\xb3\x65\xc0\x1a\x7e\x72\x05\xe6\xa7\x34\x98\x58\x0d\x58\x2c\xdc\x99\xfb\xfe\x2b\x17\x1b\xf0\xaf\xd7\x45\xbd\xc8\x11\x09\xf3\x39\xf8\xa1\x4e\x37\x66\x3d\x43\xd0\xc7\x23\x7a\xbb\x4f\x2a\x17\x9b\x84\x2c\xa5\x6b\x68\x98\xc2\xaa\x2d\x96\xd0\xfd\xf3\x3c\x31\x11\xb2\xd2\x4c\x66\xfc\x2a\x1f\x6c\xac\xb8\x26\x79\x54\xe1\xbd\x8d\xb6\x20\xe3\xe1\x4c\x0e\x89\x1c\x5c\xfa\xc3\xab\x39\x48\x51\x90\xa1\x62\x09\xf3\x6e\x27\xfd\x0b\x2b\x8a\x28\xe4\x0d\x2b\xc2\x04\xc2\xa8\xad\x45\xd1\x3e\x86\x47\xb0\x27\xd8\xbf\x87\x63\x8c\x05\xc8\xb7\xeb\x2c\x21\x09\x1c\x7c\x39\xd0\xf2\x97\x39\x1c\x9c\xd0\xde\x99\x4e\x8a\xbd\xeb\xdb\x16\x00\x88\x1c\x22\x8c\xaa\x32\x47\xca\x7c\x3e\xf7\x3b\x89\x81\x40\xab\xfa\xe7\xf7\x30\x9d\xf6\x3b\x50\x00\x70\xb4\x52\xf6\xc4\x8d\x1d\x66\xc0\xf6\x8b\x63\xa3\x0e\xda\x71\x0c\xf4\xb6\x9d\x67\xc0\xfe\xab\x63\x6f\xdb\xee\x49\x09\xb6\x2d\x0d\x04\xfc\xe6\xe9\xa7\x56\x7d\x92\xdf\xb6\xac\x01\xff\x5b\xc7\x6f\xdb\xfb\x69\x7e\xd3\xce\x06\xfc\x7f\xec\xf8\xcd\x48\x70\x92\xdf\x35\xb1\x81\x84\x3f\x39\x09\x6e\x78\x30\x32\xec\xfe\xa5\xdb\xb7\x91\x7c\x8c\xef\x7a\xad\x8e\x42\xe3\x2a\x8f\xf6\xfd\x9a\xee\x72\xd2\x8e\x19\x7b\xac\xa2\xfb\x94\xcc\x8a\xdd\xc8\x61\x4a\x7c\x97\x9e\x7b\x4b\x47\x5b\x7c\xb2\xe9\x37\x5e\x9f\x5e\x7e\x50\x8a\x1d\x4e\x42\xa4\xf0\x67\x01\xdb\xa4\xcc\x16\x86\x42\x82\xb6\xd2\xc7\x3b\xfa\xfc\xe5\x92\xfe\xfc\xf6\x2b\xfd\xb9\x7c\x9b\x40\x4d\x80\xda\x20\x6a\x0b\xa9\x2d\xa6\xb6\xa0\xbc\x28\x19\x11\x68\x41\x6c\x34\x2d\xa6\x7f\x2f\xc9\x17\x89\xad\xcc\x09\x6c\xd9\xee\xc6\xac\x6f\x3d\x2f\x25\x70\xe3\x7f\xf5\x2c\xee\xd7\x3b\xb1\x4c\x3f\xcb\xa6\xbc\xe7\xd1\x1e\x3b\xd3\x93\x21\xe4\x4e\xc8\x86\x15\x62\x89\xfd\x69\x06\x77\x70\x01\x76\x80\x4d\xe9\xde\x30\x08\x5c\xa9\xef\xdd\x5d\xd4\x80\xdf\x8f\x25\x8d\x2c\x5d\xd5\xb2\x65\xea\x55\x93\xda\x9a\xec\x15\x52\xbf\xc0\xfa\xd5\xb4\x49\x9b\x11\xf1\x98\x5e\x51\x4c\xbe\xb7\x42\x1b\xaa\x26\xb3\x39\x34\x64\x64\x14\xbf\xb7\xa4\x57\x73\x3f\x21\x49\xa5\x39\xe5\x6b\x92\x45\x3d\xef\x31\xa4\x75\x8a\xa0\x30\x31\x8c\xc7\xb8\x6f\x46\x77\xa2\xd4\x68\x77\x66\x65\xa5\x6c\xb8\xd2\x1f\xd4\xaa\xa2\x6e\x0f\x69\x9a\xf6\x62\xb7\x77\x2b\x68\x81\xe4\x0f\x1f\xec\xc0\xd0\xdb\xc3\x46\x82\x53\xc2\x7f\x68\xea\x18\x99\x13\x1a\xa4\xb5\x19\xc2\xd4\x0a\xcb\x69\x2b\x6c\x0e\x6c\xb7\xe3\x72\x19\x59\x42\xd2\x33\xba\x77\x1a\x8b\x18\x71\x2c\x95\xe0\xad\x8b\xb3\xd1\xe3\xf8\xed\xf1\x25\xb7\xdb\x8b\x7f\xfd\xba\x4f\x6e\x6b\xc3\xf3\xd7\x81\xc6\x0c\xae\x43\xe4\xb0\x53\xe5\xae\xd3\x8a\x13\xc8\x36\x76\xca\xdd\xe6\x69\x45\xe1\xa6\x9a\x41\xa7\x60\x46\x3c\x5c\xe9\x03\xcd\x34\x5b\xb8\x80\xb0\x1d\x24\x18\xb4\x65\x2e\x81\x55\xa9\x09\xd0\x6a\xe8\x67\xc0\x78\xa2\xf5\xa2\xc6\xb8\x36\x79\x12\x2e\x69\x9a\xc6\xf8\x3f\x1e\xb9\x8e\x8f\x58\x08\xa2\xb8\x2d\x08\x67\x3a\xdd\xf4\x8e\xe7\x7d\x4b\x92\xcf\x88\x75\x6b\xc1\x88\x6d\xe8\xf9\x9d\x8d\x94\x97\x7e\x29\xf4\x44\x12\xe3\xe8\x71\x3f\xcb\x25\xdf\x47\x02\xab\xc7\x0f\x49\xb4\x7c\x27\x64\xa2\x03\x85\xd4\xbf\xa3\xf3\x3e\xcb\x73\x5c\x47\x9a\x47\x2d\x6a\x87\xaa\x48\xb7\xb4\xb6\x92\x59\x39\xdd\xdc\xd5\x56\x6a\x5f\x72\x02\xda\xcf\xec\x61\x3d\xf2\x35\x11\xef\xff\x9d\xc5\xe7\xa5\xab\xd1\x36\xee\x98\x67\x2f\x8f\x8c\xfc\x91\xb4\xf8\x72\x6d\xe4\x3c\x0d\x92\xb1\x66\xf1\x37\x2e\x57\x7a\xdd\x05\xc1\xd8\x5d\xb5\x98\x11\xf6\x6f\xfc\xe1\x05\x0f\xbe\x7c\x46\x94\xf1\x23\x07\xbc\xf6\x72\x2b\x81\xc1\x28\x84\xbf\xa3\x7c\xe1\x04\xee\xd7\x95\x7d\x7c\xf3\xf3\xed\x09\xc1\x5e\x92\x9d\x23\xda\xc2\xcf\x95\xff\xf4\x5d\x68\xcc\xdd\xfe\x0f\xc5\x81\x84\xef\xaa\xd6\xeb\x43\xf4\x24\x25\x9e\xeb\xc0\x1e\x37\x85\xaf\x79\x10\xeb\x78\x89\xea\xff\xec\x18\xcb\x2a\x9b\xb0\xdd\x8f\xd7\x6e\x2e\x3c\xf9\xdb\xb9\x83\x5c\xe5\x51\x55\x88\x8c\xf7\xfd\xe9\x89\xe8\x46\x57\x83\x9b\xcd\xcd\x62\x38\xc2\xd2\x40\xf0\xce\x8e\x72\x38\x25\xd2\x02\xa7\xc2\x9b\xdb\xba\xdd\xaa\xdd\x5e\xed\x36\xdd\xf4\x68\x97\x97\x6f\xbd\x01\xb0\x33\xe4\xf1\xd4\x2c\x48\xd6\xc4\xf1\x71\xec\x55\xca\x3f\xe7\xcc\xb6\xc6\xaa\xde\xed\x4a\x85\x63\x1c\x71\xf6\x1f\xac\x22\x0d\x6f\x3a\xa6\xc1\x73\x8f\x76\xcf\x3d\xed\xcf\xe7\xde\x7b\xc1\xf0\xb9\xe2\x2b\xd7\xeb\x72\x69\x23\xca\x3c\x4c\x02\xd0\x89\xfc\x37\x8c\x37\x1d\xef\x73\x2f\x19\xd5\xa1\xca\x58\x51\x4c\x71\x08\xc0\x05\x94\xb9\x7d\xcb\xb0\x6a\xb0\xfd\x97\xd2\xd2\x7a\x8d\xde\x59\xf9\x6f\x85\x93\x96\xea\xae\x1a\x15\x0c\x6a\x52\x70\x0c\xfe\x17\x00\x00\xff\xff\x77\xd5\xe4\xae\x1b\x16\x00\x00"), }, "/src/syscall/syscall.go": &vfsgen۰CompressedFileInfo{ name: "syscall.go", @@ -624,7 +635,7 @@ var FS = func() http.FileSystem { }, "/src/time": &vfsgen۰DirInfo{ name: "time", - modTime: time.Date(2019, 4, 10, 21, 38, 35, 793313431, time.UTC), + modTime: time.Date(2019, 4, 11, 0, 31, 39, 578263110, time.UTC), }, "/src/time/time.go": &vfsgen۰CompressedFileInfo{ name: "time.go", @@ -635,7 +646,7 @@ var FS = func() http.FileSystem { }, "/src/time/time_test.go": &vfsgen۰CompressedFileInfo{ name: "time_test.go", - modTime: time.Date(2019, 4, 10, 21, 38, 13, 208335917, time.UTC), + modTime: time.Date(2019, 4, 11, 0, 31, 39, 578375649, time.UTC), uncompressedSize: 147, compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x2c\x8c\xc1\x0e\x82\x30\x10\x05\xcf\xec\x57\xbc\xf4\xd4\x6a\x02\x7f\xe2\x05\xee\xa6\xd6\x05\x56\xa0\x6d\xe8\x36\x1e\x8c\xff\x6e\x9a\x78\x9d\xc9\xcc\x30\xe0\xfa\xa8\xb2\x3f\xf1\x2a\x44\xd9\x87\xcd\x2f\x0c\x95\x83\xef\xca\x45\x89\xe4\xc8\xe9\x54\x58\xea\x4c\x03\x12\x17\x43\x8e\x68\xae\x31\x60\xe2\xa2\xe3\xce\x9c\xad\xe2\xf2\xb7\xfd\xe4\xf0\xa1\x4e\xfb\x71\x93\x6c\x4d\x3b\xf5\xb7\xf4\xb6\x0e\x52\x10\x93\xc2\x87\x50\x4f\xaf\x0c\x8e\xa9\x2e\x2b\xe6\x74\x42\x57\x46\xeb\x8d\xa3\x2f\xfd\x02\x00\x00\xff\xff\x49\x24\xa9\x3b\x93\x00\x00\x00"), @@ -843,6 +854,7 @@ var FS = func() http.FileSystem { fs["/src/sync/atomic/atomic_test.go"].(os.FileInfo), } fs["/src/syscall"].(*vfsgen۰DirInfo).entries = []os.FileInfo{ + fs["/src/syscall/js"].(os.FileInfo), fs["/src/syscall/syscall.go"].(os.FileInfo), fs["/src/syscall/syscall_darwin.go"].(os.FileInfo), fs["/src/syscall/syscall_linux.go"].(os.FileInfo), @@ -850,6 +862,9 @@ var FS = func() http.FileSystem { fs["/src/syscall/syscall_unix.go"].(os.FileInfo), fs["/src/syscall/syscall_windows.go"].(os.FileInfo), } + fs["/src/syscall/js"].(*vfsgen۰DirInfo).entries = []os.FileInfo{ + fs["/src/syscall/js/js.go"].(os.FileInfo), + } fs["/src/testing"].(*vfsgen۰DirInfo).entries = []os.FileInfo{ fs["/src/testing/example.go"].(os.FileInfo), fs["/src/testing/ioutil.go"].(os.FileInfo), diff --git a/compiler/natives/src/syscall/js/js.go b/compiler/natives/src/syscall/js/js.go new file mode 100644 index 000000000..81af995b7 --- /dev/null +++ b/compiler/natives/src/syscall/js/js.go @@ -0,0 +1,283 @@ +// +build js + +package js + +import ( + "reflect" + "unsafe" + + "github.com/gopherjs/gopherjs/js" +) + +type Type int + +const ( + TypeUndefined Type = iota + TypeNull + TypeBoolean + TypeNumber + TypeString + TypeSymbol + TypeObject + TypeFunction +) + +func (t Type) String() string { + switch t { + case TypeUndefined: + return "undefined" + case TypeNull: + return "null" + case TypeBoolean: + return "boolean" + case TypeNumber: + return "number" + case TypeString: + return "string" + case TypeSymbol: + return "symbol" + case TypeObject: + return "object" + case TypeFunction: + return "function" + default: + panic("bad type") + } +} + +func Global() Value { + return objectToValue(js.Global) +} + +func Null() Value { + return objectToValue(nil) +} + +func Undefined() Value { + return objectToValue(js.Undefined) +} + +type Func struct { + Value +} + +func (f Func) Release() { + f.Value = Null() +} + +func FuncOf(fn func(this Value, args []Value) interface{}) Func { + return Func{ + Value: objectToValue(js.MakeFunc(func(this *js.Object, args []*js.Object) interface{} { + vargs := make([]Value, len(args)) + for i, a := range args { + vargs[i] = objectToValue(a) + } + return fn(objectToValue(this), vargs) + })), + } +} + +type Error struct { + Value +} + +func (e Error) Error() string { + return "JavaScript error: " + e.Get("message").String() +} + +type Value struct { + v *js.Object + + // inited represents whether Value is non-zero value. true represents the value is not 'undefined'. + inited bool +} + +func objectToValue(obj *js.Object) Value { + if obj == js.Undefined { + return Value{} + } + return Value{obj, true} +} + +var ( + id *js.Object + instanceOf *js.Object + getValueType *js.Object +) + +func init() { + if js.Global != nil { + id = js.Global.Call("eval", "(function(x) { return x; })") + instanceOf = js.Global.Call("eval", "(function(x, y) { return x instanceof y; })") + getValueType = js.Global.Call("eval", `(function(x) { + if (typeof(x) === "undefined") { + return 0; // TypeUndefined + } + if (x === null) { + return 1; // TypeNull + } + if (typeof(x) === "boolean") { + return 2; // TypeBoolean + } + if (typeof(x) === "number") { + return 3; // TypeNumber + } + if (typeof(x) === "string") { + return 4; // TypeString + } + if (typeof(x) === "symbol") { + return 5; // TypeSymbol + } + if (typeof(x) === "function") { + return 7; // TypeFunction + } + return 6; // TypeObject +})`) + } +} + +func ValueOf(x interface{}) Value { + switch x := x.(type) { + case Value: + return x + case Func: + return x.Value + case TypedArray: + return x.Value + case nil: + return Null() + case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, unsafe.Pointer, string, map[string]interface{}, []interface{}: + return objectToValue(id.Invoke(x)) + default: + panic(`invalid arg: ` + reflect.TypeOf(x).String()) + } +} + +func (v Value) internal() *js.Object { + if !v.inited { + return js.Undefined + } + return v.v +} + +func (v Value) Bool() bool { + if vType := v.Type(); vType != TypeBoolean { + panic(&ValueError{"Value.Bool", vType}) + } + return v.internal().Bool() +} + +// convertArgs converts arguments into values for GopherJS arguments. +func convertArgs(args ...interface{}) []interface{} { + newArgs := []interface{}{} + for _, arg := range args { + v := ValueOf(arg) + newArgs = append(newArgs, v.internal()) + } + return newArgs +} + +func (v Value) Call(m string, args ...interface{}) Value { + if vType := v.Type(); vType != TypeObject && vType != TypeFunction { + panic(&ValueError{"Value.Call", vType}) + } + if propType := v.Get(m).Type(); propType != TypeFunction { + panic("js: Value.Call: property " + m + " is not a function, got " + propType.String()) + } + return objectToValue(v.internal().Call(m, convertArgs(args...)...)) +} + +func (v Value) Float() float64 { + if vType := v.Type(); vType != TypeNumber { + panic(&ValueError{"Value.Float", vType}) + } + return v.internal().Float() +} + +func (v Value) Get(p string) Value { + return objectToValue(v.internal().Get(p)) +} + +func (v Value) Index(i int) Value { + return objectToValue(v.internal().Index(i)) +} + +func (v Value) Int() int { + if vType := v.Type(); vType != TypeNumber { + panic(&ValueError{"Value.Int", vType}) + } + return v.internal().Int() +} + +func (v Value) InstanceOf(t Value) bool { + return instanceOf.Invoke(v.internal(), t.internal()).Bool() +} + +func (v Value) Invoke(args ...interface{}) Value { + if vType := v.Type(); vType != TypeFunction { + panic(&ValueError{"Value.Invoke", vType}) + } + return objectToValue(v.internal().Invoke(convertArgs(args...)...)) +} + +func (v Value) JSValue() Value { + return v +} + +func (v Value) Length() int { + return v.internal().Length() +} + +func (v Value) New(args ...interface{}) Value { + return objectToValue(v.internal().New(convertArgs(args...)...)) +} + +func (v Value) Set(p string, x interface{}) { + v.internal().Set(p, convertArgs(x)[0]) +} + +func (v Value) SetIndex(i int, x interface{}) { + v.internal().SetIndex(i, convertArgs(x)[0]) +} + +func (v Value) String() string { + return v.internal().String() +} + +func (v Value) Truthy() bool { + return v.internal().Bool() +} + +func (v Value) Type() Type { + return Type(getValueType.Invoke(v.internal()).Int()) +} + +type TypedArray struct { + Value +} + +func TypedArrayOf(slice interface{}) TypedArray { + switch slice := slice.(type) { + case []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32, []float64: + return TypedArray{objectToValue(id.Invoke(slice))} + default: + panic("TypedArrayOf: not a supported slice") + } +} + +func (t *TypedArray) Release() { + t.Value = Value{} +} + +type ValueError struct { + Method string + Type Type +} + +func (e *ValueError) Error() string { + return "syscall/js: call of " + e.Method + " on " + e.Type.String() +} + +type Wrapper interface { + JSValue() Value +} diff --git a/tests/syscalljs/js_test.go b/tests/syscalljs/js_test.go new file mode 100644 index 000000000..5a59d9346 --- /dev/null +++ b/tests/syscalljs/js_test.go @@ -0,0 +1,395 @@ +// This file is basically copied from $GOROOT/src/syscall/js/js_test.go + +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build js + +// To run these tests: +// +// - Install Node +// - Add /path/to/go/misc/wasm to your $PATH (so that "go test" can find +// "go_js_wasm_exec"). +// - GOOS=js GOARCH=wasm go test +// +// See -exec in "go help test", and "go help run" for details. + +package js_test + +import ( + "fmt" + "math" + "syscall/js" + "testing" +) + +func TestMain(m *testing.M) { + // Suppress the 'deadlock' error on GopherJS by goroutine + // (https://github.com/gopherjs/gopherjs/issues/826). + go func() { + m.Run() + }() +} + +var dummys = js.Global().Call("eval", `({ + someBool: true, + someString: "abc\u1234", + someInt: 42, + someFloat: 42.123, + someArray: [41, 42, 43], + someDate: new Date(), + add: function(a, b) { + return a + b; + }, + zero: 0, + stringZero: "0", + NaN: NaN, + emptyObj: {}, + emptyArray: [], + Infinity: Infinity, + NegInfinity: -Infinity, + objNumber0: new Number(0), + objBooleanFalse: new Boolean(false), +})`) + +func TestBool(t *testing.T) { + want := true + o := dummys.Get("someBool") + if got := o.Bool(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + dummys.Set("otherBool", want) + if got := dummys.Get("otherBool").Bool(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if dummys.Get("someBool") != dummys.Get("someBool") { + t.Errorf("same value not equal") + } +} + +func TestString(t *testing.T) { + want := "abc\u1234" + o := dummys.Get("someString") + if got := o.String(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + dummys.Set("otherString", want) + if got := dummys.Get("otherString").String(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if dummys.Get("someString") != dummys.Get("someString") { + t.Errorf("same value not equal") + } + + wantInt := "42" + o = dummys.Get("someInt") + if got := o.String(); got != wantInt { + t.Errorf("got %#v, want %#v", got, wantInt) + } +} + +func TestInt(t *testing.T) { + want := 42 + o := dummys.Get("someInt") + if got := o.Int(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + dummys.Set("otherInt", want) + if got := dummys.Get("otherInt").Int(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if dummys.Get("someInt") != dummys.Get("someInt") { + t.Errorf("same value not equal") + } + if got := dummys.Get("zero").Int(); got != 0 { + t.Errorf("got %#v, want %#v", got, 0) + } +} + +func TestIntConversion(t *testing.T) { + testIntConversion(t, 0) + testIntConversion(t, 1) + testIntConversion(t, -1) + testIntConversion(t, 1<<20) + testIntConversion(t, -1<<20) + + // Skip too big integers. They cannot be compiled with 32bit environment, and GopherJS is one of them. + // testIntConversion(t, 1<<40) + // testIntConversion(t, -1<<40) + // testIntConversion(t, 1<<60) + // testIntConversion(t, -1<<60) +} + +func testIntConversion(t *testing.T, want int) { + if got := js.ValueOf(want).Int(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } +} + +func TestFloat(t *testing.T) { + want := 42.123 + o := dummys.Get("someFloat") + if got := o.Float(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + dummys.Set("otherFloat", want) + if got := dummys.Get("otherFloat").Float(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if dummys.Get("someFloat") != dummys.Get("someFloat") { + t.Errorf("same value not equal") + } +} + +func TestObject(t *testing.T) { + if dummys.Get("someArray") != dummys.Get("someArray") { + t.Errorf("same value not equal") + } + + // An object and its prototype should not be equal. + proto := js.Global().Get("Object").Get("prototype") + o := js.Global().Call("eval", "new Object()") + if proto == o { + t.Errorf("object equals to its prototype") + } +} + +func TestFrozenObject(t *testing.T) { + o := js.Global().Call("eval", "(function () { let o = new Object(); o.field = 5; Object.freeze(o); return o; })()") + want := 5 + if got := o.Get("field").Int(); want != got { + t.Errorf("got %#v, want %#v", got, want) + } +} + +func TestTypedArrayOf(t *testing.T) { + testTypedArrayOf(t, "[]int8", []int8{0, -42, 0}, -42) + testTypedArrayOf(t, "[]int16", []int16{0, -42, 0}, -42) + testTypedArrayOf(t, "[]int32", []int32{0, -42, 0}, -42) + testTypedArrayOf(t, "[]uint8", []uint8{0, 42, 0}, 42) + testTypedArrayOf(t, "[]uint16", []uint16{0, 42, 0}, 42) + testTypedArrayOf(t, "[]uint32", []uint32{0, 42, 0}, 42) + testTypedArrayOf(t, "[]float32", []float32{0, -42.5, 0}, -42.5) + testTypedArrayOf(t, "[]float64", []float64{0, -42.5, 0}, -42.5) +} + +func testTypedArrayOf(t *testing.T, name string, slice interface{}, want float64) { + t.Run(name, func(t *testing.T) { + a := js.TypedArrayOf(slice) + got := a.Index(1).Float() + a.Release() + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } + }) +} + +func TestNaN(t *testing.T) { + t.Skip("NaN cannot be compared") + + want := js.ValueOf(math.NaN()) + got := dummys.Get("NaN") + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } +} + +func TestUndefined(t *testing.T) { + dummys.Set("test", js.Undefined()) + if dummys == js.Undefined() || dummys.Get("test") != js.Undefined() || dummys.Get("xyz") != js.Undefined() { + t.Errorf("js.Undefined expected") + } +} + +func TestNull(t *testing.T) { + dummys.Set("test1", nil) + dummys.Set("test2", js.Null()) + if dummys == js.Null() || dummys.Get("test1") != js.Null() || dummys.Get("test2") != js.Null() { + t.Errorf("js.Null expected") + } +} + +func TestLength(t *testing.T) { + if got := dummys.Get("someArray").Length(); got != 3 { + t.Errorf("got %#v, want %#v", got, 3) + } +} + +func TestIndex(t *testing.T) { + if got := dummys.Get("someArray").Index(1).Int(); got != 42 { + t.Errorf("got %#v, want %#v", got, 42) + } +} + +func TestSetIndex(t *testing.T) { + dummys.Get("someArray").SetIndex(2, 99) + if got := dummys.Get("someArray").Index(2).Int(); got != 99 { + t.Errorf("got %#v, want %#v", got, 99) + } +} + +func TestCall(t *testing.T) { + var i int64 = 40 + if got := dummys.Call("add", i, 2).Int(); got != 42 { + t.Errorf("got %#v, want %#v", got, 42) + } + if got := dummys.Call("add", js.Global().Call("eval", "40"), 2).Int(); got != 42 { + t.Errorf("got %#v, want %#v", got, 42) + } +} + +func TestInvoke(t *testing.T) { + var i int64 = 40 + if got := dummys.Get("add").Invoke(i, 2).Int(); got != 42 { + t.Errorf("got %#v, want %#v", got, 42) + } +} + +func TestNew(t *testing.T) { + if got := js.Global().Get("Array").New(42).Length(); got != 42 { + t.Errorf("got %#v, want %#v", got, 42) + } +} + +func TestInstanceOf(t *testing.T) { + someArray := js.Global().Get("Array").New() + if got, want := someArray.InstanceOf(js.Global().Get("Array")), true; got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if got, want := someArray.InstanceOf(js.Global().Get("Function")), false; got != want { + t.Errorf("got %#v, want %#v", got, want) + } +} + +func TestType(t *testing.T) { + if got, want := js.Undefined().Type(), js.TypeUndefined; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.Null().Type(), js.TypeNull; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.ValueOf(true).Type(), js.TypeBoolean; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.ValueOf(0).Type(), js.TypeNumber; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.ValueOf(42).Type(), js.TypeNumber; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.ValueOf("test").Type(), js.TypeString; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.Global().Get("Symbol").Invoke("test").Type(), js.TypeSymbol; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.Global().Get("Array").New().Type(), js.TypeObject; got != want { + t.Errorf("got %s, want %s", got, want) + } + if got, want := js.Global().Get("Array").Type(), js.TypeFunction; got != want { + t.Errorf("got %s, want %s", got, want) + } +} + +type object = map[string]interface{} +type array = []interface{} + +func TestValueOf(t *testing.T) { + a := js.ValueOf(array{0, array{0, 42, 0}, 0}) + if got := a.Index(1).Index(1).Int(); got != 42 { + t.Errorf("got %v, want %v", got, 42) + } + + o := js.ValueOf(object{"x": object{"y": 42}}) + if got := o.Get("x").Get("y").Int(); got != 42 { + t.Errorf("got %v, want %v", got, 42) + } +} + +func TestZeroValue(t *testing.T) { + var v js.Value + if v != js.Undefined() { + t.Error("zero js.Value is not js.Undefined()") + } +} + +func TestFuncOf(t *testing.T) { + c := make(chan struct{}) + cb := js.FuncOf(func(this js.Value, args []js.Value) interface{} { + if got := args[0].Int(); got != 42 { + t.Errorf("got %#v, want %#v", got, 42) + } + c <- struct{}{} + return nil + }) + defer cb.Release() + js.Global().Call("setTimeout", cb, 0, 42) + <-c +} + +func TestInvokeFunction(t *testing.T) { + called := false + cb := js.FuncOf(func(this js.Value, args []js.Value) interface{} { + cb2 := js.FuncOf(func(this js.Value, args []js.Value) interface{} { + called = true + return 42 + }) + defer cb2.Release() + return cb2.Invoke() + }) + defer cb.Release() + if got := cb.Invoke().Int(); got != 42 { + t.Errorf("got %#v, want %#v", got, 42) + } + if !called { + t.Error("function not called") + } +} + +func ExampleFuncOf() { + var cb js.Func + cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} { + fmt.Println("button clicked") + cb.Release() // release the function if the button will not be clicked again + return nil + }) + js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb) +} + +// See +// - https://developer.mozilla.org/en-US/docs/Glossary/Truthy +// - https://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript/19839953#19839953 +// - http://www.ecma-international.org/ecma-262/5.1/#sec-9.2 +func TestTruthy(t *testing.T) { + want := true + for _, key := range []string{ + "someBool", "someString", "someInt", "someFloat", "someArray", "someDate", + "stringZero", // "0" is truthy + "add", // functions are truthy + "emptyObj", "emptyArray", "Infinity", "NegInfinity", + // All objects are truthy, even if they're Number(0) or Boolean(false). + "objNumber0", "objBooleanFalse", + } { + if got := dummys.Get(key).Truthy(); got != want { + t.Errorf("%s: got %#v, want %#v", key, got, want) + } + } + + want = false + if got := dummys.Get("zero").Truthy(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if got := dummys.Get("NaN").Truthy(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if got := js.ValueOf("").Truthy(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if got := js.Null().Truthy(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } + if got := js.Undefined().Truthy(); got != want { + t.Errorf("got %#v, want %#v", got, want) + } +}