|
9 | 9 | // When values pass from Javascript to Go, a process known as internalization,
|
10 | 10 | // the following conversion table is applied:
|
11 | 11 | //
|
12 |
| -// |----------------+---------------+-------------------------+--------| |
13 |
| -// | Go target type | Translation | Javascript source value | Result | |
14 |
| -// |----------------+---------------+-------------------------+--------| |
15 |
| -// | string | UTF16 -> UTF8 | null | "" | |
16 |
| -// | | | undefined | "" | |
17 |
| -// | | | "" | "" | |
18 |
| -// | | | new String("") | "" | |
19 |
| -// | | | "ok" † | "ok" | |
20 |
| -// | | | new String("ok") † | "ok" | |
21 |
| -// |----------------+---------------+-------------------------+--------| |
22 |
| -// | bool | none | null | false | |
23 |
| -// | | | undefined | false | |
24 |
| -// | | | false † | false | |
25 |
| -// | | | new Boolean(false) † | false | |
26 |
| -// |----------------+---------------+-------------------------+--------| |
| 12 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 13 | +// | Go target type | Translation | Javascript source value | Result | |
| 14 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 15 | +// | string | UTF16 -> UTF8 | null | "" | |
| 16 | +// | | | undefined | "" | |
| 17 | +// | | | "" | "" | |
| 18 | +// | | | new String("") | "" | |
| 19 | +// | | | "ok" † | "ok" | |
| 20 | +// | | | new String("ok") † | "ok" | |
| 21 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 22 | +// | bool | none | null | false | |
| 23 | +// | | | undefined | false | |
| 24 | +// | | | false † | false | |
| 25 | +// | | | new Boolean(false) † | false | |
| 26 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 27 | +// | time.Time | none | null | zero val of time.Time | |
| 28 | +// | | | undefined | zero val of time.Time | |
| 29 | +// | | | new Date(Date.UTC(2017, 0, 1)) † | time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC) | |
| 30 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 31 | +// | int, int8, | none | null | 0 | |
| 32 | +// | int16, int32, | | undefined | 0 | |
| 33 | +// | int64 | | 1 † | 1 | |
| 34 | +// | | | new Number(1) † | 1 | |
| 35 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 36 | +// | uint, uint8, | none | null | 0 | |
| 37 | +// | uint16, uint32, | | undefined | 0 | |
| 38 | +// | uint64 | | 1 † | 1 | |
| 39 | +// | | | new Number(1) † | 1 | |
| 40 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 41 | +// | float32, float64 | none | null | 0 | |
| 42 | +// | | | undefined | 0 | |
| 43 | +// | | | 1 † | 1 | |
| 44 | +// | | | new Number(1) † | 1 | |
| 45 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 46 | +// | [n]X - array type | none | null | zero val of [n]X | |
| 47 | +// | length n and | | undefined | zero val of [n]X | |
| 48 | +// | element type X | | new Array(n) | zero val of [n]X | |
| 49 | +// | | | new Array(x1, x2, ... xn) | [n]X{x1, x2, ... xn} | |
| 50 | +// | | | new TypedArray(n) ‡ | zero val of [n]X | |
| 51 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
| 52 | +// | []X - slice type | none | null | ([]X)(nil) | |
| 53 | +// | element type X | | undefined | ([]X)(nil) | |
| 54 | +// | | | new Array(n) | ([]X)(nil) | |
| 55 | +// | | | new Array(x1, x2, ... xn) | [n]X{x1, x2, ... xn} | |
| 56 | +// | | | new TypedArray(n) ‡ | ([]X)(nil) | |
| 57 | +// |-------------------+---------------+----------------------------------+---------------------------------------------| |
27 | 58 | //
|
28 | 59 | // Any source values not listed in this table cause a runtime panic for a given
|
29 | 60 | // target type if a conversion is attempted, e.g. a Javascript number value
|
30 | 61 | // being assigned to a string type Go variable.
|
31 | 62 | //
|
32 |
| -// Source values annotated with † are generally applicable to all valid |
| 63 | +// † - Source values annotated with † are generally applicable to all valid |
33 | 64 | // values of the target type. e.g. for target type string, "ok" represents
|
34 | 65 | // all valid string primitive values.
|
35 | 66 | //
|
| 67 | +// ‡ - TypedArray types, Int8Array etc., can be used in place of Array values |
| 68 | +// where the type corresponds to the Go target type, e.g. Int8Array is valid |
| 69 | +// for target types [n]int8 or []int8. |
| 70 | +// |
36 | 71 | // Externalization
|
37 | 72 | //
|
38 | 73 | // When values pass from Go to Javascript, a process known as externalization,
|
|
0 commit comments