Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Dec 4, 2024
1 parent d02eb07 commit 73bd69a
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import '../../_core/interfaces/objc_annotatable.dart';
/// Describes a built-in Swift type (e.g Int, String, etc).
enum BuiltInDeclaration implements Declaration, ObjCAnnotatable {
swiftNSObject(id: 'c:objc(cs)NSObject', name: 'NSObject'),
swiftURL(id: 's:10Foundation3URLV', name: 'URL'), // HACK
swiftTimeInterval(id: 'c:@T@NSTimeInterval', name: 'TimeInterval'), // HACK
swiftString(id: 's:SS', name: 'String'),
swiftInt(id: 's:Si', name: 'Int'),
swiftFloat(id: 's:Sf', name: 'Float'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ T _parseCompoundDeclaration<T extends CompoundDeclaration>(

compound.nestedDeclarations.fillNestingParents(compound);


return compound;
}

Expand Down
2 changes: 1 addition & 1 deletion pkgs/swift2objc/lib/src/parser/parsers/parse_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ typedef PrefixParselet = (ReferredType, TokenList) Function(

if (symbol == null) {
throw Exception(
'The type at "${token.path}" does not exist among parsed symbols: $id');
'The type at "${token.path}" does not exist among parsed symbols.');
}

final type = parseDeclaration(symbol, symbolgraph).asDeclaredType;
Expand Down
66 changes: 0 additions & 66 deletions pkgs/swiftgen/bin/swiftgen.dart

This file was deleted.

8 changes: 6 additions & 2 deletions pkgs/swiftgen/example/avf_audio_wrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,12 @@ import Foundation
return result == nil ? nil : AVAudioTimeWrapper(result!)
}

@objc public func scheduleFile(_ file: AVAudioFileWrapper, at when: AVAudioTimeWrapper?) {
return wrappedInstance.scheduleFile(file.wrappedInstance, at: when?.wrappedInstance)
@objc public func scheduleBuffer(_ buffer: AVAudioPCMBufferWrapper) async {
return await wrappedInstance.scheduleBuffer(buffer.wrappedInstance)
}

@objc public func scheduleFile(_ file: AVAudioFileWrapper, at when: AVAudioTimeWrapper?) async {
return await wrappedInstance.scheduleFile(file.wrappedInstance, at: when?.wrappedInstance)
}

@objc public func stop() {
Expand Down
21 changes: 21 additions & 0 deletions pkgs/swiftgen/example/generate_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@ Future<void> main() async {
),
),
));

final result = Process.runSync(
'swiftc',
[
'-emit-library',
'-o',
'avf_audio_wrapper.dylib',
'-module-name',
'AVFAudioWrapper',
'avf_audio_wrapper.swift',
'-framework',
'AVFAudio',
'-framework',
'Foundation',
],
);
if (result.exitCode != 0) {
print("Failed to build the swift wrapper library");
print(result.stdout);
print(result.stderr);
}
}
15 changes: 10 additions & 5 deletions pkgs/swiftgen/example/play_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';
import 'dart:io';
import 'package:objective_c/objective_c.dart';
import 'avf_audio_bindings.dart';

Expand All @@ -12,19 +13,23 @@ import '../../objective_c/test/setup.dart' as objCSetup;
const _dylibPath =
'/System/Library/Frameworks/AVFAudio.framework/Versions/Current/AVFAudio';

// swiftc -emit-library -o avf_audio_wrapper.dylib -module-name AVFAudioWrapper avf_audio_wrapper.swift -framework AVFAudio -framework Foundation
const _wrapperDylibPath = 'avf_audio_wrapper.dylib';
const _wrapperDylib = 'avf_audio_wrapper.dylib';

