tywrap 0.5.1 → 0.6.1
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/dist/cli.js +8 -6
- package/dist/cli.js.map +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +236 -147
- package/dist/config/index.js.map +1 -1
- package/dist/core/annotation-parser.d.ts.map +1 -1
- package/dist/core/annotation-parser.js +112 -97
- package/dist/core/annotation-parser.js.map +1 -1
- package/dist/core/discovery.d.ts +17 -0
- package/dist/core/discovery.d.ts.map +1 -1
- package/dist/core/discovery.js +85 -49
- package/dist/core/discovery.js.map +1 -1
- package/dist/core/emit-call.d.ts +58 -0
- package/dist/core/emit-call.d.ts.map +1 -0
- package/dist/core/emit-call.js +103 -0
- package/dist/core/emit-call.js.map +1 -0
- package/dist/core/generator.d.ts +2 -0
- package/dist/core/generator.d.ts.map +1 -1
- package/dist/core/generator.js +54 -211
- package/dist/core/generator.js.map +1 -1
- package/dist/core/mapper.d.ts.map +1 -1
- package/dist/core/mapper.js +118 -104
- package/dist/core/mapper.js.map +1 -1
- package/dist/core/validation.d.ts +23 -0
- package/dist/core/validation.d.ts.map +1 -1
- package/dist/core/validation.js +52 -48
- package/dist/core/validation.js.map +1 -1
- package/dist/dev.d.ts.map +1 -1
- package/dist/dev.js +175 -133
- package/dist/dev.js.map +1 -1
- package/dist/index.d.ts +6 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -31
- package/dist/index.js.map +1 -1
- package/dist/runtime/bounded-context.d.ts +2 -2
- package/dist/runtime/bounded-context.js +2 -2
- package/dist/runtime/{safe-codec.d.ts → bridge-codec.d.ts} +26 -11
- package/dist/runtime/bridge-codec.d.ts.map +1 -0
- package/dist/runtime/{safe-codec.js → bridge-codec.js} +69 -72
- package/dist/runtime/bridge-codec.js.map +1 -0
- package/dist/runtime/{http-io.d.ts → http-transport.d.ts} +6 -6
- package/dist/runtime/http-transport.d.ts.map +1 -0
- package/dist/runtime/{http-io.js → http-transport.js} +3 -3
- package/dist/runtime/http-transport.js.map +1 -0
- package/dist/runtime/http.d.ts +3 -3
- package/dist/runtime/http.d.ts.map +1 -1
- package/dist/runtime/http.js +4 -4
- package/dist/runtime/http.js.map +1 -1
- package/dist/runtime/index.d.ts +9 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +9 -1
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/node.d.ts +6 -6
- package/dist/runtime/node.d.ts.map +1 -1
- package/dist/runtime/node.js +7 -7
- package/dist/runtime/node.js.map +1 -1
- package/dist/runtime/pooled-transport.d.ts +11 -11
- package/dist/runtime/pooled-transport.d.ts.map +1 -1
- package/dist/runtime/pooled-transport.js +10 -10
- package/dist/runtime/pooled-transport.js.map +1 -1
- package/dist/runtime/pyodide-bootstrap-core.generated.d.ts.map +1 -1
- package/dist/runtime/pyodide-bootstrap-core.generated.js +1 -1
- package/dist/runtime/pyodide-bootstrap-core.generated.js.map +1 -1
- package/dist/runtime/{pyodide-io.d.ts → pyodide-transport.d.ts} +8 -8
- package/dist/runtime/pyodide-transport.d.ts.map +1 -0
- package/dist/runtime/{pyodide-io.js → pyodide-transport.js} +5 -5
- package/dist/runtime/pyodide-transport.js.map +1 -0
- package/dist/runtime/pyodide.d.ts +3 -3
- package/dist/runtime/pyodide.d.ts.map +1 -1
- package/dist/runtime/pyodide.js +5 -5
- package/dist/runtime/pyodide.js.map +1 -1
- package/dist/runtime/rpc-client.d.ts +23 -5
- package/dist/runtime/rpc-client.d.ts.map +1 -1
- package/dist/runtime/rpc-client.js +36 -31
- package/dist/runtime/rpc-client.js.map +1 -1
- package/dist/runtime/{process-io.d.ts → subprocess-transport.d.ts} +25 -9
- package/dist/runtime/subprocess-transport.d.ts.map +1 -0
- package/dist/runtime/{process-io.js → subprocess-transport.js} +60 -41
- package/dist/runtime/subprocess-transport.js.map +1 -0
- package/dist/runtime/{worker-pool.d.ts → transport-pool.d.ts} +15 -15
- package/dist/runtime/transport-pool.d.ts.map +1 -0
- package/dist/runtime/{worker-pool.js → transport-pool.js} +6 -6
- package/dist/runtime/transport-pool.js.map +1 -0
- package/dist/runtime/transport.d.ts +14 -8
- package/dist/runtime/transport.d.ts.map +1 -1
- package/dist/runtime/transport.js +10 -4
- package/dist/runtime/transport.js.map +1 -1
- package/dist/types/index.d.ts +10 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/tywrap.d.ts +0 -5
- package/dist/tywrap.d.ts.map +1 -1
- package/dist/tywrap.js +0 -20
- package/dist/tywrap.js.map +1 -1
- package/dist/utils/cache.d.ts +13 -2
- package/dist/utils/cache.d.ts.map +1 -1
- package/dist/utils/cache.js +52 -60
- package/dist/utils/cache.js.map +1 -1
- package/dist/utils/codec.d.ts +1 -1
- package/dist/utils/codec.d.ts.map +1 -1
- package/dist/utils/codec.js +181 -182
- package/dist/utils/codec.js.map +1 -1
- package/dist/utils/python.d.ts.map +1 -1
- package/dist/utils/python.js +28 -17
- package/dist/utils/python.js.map +1 -1
- package/dist/utils/runtime.d.ts.map +1 -1
- package/dist/utils/runtime.js +22 -16
- package/dist/utils/runtime.js.map +1 -1
- package/dist/version.d.ts +12 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +12 -0
- package/dist/version.js.map +1 -0
- package/package.json +3 -2
- package/runtime/__pycache__/safe_codec.cpython-311.pyc +0 -0
- package/runtime/__pycache__/tywrap_bridge_core.cpython-311.pyc +0 -0
- package/runtime/python_bridge.py +54 -6
- package/runtime/safe_codec.py +8 -8
- package/runtime/tywrap_bridge_core.py +159 -23
- package/src/cli.ts +8 -12
- package/src/config/index.ts +271 -165
- package/src/core/annotation-parser.ts +135 -123
- package/src/core/discovery.ts +102 -54
- package/src/core/emit-call.ts +171 -0
- package/src/core/generator.ts +67 -263
- package/src/core/mapper.ts +125 -110
- package/src/core/validation.ts +84 -48
- package/src/dev.ts +237 -153
- package/src/index.ts +11 -68
- package/src/runtime/bounded-context.ts +2 -2
- package/src/runtime/{safe-codec.ts → bridge-codec.ts} +83 -95
- package/src/runtime/{http-io.ts → http-transport.ts} +5 -5
- package/src/runtime/http.ts +5 -5
- package/src/runtime/index.ts +23 -1
- package/src/runtime/node.ts +15 -15
- package/src/runtime/pooled-transport.ts +15 -15
- package/src/runtime/pyodide-bootstrap-core.generated.ts +1 -1
- package/src/runtime/{pyodide-io.ts → pyodide-transport.ts} +7 -7
- package/src/runtime/pyodide.ts +6 -6
- package/src/runtime/rpc-client.ts +51 -40
- package/src/runtime/{process-io.ts → subprocess-transport.ts} +77 -47
- package/src/runtime/{worker-pool.ts → transport-pool.ts} +26 -26
- package/src/runtime/transport.ts +15 -8
- package/src/types/index.ts +10 -1
- package/src/tywrap.ts +0 -24
- package/src/utils/cache.ts +61 -63
- package/src/utils/codec.ts +217 -196
- package/src/utils/python.ts +33 -20
- package/src/utils/runtime.ts +24 -13
- package/src/version.ts +12 -0
- package/dist/runtime/http-io.d.ts.map +0 -1
- package/dist/runtime/http-io.js.map +0 -1
- package/dist/runtime/optimized-node.d.ts +0 -23
- package/dist/runtime/optimized-node.d.ts.map +0 -1
- package/dist/runtime/optimized-node.js +0 -23
- package/dist/runtime/optimized-node.js.map +0 -1
- package/dist/runtime/process-io.d.ts.map +0 -1
- package/dist/runtime/process-io.js.map +0 -1
- package/dist/runtime/protocol.d.ts +0 -3
- package/dist/runtime/protocol.d.ts.map +0 -1
- package/dist/runtime/protocol.js +0 -3
- package/dist/runtime/protocol.js.map +0 -1
- package/dist/runtime/pyodide-io.d.ts.map +0 -1
- package/dist/runtime/pyodide-io.js.map +0 -1
- package/dist/runtime/safe-codec.d.ts.map +0 -1
- package/dist/runtime/safe-codec.js.map +0 -1
- package/dist/runtime/worker-pool.d.ts.map +0 -1
- package/dist/runtime/worker-pool.js.map +0 -1
- package/dist/utils/bundle-optimizer.d.ts +0 -182
- package/dist/utils/bundle-optimizer.d.ts.map +0 -1
- package/dist/utils/bundle-optimizer.js +0 -680
- package/dist/utils/bundle-optimizer.js.map +0 -1
- package/dist/utils/memory-profiler.d.ts +0 -123
- package/dist/utils/memory-profiler.d.ts.map +0 -1
- package/dist/utils/memory-profiler.js +0 -426
- package/dist/utils/memory-profiler.js.map +0 -1
- package/src/runtime/optimized-node.ts +0 -25
- package/src/runtime/protocol.ts +0 -2
- package/src/utils/bundle-optimizer.ts +0 -876
- package/src/utils/memory-profiler.ts +0 -582
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared call-emission helpers for the TypeScript wrapper generator.
|
|
3
|
+
*
|
|
4
|
+
* The function, method, and constructor wrappers all emit the same runtime
|
|
5
|
+
* prelude (assemble `__args`, fold a trailing object into `__kwargs`, unpack
|
|
6
|
+
* `*args`) and the same argument guards (reject positional-only-as-keyword,
|
|
7
|
+
* require keyword-only arguments). They differ only by indentation, the error
|
|
8
|
+
* label used in thrown messages, and the terminal RPC verb (which the call
|
|
9
|
+
* sites still emit themselves).
|
|
10
|
+
*
|
|
11
|
+
* The emitted strings MUST stay byte-identical to the previous inline copies.
|
|
12
|
+
*/
|
|
13
|
+
import type { Parameter } from '../types/index.js';
|
|
14
|
+
/** Describes one call site for prelude/guard emission. */
|
|
15
|
+
export interface CallDescriptor {
|
|
16
|
+
/** Positional parameters (no *args/**kwargs/keyword-only). */
|
|
17
|
+
positionalParams: readonly Parameter[];
|
|
18
|
+
/** The `*args` parameter, if any. */
|
|
19
|
+
varArgsParam?: Parameter;
|
|
20
|
+
/** Whether `*args` is modeled as an array param (true when kwargs coexist). */
|
|
21
|
+
needsVarArgsArray: boolean;
|
|
22
|
+
/** Whether the wrapper takes a `kwargs` parameter. */
|
|
23
|
+
hasKwArgs: boolean;
|
|
24
|
+
/** Whether the callee declares `**kwargs`. */
|
|
25
|
+
hasVarKwArgs: boolean;
|
|
26
|
+
/** Names of keyword-only parameters. */
|
|
27
|
+
keywordOnlyNames: readonly string[];
|
|
28
|
+
/** Names of required keyword-only parameters. */
|
|
29
|
+
requiredKwOnlyNames: readonly string[];
|
|
30
|
+
/** Names of positional-only parameters. */
|
|
31
|
+
positionalOnlyNames: readonly string[];
|
|
32
|
+
/** Number of leading required positional parameters. */
|
|
33
|
+
requiredPosCount: number;
|
|
34
|
+
/** Base indentation (e.g. ' ' for functions, ' ' for methods/ctors). */
|
|
35
|
+
indent: string;
|
|
36
|
+
/** Error label used in thrown messages (function/method name or '__init__'). */
|
|
37
|
+
errorLabel: string;
|
|
38
|
+
}
|
|
39
|
+
/** Helpers wired from the generator instance (they depend on `this`). */
|
|
40
|
+
export interface CallEmitHelpers {
|
|
41
|
+
escapeIdentifier(name: string): string;
|
|
42
|
+
renderLooksLikeKwargsExpr(valueExpr: string, options: {
|
|
43
|
+
keywordOnlyNames: string[];
|
|
44
|
+
requiredKwOnlyNames: string[];
|
|
45
|
+
hasVarKwArgs: boolean;
|
|
46
|
+
}): string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Emit the runtime prelude lines that build the `__args` array, fold a trailing
|
|
50
|
+
* plain object into `__kwargs`, and unpack `*args`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function emitCallPrelude(desc: CallDescriptor, helpers: CallEmitHelpers): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Emit the argument guard lines that reject positional-only arguments passed as
|
|
55
|
+
* keywords and enforce required keyword-only arguments.
|
|
56
|
+
*/
|
|
57
|
+
export declare function emitArgGuards(desc: CallDescriptor): string[];
|
|
58
|
+
//# sourceMappingURL=emit-call.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emit-call.d.ts","sourceRoot":"","sources":["../../src/core/emit-call.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,0DAA0D;AAC1D,MAAM,WAAW,cAAc;IAC7B,8DAA8D;IAC9D,gBAAgB,EAAE,SAAS,SAAS,EAAE,CAAC;IACvC,qCAAqC;IACrC,YAAY,CAAC,EAAE,SAAS,CAAC;IACzB,+EAA+E;IAC/E,iBAAiB,EAAE,OAAO,CAAC;IAC3B,sDAAsD;IACtD,SAAS,EAAE,OAAO,CAAC;IACnB,8CAA8C;IAC9C,YAAY,EAAE,OAAO,CAAC;IACtB,wCAAwC;IACxC,gBAAgB,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,iDAAiD;IACjD,mBAAmB,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,2CAA2C;IAC3C,mBAAmB,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,wDAAwD;IACxD,gBAAgB,EAAE,MAAM,CAAC;IACzB,4EAA4E;IAC5E,MAAM,EAAE,MAAM,CAAC;IACf,gFAAgF;IAChF,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,yEAAyE;AACzE,MAAM,WAAW,eAAe;IAC9B,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IACvC,yBAAyB,CACvB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE;QACP,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAC3B,mBAAmB,EAAE,MAAM,EAAE,CAAC;QAC9B,YAAY,EAAE,OAAO,CAAC;KACvB,GACA,MAAM,CAAC;CACX;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,eAAe,GAAG,MAAM,EAAE,CAqExF;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM,EAAE,CAqC5D"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared call-emission helpers for the TypeScript wrapper generator.
|
|
3
|
+
*
|
|
4
|
+
* The function, method, and constructor wrappers all emit the same runtime
|
|
5
|
+
* prelude (assemble `__args`, fold a trailing object into `__kwargs`, unpack
|
|
6
|
+
* `*args`) and the same argument guards (reject positional-only-as-keyword,
|
|
7
|
+
* require keyword-only arguments). They differ only by indentation, the error
|
|
8
|
+
* label used in thrown messages, and the terminal RPC verb (which the call
|
|
9
|
+
* sites still emit themselves).
|
|
10
|
+
*
|
|
11
|
+
* The emitted strings MUST stay byte-identical to the previous inline copies.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Emit the runtime prelude lines that build the `__args` array, fold a trailing
|
|
15
|
+
* plain object into `__kwargs`, and unpack `*args`.
|
|
16
|
+
*/
|
|
17
|
+
export function emitCallPrelude(desc, helpers) {
|
|
18
|
+
const { positionalParams, varArgsParam, needsVarArgsArray, hasKwArgs, hasVarKwArgs, keywordOnlyNames, requiredKwOnlyNames, requiredPosCount, indent: i, errorLabel, } = desc;
|
|
19
|
+
const i2 = `${i} `;
|
|
20
|
+
const i3 = `${i2} `;
|
|
21
|
+
const lines = [];
|
|
22
|
+
if (hasKwArgs) {
|
|
23
|
+
lines.push(`${i}let __kwargs = kwargs;`);
|
|
24
|
+
}
|
|
25
|
+
const positionalArgExprs = positionalParams.map(p => helpers.escapeIdentifier(p.name));
|
|
26
|
+
lines.push(`${i}const __args: unknown[] = [${positionalArgExprs.join(', ')}];`);
|
|
27
|
+
if (requiredPosCount < positionalParams.length) {
|
|
28
|
+
lines.push(`${i}while (__args.length > ${requiredPosCount} && __args[__args.length - 1] === undefined) {`);
|
|
29
|
+
lines.push(`${i2}__args.pop();`);
|
|
30
|
+
lines.push(`${i}}`);
|
|
31
|
+
}
|
|
32
|
+
if (hasKwArgs && requiredPosCount < positionalParams.length) {
|
|
33
|
+
const looksLikeKwargs = helpers.renderLooksLikeKwargsExpr('__candidate', {
|
|
34
|
+
keywordOnlyNames: [...keywordOnlyNames],
|
|
35
|
+
requiredKwOnlyNames: [...requiredKwOnlyNames],
|
|
36
|
+
hasVarKwArgs,
|
|
37
|
+
});
|
|
38
|
+
lines.push(`${i}if (__kwargs === undefined && __args.length > ${requiredPosCount}) {`);
|
|
39
|
+
lines.push(`${i2}const __candidate = __args[__args.length - 1];`);
|
|
40
|
+
lines.push(`${i2}if (${looksLikeKwargs}) {`);
|
|
41
|
+
lines.push(`${i3}__kwargs = __candidate as any;`);
|
|
42
|
+
lines.push(`${i3}__args.pop();`);
|
|
43
|
+
lines.push(`${i2}}`);
|
|
44
|
+
lines.push(`${i}}`);
|
|
45
|
+
}
|
|
46
|
+
if (varArgsParam) {
|
|
47
|
+
const vname = helpers.escapeIdentifier(varArgsParam.name);
|
|
48
|
+
if (needsVarArgsArray) {
|
|
49
|
+
const looksLikeKwargs = helpers.renderLooksLikeKwargsExpr(vname, {
|
|
50
|
+
keywordOnlyNames: [...keywordOnlyNames],
|
|
51
|
+
requiredKwOnlyNames: [...requiredKwOnlyNames],
|
|
52
|
+
hasVarKwArgs,
|
|
53
|
+
});
|
|
54
|
+
lines.push(`${i}let __varargs: unknown[] = [];`);
|
|
55
|
+
lines.push(`${i}if (${vname} !== undefined) {`);
|
|
56
|
+
lines.push(`${i2}if (globalThis.Array.isArray(${vname})) {`);
|
|
57
|
+
lines.push(`${i3}__varargs = ${vname};`);
|
|
58
|
+
lines.push(`${i2}} else if (__kwargs === undefined && ${looksLikeKwargs}) {`);
|
|
59
|
+
lines.push(`${i3}__kwargs = ${vname} as any;`);
|
|
60
|
+
lines.push(`${i2}} else {`);
|
|
61
|
+
lines.push(`${i3}throw new Error(\`${errorLabel} expected ${varArgsParam.name} to be an array\`);`);
|
|
62
|
+
lines.push(`${i2}}`);
|
|
63
|
+
lines.push(`${i}}`);
|
|
64
|
+
lines.push(`${i}__args.push(...__varargs);`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
lines.push(`${i}__args.push(...${vname});`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return lines;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Emit the argument guard lines that reject positional-only arguments passed as
|
|
74
|
+
* keywords and enforce required keyword-only arguments.
|
|
75
|
+
*/
|
|
76
|
+
export function emitArgGuards(desc) {
|
|
77
|
+
const { hasKwArgs, positionalOnlyNames, requiredKwOnlyNames, indent: i, errorLabel, } = desc;
|
|
78
|
+
const i2 = `${i} `;
|
|
79
|
+
const i3 = `${i2} `;
|
|
80
|
+
const lines = [];
|
|
81
|
+
if (hasKwArgs && positionalOnlyNames.length > 0) {
|
|
82
|
+
lines.push(`${i}const __positionalOnly = ${JSON.stringify(positionalOnlyNames)} as const;`);
|
|
83
|
+
lines.push(`${i}for (const key of __positionalOnly) {`);
|
|
84
|
+
lines.push(`${i2}if (__kwargs && Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
85
|
+
lines.push(`${i3}throw new Error(\`${errorLabel} does not accept positional-only argument "\${key}" as a keyword argument\`);`);
|
|
86
|
+
lines.push(`${i2}}`);
|
|
87
|
+
lines.push(`${i}}`);
|
|
88
|
+
}
|
|
89
|
+
if (hasKwArgs && requiredKwOnlyNames.length > 0) {
|
|
90
|
+
lines.push(`${i}const __requiredKwOnly = ${JSON.stringify(requiredKwOnlyNames)} as const;`);
|
|
91
|
+
lines.push(`${i}const __missing: string[] = [];`);
|
|
92
|
+
lines.push(`${i}for (const key of __requiredKwOnly) {`);
|
|
93
|
+
lines.push(`${i2}if (!__kwargs || !Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
94
|
+
lines.push(`${i3}__missing.push(key);`);
|
|
95
|
+
lines.push(`${i2}}`);
|
|
96
|
+
lines.push(`${i}}`);
|
|
97
|
+
lines.push(`${i}if (__missing.length > 0) {`);
|
|
98
|
+
lines.push(`${i2}throw new Error(\`Missing required keyword-only arguments for ${errorLabel}: \${__missing.join(', ')}\`);`);
|
|
99
|
+
lines.push(`${i}}`);
|
|
100
|
+
}
|
|
101
|
+
return lines;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=emit-call.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emit-call.js","sourceRoot":"","sources":["../../src/core/emit-call.ts"],"names":[],"mappings":"AAAA;;;;;;;;;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AAG,EAAE,GAAG,CAAC,CAAC;QACrB,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACtB,CAAC;IACD,IAAI,SAAS,IAAI,mBAAmB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAChD,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,4BAA4B,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,YAAY,CAAC,CAAC;QAC5F,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,iCAAiC,CAAC,CAAC;QAClD,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAC;QACxD,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,0EAA0E,CAAC,CAAC;QAC5F,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,sBAAsB,CAAC,CAAC;QACxC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACrB,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACpB,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;QAC9C,KAAK,CAAC,IAAI,CACR,GAAG,EAAE,iEAAiE,UAAU,gCAAgC,CACjH,CAAC;QACF,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/core/generator.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ export declare class CodeGenerator {
|
|
|
26
26
|
private sanitizeType;
|
|
27
27
|
private typeToTsFromPython;
|
|
28
28
|
private renderLooksLikeKwargsExpr;
|
|
29
|
+
/** Helpers passed to the shared call-emission module (they depend on `this`). */
|
|
30
|
+
private callEmitHelpers;
|
|
29
31
|
generateFunctionWrapper(func: PythonFunction, moduleName?: string, annotatedJSDoc?: boolean): GeneratedCode;
|
|
30
32
|
generateClassWrapper(cls: PythonClass, moduleName?: string, _annotatedJSDoc?: boolean): GeneratedCode;
|
|
31
33
|
generateTypeAlias(alias: PythonTypeAlias, moduleName?: string): GeneratedCode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/core/generator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAEV,cAAc,EACd,WAAW,EACX,YAAY,EAEZ,eAAe,EACf,aAAa,EAEd,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/core/generator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAEV,cAAc,EACd,WAAW,EACX,YAAY,EAEZ,eAAe,EACf,aAAa,EAEd,MAAM,mBAAmB,CAAC;AAS3B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAezC,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IACpC,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CA2BnC;gBAES,MAAM,GAAE,UAA6B;IAIjD;;OAEG;IACH,OAAO,CAAC,gBAAgB;IA2BxB;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAqBxB,OAAO,CAAC,iBAAiB;IAMzB,OAAO,CAAC,yBAAyB;IAyCjC,OAAO,CAAC,0BAA0B;IAalC,OAAO,CAAC,yBAAyB;IAuCjC,OAAO,CAAC,YAAY;IAyEpB,OAAO,CAAC,kBAAkB;IAQ1B,OAAO,CAAC,yBAAyB;IA+BjC,iFAAiF;IACjF,OAAO,CAAC,eAAe;IAcvB,uBAAuB,CACrB,IAAI,EAAE,cAAc,EACpB,UAAU,CAAC,EAAE,MAAM,EACnB,cAAc,UAAQ,GACrB,aAAa;IA6LhB,oBAAoB,CAClB,GAAG,EAAE,WAAW,EAChB,UAAU,CAAC,EAAE,MAAM,EACnB,eAAe,UAAQ,GACtB,aAAa;IAqXhB,iBAAiB,CAAC,KAAK,EAAE,eAAe,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,aAAa;IAY7E;;OAEG;IACG,cAAc,CAClB,MAAM,EAAE,YAAY,EACpB,OAAO,GAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,CAAC;QAAC,cAAc,CAAC,EAAE,OAAO,CAAA;KAE3E,GACA,OAAO,CAAC,aAAa,CAAC;IAiBzB,wBAAwB,CAAC,MAAM,EAAE,YAAY,EAAE,cAAc,UAAQ,GAAG,aAAa;IAiCrF,OAAO,CAAC,aAAa;IAiBrB,OAAO,CAAC,IAAI;IAcZ,OAAO,CAAC,QAAQ;CAgEjB"}
|
package/dist/core/generator.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* CodeGenerator - TypeScript wrapper generation
|
|
3
3
|
*/
|
|
4
4
|
import { globalCache } from '../utils/cache.js';
|
|
5
|
+
import { emitArgGuards, emitCallPrelude, } from './emit-call.js';
|
|
5
6
|
import { TypeMapper } from './mapper.js';
|
|
6
7
|
export class CodeGenerator {
|
|
7
8
|
mapper;
|
|
@@ -262,6 +263,13 @@ export class CodeGenerator {
|
|
|
262
263
|
})();
|
|
263
264
|
return `(${base} && (${keyCheck}))`;
|
|
264
265
|
}
|
|
266
|
+
/** Helpers passed to the shared call-emission module (they depend on `this`). */
|
|
267
|
+
callEmitHelpers() {
|
|
268
|
+
return {
|
|
269
|
+
escapeIdentifier: (name) => this.escapeIdentifier(name),
|
|
270
|
+
renderLooksLikeKwargsExpr: (valueExpr, options) => this.renderLooksLikeKwargsExpr(valueExpr, options),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
265
273
|
generateFunctionWrapper(func, moduleName, annotatedJSDoc = false) {
|
|
266
274
|
const jsdoc = this.generateJsDoc(func.docstring, annotatedJSDoc ? func.parameters.map(p => String(p.type)) : undefined);
|
|
267
275
|
const filteredParams = func.parameters.filter(p => p.name !== 'self' && p.name !== 'cls');
|
|
@@ -386,79 +394,24 @@ export class CodeGenerator {
|
|
|
386
394
|
}
|
|
387
395
|
}
|
|
388
396
|
const overloadDecl = overloads.length > 0 ? `${overloads.join('\n')}\n` : '';
|
|
389
|
-
const guardLines = [];
|
|
390
|
-
if (hasKwArgs && positionalOnlyNames.length > 0) {
|
|
391
|
-
guardLines.push(` const __positionalOnly = ${JSON.stringify(positionalOnlyNames)} as const;`);
|
|
392
|
-
guardLines.push(` for (const key of __positionalOnly) {`);
|
|
393
|
-
guardLines.push(` if (__kwargs && Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
394
|
-
guardLines.push(` throw new Error(\`${func.name} does not accept positional-only argument "\${key}" as a keyword argument\`);`);
|
|
395
|
-
guardLines.push(` }`);
|
|
396
|
-
guardLines.push(` }`);
|
|
397
|
-
}
|
|
398
|
-
if (hasKwArgs && requiredKwOnlyNames.length > 0) {
|
|
399
|
-
guardLines.push(` const __requiredKwOnly = ${JSON.stringify(requiredKwOnlyNames)} as const;`);
|
|
400
|
-
guardLines.push(` const __missing: string[] = [];`);
|
|
401
|
-
guardLines.push(` for (const key of __requiredKwOnly) {`);
|
|
402
|
-
guardLines.push(` if (!__kwargs || !Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
403
|
-
guardLines.push(` __missing.push(key);`);
|
|
404
|
-
guardLines.push(` }`);
|
|
405
|
-
guardLines.push(` }`);
|
|
406
|
-
guardLines.push(` if (__missing.length > 0) {`);
|
|
407
|
-
guardLines.push(` throw new Error(\`Missing required keyword-only arguments for ${func.name}: \${__missing.join(', ')}\`);`);
|
|
408
|
-
guardLines.push(` }`);
|
|
409
|
-
}
|
|
410
|
-
const guards = guardLines.length > 0 ? `${guardLines.join('\n')}\n` : '';
|
|
411
397
|
const firstOptionalPosIndex = positionalParams.findIndex(p => p.optional);
|
|
412
398
|
const requiredPosCount = firstOptionalPosIndex >= 0 ? firstOptionalPosIndex : positionalParams.length;
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
});
|
|
430
|
-
callPreludeLines.push(` if (__kwargs === undefined && __args.length > ${requiredPosCount}) {`);
|
|
431
|
-
callPreludeLines.push(` const __candidate = __args[__args.length - 1];`);
|
|
432
|
-
callPreludeLines.push(` if (${looksLikeKwargs}) {`);
|
|
433
|
-
callPreludeLines.push(` __kwargs = __candidate as any;`);
|
|
434
|
-
callPreludeLines.push(` __args.pop();`);
|
|
435
|
-
callPreludeLines.push(` }`);
|
|
436
|
-
callPreludeLines.push(` }`);
|
|
437
|
-
}
|
|
438
|
-
if (varArgsParam) {
|
|
439
|
-
const vname = this.escapeIdentifier(varArgsParam.name);
|
|
440
|
-
if (needsVarArgsArray) {
|
|
441
|
-
const looksLikeKwargs = this.renderLooksLikeKwargsExpr(vname, {
|
|
442
|
-
keywordOnlyNames,
|
|
443
|
-
requiredKwOnlyNames,
|
|
444
|
-
hasVarKwArgs,
|
|
445
|
-
});
|
|
446
|
-
callPreludeLines.push(` let __varargs: unknown[] = [];`);
|
|
447
|
-
callPreludeLines.push(` if (${vname} !== undefined) {`);
|
|
448
|
-
callPreludeLines.push(` if (globalThis.Array.isArray(${vname})) {`);
|
|
449
|
-
callPreludeLines.push(` __varargs = ${vname};`);
|
|
450
|
-
callPreludeLines.push(` } else if (__kwargs === undefined && ${looksLikeKwargs}) {`);
|
|
451
|
-
callPreludeLines.push(` __kwargs = ${vname} as any;`);
|
|
452
|
-
callPreludeLines.push(` } else {`);
|
|
453
|
-
callPreludeLines.push(` throw new Error(\`${func.name} expected ${varArgsParam.name} to be an array\`);`);
|
|
454
|
-
callPreludeLines.push(` }`);
|
|
455
|
-
callPreludeLines.push(` }`);
|
|
456
|
-
callPreludeLines.push(` __args.push(...__varargs);`);
|
|
457
|
-
}
|
|
458
|
-
else {
|
|
459
|
-
callPreludeLines.push(` __args.push(...${vname});`);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
399
|
+
const callDescriptor = {
|
|
400
|
+
positionalParams,
|
|
401
|
+
varArgsParam,
|
|
402
|
+
needsVarArgsArray,
|
|
403
|
+
hasKwArgs,
|
|
404
|
+
hasVarKwArgs,
|
|
405
|
+
keywordOnlyNames,
|
|
406
|
+
requiredKwOnlyNames,
|
|
407
|
+
positionalOnlyNames,
|
|
408
|
+
requiredPosCount,
|
|
409
|
+
indent: ' ',
|
|
410
|
+
errorLabel: func.name,
|
|
411
|
+
};
|
|
412
|
+
const guardLines = emitArgGuards(callDescriptor);
|
|
413
|
+
const guards = guardLines.length > 0 ? `${guardLines.join('\n')}\n` : '';
|
|
414
|
+
const callPreludeLines = emitCallPrelude(callDescriptor, this.callEmitHelpers());
|
|
462
415
|
const callPrelude = callPreludeLines.length > 0 ? `${callPreludeLines.join('\n')}\n` : '';
|
|
463
416
|
const ts = `${jsdoc}${overloadDecl}export async function ${fname}${typeParamDecl}(${paramDecl}): Promise<${returnType}> {
|
|
464
417
|
${callPrelude}${guards} return getRuntimeBridge().call('${moduleId}', '${func.name}', __args${hasKwArgs ? ', __kwargs' : ''});
|
|
@@ -605,77 +558,22 @@ ${callPrelude}${guards} return getRuntimeBridge().call('${moduleId}', '${func.n
|
|
|
605
558
|
}
|
|
606
559
|
}
|
|
607
560
|
const overloadDecl = overloads.length > 0 ? `${overloads.join('\n')}\n` : '';
|
|
608
|
-
const
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
requiredKwOnlyNames,
|
|
623
|
-
hasVarKwArgs,
|
|
624
|
-
});
|
|
625
|
-
callPreludeLines.push(` if (__kwargs === undefined && __args.length > ${requiredPosCount}) {`);
|
|
626
|
-
callPreludeLines.push(` const __candidate = __args[__args.length - 1];`);
|
|
627
|
-
callPreludeLines.push(` if (${looksLikeKwargs}) {`);
|
|
628
|
-
callPreludeLines.push(` __kwargs = __candidate as any;`);
|
|
629
|
-
callPreludeLines.push(` __args.pop();`);
|
|
630
|
-
callPreludeLines.push(` }`);
|
|
631
|
-
callPreludeLines.push(` }`);
|
|
632
|
-
}
|
|
633
|
-
if (varArgsParam) {
|
|
634
|
-
const vname = this.escapeIdentifier(varArgsParam.name);
|
|
635
|
-
if (needsVarArgsArray) {
|
|
636
|
-
const looksLikeKwargs = this.renderLooksLikeKwargsExpr(vname, {
|
|
637
|
-
keywordOnlyNames,
|
|
638
|
-
requiredKwOnlyNames,
|
|
639
|
-
hasVarKwArgs,
|
|
640
|
-
});
|
|
641
|
-
callPreludeLines.push(` let __varargs: unknown[] = [];`);
|
|
642
|
-
callPreludeLines.push(` if (${vname} !== undefined) {`);
|
|
643
|
-
callPreludeLines.push(` if (globalThis.Array.isArray(${vname})) {`);
|
|
644
|
-
callPreludeLines.push(` __varargs = ${vname};`);
|
|
645
|
-
callPreludeLines.push(` } else if (__kwargs === undefined && ${looksLikeKwargs}) {`);
|
|
646
|
-
callPreludeLines.push(` __kwargs = ${vname} as any;`);
|
|
647
|
-
callPreludeLines.push(` } else {`);
|
|
648
|
-
callPreludeLines.push(` throw new Error(\`${method.name} expected ${varArgsParam.name} to be an array\`);`);
|
|
649
|
-
callPreludeLines.push(` }`);
|
|
650
|
-
callPreludeLines.push(` }`);
|
|
651
|
-
callPreludeLines.push(` __args.push(...__varargs);`);
|
|
652
|
-
}
|
|
653
|
-
else {
|
|
654
|
-
callPreludeLines.push(` __args.push(...${vname});`);
|
|
655
|
-
}
|
|
656
|
-
}
|
|
561
|
+
const callDescriptor = {
|
|
562
|
+
positionalParams,
|
|
563
|
+
varArgsParam,
|
|
564
|
+
needsVarArgsArray,
|
|
565
|
+
hasKwArgs: needsKwargsParam,
|
|
566
|
+
hasVarKwArgs,
|
|
567
|
+
keywordOnlyNames,
|
|
568
|
+
requiredKwOnlyNames,
|
|
569
|
+
positionalOnlyNames,
|
|
570
|
+
requiredPosCount,
|
|
571
|
+
indent: ' ',
|
|
572
|
+
errorLabel: method.name,
|
|
573
|
+
};
|
|
574
|
+
const callPreludeLines = emitCallPrelude(callDescriptor, this.callEmitHelpers());
|
|
657
575
|
const callPrelude = callPreludeLines.length > 0 ? `${callPreludeLines.join('\n')}\n` : '';
|
|
658
|
-
const guardLines =
|
|
659
|
-
if (needsKwargsParam && positionalOnlyNames.length > 0) {
|
|
660
|
-
guardLines.push(` const __positionalOnly = ${JSON.stringify(positionalOnlyNames)} as const;`);
|
|
661
|
-
guardLines.push(` for (const key of __positionalOnly) {`);
|
|
662
|
-
guardLines.push(` if (__kwargs && Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
663
|
-
guardLines.push(` throw new Error(\`${method.name} does not accept positional-only argument "\${key}" as a keyword argument\`);`);
|
|
664
|
-
guardLines.push(` }`);
|
|
665
|
-
guardLines.push(` }`);
|
|
666
|
-
}
|
|
667
|
-
if (needsKwargsParam && requiredKwOnlyNames.length > 0) {
|
|
668
|
-
guardLines.push(` const __requiredKwOnly = ${JSON.stringify(requiredKwOnlyNames)} as const;`);
|
|
669
|
-
guardLines.push(` const __missing: string[] = [];`);
|
|
670
|
-
guardLines.push(` for (const key of __requiredKwOnly) {`);
|
|
671
|
-
guardLines.push(` if (!__kwargs || !Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
672
|
-
guardLines.push(` __missing.push(key);`);
|
|
673
|
-
guardLines.push(` }`);
|
|
674
|
-
guardLines.push(` }`);
|
|
675
|
-
guardLines.push(` if (__missing.length > 0) {`);
|
|
676
|
-
guardLines.push(` throw new Error(\`Missing required keyword-only arguments for ${method.name}: \${__missing.join(', ')}\`);`);
|
|
677
|
-
guardLines.push(` }`);
|
|
678
|
-
}
|
|
576
|
+
const guardLines = emitArgGuards(callDescriptor);
|
|
679
577
|
const guards = guardLines.length > 0 ? `${guardLines.join('\n')}\n` : '';
|
|
680
578
|
methodBodies.push(`${overloadDecl} async ${mname}${methodTypeParamDecl}(${paramsDecl}): Promise<${returnType}> {
|
|
681
579
|
${callPrelude}${guards} return getRuntimeBridge().callMethod(this.__handle, '${method.name}', __args${needsKwargsParam ? ', __kwargs' : ''});
|
|
@@ -758,77 +656,22 @@ ${callPrelude}${guards} return getRuntimeBridge().callMethod(this.__handle, '
|
|
|
758
656
|
const declaration = overloads.length > 0
|
|
759
657
|
? overloadDecl
|
|
760
658
|
: ` static create${classTypeParamDecl}(${paramsDecl}): Promise<${classSelfType}>;\n`;
|
|
761
|
-
const
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
keywordOnlyNames,
|
|
776
|
-
requiredKwOnlyNames,
|
|
777
|
-
hasVarKwArgs,
|
|
778
|
-
});
|
|
779
|
-
callPreludeLines.push(` if (__kwargs === undefined && __args.length > ${requiredPosCount}) {`);
|
|
780
|
-
callPreludeLines.push(` const __candidate = __args[__args.length - 1];`);
|
|
781
|
-
callPreludeLines.push(` if (${looksLikeKwargs}) {`);
|
|
782
|
-
callPreludeLines.push(` __kwargs = __candidate as any;`);
|
|
783
|
-
callPreludeLines.push(` __args.pop();`);
|
|
784
|
-
callPreludeLines.push(` }`);
|
|
785
|
-
callPreludeLines.push(` }`);
|
|
786
|
-
}
|
|
787
|
-
if (varArgsParam) {
|
|
788
|
-
const vname = this.escapeIdentifier(varArgsParam.name);
|
|
789
|
-
if (needsVarArgsArray) {
|
|
790
|
-
const looksLikeKwargs = this.renderLooksLikeKwargsExpr(vname, {
|
|
791
|
-
keywordOnlyNames,
|
|
792
|
-
requiredKwOnlyNames,
|
|
793
|
-
hasVarKwArgs,
|
|
794
|
-
});
|
|
795
|
-
callPreludeLines.push(` let __varargs: unknown[] = [];`);
|
|
796
|
-
callPreludeLines.push(` if (${vname} !== undefined) {`);
|
|
797
|
-
callPreludeLines.push(` if (globalThis.Array.isArray(${vname})) {`);
|
|
798
|
-
callPreludeLines.push(` __varargs = ${vname};`);
|
|
799
|
-
callPreludeLines.push(` } else if (__kwargs === undefined && ${looksLikeKwargs}) {`);
|
|
800
|
-
callPreludeLines.push(` __kwargs = ${vname} as any;`);
|
|
801
|
-
callPreludeLines.push(` } else {`);
|
|
802
|
-
callPreludeLines.push(` throw new Error(\`__init__ expected ${varArgsParam.name} to be an array\`);`);
|
|
803
|
-
callPreludeLines.push(` }`);
|
|
804
|
-
callPreludeLines.push(` }`);
|
|
805
|
-
callPreludeLines.push(` __args.push(...__varargs);`);
|
|
806
|
-
}
|
|
807
|
-
else {
|
|
808
|
-
callPreludeLines.push(` __args.push(...${vname});`);
|
|
809
|
-
}
|
|
810
|
-
}
|
|
659
|
+
const callDescriptor = {
|
|
660
|
+
positionalParams,
|
|
661
|
+
varArgsParam,
|
|
662
|
+
needsVarArgsArray,
|
|
663
|
+
hasKwArgs: needsKwargsParam,
|
|
664
|
+
hasVarKwArgs,
|
|
665
|
+
keywordOnlyNames,
|
|
666
|
+
requiredKwOnlyNames,
|
|
667
|
+
positionalOnlyNames,
|
|
668
|
+
requiredPosCount,
|
|
669
|
+
indent: ' ',
|
|
670
|
+
errorLabel: '__init__',
|
|
671
|
+
};
|
|
672
|
+
const callPreludeLines = emitCallPrelude(callDescriptor, this.callEmitHelpers());
|
|
811
673
|
const callPrelude = callPreludeLines.length > 0 ? `${callPreludeLines.join('\n')}\n` : '';
|
|
812
|
-
|
|
813
|
-
guardLines.push(` const __positionalOnly = ${JSON.stringify(positionalOnlyNames)} as const;`);
|
|
814
|
-
guardLines.push(` for (const key of __positionalOnly) {`);
|
|
815
|
-
guardLines.push(` if (__kwargs && Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
816
|
-
guardLines.push(` throw new Error(\`__init__ does not accept positional-only argument "\${key}" as a keyword argument\`);`);
|
|
817
|
-
guardLines.push(` }`);
|
|
818
|
-
guardLines.push(` }`);
|
|
819
|
-
}
|
|
820
|
-
if (needsKwargsParam && requiredKwOnlyNames.length > 0) {
|
|
821
|
-
guardLines.push(` const __requiredKwOnly = ${JSON.stringify(requiredKwOnlyNames)} as const;`);
|
|
822
|
-
guardLines.push(` const __missing: string[] = [];`);
|
|
823
|
-
guardLines.push(` for (const key of __requiredKwOnly) {`);
|
|
824
|
-
guardLines.push(` if (!__kwargs || !Object.prototype.hasOwnProperty.call(__kwargs, key)) {`);
|
|
825
|
-
guardLines.push(` __missing.push(key);`);
|
|
826
|
-
guardLines.push(` }`);
|
|
827
|
-
guardLines.push(` }`);
|
|
828
|
-
guardLines.push(` if (__missing.length > 0) {`);
|
|
829
|
-
guardLines.push(` throw new Error(\`Missing required keyword-only arguments for __init__: \${__missing.join(', ')}\`);`);
|
|
830
|
-
guardLines.push(` }`);
|
|
831
|
-
}
|
|
674
|
+
const guardLines = emitArgGuards(callDescriptor);
|
|
832
675
|
return {
|
|
833
676
|
overloadDecl,
|
|
834
677
|
declaration,
|