goscript 0.2.4 → 0.2.6
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 +8 -8
- package/cmd/go_js_wasm_exec/main.go +1 -1
- package/cmd/go_js_wasm_exec/main_test.go +1 -1
- package/cmd/goscript/cmd-compile.go +9 -1
- package/cmd/goscript/cmd-test.go +1 -1
- package/cmd/goscript/cmd_compile_test.go +44 -0
- package/cmd/goscript/deps.go +1 -1
- package/cmd/goscript-wasm/main.go +2 -2
- package/compiler/compile-request.go +19 -0
- package/compiler/compile_bench_test.go +121 -0
- package/compiler/compliance_test.go +17 -1
- package/compiler/config.go +2 -0
- package/compiler/gotest/result.go +1 -1
- package/compiler/gotest/runner.go +2 -2
- package/compiler/gotest/runner_test.go +4 -7
- package/compiler/index.test.ts +28 -0
- package/compiler/index.ts +32 -16
- package/compiler/lowering.go +1238 -194
- package/compiler/lowering_bench_test.go +4 -0
- package/compiler/override-facts.go +1 -1
- package/compiler/package-graph.go +92 -0
- package/compiler/package-graph_test.go +113 -0
- package/compiler/runtime-contract.go +1 -1
- package/compiler/semantic-model.go +32 -0
- package/compiler/skeleton_test.go +241 -15
- package/compiler/wasm/compile.go +1 -1
- package/compiler/wasm/compile_test.go +1 -1
- package/dist/compiler/index.d.ts +4 -0
- package/dist/compiler/index.js +26 -15
- package/dist/compiler/index.js.map +1 -1
- package/dist/gs/database/sql/driver/index.d.ts +165 -0
- package/dist/gs/database/sql/driver/index.js +432 -0
- package/dist/gs/database/sql/driver/index.js.map +1 -0
- package/dist/gs/encoding/binary/index.d.ts +71 -0
- package/dist/gs/encoding/binary/index.js +778 -0
- package/dist/gs/encoding/binary/index.js.map +1 -0
- package/dist/gs/fmt/fmt.js +156 -57
- package/dist/gs/fmt/fmt.js.map +1 -1
- package/dist/gs/github.com/klauspost/cpuid/v2/index.d.ts +11 -0
- package/dist/gs/github.com/klauspost/cpuid/v2/index.js +28 -0
- package/dist/gs/github.com/klauspost/cpuid/v2/index.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/errors.d.ts +0 -2
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/index.d.ts +2 -1
- package/dist/gs/github.com/pkg/errors/index.js +1 -1
- package/dist/gs/github.com/pkg/errors/index.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/stack.d.ts +8 -19
- package/dist/gs/github.com/pkg/errors/stack.js +26 -61
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -1
- package/dist/gs/golang.org/x/crypto/cryptobyte/asn1/index.d.ts +19 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/asn1/index.js +25 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/asn1/index.js.map +1 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/index.d.ts +104 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/index.js +1107 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/index.js.map +1 -0
- package/dist/gs/golang.org/x/crypto/internal/alias/index.d.ts +3 -0
- package/dist/gs/golang.org/x/crypto/internal/alias/index.js +39 -0
- package/dist/gs/golang.org/x/crypto/internal/alias/index.js.map +1 -0
- package/dist/gs/runtime/runtime.d.ts +6 -1
- package/dist/gs/runtime/runtime.js +15 -8
- package/dist/gs/runtime/runtime.js.map +1 -1
- package/dist/gs/runtime/trace/index.d.ts +8 -5
- package/dist/gs/runtime/trace/index.js +324 -23
- package/dist/gs/runtime/trace/index.js.map +1 -1
- package/dist/gs/slices/slices.d.ts +2 -1
- package/dist/gs/slices/slices.js +9 -3
- package/dist/gs/slices/slices.js.map +1 -1
- package/dist/gs/sort/search.gs.d.ts +3 -1
- package/dist/gs/sort/search.gs.js +18 -53
- package/dist/gs/sort/search.gs.js.map +1 -1
- package/dist/gs/sync/sync.d.ts +1 -1
- package/dist/gs/sync/sync.js +3 -0
- package/dist/gs/sync/sync.js.map +1 -1
- package/dist/gs/time/time.d.ts +22 -29
- package/dist/gs/time/time.js +111 -32
- package/dist/gs/time/time.js.map +1 -1
- package/dist/gs/unsafe/unsafe.d.ts +3 -2
- package/dist/gs/unsafe/unsafe.js.map +1 -1
- package/go.mod +7 -5
- package/go.sum +12 -26
- package/gs/database/sql/driver/index.test.ts +88 -0
- package/gs/database/sql/driver/index.ts +675 -0
- package/gs/database/sql/driver/meta.json +3 -0
- package/gs/database/sql/driver/parity.json +144 -0
- package/gs/encoding/binary/index.test.ts +239 -0
- package/gs/encoding/binary/index.ts +999 -0
- package/gs/encoding/binary/meta.json +9 -0
- package/gs/encoding/binary/parity.json +72 -0
- package/gs/fmt/fmt.test.ts +28 -0
- package/gs/fmt/fmt.ts +198 -61
- package/gs/fmt/meta.json +2 -1
- package/gs/github.com/klauspost/cpuid/v2/index.ts +38 -0
- package/gs/github.com/klauspost/cpuid/v2/meta.json +3 -0
- package/gs/github.com/pkg/errors/errors.ts +1 -2
- package/gs/github.com/pkg/errors/index.ts +2 -1
- package/gs/github.com/pkg/errors/stack.ts +34 -62
- package/gs/golang.org/x/crypto/cryptobyte/asn1/index.test.ts +19 -0
- package/gs/golang.org/x/crypto/cryptobyte/asn1/index.ts +29 -0
- package/gs/golang.org/x/crypto/cryptobyte/index.test.ts +255 -0
- package/gs/golang.org/x/crypto/cryptobyte/index.ts +1441 -0
- package/gs/golang.org/x/crypto/cryptobyte/meta.json +3 -0
- package/gs/golang.org/x/crypto/internal/alias/index.test.ts +40 -0
- package/gs/golang.org/x/crypto/internal/alias/index.ts +40 -0
- package/gs/runtime/runtime.test.ts +16 -0
- package/gs/runtime/runtime.ts +17 -9
- package/gs/runtime/trace/index.test.ts +113 -14
- package/gs/runtime/trace/index.ts +384 -34
- package/gs/runtime/trace/meta.json +1 -0
- package/gs/slices/slices.test.ts +24 -1
- package/gs/slices/slices.ts +14 -4
- package/gs/sort/meta.json +1 -0
- package/gs/sort/search.gs.ts +20 -5
- package/gs/sync/sync.ts +4 -1
- package/gs/time/time.test.ts +79 -2
- package/gs/time/time.ts +133 -33
- package/gs/unsafe/unsafe.ts +4 -2
- package/package.json +3 -3
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
import * as $ from '@goscript/builtin/index.js'
|
|
2
|
+
import * as context from '@goscript/context/index.js'
|
|
3
|
+
import * as time from '@goscript/time/index.js'
|
|
4
|
+
|
|
5
|
+
export type Value = any
|
|
6
|
+
|
|
7
|
+
export class NamedValue {
|
|
8
|
+
public Name: string
|
|
9
|
+
public Ordinal: number
|
|
10
|
+
public Value: Value
|
|
11
|
+
|
|
12
|
+
constructor(init?: Partial<{ Name: string; Ordinal: number; Value: Value }>) {
|
|
13
|
+
this.Name = init?.Name ?? ''
|
|
14
|
+
this.Ordinal = init?.Ordinal ?? 0
|
|
15
|
+
this.Value = init?.Value ?? null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public clone(): NamedValue {
|
|
19
|
+
return new NamedValue({
|
|
20
|
+
Name: this.Name,
|
|
21
|
+
Ordinal: this.Ordinal,
|
|
22
|
+
Value: this.Value,
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Driver {
|
|
28
|
+
Open(name: string): [Conn | null, $.GoError]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface DriverContext {
|
|
32
|
+
OpenConnector(name: string): [Connector | null, $.GoError]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Connector {
|
|
36
|
+
Connect(ctx: context.Context | null): [Conn | null, $.GoError]
|
|
37
|
+
Driver(): Driver | null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const ErrSkip = $.newError('driver: skip fast-path; continue as if unimplemented')
|
|
41
|
+
export const ErrBadConn = $.newError('driver: bad connection')
|
|
42
|
+
|
|
43
|
+
export interface Pinger {
|
|
44
|
+
Ping(ctx: context.Context | null): $.GoError
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface Execer {
|
|
48
|
+
Exec(query: string, args: $.Slice<Value>): [Result | null, $.GoError]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ExecerContext {
|
|
52
|
+
ExecContext(
|
|
53
|
+
ctx: context.Context | null,
|
|
54
|
+
query: string,
|
|
55
|
+
args: $.Slice<NamedValue>,
|
|
56
|
+
): [Result | null, $.GoError]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface Queryer {
|
|
60
|
+
Query(query: string, args: $.Slice<Value>): [Rows | null, $.GoError]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface QueryerContext {
|
|
64
|
+
QueryContext(
|
|
65
|
+
ctx: context.Context | null,
|
|
66
|
+
query: string,
|
|
67
|
+
args: $.Slice<NamedValue>,
|
|
68
|
+
): [Rows | null, $.GoError]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface Conn {
|
|
72
|
+
Prepare(query: string): [Stmt | null, $.GoError]
|
|
73
|
+
Close(): $.GoError
|
|
74
|
+
Begin(): [Tx | null, $.GoError]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface ConnPrepareContext {
|
|
78
|
+
PrepareContext(
|
|
79
|
+
ctx: context.Context | null,
|
|
80
|
+
query: string,
|
|
81
|
+
): [Stmt | null, $.GoError]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type IsolationLevel = number
|
|
85
|
+
|
|
86
|
+
export class TxOptions {
|
|
87
|
+
public Isolation: IsolationLevel
|
|
88
|
+
public ReadOnly: boolean
|
|
89
|
+
|
|
90
|
+
constructor(init?: Partial<{ Isolation: IsolationLevel; ReadOnly: boolean }>) {
|
|
91
|
+
this.Isolation = init?.Isolation ?? 0
|
|
92
|
+
this.ReadOnly = init?.ReadOnly ?? false
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public clone(): TxOptions {
|
|
96
|
+
return new TxOptions({
|
|
97
|
+
Isolation: this.Isolation,
|
|
98
|
+
ReadOnly: this.ReadOnly,
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface ConnBeginTx {
|
|
104
|
+
BeginTx(ctx: context.Context | null, opts: TxOptions): [Tx | null, $.GoError]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface SessionResetter {
|
|
108
|
+
ResetSession(ctx: context.Context | null): $.GoError
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface Validator {
|
|
112
|
+
IsValid(): boolean
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface Result {
|
|
116
|
+
LastInsertId(): [number, $.GoError]
|
|
117
|
+
RowsAffected(): [number, $.GoError]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface Stmt {
|
|
121
|
+
Close(): $.GoError
|
|
122
|
+
NumInput(): number
|
|
123
|
+
Exec(args: $.Slice<Value>): [Result | null, $.GoError]
|
|
124
|
+
Query(args: $.Slice<Value>): [Rows | null, $.GoError]
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface StmtExecContext {
|
|
128
|
+
ExecContext(
|
|
129
|
+
ctx: context.Context | null,
|
|
130
|
+
args: $.Slice<NamedValue>,
|
|
131
|
+
): [Result | null, $.GoError]
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface StmtQueryContext {
|
|
135
|
+
QueryContext(
|
|
136
|
+
ctx: context.Context | null,
|
|
137
|
+
args: $.Slice<NamedValue>,
|
|
138
|
+
): [Rows | null, $.GoError]
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export const ErrRemoveArgument = $.newError('driver: remove argument from query')
|
|
142
|
+
|
|
143
|
+
export interface NamedValueChecker {
|
|
144
|
+
CheckNamedValue(value: NamedValue | $.VarRef<NamedValue> | null): $.GoError
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface ColumnConverter {
|
|
148
|
+
ColumnConverter(idx: number): ValueConverter | null
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface Rows {
|
|
152
|
+
Columns(): $.Slice<string>
|
|
153
|
+
Close(): $.GoError
|
|
154
|
+
Next(dest: $.Slice<Value>): $.GoError
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface RowsNextResultSet extends Rows {
|
|
158
|
+
HasNextResultSet(): boolean
|
|
159
|
+
NextResultSet(): $.GoError
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface RowsColumnTypeScanType extends Rows {
|
|
163
|
+
ColumnTypeScanType(index: number): unknown
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface RowsColumnTypeDatabaseTypeName extends Rows {
|
|
167
|
+
ColumnTypeDatabaseTypeName(index: number): string
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface RowsColumnTypeLength extends Rows {
|
|
171
|
+
ColumnTypeLength(index: number): [number, boolean]
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface RowsColumnTypeNullable extends Rows {
|
|
175
|
+
ColumnTypeNullable(index: number): [boolean, boolean]
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface RowsColumnTypePrecisionScale extends Rows {
|
|
179
|
+
ColumnTypePrecisionScale(index: number): [number, number, boolean]
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface Tx {
|
|
183
|
+
Commit(): $.GoError
|
|
184
|
+
Rollback(): $.GoError
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export type RowsAffected = number
|
|
188
|
+
|
|
189
|
+
export function RowsAffected_LastInsertId(_v: RowsAffected): [number, $.GoError] {
|
|
190
|
+
return [0, $.newError('LastInsertId is not supported by this driver')]
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function RowsAffected_RowsAffected(v: RowsAffected): [number, $.GoError] {
|
|
194
|
+
return [v, null]
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
class noRows implements Result {
|
|
198
|
+
public LastInsertId(): [number, $.GoError] {
|
|
199
|
+
return [0, $.newError('no LastInsertId available after DDL statement')]
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public RowsAffected(): [number, $.GoError] {
|
|
203
|
+
return [0, $.newError('no RowsAffected available after DDL statement')]
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export const ResultNoRows: Result = new noRows()
|
|
208
|
+
|
|
209
|
+
export interface ValueConverter {
|
|
210
|
+
ConvertValue(v: any): [Value, $.GoError]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface Valuer {
|
|
214
|
+
Value(): [Value, $.GoError]
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
class boolType implements ValueConverter {
|
|
218
|
+
public String(): string {
|
|
219
|
+
return 'Bool'
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
public ConvertValue(src: any): [Value, $.GoError] {
|
|
223
|
+
guardReflectValue('Bool.ConvertValue', src)
|
|
224
|
+
if (typeof src === 'boolean') {
|
|
225
|
+
return [src, null]
|
|
226
|
+
}
|
|
227
|
+
if (typeof src === 'string') {
|
|
228
|
+
return parseBoolValue(src, JSON.stringify(src))
|
|
229
|
+
}
|
|
230
|
+
if (isBytes(src)) {
|
|
231
|
+
const text = $.bytesToString(src)
|
|
232
|
+
return parseBoolValue(text, JSON.stringify(text))
|
|
233
|
+
}
|
|
234
|
+
if (isIntegerLike(src)) {
|
|
235
|
+
const value = toBigInt(src)
|
|
236
|
+
if (value === 0n || value === 1n) {
|
|
237
|
+
return [value === 1n, null]
|
|
238
|
+
}
|
|
239
|
+
return [null, $.newError(`sql/driver: couldn't convert ${value} into type bool`)]
|
|
240
|
+
}
|
|
241
|
+
return [
|
|
242
|
+
null,
|
|
243
|
+
$.newError(
|
|
244
|
+
`sql/driver: couldn't convert ${globalThis.String(src)} (${typeName(src)}) into type bool`,
|
|
245
|
+
),
|
|
246
|
+
]
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export const Bool = new boolType()
|
|
251
|
+
|
|
252
|
+
class int32Type implements ValueConverter {
|
|
253
|
+
public ConvertValue(v: any): [Value, $.GoError] {
|
|
254
|
+
guardReflectValue('Int32.ConvertValue', v)
|
|
255
|
+
const [value, parseErr] = int32InputValue(v)
|
|
256
|
+
if (parseErr !== null) {
|
|
257
|
+
return [null, parseErr]
|
|
258
|
+
}
|
|
259
|
+
if (value == null) {
|
|
260
|
+
return [
|
|
261
|
+
null,
|
|
262
|
+
$.newError(
|
|
263
|
+
`sql/driver: unsupported value ${globalThis.String(v)} (${typeName(v)}) converting to int32`,
|
|
264
|
+
),
|
|
265
|
+
]
|
|
266
|
+
}
|
|
267
|
+
if (value > 2147483647n || value < -2147483648n) {
|
|
268
|
+
return [null, $.newError(`sql/driver: value ${globalThis.String(v)} overflows int32`)]
|
|
269
|
+
}
|
|
270
|
+
return [Number(value), null]
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export const Int32 = new int32Type()
|
|
275
|
+
|
|
276
|
+
class stringType implements ValueConverter {
|
|
277
|
+
public ConvertValue(v: any): [Value, $.GoError] {
|
|
278
|
+
guardReflectValue('String.ConvertValue', v)
|
|
279
|
+
if (typeof v === 'string' || isBytes(v)) {
|
|
280
|
+
return [v, null]
|
|
281
|
+
}
|
|
282
|
+
if (typeof v === 'number' || typeof v === 'bigint' || typeof v === 'boolean') {
|
|
283
|
+
return [globalThis.String(v), null]
|
|
284
|
+
}
|
|
285
|
+
return [
|
|
286
|
+
null,
|
|
287
|
+
$.newError(
|
|
288
|
+
`database/sql/driver: String.ConvertValue of ${typeName(v)} is not supported in the GoScript browser build`,
|
|
289
|
+
),
|
|
290
|
+
]
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export const String = new stringType()
|
|
295
|
+
|
|
296
|
+
export class Null implements ValueConverter {
|
|
297
|
+
public Converter: ValueConverter | null
|
|
298
|
+
|
|
299
|
+
constructor(init?: Partial<{ Converter: ValueConverter | null }>) {
|
|
300
|
+
this.Converter = init?.Converter ?? null
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
public clone(): Null {
|
|
304
|
+
return new Null({ Converter: this.Converter })
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
public ConvertValue(v: any): [Value, $.GoError] {
|
|
308
|
+
if (v === null || v === undefined) {
|
|
309
|
+
return [null, null]
|
|
310
|
+
}
|
|
311
|
+
return converterValue(this.Converter).ConvertValue(v)
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export class NotNull implements ValueConverter {
|
|
316
|
+
public Converter: ValueConverter | null
|
|
317
|
+
|
|
318
|
+
constructor(init?: Partial<{ Converter: ValueConverter | null }>) {
|
|
319
|
+
this.Converter = init?.Converter ?? null
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
public clone(): NotNull {
|
|
323
|
+
return new NotNull({ Converter: this.Converter })
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
public ConvertValue(v: any): [Value, $.GoError] {
|
|
327
|
+
if (v === null || v === undefined) {
|
|
328
|
+
return [null, $.newError('nil value not allowed')]
|
|
329
|
+
}
|
|
330
|
+
return converterValue(this.Converter).ConvertValue(v)
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
class defaultConverter implements ValueConverter {
|
|
335
|
+
public ConvertValue(v: any): [Value, $.GoError] {
|
|
336
|
+
if (IsValue(v)) {
|
|
337
|
+
return [v ?? null, null]
|
|
338
|
+
}
|
|
339
|
+
if (isValuer(v)) {
|
|
340
|
+
const [sv, err] = callValuerValue(v)
|
|
341
|
+
if (err !== null) {
|
|
342
|
+
return [null, err]
|
|
343
|
+
}
|
|
344
|
+
if (!IsValue(sv)) {
|
|
345
|
+
return [null, $.newError(`non-Value type ${typeName(sv)} returned from Value`)]
|
|
346
|
+
}
|
|
347
|
+
return [sv, null]
|
|
348
|
+
}
|
|
349
|
+
if (isReflectPointer(v)) {
|
|
350
|
+
unsupportedReflect('DefaultParameterConverter.ConvertValue', 'pointer')
|
|
351
|
+
}
|
|
352
|
+
if (isNamedValue(v)) {
|
|
353
|
+
unsupportedReflect('DefaultParameterConverter.ConvertValue', 'named type')
|
|
354
|
+
}
|
|
355
|
+
return [
|
|
356
|
+
null,
|
|
357
|
+
$.newError(
|
|
358
|
+
`database/sql/driver: DefaultParameterConverter.ConvertValue of ${typeName(v)} is not supported in the GoScript browser build`,
|
|
359
|
+
),
|
|
360
|
+
]
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export const DefaultParameterConverter = new defaultConverter()
|
|
365
|
+
|
|
366
|
+
function callValuerValue(vr: Valuer | null): [Value, $.GoError] {
|
|
367
|
+
if (vr === null) {
|
|
368
|
+
return [null, null]
|
|
369
|
+
}
|
|
370
|
+
return vr.Value()
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export function IsValue(v: any): boolean {
|
|
374
|
+
if (v === null || v === undefined) {
|
|
375
|
+
return true
|
|
376
|
+
}
|
|
377
|
+
return (
|
|
378
|
+
isBytes(v) ||
|
|
379
|
+
typeof v === 'string' ||
|
|
380
|
+
typeof v === 'boolean' ||
|
|
381
|
+
typeof v === 'number' ||
|
|
382
|
+
typeof v === 'bigint' ||
|
|
383
|
+
v instanceof time.Time ||
|
|
384
|
+
isDecimalDecompose(v)
|
|
385
|
+
)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export function IsScanValue(v: any): boolean {
|
|
389
|
+
return IsValue(v)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function converterValue(converter: ValueConverter | null): ValueConverter {
|
|
393
|
+
if (converter === null) {
|
|
394
|
+
throw new Error('runtime error: invalid memory address or nil pointer dereference')
|
|
395
|
+
}
|
|
396
|
+
return converter
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function parseBoolValue(value: string, quoted: string): [Value, $.GoError] {
|
|
400
|
+
switch (value) {
|
|
401
|
+
case '1':
|
|
402
|
+
case 't':
|
|
403
|
+
case 'T':
|
|
404
|
+
case 'TRUE':
|
|
405
|
+
case 'true':
|
|
406
|
+
case 'True':
|
|
407
|
+
return [true, null]
|
|
408
|
+
case '0':
|
|
409
|
+
case 'f':
|
|
410
|
+
case 'F':
|
|
411
|
+
case 'FALSE':
|
|
412
|
+
case 'false':
|
|
413
|
+
case 'False':
|
|
414
|
+
return [false, null]
|
|
415
|
+
default:
|
|
416
|
+
return [
|
|
417
|
+
null,
|
|
418
|
+
$.newError(`sql/driver: couldn't convert ${quoted} into type bool`),
|
|
419
|
+
]
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function isBytes(v: any): v is $.Bytes {
|
|
424
|
+
return v instanceof Uint8Array
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function isIntegerLike(v: any): boolean {
|
|
428
|
+
return (
|
|
429
|
+
typeof v === 'bigint' ||
|
|
430
|
+
(typeof v === 'number' && Number.isFinite(v) && Number.isInteger(v))
|
|
431
|
+
)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function toBigInt(v: number | bigint): bigint {
|
|
435
|
+
if (typeof v === 'bigint') {
|
|
436
|
+
return v
|
|
437
|
+
}
|
|
438
|
+
return BigInt(Math.trunc(v))
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function int32InputValue(v: any): [bigint | null, $.GoError] {
|
|
442
|
+
if (isIntegerLike(v)) {
|
|
443
|
+
return [toBigInt(v), null]
|
|
444
|
+
}
|
|
445
|
+
if (typeof v !== 'string') {
|
|
446
|
+
return [null, null]
|
|
447
|
+
}
|
|
448
|
+
if (!/^[+-]?\d+$/.test(v)) {
|
|
449
|
+
return [
|
|
450
|
+
null,
|
|
451
|
+
$.newError(`sql/driver: value ${JSON.stringify(v)} can't be converted to int32`),
|
|
452
|
+
]
|
|
453
|
+
}
|
|
454
|
+
return [BigInt(v), null]
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function isValuer(v: any): v is Valuer {
|
|
458
|
+
return v !== null && typeof v === 'object' && typeof v.Value === 'function'
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function isDecimalDecompose(v: any): boolean {
|
|
462
|
+
return v !== null && typeof v === 'object' && typeof v.Decompose === 'function'
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function isNamedValue(v: any): boolean {
|
|
466
|
+
return v !== null && typeof v === 'object' && typeof v.__goType === 'string'
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function isReflectPointer(v: any): boolean {
|
|
470
|
+
return $.isVarRef(v) || (isNamedValue(v) && v.__goType.startsWith('*'))
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function guardReflectValue(fn: string, v: any): void {
|
|
474
|
+
if (isReflectPointer(v)) {
|
|
475
|
+
unsupportedReflect(fn, 'pointer')
|
|
476
|
+
}
|
|
477
|
+
if (isNamedValue(v)) {
|
|
478
|
+
unsupportedReflect(fn, 'named type')
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function unsupportedReflect(fn: string, kind: string): never {
|
|
483
|
+
throw $.newError(
|
|
484
|
+
`database/sql/driver: ${fn} of ${kind} is not supported in the GoScript browser build`,
|
|
485
|
+
)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function typeName(v: any): string {
|
|
489
|
+
if (v === null || v === undefined) {
|
|
490
|
+
return '<nil>'
|
|
491
|
+
}
|
|
492
|
+
if (isNamedValue(v)) {
|
|
493
|
+
return v.__goType
|
|
494
|
+
}
|
|
495
|
+
if ($.isVarRef(v)) {
|
|
496
|
+
return 'pointer'
|
|
497
|
+
}
|
|
498
|
+
if (isBytes(v)) {
|
|
499
|
+
return '[]uint8'
|
|
500
|
+
}
|
|
501
|
+
if (v instanceof time.Time) {
|
|
502
|
+
return 'time.Time'
|
|
503
|
+
}
|
|
504
|
+
return typeof v
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const anyType: $.InterfaceTypeInfo = {
|
|
508
|
+
kind: $.TypeKind.Interface,
|
|
509
|
+
methods: [],
|
|
510
|
+
}
|
|
511
|
+
const boolTypeInfo: $.BasicTypeInfo = { kind: $.TypeKind.Basic, name: 'bool' }
|
|
512
|
+
const intTypeInfo: $.BasicTypeInfo = { kind: $.TypeKind.Basic, name: 'int' }
|
|
513
|
+
const int64TypeInfo: $.BasicTypeInfo = { kind: $.TypeKind.Basic, name: 'int64' }
|
|
514
|
+
const stringTypeInfo: $.BasicTypeInfo = {
|
|
515
|
+
kind: $.TypeKind.Basic,
|
|
516
|
+
name: 'string',
|
|
517
|
+
}
|
|
518
|
+
const errorType = 'error'
|
|
519
|
+
const valueType = anyType
|
|
520
|
+
const valueSliceType: $.SliceTypeInfo = {
|
|
521
|
+
kind: $.TypeKind.Slice,
|
|
522
|
+
elemType: valueType,
|
|
523
|
+
}
|
|
524
|
+
const namedValueType = 'driver.NamedValue'
|
|
525
|
+
const namedValueSliceType: $.SliceTypeInfo = {
|
|
526
|
+
kind: $.TypeKind.Slice,
|
|
527
|
+
elemType: namedValueType,
|
|
528
|
+
}
|
|
529
|
+
const namedValuePointerType: $.PointerTypeInfo = {
|
|
530
|
+
kind: $.TypeKind.Pointer,
|
|
531
|
+
elemType: namedValueType,
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function arg(name: string, type: $.TypeInfo | string): $.MethodArg {
|
|
535
|
+
return { name, type }
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function ret(name: string, type: $.TypeInfo | string): $.MethodArg {
|
|
539
|
+
return { name, type }
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const rowsMethods: $.MethodSignature[] = [
|
|
543
|
+
{ name: 'Close', args: [], returns: [ret('_r0', errorType)] },
|
|
544
|
+
{ name: 'Columns', args: [], returns: [ret('_r0', { kind: $.TypeKind.Slice, elemType: stringTypeInfo })] },
|
|
545
|
+
{ name: 'Next', args: [arg('dest', valueSliceType)], returns: [ret('_r0', errorType)] },
|
|
546
|
+
]
|
|
547
|
+
|
|
548
|
+
$.registerInterfaceType('driver.Driver', null, [
|
|
549
|
+
{ name: 'Open', args: [arg('name', stringTypeInfo)], returns: [ret('_r0', 'driver.Conn'), ret('_r1', errorType)] },
|
|
550
|
+
])
|
|
551
|
+
|
|
552
|
+
$.registerInterfaceType('driver.DriverContext', null, [
|
|
553
|
+
{ name: 'OpenConnector', args: [arg('name', stringTypeInfo)], returns: [ret('_r0', 'driver.Connector'), ret('_r1', errorType)] },
|
|
554
|
+
])
|
|
555
|
+
|
|
556
|
+
$.registerInterfaceType('driver.Connector', null, [
|
|
557
|
+
{ name: 'Connect', args: [arg('ctx', 'context.Context')], returns: [ret('_r0', 'driver.Conn'), ret('_r1', errorType)] },
|
|
558
|
+
{ name: 'Driver', args: [], returns: [ret('_r0', 'driver.Driver')] },
|
|
559
|
+
])
|
|
560
|
+
|
|
561
|
+
$.registerInterfaceType('driver.Pinger', null, [
|
|
562
|
+
{ name: 'Ping', args: [arg('ctx', 'context.Context')], returns: [ret('_r0', errorType)] },
|
|
563
|
+
])
|
|
564
|
+
|
|
565
|
+
$.registerInterfaceType('driver.Execer', null, [
|
|
566
|
+
{ name: 'Exec', args: [arg('query', stringTypeInfo), arg('args', valueSliceType)], returns: [ret('_r0', 'driver.Result'), ret('_r1', errorType)] },
|
|
567
|
+
])
|
|
568
|
+
|
|
569
|
+
$.registerInterfaceType('driver.ExecerContext', null, [
|
|
570
|
+
{ name: 'ExecContext', args: [arg('ctx', 'context.Context'), arg('query', stringTypeInfo), arg('args', namedValueSliceType)], returns: [ret('_r0', 'driver.Result'), ret('_r1', errorType)] },
|
|
571
|
+
])
|
|
572
|
+
|
|
573
|
+
$.registerInterfaceType('driver.Queryer', null, [
|
|
574
|
+
{ name: 'Query', args: [arg('query', stringTypeInfo), arg('args', valueSliceType)], returns: [ret('_r0', 'driver.Rows'), ret('_r1', errorType)] },
|
|
575
|
+
])
|
|
576
|
+
|
|
577
|
+
$.registerInterfaceType('driver.QueryerContext', null, [
|
|
578
|
+
{ name: 'QueryContext', args: [arg('ctx', 'context.Context'), arg('query', stringTypeInfo), arg('args', namedValueSliceType)], returns: [ret('_r0', 'driver.Rows'), ret('_r1', errorType)] },
|
|
579
|
+
])
|
|
580
|
+
|
|
581
|
+
$.registerInterfaceType('driver.Conn', null, [
|
|
582
|
+
{ name: 'Begin', args: [], returns: [ret('_r0', 'driver.Tx'), ret('_r1', errorType)] },
|
|
583
|
+
{ name: 'Close', args: [], returns: [ret('_r0', errorType)] },
|
|
584
|
+
{ name: 'Prepare', args: [arg('query', stringTypeInfo)], returns: [ret('_r0', 'driver.Stmt'), ret('_r1', errorType)] },
|
|
585
|
+
])
|
|
586
|
+
|
|
587
|
+
$.registerInterfaceType('driver.ConnPrepareContext', null, [
|
|
588
|
+
{ name: 'PrepareContext', args: [arg('ctx', 'context.Context'), arg('query', stringTypeInfo)], returns: [ret('_r0', 'driver.Stmt'), ret('_r1', errorType)] },
|
|
589
|
+
])
|
|
590
|
+
|
|
591
|
+
$.registerInterfaceType('driver.ConnBeginTx', null, [
|
|
592
|
+
{ name: 'BeginTx', args: [arg('ctx', 'context.Context'), arg('opts', 'driver.TxOptions')], returns: [ret('_r0', 'driver.Tx'), ret('_r1', errorType)] },
|
|
593
|
+
])
|
|
594
|
+
|
|
595
|
+
$.registerInterfaceType('driver.SessionResetter', null, [
|
|
596
|
+
{ name: 'ResetSession', args: [arg('ctx', 'context.Context')], returns: [ret('_r0', errorType)] },
|
|
597
|
+
])
|
|
598
|
+
|
|
599
|
+
$.registerInterfaceType('driver.Validator', null, [
|
|
600
|
+
{ name: 'IsValid', args: [], returns: [ret('_r0', boolTypeInfo)] },
|
|
601
|
+
])
|
|
602
|
+
|
|
603
|
+
$.registerInterfaceType('driver.Result', null, [
|
|
604
|
+
{ name: 'LastInsertId', args: [], returns: [ret('_r0', int64TypeInfo), ret('_r1', errorType)] },
|
|
605
|
+
{ name: 'RowsAffected', args: [], returns: [ret('_r0', int64TypeInfo), ret('_r1', errorType)] },
|
|
606
|
+
])
|
|
607
|
+
|
|
608
|
+
$.registerInterfaceType('driver.Stmt', null, [
|
|
609
|
+
{ name: 'Close', args: [], returns: [ret('_r0', errorType)] },
|
|
610
|
+
{ name: 'Exec', args: [arg('args', valueSliceType)], returns: [ret('_r0', 'driver.Result'), ret('_r1', errorType)] },
|
|
611
|
+
{ name: 'NumInput', args: [], returns: [ret('_r0', intTypeInfo)] },
|
|
612
|
+
{ name: 'Query', args: [arg('args', valueSliceType)], returns: [ret('_r0', 'driver.Rows'), ret('_r1', errorType)] },
|
|
613
|
+
])
|
|
614
|
+
|
|
615
|
+
$.registerInterfaceType('driver.StmtExecContext', null, [
|
|
616
|
+
{ name: 'ExecContext', args: [arg('ctx', 'context.Context'), arg('args', namedValueSliceType)], returns: [ret('_r0', 'driver.Result'), ret('_r1', errorType)] },
|
|
617
|
+
])
|
|
618
|
+
|
|
619
|
+
$.registerInterfaceType('driver.StmtQueryContext', null, [
|
|
620
|
+
{ name: 'QueryContext', args: [arg('ctx', 'context.Context'), arg('args', namedValueSliceType)], returns: [ret('_r0', 'driver.Rows'), ret('_r1', errorType)] },
|
|
621
|
+
])
|
|
622
|
+
|
|
623
|
+
$.registerInterfaceType('driver.NamedValueChecker', null, [
|
|
624
|
+
{ name: 'CheckNamedValue', args: [arg('_p0', namedValuePointerType)], returns: [ret('_r0', errorType)] },
|
|
625
|
+
])
|
|
626
|
+
|
|
627
|
+
$.registerInterfaceType('driver.ColumnConverter', null, [
|
|
628
|
+
{ name: 'ColumnConverter', args: [arg('idx', intTypeInfo)], returns: [ret('_r0', 'driver.ValueConverter')] },
|
|
629
|
+
])
|
|
630
|
+
|
|
631
|
+
$.registerInterfaceType('driver.Rows', null, rowsMethods)
|
|
632
|
+
|
|
633
|
+
$.registerInterfaceType('driver.RowsNextResultSet', null, [
|
|
634
|
+
...rowsMethods,
|
|
635
|
+
{ name: 'HasNextResultSet', args: [], returns: [ret('_r0', boolTypeInfo)] },
|
|
636
|
+
{ name: 'NextResultSet', args: [], returns: [ret('_r0', errorType)] },
|
|
637
|
+
])
|
|
638
|
+
|
|
639
|
+
$.registerInterfaceType('driver.RowsColumnTypeScanType', null, [
|
|
640
|
+
...rowsMethods,
|
|
641
|
+
{ name: 'ColumnTypeScanType', args: [arg('index', intTypeInfo)], returns: [ret('_r0', 'reflect.Type')] },
|
|
642
|
+
])
|
|
643
|
+
|
|
644
|
+
$.registerInterfaceType('driver.RowsColumnTypeDatabaseTypeName', null, [
|
|
645
|
+
...rowsMethods,
|
|
646
|
+
{ name: 'ColumnTypeDatabaseTypeName', args: [arg('index', intTypeInfo)], returns: [ret('_r0', stringTypeInfo)] },
|
|
647
|
+
])
|
|
648
|
+
|
|
649
|
+
$.registerInterfaceType('driver.RowsColumnTypeLength', null, [
|
|
650
|
+
...rowsMethods,
|
|
651
|
+
{ name: 'ColumnTypeLength', args: [arg('index', intTypeInfo)], returns: [ret('length', int64TypeInfo), ret('ok', boolTypeInfo)] },
|
|
652
|
+
])
|
|
653
|
+
|
|
654
|
+
$.registerInterfaceType('driver.RowsColumnTypeNullable', null, [
|
|
655
|
+
...rowsMethods,
|
|
656
|
+
{ name: 'ColumnTypeNullable', args: [arg('index', intTypeInfo)], returns: [ret('nullable', boolTypeInfo), ret('ok', boolTypeInfo)] },
|
|
657
|
+
])
|
|
658
|
+
|
|
659
|
+
$.registerInterfaceType('driver.RowsColumnTypePrecisionScale', null, [
|
|
660
|
+
...rowsMethods,
|
|
661
|
+
{ name: 'ColumnTypePrecisionScale', args: [arg('index', intTypeInfo)], returns: [ret('precision', int64TypeInfo), ret('scale', int64TypeInfo), ret('ok', boolTypeInfo)] },
|
|
662
|
+
])
|
|
663
|
+
|
|
664
|
+
$.registerInterfaceType('driver.Tx', null, [
|
|
665
|
+
{ name: 'Commit', args: [], returns: [ret('_r0', errorType)] },
|
|
666
|
+
{ name: 'Rollback', args: [], returns: [ret('_r0', errorType)] },
|
|
667
|
+
])
|
|
668
|
+
|
|
669
|
+
$.registerInterfaceType('driver.ValueConverter', null, [
|
|
670
|
+
{ name: 'ConvertValue', args: [arg('v', anyType)], returns: [ret('_r0', valueType), ret('_r1', errorType)] },
|
|
671
|
+
])
|
|
672
|
+
|
|
673
|
+
$.registerInterfaceType('driver.Valuer', null, [
|
|
674
|
+
{ name: 'Value', args: [], returns: [ret('_r0', valueType), ret('_r1', errorType)] },
|
|
675
|
+
])
|