MCPcopy
hub / github.com/gopherjs/gopherjs / loadStruct

Method loadStruct

compiler/expressions.go:1429–1463  ·  view source on GitHub ↗
(array, target string, s *types.Struct)

Source from the content-addressed store, hash-verified

1427}
1428
1429func (fc *funcContext) loadStruct(array, target string, s *types.Struct) string {
1430 view := fc.newLocalVariable("_view")
1431 code := fmt.Sprintf("%s = new DataView(%s.buffer, %s.byteOffset)", view, array, array)
1432 var fields []*types.Var
1433 var collectFields func(s *types.Struct, path string)
1434 collectFields = func(s *types.Struct, path string) {
1435 for i := 0; i < s.NumFields(); i++ {
1436 fieldName := path + "." + fieldName(s, i)
1437 fieldType := fc.fieldType(s, i)
1438 if fs, isStruct := fieldType.Underlying().(*types.Struct); isStruct {
1439 collectFields(fs, fieldName)
1440 continue
1441 }
1442 fields = append(fields, types.NewVar(0, nil, fieldName, fieldType))
1443 }
1444 }
1445 collectFields(s, target)
1446 offsets := sizes32.Offsetsof(fields)
1447 for i, field := range fields {
1448 switch t := field.Type().Underlying().(type) {
1449 case *types.Basic:
1450 if isNumeric(t) {
1451 if is64Bit(t) {
1452 code += fmt.Sprintf(", %s = new %s(%s.getUint32(%d, true), %s.getUint32(%d, true))", field.Name(), fc.typeName(field.Type()), view, offsets[i]+4, view, offsets[i])
1453 break
1454 }
1455 code += fmt.Sprintf(", %s = %s.get%s(%d, true)", field.Name(), view, toJavaScriptType(t), offsets[i])
1456 }
1457 case *types.Array:
1458 code += fmt.Sprintf(`, %s = new ($nativeArray(%s))(%s.buffer, $min(%s.byteOffset + %d, %s.buffer.byteLength))`, field.Name(), typeKind(t.Elem()), array, array, offsets[i], array)
1459 }
1460 // TODO(nevkontakte): Explicitly panic if unsupported field type is encountered?
1461 }
1462 return code
1463}
1464
1465func (fc *funcContext) fixNumber(value *expression, basic *types.Basic) *expression {
1466 switch basic.Kind() {

Callers 1

translateConversionMethod · 0.95

Calls 11

newLocalVariableMethod · 0.95
fieldTypeMethod · 0.95
typeNameMethod · 0.95
fieldNameFunction · 0.85
isNumericFunction · 0.85
is64BitFunction · 0.85
toJavaScriptTypeFunction · 0.85
typeKindFunction · 0.85
TypeMethod · 0.65
NameMethod · 0.45
ElemMethod · 0.45

Tested by

no test coverage detected