MCPcopy
hub / github.com/gopherjs/gopherjs / translateFunctionBody

Method translateFunctionBody

compiler/functions.go:219–355  ·  view source on GitHub ↗

translateFunctionBody translates body of a top-level or literal function. It returns a JS function expression that represents the given Go function. Function receiver must have been created with nestedFunctionContext() to have required metadata set up.

(typ *ast.FuncType, recv *ast.Ident, body *ast.BlockStmt)

Source from the content-addressed store, hash-verified

217// Function receiver must have been created with nestedFunctionContext() to have
218// required metadata set up.
219func (fc *funcContext) translateFunctionBody(typ *ast.FuncType, recv *ast.Ident, body *ast.BlockStmt) string {
220 prevEV := fc.pkgCtx.escapingVars
221
222 // Generate a list of function argument variables. Since Go allows nameless
223 // arguments, we have to generate synthetic names for their JS counterparts.
224 var args []string
225 for _, param := range typ.Params.List {
226 if len(param.Names) == 0 {
227 args = append(args, fc.newLocalVariable("param"))
228 continue
229 }
230 for _, ident := range param.Names {
231 if isBlank(ident) {
232 args = append(args, fc.newLocalVariable("param"))
233 continue
234 }
235 args = append(args, fc.objectName(fc.pkgCtx.Defs[ident]))
236 }
237 }
238
239 bodyOutput := string(fc.CatchOutput(2, func() {
240 if fc.IsBlocking() {
241 fc.pkgCtx.Scopes[body] = fc.pkgCtx.Scopes[typ]
242 fc.handleEscapingVars(body)
243 }
244
245 if fc.sig != nil && fc.sig.HasNamedResults() {
246 fc.resultNames = make([]ast.Expr, fc.sig.Sig.Results().Len())
247 for i := 0; i < fc.sig.Sig.Results().Len(); i++ {
248 result := fc.sig.Sig.Results().At(i)
249 typ := fc.typeResolver.Substitute(result.Type())
250 fc.Printf("%s = %s;", fc.objectName(result), fc.translateExpr(fc.zeroValue(typ)).String())
251 id := ast.NewIdent("")
252 fc.pkgCtx.Uses[id] = result
253 fc.resultNames[i] = fc.setType(id, typ)
254 }
255 }
256
257 if recv != nil && !isBlank(recv) {
258 this := "this"
259 if isWrapped(fc.typeOf(recv)) {
260 this = "this.$val" // Unwrap receiver value.
261 }
262 fc.Printf("%s = %s;", fc.translateExpr(recv), this)
263 }
264
265 fc.translateStmtList(body.List)
266 if len(fc.Flattened) != 0 && !astutil.EndsWithReturn(body.List) {
267 fc.translateStmt(&ast.ReturnStmt{Return: body.Rbrace}, nil)
268 }
269 }))
270
271 sort.Strings(fc.localVars)
272
273 var prefix, suffix string
274
275 if len(fc.Flattened) != 0 {
276 // $s contains an index of the switch case a blocking function reached

Callers 3

translateMethodMethod · 0.95
translateExprMethod · 0.80

Calls 15

newLocalVariableMethod · 0.95
objectNameMethod · 0.95
CatchOutputMethod · 0.95
handleEscapingVarsMethod · 0.95
PrintfMethod · 0.95
translateExprMethod · 0.95
zeroValueMethod · 0.95
setTypeMethod · 0.95
typeOfMethod · 0.95
translateStmtListMethod · 0.95
translateStmtMethod · 0.95
translateResultsMethod · 0.95

Tested by

no test coverage detected