-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ffigen] Add selector conversion utils (#1616)
- Loading branch information
1 parent
8490510
commit 4bc8c16
Showing
14 changed files
with
112 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2024, 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 'dart:ffi'; | ||
|
||
import 'package:ffi/ffi.dart'; | ||
|
||
import 'c_bindings_generated.dart' as c; | ||
import 'internal.dart'; | ||
|
||
extension StringToSelector on String { | ||
/// Returns an Objective-C selector (aka `SEL`) for this [String]. | ||
/// | ||
/// This is equivalent to the Objective-C `@selector()` directive, or the | ||
/// `NSSelectorFromString` function. | ||
Pointer<c.ObjCSelector> toSelector() => registerName(this); | ||
} | ||
|
||
extension SelectorToString on Pointer<c.ObjCSelector> { | ||
/// Returns the string that this Objective-C selector represents. | ||
/// | ||
/// This is equivalent to the Objective-C `NSSelectorFromString` function. | ||
String toDartString() => c.getName(this).cast<Utf8>().toDartString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2024, 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. | ||
|
||
// Objective C support is only available on mac. | ||
@TestOn('mac-os') | ||
library; | ||
|
||
import 'dart:ffi'; | ||
|
||
import 'package:objective_c/objective_c.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Selector', () { | ||
setUpAll(() { | ||
// TODO(https://github.com/dart-lang/native/issues/1068): Remove this. | ||
DynamicLibrary.open('test/objective_c.dylib'); | ||
}); | ||
|
||
test('from String and back', () { | ||
expect('hello'.toSelector().toDartString(), 'hello'); | ||
expect(''.toSelector().toDartString(), ''); | ||
expect('foo:with:args:'.toSelector().toDartString(), 'foo:with:args:'); | ||
}); | ||
|
||
test('responds to selector', () { | ||
final sel1 = 'addObserver:forKeyPath:options:context:'.toSelector(); | ||
expect(NSObject.new1().respondsToSelector_(sel1), isTrue); | ||
expect(NSObject.new1().respondsToSelector_('foo'.toSelector()), isFalse); | ||
|
||
final sel2 = 'canBeConvertedToEncoding:'.toSelector(); | ||
expect(NSString.new1().respondsToSelector_(sel2), isTrue); | ||
expect(NSString.new1().respondsToSelector_('bar'.toSelector()), isFalse); | ||
}); | ||
}); | ||
} |