blob: 8729785f21801ad1c6e707728e322d3852b7f078 [file] [log] [blame]
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:meta/meta.dart';
import 'binding.dart';
import 'binding_string.dart';
import 'type.dart';
import 'writer.dart';
/// A binding to a global variable
///
/// For a C global variable -
/// ```c
/// int a;
/// ```
/// The generated dart code is -
/// ```dart
/// final int a = _dylib.lookup<ffi.Int32>('a').value;
/// ```
class Global extends Binding {
final Type type;
const Global({
@required String name,
@required this.type,
String dartDoc,
}) : super(name: name, dartDoc: dartDoc);
@override
BindingString toBindingString(Writer w) {
final s = StringBuffer();
if (dartDoc != null) {
s.write('/// ');
s.writeAll(dartDoc.split('\n'), '\n/// ');
s.write('\n');
}
s.write(
"final ${type.getDartType(w)} $name = ${w.dylibIdentifier}.lookup<${type.getCType(w)}>('$name').value;\n\n");
return BindingString(type: BindingStringType.global, string: s.toString());
}
}