void main(List<String> args) async {
if (args.length == 0) {
print("Usage: dart play_audio.dart file1.wav file2.mp3 ...");
return;
}

objCSetup.main([]);
DynamicLibrary.open(_dylibPath);
DynamicLibrary.open(_wrapperDylibPath);
DynamicLibrary.open(Platform.script.resolve(_wrapperDylib).toFilePath());
for (final file in args) {
final fileStr = NSString(file);
print('Loading $fileStr');
final fileUrl = NSURL.fileURLWithPath_(fileStr);
final player =
AVAudioPlayerWrapper.alloc().initWithContentsOf_error_(fileUrl, nullptr);
final player = AVAudioPlayerWrapper.alloc()
.initWithContentsOf_error_(fileUrl, nullptr);
if (player == null) {
print('Failed to load audio');
continue;
Expand Down
55 changes: 33 additions & 22 deletions pkgs/swiftgen/test/integration/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class TestGenerator {
late final String actualOutputFile;

TestGenerator(this.name) {
testDir = path.absolute(path.join(Directory.current.path, 'test/integration'));
testDir =
path.absolute(path.join(Directory.current.path, 'test/integration'));
tempDir = path.join(testDir, 'temp');
inputFile = path.join(testDir, '${name}.swift');
wrapperFile = path.join(tempDir, '${name}_wrapper.swift');
Expand All @@ -38,21 +39,21 @@ class TestGenerator {
}

Future<void> generateBindings() async => generate(Config(
target: await Target.host(),
input: SwiftFileInput(
module: name,
files: [Uri.file(inputFile)],
),
objcSwiftFile: Uri.file(wrapperFile),
tempDir: Directory(tempDir).uri,
ffigen: FfiGenConfig(
output: Uri.file(outputFile),
outputObjC: Uri.file(outputObjCFile),
objcInterfaces: DeclarationFilters(
shouldInclude: (decl) => decl.originalName.startsWith('Test'),
target: await Target.host(),
input: SwiftFileInput(
module: name,
files: [Uri.file(inputFile)],
),
),
));
objcSwiftFile: Uri.file(wrapperFile),
tempDir: Directory(tempDir).uri,
ffigen: FfiGenConfig(
output: Uri.file(outputFile),
outputObjC: Uri.file(outputObjCFile),
objcInterfaces: DeclarationFilters(
shouldInclude: (decl) => decl.originalName.startsWith('Test'),
),
),
));

Future<void> generateAndVerifyBindings() async {
// Run the generation pipeline. This produces the swift compatability
Expand All @@ -68,16 +69,26 @@ class TestGenerator {
expect(File(objWrapperFile).existsSync(), isTrue);

// We also need to compile outputObjCFile to an obj file.
await run('clang',
['-x', 'objective-c', '-c', outputObjCFile, '-fpic', '-o', objObjCFile],
tempDir);
await run(
'clang',
['-x', 'objective-c', '-c', outputObjCFile, '-fpic', '-o', objObjCFile],
tempDir);
expect(File(objObjCFile).existsSync(), isTrue);

// Link all the obj files into a dylib.
await run('clang',
['-shared', '-framework', 'Foundation', objInputFile,
objWrapperFile, objObjCFile, '-o', dylibFile],
tempDir);
await run(
'clang',
[
'-shared',
'-framework',
'Foundation',
objInputFile,
objWrapperFile,
objObjCFile,
'-o',
dylibFile
],
tempDir);
expect(File(dylibFile).existsSync(), isTrue);
}
}
12 changes: 6 additions & 6 deletions pkgs/swiftgen/tool/regen_test_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Future<void> regenIntegrationTestBindings(String name) async {
}

const testSuffix = '_test.dart';
List<String> findAllIntegrationTests() =>
Directory(TestGenerator('').testDir)
.listSync()
.map((entity) => path.basename(entity.path))
.where((f) => f.endsWith(testSuffix))
.map((f) => f.substring(0, f.length - testSuffix.length)).toList();
List<String> findAllIntegrationTests() => Directory(TestGenerator('').testDir)
.listSync()
.map((entity) => path.basename(entity.path))
.where((f) => f.endsWith(testSuffix))
.map((f) => f.substring(0, f.length - testSuffix.length))
.toList();

Future<void> main(List<String> args) async {
for (final name in args.isEmpty ? findAllIntegrationTests() : args) {
Expand Down

0 comments on commit 73bd69a

Please sign in to comment.