wasm-idle 1.1.0 → 1.2.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.
Files changed (197) hide show
  1. package/README.md +267 -112
  2. package/dist/container/bash-profile.d.ts +80 -0
  3. package/dist/container/bash-profile.js +328 -0
  4. package/dist/container/binding.d.ts +39 -0
  5. package/dist/container/binding.js +331 -0
  6. package/dist/container/clang-runtime.d.ts +42 -0
  7. package/dist/container/clang-runtime.js +561 -0
  8. package/dist/container/client.d.ts +14 -0
  9. package/dist/container/client.js +185 -0
  10. package/dist/container/explorer.d.ts +31 -0
  11. package/dist/container/explorer.js +112 -0
  12. package/dist/container/index.d.ts +11 -0
  13. package/dist/container/index.js +11 -0
  14. package/dist/container/owner-worker.d.ts +19 -0
  15. package/dist/container/owner-worker.js +464 -0
  16. package/dist/container/protocol.d.ts +84 -0
  17. package/dist/container/protocol.js +28 -0
  18. package/dist/container/runtime-adapter.d.ts +75 -0
  19. package/dist/container/runtime-adapter.js +537 -0
  20. package/dist/container/shell-control.d.ts +49 -0
  21. package/dist/container/shell-control.js +360 -0
  22. package/dist/container/workspace.d.ts +12 -0
  23. package/dist/container/workspace.js +29 -0
  24. package/dist/index.d.ts +1 -1
  25. package/dist/playground/assetBridge.js +111 -33
  26. package/dist/playground/assets.d.ts +10 -2
  27. package/dist/playground/c3.js +22 -0
  28. package/dist/playground/c3Memory.d.ts +19 -0
  29. package/dist/playground/c3Memory.js +117 -0
  30. package/dist/playground/cachedClang.js +143 -34
  31. package/dist/playground/clang.d.ts +5 -1
  32. package/dist/playground/clang.js +61 -5
  33. package/dist/playground/clangAssetIntegrity.js +6 -6
  34. package/dist/playground/clangWorkerProtocol.d.ts +12 -1
  35. package/dist/playground/cobol.d.ts +3 -0
  36. package/dist/playground/cobol.js +10 -1
  37. package/dist/playground/commonlispAssets.d.ts +2 -2
  38. package/dist/playground/compiledArtifactCache.d.ts +3 -1
  39. package/dist/playground/compiledArtifactCache.js +31 -5
  40. package/dist/playground/compiledLldb.d.ts +41 -0
  41. package/dist/playground/compiledLldb.js +167 -0
  42. package/dist/playground/d.d.ts +10 -2
  43. package/dist/playground/d.js +161 -14
  44. package/dist/playground/debugDeadline.d.ts +12 -0
  45. package/dist/playground/debugDeadline.js +34 -0
  46. package/dist/playground/debugMailbox.d.ts +12 -0
  47. package/dist/playground/debugMailbox.js +77 -0
  48. package/dist/playground/dotnet.js +6 -8
  49. package/dist/playground/fortran.d.ts +3 -0
  50. package/dist/playground/fortran.js +10 -1
  51. package/dist/playground/go.js +23 -7
  52. package/dist/playground/goDebugInstrumentation.js +207 -97
  53. package/dist/playground/java-debug-FMHDKUN6.js +124 -0
  54. package/dist/playground/java-debug-notices.txt +492 -0
  55. package/dist/playground/java.d.ts +17 -1
  56. package/dist/playground/java.js +162 -5
  57. package/dist/playground/javaDebug.d.ts +62 -0
  58. package/dist/playground/javaDebug.js +305 -0
  59. package/dist/playground/javaDebugBridge.d.ts +6 -0
  60. package/dist/playground/javaDebugBridge.js +250 -0
  61. package/dist/playground/javaDebugSource.d.ts +9 -0
  62. package/dist/playground/javaDebugSource.js +4 -0
  63. package/dist/playground/javaRuntimeErrors.d.ts +9 -0
  64. package/dist/playground/javaRuntimeErrors.js +93 -0
  65. package/dist/playground/lean4Assets.d.ts +1 -25
  66. package/dist/playground/lean4Assets.js +2 -123
  67. package/dist/playground/lfortran.js +10 -0
  68. package/dist/playground/lfortranAssets.d.ts +2 -2
  69. package/dist/playground/lldbSession.d.ts +23 -2
  70. package/dist/playground/lldbSession.js +278 -58
  71. package/dist/playground/lldbSourcePath.d.ts +2 -0
  72. package/dist/playground/lldbSourcePath.js +9 -0
  73. package/dist/playground/lua.d.ts +20 -1
  74. package/dist/playground/lua.js +190 -8
  75. package/dist/playground/luaDebug.d.ts +1 -0
  76. package/dist/playground/luaDebug.js +1 -0
  77. package/dist/playground/nim.d.ts +1 -0
  78. package/dist/playground/nim.js +30 -0
  79. package/dist/playground/objectivec.d.ts +10 -6
  80. package/dist/playground/objectivec.js +85 -10
  81. package/dist/playground/options.d.ts +12 -4
  82. package/dist/playground/python.d.ts +8 -1
  83. package/dist/playground/python.js +89 -6
  84. package/dist/playground/ruby.d.ts +10 -1
  85. package/dist/playground/ruby.js +66 -1
  86. package/dist/playground/rubyDebug.d.ts +21 -0
  87. package/dist/playground/rubyDebug.js +128 -0
  88. package/dist/playground/runtimeAssetCache.d.ts +1 -32
  89. package/dist/playground/runtimeAssetCache.js +1 -193
  90. package/dist/playground/runtimeSession.d.ts +2 -2
  91. package/dist/playground/runtimeSession.js +11 -11
  92. package/dist/playground/rust.d.ts +10 -0
  93. package/dist/playground/rust.js +230 -51
  94. package/dist/playground/sandbox.d.ts +8 -1
  95. package/dist/playground/staticStdinRing.d.ts +2 -1
  96. package/dist/playground/staticStdinRing.js +21 -10
  97. package/dist/playground/staticWorkerRuntime.d.ts +15 -1
  98. package/dist/playground/staticWorkerRuntime.js +127 -19
  99. package/dist/playground/stdinBuffer.d.ts +1 -1
  100. package/dist/playground/stdinBuffer.js +19 -2
  101. package/dist/playground/tinygo.d.ts +3 -0
  102. package/dist/playground/tinygo.js +88 -6
  103. package/dist/playground/typescript.js +8 -0
  104. package/dist/playground/v.d.ts +6 -2
  105. package/dist/playground/v.js +76 -13
  106. package/dist/playground/wasmAwkVersion.d.ts +13 -13
  107. package/dist/playground/wasmAwkVersion.js +4 -4
  108. package/dist/playground/wasmBqnVersion.d.ts +2 -2
  109. package/dist/playground/wasmBqnVersion.js +2 -2
  110. package/dist/playground/wasmC3Version.d.ts +2 -2
  111. package/dist/playground/wasmC3Version.js +2 -2
  112. package/dist/playground/wasmClojureScriptVersion.d.ts +2 -2
  113. package/dist/playground/wasmClojureScriptVersion.js +2 -2
  114. package/dist/playground/wasmCommonLispVersion.d.ts +2 -2
  115. package/dist/playground/wasmCommonLispVersion.js +2 -2
  116. package/dist/playground/wasmDIntegrity.d.ts +5 -5
  117. package/dist/playground/wasmDIntegrity.js +5 -5
  118. package/dist/playground/wasmDVersion.d.ts +16 -1
  119. package/dist/playground/wasmDVersion.js +16 -1
  120. package/dist/playground/wasmDebugVersion.d.ts +1 -1
  121. package/dist/playground/wasmDebugVersion.js +1 -1
  122. package/dist/playground/wasmForthVersion.d.ts +2 -2
  123. package/dist/playground/wasmForthVersion.js +2 -2
  124. package/dist/playground/wasmGleamVersion.d.ts +2 -2
  125. package/dist/playground/wasmGleamVersion.js +2 -2
  126. package/dist/playground/wasmGrainVersion.d.ts +2 -2
  127. package/dist/playground/wasmGrainVersion.js +2 -2
  128. package/dist/playground/wasmJVersion.d.ts +2 -2
  129. package/dist/playground/wasmJVersion.js +2 -2
  130. package/dist/playground/wasmJanetVersion.js +2 -2
  131. package/dist/playground/wasmJuliaVersion.js +2 -2
  132. package/dist/playground/wasmLean4Version.d.ts +1 -110
  133. package/dist/playground/wasmLean4Version.js +2 -111
  134. package/dist/playground/wasmLfortranVersion.d.ts +2 -2
  135. package/dist/playground/wasmLfortranVersion.js +2 -2
  136. package/dist/playground/wasmLuaVersion.d.ts +1 -1
  137. package/dist/playground/wasmLuaVersion.js +1 -1
  138. package/dist/playground/wasmNimVersion.d.ts +9 -9
  139. package/dist/playground/wasmNimVersion.js +6 -6
  140. package/dist/playground/wasmOcamlVersion.d.ts +2 -2
  141. package/dist/playground/wasmOcamlVersion.js +1 -1
  142. package/dist/playground/wasmPascalVersion.js +2 -2
  143. package/dist/playground/wasmPerlVersion.js +2 -2
  144. package/dist/playground/wasmPrologVersion.d.ts +2 -2
  145. package/dist/playground/wasmPrologVersion.js +2 -2
  146. package/dist/playground/wasmReScriptVersion.d.ts +2 -2
  147. package/dist/playground/wasmReScriptVersion.js +2 -2
  148. package/dist/playground/wasmRustVersion.d.ts +26 -38
  149. package/dist/playground/wasmRustVersion.js +25 -40
  150. package/dist/playground/wasmTclVersion.d.ts +4 -4
  151. package/dist/playground/wasmTclVersion.js +2 -2
  152. package/dist/playground/wasmTinyGoVersion.d.ts +14 -14
  153. package/dist/playground/wasmTinyGoVersion.js +14 -14
  154. package/dist/playground/wasmTypeScriptVersion.d.ts +5 -5
  155. package/dist/playground/wasmTypeScriptVersion.js +5 -5
  156. package/dist/playground/worker/cachedClangCompile.js +52 -4
  157. package/dist/playground/worker/cachedClangExecute.js +6 -5
  158. package/dist/playground/worker/clang.js +33 -11
  159. package/dist/playground/worker/cobol.js +3 -3
  160. package/dist/playground/worker/d.js +63 -7
  161. package/dist/playground/worker/fortran.js +3 -3
  162. package/dist/playground/worker/go.js +16 -5
  163. package/dist/playground/worker/haskell.js +10 -7
  164. package/dist/playground/worker/java.js +263 -45
  165. package/dist/playground/worker/javaDebugEvaluator.d.ts +16 -0
  166. package/dist/playground/worker/javaDebugEvaluator.js +44 -0
  167. package/dist/playground/worker/javaDebugRuntime.d.ts +24 -0
  168. package/dist/playground/worker/javaDebugRuntime.js +315 -0
  169. package/dist/playground/worker/javaOutput.d.ts +11 -0
  170. package/dist/playground/worker/javaOutput.js +54 -0
  171. package/dist/playground/worker/lua.js +15 -3
  172. package/dist/playground/worker/php.js +15 -9
  173. package/dist/playground/worker/python.js +102 -30
  174. package/dist/playground/worker/pythonExecution.js +17 -1
  175. package/dist/playground/worker/pythonWorkspaceFiles.d.ts +6 -0
  176. package/dist/playground/worker/pythonWorkspaceFiles.js +26 -0
  177. package/dist/playground/worker/pythonWorkspaceImports.d.ts +4 -0
  178. package/dist/playground/worker/pythonWorkspaceImports.js +129 -0
  179. package/dist/playground/worker/r.js +13 -6
  180. package/dist/playground/worker/ruby.js +13 -0
  181. package/dist/playground/worker/rubyDebugPreview.d.ts +2 -0
  182. package/dist/playground/worker/rubyDebugPreview.js +80 -0
  183. package/dist/playground/worker/rubyTracepoint.d.ts +16 -0
  184. package/dist/playground/worker/rubyTracepoint.js +162 -0
  185. package/dist/playground/worker/rust.js +38 -3
  186. package/dist/playground/worker/sqlite.js +4 -3
  187. package/dist/playground/worker/tinygo.js +11 -3
  188. package/dist/playground/worker/typescript.js +2 -3
  189. package/dist/playground/worker/v.js +32 -7
  190. package/dist/playground/worker/wat.js +2 -3
  191. package/dist/playground/worker/zig.js +20 -6
  192. package/dist/playground/workerSession.d.ts +1 -28
  193. package/dist/playground/workerSession.js +1 -164
  194. package/dist/playground/zig.js +4 -0
  195. package/package.json +66 -15
  196. package/dist/playground/worker/python.runtime.cases.d.ts +0 -1
  197. package/dist/playground/worker/python.runtime.cases.js +0 -374
