Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Globals #139

Merged
merged 10 commits into from
Jan 13, 2021
Merged

Globals #139

merged 10 commits into from
Jan 13, 2021

Conversation

TimWhiting
Copy link

@TimWhiting TimWhiting commented Jan 12, 2021

Closes dart-lang/native#378

@mannprerak2
I think I made the needed changes. Tested it locally and it seems to work.

@TimWhiting
Copy link
Author

Okay, this should be ready for review. The only issue right now is the large integration test, which I'm assuming fails because the library maybe has some globals that weren't generated before?

Copy link
Contributor

@mannprerak2 mannprerak2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a new test in test/header_parser_tests? You can pretty much copy the unnamed_enum_test.dart file and modify that to add a few tests. (say for a pointer, int, struct, Long Double, pointer to Long double)

Update: Also update the version in pubspec, changelog, and readme(Add globals in the configuration table in row 6, column 1)

lib/src/header_parser/sub_parsers/var_parser.dart Outdated Show resolved Hide resolved
final type = cursor.type().toCodeGenType();
if (type.broadType == BroadType.Unimplemented) {
_logger
.warning('Global Type not supported $type ${cursor.type().spelling()}');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add these logs instead.

_logger.fine(
          '---- Removed Global, reason: unsupported type: ${cursor.completeStringRepr()}');
_logger.warning(
      "Skipped global variable '$globalName', type not supported.");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

s.write(
"${type.getDartType(w)} get $globalVarName => ($holderVarName ??= ${w.dylibIdentifier}.lookup<${type.getCType(w)}>('$originalName')).value;\n\n");
"late final ${type.getDartType(w)} $globalVarName = ${w.dylibIdentifier}.lookup<${type.getCType(w)}>('$originalName').$refOrValue;\n\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think global variables are not constants, they can be changed.

If that's the case, we should instead add getters and setters for this.

So for an int global variable, we can do this -

  late final ffi.Pointer<ffi.Int32> _globalVar =
      _dylib.lookup<ffi.Int32>('globalVar');

  int get globalVar => _globalVar.value;

  set globalVar(int value) => _globalVar.value = value;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this works for all types except structs, which just don't have a setter, but fields for structs can be set from the ref that is obtained via the getter.

lib/src/header_parser/translation_unit_parser.dart Outdated Show resolved Hide resolved
@mannprerak2
Copy link
Contributor

mannprerak2 commented Jan 12, 2021

The only issue right now is the large integration test, which I'm assuming fails because the library maybe has some globals that weren't generated before?

The test/debug_generated folder will contain generated output of failed tests, you can compare those with _expected.* files in large_integration_tests folder, and subsequently add the changes there if everything seems okay.

@TimWhiting
Copy link
Author

@mannprerak2
I addressed everything you mentioned, and added a few features that were missing in my previous implementation (the exclude configuration). Added tests, and updated the _expected file in the large integration test.

Copy link
Contributor

@mannprerak2 mannprerak2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @TimWhiting, Just a few last changes.

lib/src/code_generator/global.dart Outdated Show resolved Hide resolved
lib/src/header_parser/sub_parsers/var_parser.dart Outdated Show resolved Hide resolved
lib/src/header_parser/sub_parsers/var_parser.dart Outdated Show resolved Hide resolved
Copy link
Contributor

@mannprerak2 mannprerak2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! Thanks @TimWhiting

Copy link
Contributor

@dcharkes dcharkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff! Some small comments.

lib/src/header_parser/sub_parsers/var_parser.dart Outdated Show resolved Hide resolved
test/header_parser_tests/globals.h Outdated Show resolved Hide resolved
test/header_parser_tests/globals_test.dart Outdated Show resolved Hide resolved
pubspec.yaml Outdated Show resolved Hide resolved
pubspec.yaml Outdated Show resolved Hide resolved
@dcharkes
Copy link
Contributor

@TimWhiting

From the CI:

00:13 +40: test/header_parser_tests/globals_test.dart: globals_test (setUpAll)                                                                                                                         
WARNING : Skipped global variable 'longDouble', type not supported.
WARNING : Skipped global variable 'pointerToLongDouble', type not supported.

Are those on purpose?

@TimWhiting
Copy link
Author

TimWhiting commented Jan 13, 2021

From the CI:

00:13 +40: test/header_parser_tests/globals_test.dart: globals_test (setUpAll)                                                                                                                         
WARNING : Skipped global variable 'longDouble', type not supported.
WARNING : Skipped global variable 'pointerToLongDouble', type not supported.

Are those on purpose?

Yes, that is on purpose. I test that the globals whose types dart:ffi does not support are not in the generated bindings and those messages are logged so the user knows those variables will not be present in the generated bindings.

@dcharkes dcharkes merged commit 568695c into dart-archive:dart-dev Jan 13, 2021
mannprerak2 added a commit to mannprerak2/ffigen that referenced this pull request Mar 1, 2021
dcharkes pushed a commit that referenced this pull request Mar 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants