Skip to content

Commit 9f21cb5

Browse files
committed
feat: support multi-isolates
1 parent d04e371 commit 9f21cb5

File tree

18 files changed

+116
-55
lines changed

18 files changed

+116
-55
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ build/
88

99
.idea/
1010

11-
.cxx/
11+
.cxx/
12+
13+
.fvm/

dart_native/example/lib/ios/unit_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import 'dart:async';
12
import 'dart:ffi';
3+
import 'dart:isolate';
24

35
import 'package:dart_native/dart_native.dart';
46
import 'package:dart_native_example/ios/delegatestub.dart';
@@ -153,4 +155,25 @@ testIOS(RuntimeStub stub, DelegateStub delegate) {
153155

154156
NSNotificationCenter.defaultCenter.addObserver(
155157
delegate, delegate.handleNotification, 'SampleDartNotification', nil);
158+
159+
Isolate.spawn(_checkTimer, stub.pointer.address);
160+
Isolate.spawn(_checkTimer1, stub.pointer.address);
161+
}
162+
163+
void _checkTimer(int addr) async {
164+
RuntimeStub stub = RuntimeStub.fromPointer(Pointer.fromAddress(addr));
165+
Timer.periodic(new Duration(seconds: 1), (Timer t) {
166+
stub.fooCompletion(() {
167+
print('hello completion block on another isolate!');
168+
});
169+
});
170+
}
171+
172+
void _checkTimer1(int addr) async {
173+
RuntimeStub stub = RuntimeStub.fromPointer(Pointer.fromAddress(addr));
174+
Timer.periodic(new Duration(seconds: 1), (Timer t) {
175+
stub.fooCompletion(() {
176+
print('hello completion block on third isolate!');
177+
});
178+
});
156179
}

dart_native/example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ packages:
168168
path: ".."
169169
relative: true
170170
source: path
171-
version: "0.3.16"
171+
version: "0.3.17"
172172
dart_native_gen:
173173
dependency: transitive
174174
description:

dart_native/ios/Classes/DNBlockWrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#import <Foundation/Foundation.h>
99
#import "DNMacro.h"
10+
#import "dart_api_dl.h"
1011

1112
NS_ASSUME_NONNULL_BEGIN
1213