@@ -69,59 +69,113 @@ function stripLineComment(line) {
69
69
  const commentStart = lineCommentStart(line);
70
70
  return commentStart === -1 ? line : line.slice(0, commentStart);
71
71
  }
72
- function countNesting(line) {
73
- const source = stripLineComment(line);
74
- let quote = '';
75
- let escaped = false;
76
- let braces = 0;
77
- let parens = 0;
78
- let brackets = 0;
79
- for (const character of source) {
80
- if (quote) {
81
- if (quote === '`') {
82
- if (character === '`')
83
- quote = '';
84
- continue;
85
- }
86
- if (escaped) {
87
- escaped = false;
88
- continue;
89
- }
90
- if (character === '\\') {
91
- escaped = true;
92
- continue;
72
+ const goKeywords = new Set([
73
+ ...'break case chan const continue default defer else fallthrough for func go'.split(' '),
74
+ ...'goto if import interface map package range return select struct switch type var'.split(' ')
75
+ ]);
76
+ // Final tokens after which Go inserts a semicolon at a line break. Literals are tokenized as
77
+ // '0' (number), '"' (string), "'" (rune) and '`' (raw string).
78
+ const statementEndTokens = new Set([
79
+ ...'break continue fallthrough return ++ -- ) ] }'.split(' '),
80
+ ...['0', '"', "'", '`']
81
+ ]);
82
+ function isIdentifier(token) {
83
+ return token !== undefined && /^[\p{L}_]/u.test(token) && !goKeywords.has(token);
84
+ }
85
+ function endsStatement(token) {
86
+ return statementEndTokens.has(token) || isIdentifier(token);
87
+ }
88
+ // Inside an if/for/switch header Go reads `T{` as the block, but `[]T{`, `[N]*pkg.T{`,
89
+ // `map[K]V{` and `struct{...}{` still open composite literals.
90
+ function followsLiteralType(tokens, braceIndex, closedFrame) {
91
+ let index = braceIndex - 1;
92
+ if (tokens[index] === '}')
93
+ return closedFrame === 'type';
94
+ if (!isIdentifier(tokens[index]))
95
+ return false;
96
+ index -= 1;
97
+ if (tokens[index] === '.' && isIdentifier(tokens[index - 1]))
98
+ index -= 2;
99
+ const pointer = tokens[index] === '*';
100
+ while (tokens[index] === '*')
101
+ index -= 1;
102
+ if (tokens[index] !== ']')
103
+ return false;
104
+ if (!pointer)
105
+ return true;
106
+ // `a[i] * b {` multiplies; only `[]*T` and `map[K]*V` are element types.
107
+ let open = index - 1;
108
+ for (let depth = 1; open >= 0; open -= 1) {
109
+ if (tokens[open] === ']')
110
+ depth += 1;
111
+ else if (tokens[open] === '[' && --depth === 0)
112
+ break;
113
+ }
114
+ return open === index - 1 || tokens[open - 1] === 'map';
115
+ }
116
+ function lineTokens(line, state, onWord) {
117
+ const tokens = [];
118
+ const wordPattern = /[\p{L}_][\p{L}\p{N}_]*|\.?\d[\p{L}\p{N}_.]*/uy;
119
+ let index = 0;
120
+ while (index < line.length) {
121
+ if (state.rawString || state.blockComment) {
122
+ const end = line.indexOf(state.rawString ? '`' : '*/', index);
123
+ if (end === -1)
124
+ break;
125
+ if (state.rawString)
126
+ tokens.push('`');
127
+ index = end + (state.rawString ? 1 : 2);
128
+ state.rawString = false;
129
+ state.blockComment = false;
130
+ continue;
131
+ }
132
+ const character = line[index] || '';
133
+ if (/\s/.test(character)) {
134
+ index += 1;
135
+ continue;
136
+ }
137
+ if (line.startsWith('//', index))
138
+ break;
139
+ if (line.startsWith('/*', index)) {
140
+ state.blockComment = true;
141
+ index += 2;
142
+ continue;
143
+ }
144
+ if (character === '`') {
145
+ state.rawString = true;
146
+ index += 1;
147
+ continue;
148
+ }
149
+ if (character === '"' || character === "'") {
150
+ index += 1;
151
+ while (index < line.length && line[index] !== character) {
152
+ index += line[index] === '\\' ? 2 : 1;
93
153
  }
94
- if (character === quote)
95
- quote = '';
154
+ tokens.push(character);
155
+ index += 1;
96
156
  continue;
97
157
  }
98
- if (character === '"' || character === "'" || character === '`') {
99
- quote = character;
158
+ wordPattern.lastIndex = index;
159
+ const word = wordPattern.exec(line)?.[0];
160
+ if (word) {
161
+ tokens.push(/^[\p{L}_]/u.test(word) ? word : '0');
162
+ index += word.length;
163
+ onWord?.(word, index);
100
164
  continue;
101
165
  }
102
- if (character === '{')
103
- braces += 1;
104
- else if (character === '}')
105
- braces -= 1;
106
- else if (character === '(')
107
- parens += 1;
108
- else if (character === ')')
109
- parens -= 1;
110
- else if (character === '[')
111
- brackets += 1;
112
- else if (character === ']')
113
- brackets -= 1;
166
+ const operator = ['++', '--', ':=', '...'].find((candidate) => line.startsWith(candidate, index)) ||
167
+ character;
168
+ tokens.push(operator);
169
+ index += operator.length;
114
170
  }
115
- return { braces, parens, brackets };
171
+ return tokens;
116
172
  }
117
173
  function functionNameFromLine(line) {
118
174
  const source = stripLineComment(line).trim();
119
175
  const match = source.match(/^func\s+(?:\([^)]*\)\s*)?([A-Za-z_]\w*)\s*\(/);
120
176
  return match?.[1] || null;
121
177
  }
122
- function shouldInstrumentLine(line, parenDepth, bracketDepth) {
123
- if (parenDepth > 0 || bracketDepth > 0)
124
- return false;
178
+ function shouldInstrumentLine(line) {
125
179
  const trimmed = stripLineComment(line).trim();
126
180
  if (!trimmed)
127
181
  return false;
@@ -141,80 +195,134 @@ function shouldInstrumentLine(line, parenDepth, bracketDepth) {
141
195
  }
142
196
  function insertDebugImports(source) {
143
197
  const lines = source.split('\n');
144
- const packageIndex = lines.findIndex((line) => /^\s*package\s+\w+/.test(line));
145
- if (packageIndex === -1)
146
- return source;
147
- const importStart = lines.findIndex((line, index) => index > packageIndex && /^\s*import\b/.test(line));
148
- if (importStart === -1) {
149
- lines.splice(packageIndex + 1, 0, '', 'import (', ...debugImports.map((entry) => `\t${entry}`), ')');
150
- return lines.join('\n');
151
- }
152
- const importLine = lines[importStart] || '';
153
- if (/^\s*import\s*\(/.test(importLine)) {
154
- let importEnd = importStart + 1;
155
- for (; importEnd < lines.length; importEnd += 1) {
156
- if (/^\s*\)/.test(lines[importEnd] || ''))
157
- break;
158
- }
159
- lines.splice(importEnd, 0, ...debugImports.map((entry) => `\t${entry}`));
198
+ const state = { rawString: false, blockComment: false };
199
+ let packageKeyword = false;
200
+ for (let index = 0; index < lines.length; index += 1) {
201
+ const line = lines[index];
202
+ let packageNameEnd;
203
+ lineTokens(line, state, (word, end) => {
204
+ if (packageNameEnd !== undefined)
205
+ return;
206
+ if (packageKeyword)
207
+ packageNameEnd = end;
208
+ else if (word === 'package')
209
+ packageKeyword = true;
210
+ });
211
+ if (packageNameEnd === undefined)
212
+ continue;
213
+ // Insert before trailing comments, which may span lines or end beside an import.
214
+ // Keep the suffix on ')' so an explicit package semicolon still terminates a declaration.
215
+ lines.splice(index, 1, line.slice(0, packageNameEnd), '', 'import (', ...debugImports.map((entry) => `\t${entry}`), ')' + line.slice(packageNameEnd));
160
216
  return lines.join('\n');
161
217
  }
162
- const singleImport = importLine.replace(/^\s*import\s+/, '').trim();
163
- lines.splice(importStart, 1, 'import (', `\t${singleImport}`, ...debugImports.map((entry) => `\t${entry}`), ')');
164
- return lines.join('\n');
165
- }
166
- function insertDebugSupport(source) {
167
- const lines = source.split('\n');
168
- let insertIndex = lines.findIndex((line) => /^\s*import\b/.test(line));
169
- if (insertIndex === -1) {
170
- insertIndex = lines.findIndex((line) => /^\s*package\s+\w+/.test(line));
171
- if (insertIndex === -1)
172
- return source;
173
- insertIndex += 1;
174
- }
175
- else if (/^\s*import\s*\(/.test(lines[insertIndex] || '')) {
176
- for (; insertIndex < lines.length; insertIndex += 1) {
177
- if (/^\s*\)/.test(lines[insertIndex] || '')) {
178
- insertIndex += 1;
179
- break;
180
- }
181
- }
182
- }
183
- else {
184
- insertIndex += 1;
185
- }
186
- lines.splice(insertIndex, 0, debugSupportSource.trim());
187
- return lines.join('\n');
218
+ return source;
188
219
  }
189
220
  function instrumentFunctionLines(source) {
190
221
  const lines = source.split('\n');
191
222
  const instrumented = [];
192
223
  const functionStack = [];
224
+ const lexState = { rawString: false, blockComment: false };
225
+ const frames = [];
226
+ // Frame depths where a `func` signature still awaits its body, and where an if/for/switch
227
+ // header still awaits its block.
228
+ let pendingFunctionDepths = [];
229
+ let headerDepth = -1;
230
+ let closedFrame;
231
+ let previousToken = ';';
232
+ let continuesStatement = false;
233
+ let followsLabel = false;
193
234
  let braceDepth = 0;
194
- let parenDepth = 0;
195
- let bracketDepth = 0;
196
235
  for (let index = 0; index < lines.length; index += 1) {
197
236
  const line = lines[index] || '';
198
237
  const lineNumber = index + 1;
199
- const functionName = functionNameFromLine(line);
238
+ const startsInCode = !lexState.rawString && !lexState.blockComment;
239
+ const functionName = startsInCode ? functionNameFromLine(line) : null;
200
240
  const inFunction = functionStack.length > 0;
201
241
  const indent = line.match(/^\s*/)?.[0] || '';
202
- if (inFunction && shouldInstrumentLine(line, parenDepth, bracketDepth)) {
242
+ // Only hook lines that start a statement directly inside a block: not inside a raw
243
+ // string, comment, composite literal, call or index, and not continuing the previous line.
244
+ if (inFunction &&
245
+ startsInCode &&
246
+ frames.at(-1) === 'block' &&
247
+ !frames.includes('(') &&
248
+ !frames.includes('[') &&
249
+ !continuesStatement &&
250
+ !followsLabel &&
251
+ shouldInstrumentLine(line)) {
203
252
  instrumented.push(`${indent}__wasmIdleDebugLine(${lineNumber})`);
204
253
  }
205
254
  instrumented.push(line);
206
- const nesting = countNesting(line);
207
- const nextBraceDepth = braceDepth + nesting.braces;
208
- const nextParenDepth = Math.max(0, parenDepth + nesting.parens);
209
- const nextBracketDepth = Math.max(0, bracketDepth + nesting.brackets);
210
- if (functionName && nesting.braces > 0) {
255
+ const tokens = lineTokens(line, lexState);
256
+ let braces = 0;
257
+ for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 1) {
258
+ const token = tokens[tokenIndex] || '';
259
+ const depth = frames.length;
260
+ if (token === '(' || token === '[') {
261
+ frames.push(token);
262
+ }
263
+ else if (token === ')' || token === ']' || token === '}') {
264
+ closedFrame = frames.pop();
265
+ if (token === '}')
266
+ braces -= 1;
267
+ pendingFunctionDepths = pendingFunctionDepths.filter((pending) => pending <= frames.length);
268
+ if (headerDepth > frames.length)
269
+ headerDepth = -1;
270
+ }
271
+ else if (token === '{') {
272
+ let frame = '{';
273
+ if (pendingFunctionDepths.includes(depth)) {
274
+ frame = 'block';
275
+ pendingFunctionDepths = pendingFunctionDepths.filter((pending) => pending !== depth);
276
+ }
277
+ else if (previousToken === 'struct' || previousToken === 'interface') {
278
+ frame = 'type';
279
+ }
280
+ else if (['else', 'for', 'switch', 'select'].includes(previousToken) ||
281
+ (headerDepth === depth &&
282
+ tokenIndex === tokens.length - 1 &&
283
+ !followsLiteralType(tokens, tokenIndex, closedFrame)) ||
284
+ ([';', '{', ':'].includes(previousToken) &&
285
+ (frames.at(-1) ?? 'block') === 'block')) {
286
+ // Go ends an if/for/switch header at the `{` that closes its line; any other
287
+ // `{` after an operand (`P{`, `[]int{`) opens a literal.
288
+ frame = 'block';
289
+ if (headerDepth === depth)
290
+ headerDepth = -1;
291
+ }
292
+ frames.push(frame);
293
+ braces += 1;
294
+ }
295
+ else if (token === 'func' && previousToken !== ']' && previousToken !== 'chan') {
296
+ pendingFunctionDepths.push(depth);
297
+ }
298
+ else if (['if', 'for', 'switch', 'select'].includes(token)) {
299
+ headerDepth = depth;
300
+ }
301
+ else if ([',', ';', '=', ':='].includes(token)) {
302
+ pendingFunctionDepths = pendingFunctionDepths.filter((pending) => pending !== depth);
303
+ }
304
+ previousToken = token;
305
+ }
306
+ const lastToken = tokens.at(-1);
307
+ if (lastToken !== undefined) {
308
+ const trimmed = stripLineComment(line).trim();
309
+ continuesStatement = !endsStatement(lastToken) && !['{', ':', ';'].includes(lastToken);
310
+ followsLabel = /^[A-Za-z_]\w*:$/.test(trimmed) && trimmed !== 'default:';
311
+ if (endsStatement(lastToken)) {
312
+ previousToken = ';';
313
+ pendingFunctionDepths = pendingFunctionDepths.filter((pending) => pending < frames.length);
314
+ if (headerDepth >= frames.length)
315
+ headerDepth = -1;
316
+ }
317
+ }
318
+ // A signature line that ends inside a raw string or block comment has no place for the
319
+ // entry hook; leave that function uninstrumented rather than edit the literal.
320
+ if (functionName && braces > 0 && !lexState.rawString && !lexState.blockComment) {
211
321
  const bodyIndent = `${indent}\t`;
212
322
  instrumented.push(`${bodyIndent}__wasmIdleDebugEnter(${JSON.stringify(functionName)}, ${lineNumber})`, `${bodyIndent}defer __wasmIdleDebugLeave()`);
213
- functionStack.push({ depth: nextBraceDepth, name: functionName });
323
+ functionStack.push({ depth: braceDepth + braces, name: functionName });
214
324
  }
215
- braceDepth = nextBraceDepth;
216
- parenDepth = nextParenDepth;
217
- bracketDepth = nextBracketDepth;
325
+ braceDepth += braces;
218
326
  while (functionStack.length &&
219
327
  braceDepth < functionStack[functionStack.length - 1].depth) {
220
328
  functionStack.pop();
@@ -225,5 +333,7 @@ function instrumentFunctionLines(source) {
225
333
  export function instrumentGoDebugSource(source) {
226
334
  if (source.includes('__wasmIdleDebugLine'))
227
335
  return source;
228
- return insertDebugSupport(insertDebugImports(instrumentFunctionLines(source)));
336
+ // Declarations may follow user code; placing them here keeps every user import
337
+ // ahead of declarations, including a second import block.
338
+ return `${insertDebugImports(instrumentFunctionLines(source))}\n\n${debugSupportSource.trim()}\n`;
229
339
  }