gofront 1.6.1 → 1.7.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.
@@ -184,8 +184,7 @@ export const statementGenMethods = {
184
184
  this.line(`const ${tmp} = ${rhs[0]};`);
185
185
  for (let i = 0; i < lhs.length; i++) {
186
186
  if (lhs[i] === "_") continue;
187
- const prefix = redecls[i] ? "" : "let ";
188
- this.line(`${prefix}${lhs[i]} = ${tmp}[${i}];`);
187
+ this._genDefineSingleVar([lhs[i]], [`${tmp}[${i}]`], [redecls[i]]);
189
188
  }
190
189
  } else {
191
190
  for (let i = 0; i < lhs.length; i++) {
@@ -193,12 +192,20 @@ export const statementGenMethods = {
193
192
  this.line(`${rhs[i]};`);
194
193
  continue;
195
194
  }
196
- const prefix = redecls[i] ? "" : "let ";
197
- this.line(`${prefix}${lhs[i]} = ${rhs[i]};`);
195
+ this._genDefineSingleVar([lhs[i]], [rhs[i]], [redecls[i]]);
198
196
  }
199
197
  }
200
198
  },
201
199
 
200
+ // Address-taken variables introduced by a destructuring declaration are
201
+ // re-bound to their { value } box.
202
+ _boxDeclaredNames(names) {
203
+ for (const n of names) {
204
+ if (n && n !== "_" && this._boxedVars.has(n))
205
+ this.line(`${n} = { value: ${n} };`);
206
+ }
207
+ },
208
+
202
209
  _genDefineStmt(stmt) {
203
210
  const rhsNode = stmt.rhs[0];
204
211
  if (this._isCommaOkMapExpr(stmt, rhsNode)) {
@@ -221,6 +228,7 @@ export const statementGenMethods = {
221
228
  const rhsStr = rhs.length === 1 ? rhs[0] : `[${rhs.join(", ")}]`;
222
229
  const names = lhs.map((n) => (n === "_" ? "" : n));
223
230
  this.line(`let [${names.join(", ")}] = ${rhsStr};`);
231
+ this._boxDeclaredNames(names);
224
232
  } else {
225
233
  this._genDefineMixedRedecl(lhs, rhs, redecls);
226
234
  }
@@ -863,14 +871,29 @@ export const statementGenMethods = {
863
871
  }
864
872
  },
865
873
 
874
+ // Interface-typed tags may hold boxed named values; cases compare with __ifeq.
875
+ _switchTagAndCases(stmt) {
876
+ const tag = stmt.tag ? this.genExpr(stmt.tag) : "true";
877
+ if (!stmt.tag || !this._isIfaceType(stmt.tag._type))
878
+ return { tag, caseExpr: (e) => this.genExpr(e) };
879
+ this.useHelper("ifeq");
880
+ this._swCounter = (this._swCounter ?? 0) + 1;
881
+ const tmp = `__sw${this._swCounter}`;
882
+ this.line(`const ${tmp} = ${tag};`);
883
+ return {
884
+ tag: "true",
885
+ caseExpr: (e) => `__ifeq(${tmp}, ${this._genIfaceOperand(e)})`,
886
+ };
887
+ },
888
+
866
889
  genSwitch(stmt) {
867
890
  const genBody = () => {
868
- const tag = stmt.tag ? this.genExpr(stmt.tag) : "true";
891
+ const { tag, caseExpr } = this._switchTagAndCases(stmt);
869
892
  this.line(`switch (${tag}) {`);
870
893
  this.indented(() => {
871
894
  for (const c of stmt.cases) {
872
895
  if (c.list) {
873
- for (const e of c.list) this.line(`case ${this.genExpr(e)}:`);
896
+ for (const e of c.list) this.line(`case ${caseExpr(e)}:`);
874
897
  } else {
875
898
  this.line("default:");
876
899
  }
@@ -926,6 +949,13 @@ export const statementGenMethods = {
926
949
  .join(" || ");
927
950
  this.line(`${first ? "if" : "else if"} (${cond}) {`);
928
951
  this.indented(() => {
952
+ // A single named non-struct case binds the unboxed value.
953
+ if (
954
+ stmt.assign &&
955
+ c.types.length === 1 &&
956
+ this._isNamedBoxTypeNode(c.types[0])
957
+ )
958
+ this.line(`const ${stmt.assign} = __tsw.__gfUnbox();`);
929
959
  for (const s of c.stmts) this.genStmt(s);
930
960
  });
931
961
  this.line("}");
@@ -24,6 +24,11 @@ export const errorsMethods = {
24
24
  if (concreteGoType?.kind === "interface" || typeName === "error") {
25
25
  return `__errorAs(${args[0]}, ${args[1]}, (x) => x !== null && (typeof x === "object" ? typeof x.Error === "function" : true))`;
26
26
  }
27
+ // Named non-struct error types sit in interfaces as boxes; store the raw value.
28
+ const namedBox = this._namedBoxInfo(concreteGoType);
29
+ if (namedBox) {
30
+ return `__errorAs(${args[0]}, ${args[1]}, (x) => x instanceof ${namedBox.cls}, (x) => x.__gfUnbox())`;
31
+ }
27
32
  if (typeName) {
28
33
  return `__errorAs(${args[0]}, ${args[1]}, (x) => (typeof ${typeName} === "function" && x instanceof ${typeName}) || x?.constructor?.name === "${typeName}" || x?.__struct === "${typeName}")`;
29
34
  }
@@ -12,6 +12,7 @@ import { gomMethods } from "./gom.js";
12
12
  import { hexMethods } from "./hex.js";
13
13
  import { htmlMethods } from "./html.js";
14
14
  import { ioMethods } from "./io.js";
15
+ import { jsonMethods } from "./json.js";
15
16
  import { mapsMethods } from "./maps.js";
16
17
  import { mathMethods } from "./math.js";
17
18
  import { osMethods } from "./os.js";
@@ -20,6 +21,7 @@ import { randMethods } from "./rand.js";
20
21
  import { regexpMethods } from "./regexp.js";
21
22
  import { sha256Methods } from "./sha256.js";
22
23
  import { sharedMethods } from "./shared.js";
24
+ import { simdMethods } from "./simd.js";
23
25
  import { slicesMethods } from "./slices.js";
24
26
  import { sortMethods } from "./sort.js";
25
27
  import { strconvMethods } from "./strconv.js";
@@ -62,6 +64,8 @@ const STDLIB_METHOD_MAP = {
62
64
  binary: "_genBinary",
63
65
  hex: "_genHex",
64
66
  base64: "_genBase64",
67
+ json: "_genJson",
68
+ simd: "_genSimd",
65
69
  };
66
70
 
67
71
  const dispatchMethods = {
@@ -82,6 +86,12 @@ const dispatchMethods = {
82
86
  // Dispatch table for stdlib namespace calls (pkg.Func → inline JS).
83
87
  // Returns the generated JS string, or undefined if not handled.
84
88
  _genStdlibCall(ns, fn, expr) {
89
+ // Stdlib shims take raw values; only fmt and errors use the interface box
90
+ // of a named value (Error() dispatch, error comparisons).
91
+ const isNamespace = expr.func?.expr?._type?.kind === "namespace";
92
+ if (isNamespace && ns !== "fmt" && ns !== "errors") {
93
+ for (const e of expr.args) if (e) e._noNamedBox = true;
94
+ }
85
95
  const a = () => expr.args.map((e) => this.genExpr(e));
86
96
  if (ns === "gom") return this._genGom(fn, expr);
87
97
  const method = STDLIB_METHOD_MAP[ns];
@@ -106,6 +116,7 @@ export const stdlibGenMethods = {
106
116
  ...randMethods,
107
117
  ...regexpMethods,
108
118
  ...sharedMethods,
119
+ ...simdMethods,
109
120
  ...slicesMethods,
110
121
  ...sortMethods,
111
122
  ...strconvMethods,
@@ -119,5 +130,6 @@ export const stdlibGenMethods = {
119
130
  ...binaryMethods,
120
131
  ...hexMethods,
121
132
  ...base64Methods,
133
+ ...jsonMethods,
122
134
  ...dispatchMethods,
123
135
  };