gofront 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +5 -4
- package/src/backend/js/expressions.js +849 -371
- package/src/backend/js/index.js +241 -147
- package/src/backend/js/natives.js +383 -0
- package/src/backend/js/runtime.js +512 -2547
- package/src/backend/js/statements.js +506 -103
- package/src/backend/js/stdlib/index.js +45 -93
- package/src/backend/js/templ.js +32 -45
- package/src/backend/js/type-info.js +177 -13
- package/src/backend/shared/fixed-float.js +20 -0
- package/src/backend/shared/fmt-types.js +63 -171
- package/src/backend/shared/host-unicode.js +36 -0
- package/src/backend/wasm/boundary.js +490 -965
- package/src/backend/wasm/emit-builtins.js +643 -256
- package/src/backend/wasm/emit-exprs.js +124 -57
- package/src/backend/wasm/emit-maps.js +17 -367
- package/src/backend/wasm/emit-stmts.js +325 -75
- package/src/backend/wasm/emit.js +104 -13
- package/src/backend/wasm/globals.js +47 -8
- package/src/backend/wasm/glue.js +2 -4
- package/src/backend/wasm/index.js +36 -530
- package/src/backend/wasm/method-expr.js +12 -1
- package/src/backend/wasm/module-emitter.js +92 -717
- package/src/backend/wasm/monomorph.js +103 -6
- package/src/backend/wasm/natives.js +596 -0
- package/src/backend/wasm/prescan.js +5 -153
- package/src/backend/wasm/prune-funcs.js +89 -0
- package/src/backend/wasm/stdlib/index.js +2 -32
- package/src/backend/wasm/types.js +68 -20
- package/src/backend/wasm/variadic.js +3 -3
- package/src/cli/cli-core.js +5 -1
- package/src/cli/test-runner.js +115 -342
- package/src/compiler.js +108 -130
- package/src/lexer/lexer.js +105 -11
- package/src/lexer/templ-lexer.js +61 -18
- package/src/lexer/tokens.js +1 -0
- package/src/link/stdlib.js +354 -132
- package/src/link/type-names.js +148 -0
- package/src/link/wasm.js +30 -6
- package/src/lower/bounds.js +153 -0
- package/src/lower/captures.js +92 -33
- package/src/lower/embedding.js +32 -3
- package/src/lower/errors-as.js +196 -0
- package/src/lower/fmt-formatters.js +905 -0
- package/src/lower/functions.js +2 -0
- package/src/lower/gen-types.js +274 -0
- package/src/lower/index.js +7 -1
- package/src/lower/json-codecs.js +228 -247
- package/src/lower/ownership.js +25 -7
- package/src/lower/reachability.js +69 -51
- package/src/lower/walk.js +12 -0
- package/src/parser/declarations.js +28 -5
- package/src/parser/expressions.js +86 -16
- package/src/parser/statements.js +1 -0
- package/src/parser/templ.js +44 -5
- package/src/parser/types.js +1 -0
- package/src/resolver.js +1 -2
- package/src/typechecker/assignability.js +124 -11
- package/src/typechecker/const-fold.js +70 -27
- package/src/typechecker/constants.js +32 -6
- package/src/typechecker/expressions.js +326 -98
- package/src/typechecker/index.js +208 -33
- package/src/typechecker/init-order.js +136 -40
- package/src/typechecker/operand-text.js +40 -0
- package/src/typechecker/resolve.js +192 -36
- package/src/typechecker/statements.js +122 -11
- package/src/typechecker/stdlib/excluded.js +9 -5
- package/src/typechecker/stdlib/index.js +25 -9
- package/src/typechecker/termination.js +10 -1
- package/src/typechecker/types.js +203 -573
- package/stdlib/bytes/buffer.go +479 -0
- package/stdlib/bytes/bytes.go +1010 -0
- package/stdlib/bytes/reader.go +161 -0
- package/stdlib/context/context.go +115 -0
- package/stdlib/crypto/rand/rand.go +34 -0
- package/stdlib/crypto/sha256/sha256.go +249 -0
- package/stdlib/encoding/base64/base64.go +331 -0
- package/stdlib/encoding/binary/binary.go +332 -0
- package/stdlib/encoding/hex/hex.go +140 -0
- package/stdlib/encoding/json/json.go +62 -0
- package/stdlib/errors/errors.go +174 -0
- package/stdlib/fmt/errors.go +110 -0
- package/stdlib/fmt/format.go +732 -0
- package/stdlib/fmt/print.go +1232 -0
- package/stdlib/gom/gom.go +293 -0
- package/stdlib/html/html.go +251 -0
- package/stdlib/internal/fmtrt/fmtrt.go +163 -0
- package/stdlib/internal/jsonrt/dec.go +171 -38
- package/stdlib/internal/jsonrt/enc.go +186 -36
- package/stdlib/internal/jsonrt/scanner.go +17 -8
- package/stdlib/io/io.go +157 -1
- package/stdlib/maps/maps.go +131 -0
- package/stdlib/math/bits/bits.go +507 -0
- package/stdlib/math/extra.go +16 -14
- package/stdlib/math/math.go +439 -0
- package/stdlib/math/rand/rand.go +297 -0
- package/stdlib/os/os.go +93 -0
- package/stdlib/path/filepath/filepath.go +47 -0
- package/stdlib/path/path.go +394 -0
- package/stdlib/regexp/regexp.go +231 -0
- package/stdlib/slices/slices.go +526 -0
- package/stdlib/slices/sort.go +209 -0
- package/stdlib/slices/zsort_cmp.go +480 -0
- package/stdlib/slices/zsort_ordered.go +482 -0
- package/stdlib/sort/sort.go +391 -452
- package/stdlib/sort/zsort_func.go +480 -0
- package/stdlib/sort/zsort_iface.go +480 -0
- package/stdlib/strconv/float.go +1699 -0
- package/stdlib/strconv/isprint.go +752 -0
- package/stdlib/strconv/quote.go +235 -50
- package/stdlib/strconv/strconv.go +616 -0
- package/stdlib/strings/builder.go +95 -0
- package/stdlib/strings/reader.go +163 -0
- package/stdlib/strings/replacer.go +102 -0
- package/stdlib/strings/strings.go +807 -0
- package/stdlib/sync/sync.go +268 -0
- package/stdlib/testing/async_js.go +61 -0
- package/stdlib/testing/benchmark.go +297 -0
- package/stdlib/testing/testing.go +523 -0
- package/stdlib/time/duration.go +233 -0
- package/stdlib/time/format.go +745 -0
- package/stdlib/time/parse.go +790 -0
- package/stdlib/time/sleep_js.go +63 -0
- package/stdlib/time/time.go +834 -0
- package/stdlib/unicode/unicode.go +141 -3
- package/stdlib/unicode/utf8/utf8.go +464 -0
- package/src/backend/js/fmt-runtime.js +0 -573
- package/src/backend/js/stdlib/base64.js +0 -12
- package/src/backend/js/stdlib/binary.js +0 -102
- package/src/backend/js/stdlib/bits.js +0 -119
- package/src/backend/js/stdlib/builder.js +0 -98
- package/src/backend/js/stdlib/bytes.js +0 -368
- package/src/backend/js/stdlib/errors.js +0 -45
- package/src/backend/js/stdlib/fmt.js +0 -517
- package/src/backend/js/stdlib/gom.js +0 -155
- package/src/backend/js/stdlib/hex.js +0 -17
- package/src/backend/js/stdlib/html.js +0 -20
- package/src/backend/js/stdlib/io.js +0 -137
- package/src/backend/js/stdlib/json.js +0 -1167
- package/src/backend/js/stdlib/maps.js +0 -37
- package/src/backend/js/stdlib/math.js +0 -85
- package/src/backend/js/stdlib/os.js +0 -13
- package/src/backend/js/stdlib/path.js +0 -48
- package/src/backend/js/stdlib/rand.js +0 -32
- package/src/backend/js/stdlib/regexp.js +0 -54
- package/src/backend/js/stdlib/sha256.js +0 -12
- package/src/backend/js/stdlib/shared.js +0 -17
- package/src/backend/js/stdlib/simd.js +0 -34
- package/src/backend/js/stdlib/slices.js +0 -107
- package/src/backend/js/stdlib/sort.js +0 -30
- package/src/backend/js/stdlib/strconv.js +0 -79
- package/src/backend/js/stdlib/strings.js +0 -301
- package/src/backend/js/stdlib/sync.js +0 -13
- package/src/backend/js/stdlib/testing.js +0 -41
- package/src/backend/js/stdlib/time.js +0 -152
- package/src/backend/js/stdlib/unicode.js +0 -25
- package/src/backend/js/stdlib/utf8.js +0 -26
- package/src/backend/wasm/stdlib/bytes.js +0 -6716
- package/src/backend/wasm/stdlib/core.js +0 -10278
- package/src/backend/wasm/stdlib/io.js +0 -3578
- package/src/backend/wasm/stdlib/packages.js +0 -2402
- package/src/backend/wasm/stdlib/pure.js +0 -720
- package/src/backend/wasm/stdlib/time.js +0 -2229
- package/src/typechecker/stdlib/core.js +0 -1079
- package/src/typechecker/stdlib/extended.js +0 -1210
- package/stdlib/bytes/iter.go +0 -192
- package/stdlib/bytes/search.go +0 -240
- package/stdlib/bytes/split.go +0 -218
- package/stdlib/errors/overlay.go +0 -12
- package/stdlib/maps/iter.go +0 -65
- package/stdlib/slices/extra.go +0 -73
- package/stdlib/slices/iter.go +0 -113
- package/stdlib/strings/extra.go +0 -35
- package/stdlib/strings/iter.go +0 -122
- package/stdlib/sync/extra.go +0 -105
- package/stdlib/unicode/utf8/bytes.go +0 -239
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
//go:build gofront
|
|
2
|
+
|
|
3
|
+
//gofront:target both
|
|
4
|
+
|
|
5
|
+
// Copyright 2009 The Go Authors. All rights reserved.
|
|
6
|
+
// Use of this source code is governed by a BSD-style
|
|
7
|
+
// license that can be found in the LICENSE file.
|
|
8
|
+
|
|
9
|
+
// Package rand implements pseudo-random number generators.
|
|
10
|
+
//
|
|
11
|
+
// The top-level functions draw from the host's random source and are not
|
|
12
|
+
// reproducible; Seed is a no-op. A generator made with New(NewSource(seed))
|
|
13
|
+
// is deterministic for a seed, but its sequence is not the one Go's
|
|
14
|
+
// generator produces.
|
|
15
|
+
package rand
|
|
16
|
+
|
|
17
|
+
// A Source represents a source of pseudo-random int64 values in [0, 1<<63).
|
|
18
|
+
type Source interface {
|
|
19
|
+
Int63() int64
|
|
20
|
+
Seed(seed int64)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// A Source64 is a Source that can also generate uniformly-distributed
|
|
24
|
+
// pseudo-random uint64 values in [0, 1<<64) directly.
|
|
25
|
+
type Source64 interface {
|
|
26
|
+
Source
|
|
27
|
+
Uint64() uint64
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// hostFloat returns a uniformly distributed float64 in [0, 1) from the host.
|
|
31
|
+
//
|
|
32
|
+
//gofront:intrinsic rand_source
|
|
33
|
+
func hostFloat() float64
|
|
34
|
+
|
|
35
|
+
// hostSource is the Source behind the top-level functions.
|
|
36
|
+
type hostSource struct{}
|
|
37
|
+
|
|
38
|
+
func (hostSource) Uint64() uint64 {
|
|
39
|
+
hi := uint64(hostFloat() * (1 << 32))
|
|
40
|
+
lo := uint64(hostFloat() * (1 << 32))
|
|
41
|
+
return hi<<32 | lo
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
func (s hostSource) Int63() int64 { return int64(s.Uint64() >> 1) }
|
|
45
|
+
|
|
46
|
+
func (hostSource) Seed(seed int64) {}
|
|
47
|
+
|
|
48
|
+
var host Source = hostSource{}
|
|
49
|
+
|
|
50
|
+
// splitMix is a SplitMix64 generator, the Source behind NewSource.
|
|
51
|
+
type splitMix struct {
|
|
52
|
+
state uint64
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func (s *splitMix) Uint64() uint64 {
|
|
56
|
+
// Variables, not constants: JS output of a typed 64-bit constant above
|
|
57
|
+
// 2^53 is a Number.
|
|
58
|
+
var gamma, mul1, mul2 uint64 = 0x9e3779b97f4a7c15, 0xbf58476d1ce4e5b9, 0x94d049bb133111eb
|
|
59
|
+
s.state += gamma
|
|
60
|
+
z := s.state
|
|
61
|
+
z = (z ^ (z >> 30)) * mul1
|
|
62
|
+
z = (z ^ (z >> 27)) * mul2
|
|
63
|
+
return z ^ (z >> 31)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
func (s *splitMix) Int63() int64 { return int64(s.Uint64() >> 1) }
|
|
67
|
+
|
|
68
|
+
func (s *splitMix) Seed(seed int64) { s.state = uint64(seed) }
|
|
69
|
+
|
|
70
|
+
// NewSource returns a new pseudo-random Source seeded with the given value.
|
|
71
|
+
func NewSource(seed int64) Source {
|
|
72
|
+
return &splitMix{state: uint64(seed)}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// A Rand is a source of random numbers.
|
|
76
|
+
type Rand struct {
|
|
77
|
+
src Source
|
|
78
|
+
s64 Source64 // non-nil if src is source64
|
|
79
|
+
|
|
80
|
+
// readVal contains remainder of 63-bit integer used for bytes
|
|
81
|
+
// generation during Read; readPos is the number of bytes left in it.
|
|
82
|
+
readVal int64
|
|
83
|
+
readPos int8
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// New returns a new Rand that uses random values from src to generate other
|
|
87
|
+
// random values.
|
|
88
|
+
func New(src Source) *Rand {
|
|
89
|
+
s64, _ := src.(Source64)
|
|
90
|
+
return &Rand{src: src, s64: s64}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Seed uses the provided seed value to initialize the generator to a
|
|
94
|
+
// deterministic state.
|
|
95
|
+
func (r *Rand) Seed(seed int64) {
|
|
96
|
+
r.src.Seed(seed)
|
|
97
|
+
r.readPos = 0
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
|
|
101
|
+
func (r *Rand) Int63() int64 { return r.src.Int63() }
|
|
102
|
+
|
|
103
|
+
// Uint32 returns a pseudo-random 32-bit value as a uint32.
|
|
104
|
+
func (r *Rand) Uint32() uint32 { return uint32(r.Int63() >> 31) }
|
|
105
|
+
|
|
106
|
+
// Uint64 returns a pseudo-random 64-bit value as a uint64.
|
|
107
|
+
func (r *Rand) Uint64() uint64 {
|
|
108
|
+
if r.s64 != nil {
|
|
109
|
+
return r.s64.Uint64()
|
|
110
|
+
}
|
|
111
|
+
return uint64(r.Int63())>>31 | uint64(r.Int63())<<32
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Int31 returns a non-negative pseudo-random 31-bit integer as an int32.
|
|
115
|
+
func (r *Rand) Int31() int32 { return int32(r.Int63() >> 32) }
|
|
116
|
+
|
|
117
|
+
// Int returns a non-negative pseudo-random int.
|
|
118
|
+
func (r *Rand) Int() int {
|
|
119
|
+
u := uint(r.Int63())
|
|
120
|
+
return int(u << 1 >> 1)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Int63n returns, as an int64, a non-negative pseudo-random number in the
|
|
124
|
+
// half-open interval [0,n). It panics if n <= 0.
|
|
125
|
+
func (r *Rand) Int63n(n int64) int64 {
|
|
126
|
+
if n <= 0 {
|
|
127
|
+
panic("invalid argument to Int63n")
|
|
128
|
+
}
|
|
129
|
+
if n&(n-1) == 0 { // n is power of two, can mask
|
|
130
|
+
return r.Int63() & (n - 1)
|
|
131
|
+
}
|
|
132
|
+
// A typed variable: an untyped left operand of % is signed in WASM, and
|
|
133
|
+
// JS output of a typed constant above 2^53 is a Number.
|
|
134
|
+
var half uint64 = 1 << 63
|
|
135
|
+
max := int64(half - 1 - half%uint64(n))
|
|
136
|
+
v := r.Int63()
|
|
137
|
+
for v > max {
|
|
138
|
+
v = r.Int63()
|
|
139
|
+
}
|
|
140
|
+
return v % n
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Int31n returns, as an int32, a non-negative pseudo-random number in the
|
|
144
|
+
// half-open interval [0,n). It panics if n <= 0.
|
|
145
|
+
func (r *Rand) Int31n(n int32) int32 {
|
|
146
|
+
if n <= 0 {
|
|
147
|
+
panic("invalid argument to Int31n")
|
|
148
|
+
}
|
|
149
|
+
if n&(n-1) == 0 { // n is power of two, can mask
|
|
150
|
+
return r.Int31() & (n - 1)
|
|
151
|
+
}
|
|
152
|
+
const half uint32 = 1 << 31 // typed: an untyped left operand of % is signed in WASM
|
|
153
|
+
max := int32(half - 1 - half%uint32(n))
|
|
154
|
+
v := r.Int31()
|
|
155
|
+
for v > max {
|
|
156
|
+
v = r.Int31()
|
|
157
|
+
}
|
|
158
|
+
return v % n
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Intn returns, as an int, a non-negative pseudo-random number in the
|
|
162
|
+
// half-open interval [0,n). It panics if n <= 0.
|
|
163
|
+
func (r *Rand) Intn(n int) int {
|
|
164
|
+
if n <= 0 {
|
|
165
|
+
panic("invalid argument to Intn")
|
|
166
|
+
}
|
|
167
|
+
if n <= 1<<31-1 {
|
|
168
|
+
return int(r.Int31n(int32(n)))
|
|
169
|
+
}
|
|
170
|
+
return int(r.Int63n(int64(n)))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Float64 returns, as a float64, a pseudo-random number in the half-open
|
|
174
|
+
// interval [0.0,1.0).
|
|
175
|
+
func (r *Rand) Float64() float64 {
|
|
176
|
+
for {
|
|
177
|
+
f := float64(r.Int63n(1<<53)) / (1 << 53)
|
|
178
|
+
if f != 1 {
|
|
179
|
+
return f
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Float32 returns, as a float32, a pseudo-random number in the half-open
|
|
185
|
+
// interval [0.0,1.0).
|
|
186
|
+
func (r *Rand) Float32() float32 {
|
|
187
|
+
for {
|
|
188
|
+
f := float32(r.Float64())
|
|
189
|
+
if f != 1 {
|
|
190
|
+
return f
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Perm returns, as a slice of n ints, a pseudo-random permutation of the
|
|
196
|
+
// integers in the half-open interval [0,n).
|
|
197
|
+
func (r *Rand) Perm(n int) []int {
|
|
198
|
+
if n < 0 {
|
|
199
|
+
// make([]int, n) panics like this; JS output of make does not check.
|
|
200
|
+
panic("runtime error: makeslice: len out of range")
|
|
201
|
+
}
|
|
202
|
+
m := make([]int, n)
|
|
203
|
+
for i := 0; i < n; i++ {
|
|
204
|
+
j := r.Intn(i + 1)
|
|
205
|
+
m[i] = m[j]
|
|
206
|
+
m[j] = i
|
|
207
|
+
}
|
|
208
|
+
return m
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Shuffle pseudo-randomizes the order of elements. n is the number of
|
|
212
|
+
// elements. Shuffle panics if n < 0. swap swaps the elements with indexes i
|
|
213
|
+
// and j.
|
|
214
|
+
func (r *Rand) Shuffle(n int, swap func(i, j int)) {
|
|
215
|
+
if n < 0 {
|
|
216
|
+
panic("invalid argument to Shuffle")
|
|
217
|
+
}
|
|
218
|
+
for i := n - 1; i > 0; i-- {
|
|
219
|
+
j := int(r.Int63n(int64(i + 1)))
|
|
220
|
+
swap(i, j)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Read generates len(p) random bytes and writes them into p. It always
|
|
225
|
+
// returns len(p) and a nil error.
|
|
226
|
+
func (r *Rand) Read(p []byte) (n int, err error) {
|
|
227
|
+
pos := r.readPos
|
|
228
|
+
val := r.readVal
|
|
229
|
+
for n = 0; n < len(p); n++ {
|
|
230
|
+
if pos == 0 {
|
|
231
|
+
val = r.Int63()
|
|
232
|
+
pos = 7
|
|
233
|
+
}
|
|
234
|
+
p[n] = byte(val)
|
|
235
|
+
val >>= 8
|
|
236
|
+
pos--
|
|
237
|
+
}
|
|
238
|
+
r.readPos = pos
|
|
239
|
+
r.readVal = val
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// The top-level functions draw from the host source through a Rand that
|
|
244
|
+
// holds no state of its own.
|
|
245
|
+
|
|
246
|
+
func hostRand() *Rand { return &Rand{src: host} }
|
|
247
|
+
|
|
248
|
+
// Seed does nothing: the top-level functions always draw from the host.
|
|
249
|
+
func Seed(seed int64) {}
|
|
250
|
+
|
|
251
|
+
// Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
|
|
252
|
+
func Int63() int64 { return host.Int63() }
|
|
253
|
+
|
|
254
|
+
// Uint32 returns a pseudo-random 32-bit value as a uint32.
|
|
255
|
+
func Uint32() uint32 { return hostRand().Uint32() }
|
|
256
|
+
|
|
257
|
+
// Uint64 returns a pseudo-random 64-bit value as a uint64.
|
|
258
|
+
func Uint64() uint64 { return hostSource{}.Uint64() }
|
|
259
|
+
|
|
260
|
+
// Int31 returns a non-negative pseudo-random 31-bit integer as an int32.
|
|
261
|
+
func Int31() int32 { return hostRand().Int31() }
|
|
262
|
+
|
|
263
|
+
// Int returns a non-negative pseudo-random int.
|
|
264
|
+
func Int() int { return hostRand().Int() }
|
|
265
|
+
|
|
266
|
+
// Int63n returns, as an int64, a non-negative pseudo-random number in the
|
|
267
|
+
// half-open interval [0,n). It panics if n <= 0.
|
|
268
|
+
func Int63n(n int64) int64 { return hostRand().Int63n(n) }
|
|
269
|
+
|
|
270
|
+
// Int31n returns, as an int32, a non-negative pseudo-random number in the
|
|
271
|
+
// half-open interval [0,n). It panics if n <= 0.
|
|
272
|
+
func Int31n(n int32) int32 { return hostRand().Int31n(n) }
|
|
273
|
+
|
|
274
|
+
// Intn returns, as an int, a non-negative pseudo-random number in the
|
|
275
|
+
// half-open interval [0,n). It panics if n <= 0.
|
|
276
|
+
func Intn(n int) int { return hostRand().Intn(n) }
|
|
277
|
+
|
|
278
|
+
// Float64 returns, as a float64, a pseudo-random number in the half-open
|
|
279
|
+
// interval [0.0,1.0).
|
|
280
|
+
func Float64() float64 { return hostRand().Float64() }
|
|
281
|
+
|
|
282
|
+
// Float32 returns, as a float32, a pseudo-random number in the half-open
|
|
283
|
+
// interval [0.0,1.0).
|
|
284
|
+
func Float32() float32 { return hostRand().Float32() }
|
|
285
|
+
|
|
286
|
+
// Perm returns, as a slice of n ints, a pseudo-random permutation of the
|
|
287
|
+
// integers in the half-open interval [0,n).
|
|
288
|
+
func Perm(n int) []int { return hostRand().Perm(n) }
|
|
289
|
+
|
|
290
|
+
// Shuffle pseudo-randomizes the order of elements. n is the number of
|
|
291
|
+
// elements. Shuffle panics if n < 0. swap swaps the elements with indexes i
|
|
292
|
+
// and j.
|
|
293
|
+
func Shuffle(n int, swap func(i, j int)) { hostRand().Shuffle(n, swap) }
|
|
294
|
+
|
|
295
|
+
// Read generates len(p) random bytes and writes them into p. It always
|
|
296
|
+
// returns len(p) and a nil error.
|
|
297
|
+
func Read(p []byte) (n int, err error) { return hostRand().Read(p) }
|
package/stdlib/os/os.go
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//go:build gofront
|
|
2
|
+
|
|
3
|
+
//gofront:target both
|
|
4
|
+
|
|
5
|
+
// Package os provides the process-level part of the real package: the
|
|
6
|
+
// standard streams, Exit and the environment. Files, directories and
|
|
7
|
+
// processes are not supported.
|
|
8
|
+
//
|
|
9
|
+
// Writes to Stdout and Stderr go through the host's output; Stdin is always
|
|
10
|
+
// at end of input.
|
|
11
|
+
package os
|
|
12
|
+
|
|
13
|
+
import "io"
|
|
14
|
+
|
|
15
|
+
// File represents an open file descriptor.
|
|
16
|
+
type File struct {
|
|
17
|
+
fd int
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Stdin, Stdout and Stderr are open Files pointing to the standard input,
|
|
21
|
+
// standard output, and standard error file descriptors.
|
|
22
|
+
var (
|
|
23
|
+
Stdin = &File{0}
|
|
24
|
+
Stdout = &File{1}
|
|
25
|
+
Stderr = &File{2}
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
// Args hold the command-line arguments. It is always empty.
|
|
29
|
+
var Args []string
|
|
30
|
+
|
|
31
|
+
// hostWrite writes s to the host's output for the file descriptor fd.
|
|
32
|
+
//
|
|
33
|
+
//gofront:intrinsic os_write
|
|
34
|
+
func hostWrite(fd int, s string)
|
|
35
|
+
|
|
36
|
+
// hostExit terminates the process with the given status code.
|
|
37
|
+
//
|
|
38
|
+
//gofront:intrinsic os_exit
|
|
39
|
+
func hostExit(code int)
|
|
40
|
+
|
|
41
|
+
// hostGetenv returns the value of the host environment variable key, or
|
|
42
|
+
// the empty string.
|
|
43
|
+
//
|
|
44
|
+
//gofront:intrinsic os_getenv
|
|
45
|
+
func hostGetenv(key string) string
|
|
46
|
+
|
|
47
|
+
// hostSetenv sets the host environment variable key to value.
|
|
48
|
+
//
|
|
49
|
+
//gofront:intrinsic os_setenv
|
|
50
|
+
func hostSetenv(key, value string)
|
|
51
|
+
|
|
52
|
+
// Write writes len(b) bytes from b to the File.
|
|
53
|
+
func (f *File) Write(b []byte) (n int, err error) {
|
|
54
|
+
hostWrite(f.fd, string(b))
|
|
55
|
+
return len(b), nil
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// WriteString is like Write, but writes the contents of string s rather
|
|
59
|
+
// than a slice of bytes. It reports the bytes written, not the characters.
|
|
60
|
+
func (f *File) WriteString(s string) (n int, err error) {
|
|
61
|
+
hostWrite(f.fd, s)
|
|
62
|
+
// The JavaScript backend counts a string in UTF-16 units; the bytes are
|
|
63
|
+
// what Go reports.
|
|
64
|
+
return len([]byte(s)), nil
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Read reads up to len(b) bytes from the File. Standard input is always at
|
|
68
|
+
// end of input, so it returns 0, io.EOF.
|
|
69
|
+
func (f *File) Read(b []byte) (n int, err error) {
|
|
70
|
+
return 0, io.EOF
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Close closes the File. It has no effect on the standard streams.
|
|
74
|
+
func (f *File) Close() error {
|
|
75
|
+
return nil
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Exit causes the current program to exit with the given status code.
|
|
79
|
+
func Exit(code int) {
|
|
80
|
+
hostExit(code)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Getenv retrieves the value of the environment variable named by the key.
|
|
84
|
+
// It returns the value, which will be empty if the variable is not present.
|
|
85
|
+
func Getenv(key string) string {
|
|
86
|
+
return hostGetenv(key)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Setenv sets the value of the environment variable named by the key.
|
|
90
|
+
func Setenv(key, value string) error {
|
|
91
|
+
hostSetenv(key, value)
|
|
92
|
+
return nil
|
|
93
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//go:build gofront
|
|
2
|
+
|
|
3
|
+
//gofront:target both
|
|
4
|
+
|
|
5
|
+
// Package filepath implements utility routines for manipulating filename
|
|
6
|
+
// paths. GoFront targets the browser and the JavaScript runtime, where paths
|
|
7
|
+
// are slash-separated, so every function is the one in package path.
|
|
8
|
+
package filepath
|
|
9
|
+
|
|
10
|
+
import "path"
|
|
11
|
+
|
|
12
|
+
// ErrBadPattern indicates a pattern was malformed.
|
|
13
|
+
var ErrBadPattern = path.ErrBadPattern
|
|
14
|
+
|
|
15
|
+
// Separator is the OS-specific path separator.
|
|
16
|
+
const Separator = '/'
|
|
17
|
+
|
|
18
|
+
// ListSeparator is the OS-specific path list separator.
|
|
19
|
+
const ListSeparator = ':'
|
|
20
|
+
|
|
21
|
+
// Join joins any number of path elements into a single path, separating them
|
|
22
|
+
// with a slash and cleaning the result.
|
|
23
|
+
func Join(elem ...string) string { return path.Join(elem...) }
|
|
24
|
+
|
|
25
|
+
// Clean returns the shortest path name equivalent to path by purely lexical
|
|
26
|
+
// processing.
|
|
27
|
+
func Clean(p string) string { return path.Clean(p) }
|
|
28
|
+
|
|
29
|
+
// Base returns the last element of path.
|
|
30
|
+
func Base(p string) string { return path.Base(p) }
|
|
31
|
+
|
|
32
|
+
// Dir returns all but the last element of path, typically the path's
|
|
33
|
+
// directory.
|
|
34
|
+
func Dir(p string) string { return path.Dir(p) }
|
|
35
|
+
|
|
36
|
+
// Ext returns the file name extension used by path.
|
|
37
|
+
func Ext(p string) string { return path.Ext(p) }
|
|
38
|
+
|
|
39
|
+
// Split splits path immediately following the final slash, separating it into
|
|
40
|
+
// a directory and file name component.
|
|
41
|
+
func Split(p string) (dir, file string) { return path.Split(p) }
|
|
42
|
+
|
|
43
|
+
// IsAbs reports whether the path is absolute.
|
|
44
|
+
func IsAbs(p string) bool { return path.IsAbs(p) }
|
|
45
|
+
|
|
46
|
+
// Match reports whether name matches the shell file name pattern.
|
|
47
|
+
func Match(pattern, name string) (matched bool, err error) { return path.Match(pattern, name) }
|