@@ -21,11 +22,13 @@ typedef void (*NativeBlockCallback)(void *_Nullable *_Null_unspecified args, voi
2122
@property (nonatomic, readonly) NativeBlockCallback callback;
2223
@property (nonatomic, getter=hasStret, readonly) BOOL stret;
2324
@property (nonatomic, readonly) int64_t sequence;
25+
@property (nonatomic, readonly) Dart_Port dartPort;
2426

2527
- (intptr_t)blockAddress;
2628

2729
- (instancetype)initWithTypeString:(char *)typeString
2830
callback:(NativeBlockCallback)callback
31+
dartPort:(Dart_Port)dartPort
2932
error:(out NSError **)error;
3033

3134
@end

dart_native/ios/Classes/DNBlockWrapper.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ @implementation DNBlockWrapper
125125

126126
- (instancetype)initWithTypeString:(char *)typeString
127127
callback:(NativeBlockCallback)callback
128+
dartPort:(Dart_Port)dartPort
128129
error:(out NSError **)error {
129130
self = [super init];
130131
if (self) {
@@ -134,6 +135,7 @@ - (instancetype)initWithTypeString:(char *)typeString
134135
if (_typeString.length > 0) {
135136
_callback = callback;
136137
_thread = NSThread.currentThread;
138+
_dartPort = dartPort;
137139
[self initBlockWithError:error];
138140
atomic_fetch_add(&_seq, 1);
139141
_sequence = _seq;
@@ -149,7 +151,7 @@ - (void)dealloc {
149151
free((void *)_typeEncodings[i]);
150152
}
151153
free(_typeEncodings);
152-
NotifyDeallocToDart(_sequence);
154+
NotifyDeallocToDart(_sequence, _dartPort);
153155
}
154156

155157
- (void)initBlockWithError:(out NSError **)error {

dart_native/ios/Classes/DNMethodIMP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
#import <Foundation/Foundation.h>
9+
#import "dart_api_dl.h"
910

1011
NS_ASSUME_NONNULL_BEGIN
1112

@@ -19,9 +20,11 @@ typedef void (*NativeMethodCallback)(void *_Nullable *_Null_unspecified args,
1920

2021
@property (nonatomic, readonly) NativeMethodCallback callback;
2122
@property (nonatomic, getter=hasStret, readonly) BOOL stret;
23+
@property (nonatomic, readonly) Dart_Port dartPort;
2224

2325
- (instancetype)initWithTypeEncoding:(const char *)typeEncodings
2426
callback:(NativeMethodCallback)callback
27+
dartPort:(Dart_Port)dartPort
2528
error:(NSError **)error;
2629
- (IMP)imp;
2730

dart_native/ios/Classes/DNMethodIMP.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ @implementation DNMethodIMP
4040

4141
- (instancetype)initWithTypeEncoding:(const char *)typeEncoding
4242
callback:(NativeMethodCallback)callback
43+
dartPort:(Dart_Port)dartPort
4344
error:(out NSError **)error {
4445
self = [super init];
4546
if (self) {
@@ -54,6 +55,7 @@ - (instancetype)initWithTypeEncoding:(const char *)typeEncoding
5455
strlcpy(_typeEncoding, typeEncoding, length);
5556
_callback = callback;
5657
_thread = NSThread.currentThread;
58+
_dartPort = dartPort;
5759
_signature = [NSMethodSignature signatureWithObjCTypes:_typeEncoding];
5860
}
5961
return self;

dart_native/ios/Classes/DNObjectDealloc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
//
77

88
#import <Foundation/Foundation.h>
9+
#import "dart_api_dl.h"
910

1011
NS_ASSUME_NONNULL_BEGIN
1112

1213
@interface DNObjectDealloc : NSObject
1314

14-
+ (void)attachHost:(NSObject *)host;
15+
+ (void)attachHost:(NSObject *)host dartPort:(Dart_Port)dartPort;
1516

1617
@end
1718

dart_native/ios/Classes/DNObjectDealloc.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,40 @@
3434

3535
@interface DNObjectDealloc ()
3636

37-
@property (nonatomic, weak) NSObject *host;
38-
@property (nonatomic) int64_t hostAddress;
37+
@property (nonatomic, readonly, weak) NSObject *host;
38+
@property (nonatomic, readonly) int64_t hostAddress;
39+
@property (nonatomic, readonly) Dart_Port dartPort;
3940

4041
@end
4142

4243
@implementation DNObjectDealloc
4344

44-
+ (void)attachHost:(NSObject *)host {
45-
if (!host || objc_getAssociatedObject(host, @selector(initWithHost:))) {
45+
+ (void)attachHost:(NSObject *)host
46+
dartPort:(Dart_Port)dartPort {
47+
if (!host || objc_getAssociatedObject(host, @selector(initWithHost:dartPort:))) {
4648
return;
4749
}
4850
if (!_objc_isTaggedPointer((__bridge const void *)(host)) ||
4951
[host isKindOfClass:NSClassFromString(@"__NSMallocBlock")]) {
50-
__unused DNObjectDealloc *dealloc = [[self alloc] initWithHost:host];
52+
__unused DNObjectDealloc *dealloc = [[self alloc] initWithHost:host
53+
dartPort:dartPort];
5154
}
5255
}
5356

54-
- (instancetype)initWithHost:(NSObject *)host {
57+
- (instancetype)initWithHost:(NSObject *)host
58+
dartPort:(Dart_Port)dartPort {
5559
self = [super init];
5660
if (self) {
5761
_host = host;
5862
_hostAddress = (int64_t)host;
63+
_dartPort = dartPort;
5964
objc_setAssociatedObject(host, _cmd, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
6065
}
6166
return self;
6267
}
6368

6469
- (void)dealloc {
65-
NotifyDeallocToDart(_hostAddress);
70+
NotifyDeallocToDart(_hostAddress, _dartPort);
6671
}
6772

6873
@end

dart_native/ios/Classes/native_runtime.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ DN_EXTERN void
2525
native_signature_encoding_list(NSMethodSignature *signature, const char * _Nonnull * _Nonnull typeEncodings);
2626

2727
DN_EXTERN BOOL
28-
native_add_method(id target, SEL selector, char *types, void *callback);
28+
native_add_method(id target, SEL selector, char *types, void *callback, Dart_Port dartPort);
2929

3030
DN_EXTERN char * _Nullable
3131
native_protocol_method_types(Protocol *proto, SEL selector);
@@ -34,13 +34,13 @@ DN_EXTERN Class _Nullable
3434
native_get_class(const char *className, Class superclass);
3535

3636
DN_EXTERN void * _Nullable
37-
native_instance_invoke(id object, SEL selector, NSMethodSignature *signature, dispatch_queue_t queue, void * _Nonnull * _Nullable args, void (^callback)(void *));
37+
native_instance_invoke(id object, SEL selector, NSMethodSignature *signature, dispatch_queue_t queue, void * _Nonnull * _Nullable args, void (^callback)(void *), Dart_Port dartPort);
3838

3939
DN_EXTERN void *
40-
native_block_create(char *types, void *callback);
40+
native_block_create(char *types, void *callback, Dart_Port dartPort);
4141

4242
DN_EXTERN void *
43-
native_block_invoke(void *block, void * _Nonnull * _Nullable args);
43+
native_block_invoke(void *block, void * _Nonnull * _Nullable args, Dart_Port dartPort);
4444

4545
DN_EXTERN const char * _Nonnull * _Nonnull
4646
native_all_type_encodings(void);
@@ -72,7 +72,7 @@ native_convert_nsstring_to_utf16(NSString *string, NSUInteger *length);
7272
#pragma mark - Dart VM API
7373

7474
DN_EXTERN
75-
intptr_t InitDartApiDL(void *data, Dart_Port port);
75+
intptr_t InitDartApiDL(void *data);
7676

7777
#pragma mark - Async Block Callback
7878

@@ -95,7 +95,7 @@ DN_EXTERN
9595
void PassObjectToCUseDynamicLinking(Dart_Handle h, id object);
9696

9797
DN_EXTERN
98-
void NotifyDeallocToDart(intptr_t address);
98+
void NotifyDeallocToDart(intptr_t address, Dart_Port dartPort);
9999

100100
DN_EXTERN
101101
void RegisterDeallocCallback(void (*callback)(intptr_t));

0 commit comments

Comments
 (0)