Skip to content

Commit

Permalink
Do awful things to get at the JSContext with RN 0.58
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Feb 20, 2019
1 parent 36f0f9f commit f6c0e83
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ x.x.x Release notes (yyyy-MM-dd)
=============================================================
### Enhancements
* Added a `_updateSchema` method on an Realm instance to perform schema manipulation. Specifically creating an object schema and a property on an existing object schema. (partly solving [#2216](https://github.com/realm/realm-js/issues/2216))
* Add support for react-native 0.58 ([#2239](https://github.com/realm/realm-js/issues/2239)).

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
Expand Down
15 changes: 14 additions & 1 deletion react-native/ios/RealmReact/RealmReact.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ - (JSContext *)context;
// the part of the RCTCxxBridge private class we care about
@interface RCTBridge (Realm_RCTCxxBridge)
- (JSGlobalContextRef)jsContextRef;
- (void *)runtime;
@end

extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool create) {
Expand Down Expand Up @@ -319,7 +320,19 @@ - (void)setBridge:(RCTBridge *)bridge {
}

_initializeOnJSThread(^{
return [bridge jsContextRef];
// RN < 0.58 has a private method that returns the js context
if ([bridge respondsToSelector:@selector(jsContextRef)]) {
return [bridge jsContextRef];
}
// RN 0.58+ wraps the js context in the jsi abstraction layer,
// which doesn't have any way to obtain the JSGlobalContextRef,
// so engage in some undefined behavior and slurp out the
// member variable
struct RealmJSCRuntime {
virtual ~RealmJSCRuntime() = 0;
JSGlobalContextRef ctx_;
};
return static_cast<RealmJSCRuntime*>(bridge.runtime)->ctx_;
});
} queue:RCTJSThread];
} else { // React Native 0.44 and older
Expand Down
2 changes: 1 addition & 1 deletion src/object-store
13 changes: 7 additions & 6 deletions tests/react-test-app/ios/ReactTests/RealmReactTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@
@interface RCTWebSocketExecutor : NSObject
@end

@interface RCTBridge (Realm_RCTCxxBridge)
- (JSGlobalContextRef)jsContextRef;
@end

extern void JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(JSGlobalContextRef ctx, bool includesNativeCallStack);
extern NSMutableArray *RCTGetModuleClasses(void);

struct RealmJSCRuntime {
void* vtbl;
JSGlobalContextRef ctx;
};

@interface RCTBridge ()
+ (instancetype)currentBridge;
- (void)setUp;
- (void *)runtime;
@end

@interface RealmReactTests : RealmJSTests
Expand Down Expand Up @@ -79,9 +81,8 @@ + (XCTestSuite *)defaultTestSuite {
[bridge reload];
[self waitForNotification:RCTJavaScriptDidLoadNotification];
bridge = [RCTBridge currentBridge];
NSAssert(bridge.jsContextRef, @"No JS context after loading");

JSGlobalContextRef ctx = RCTBridge.currentBridge.jsContextRef;
JSGlobalContextRef ctx = ((struct RealmJSCRuntime *)bridge.runtime)->ctx;
JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(ctx, false);

[bridge.eventDispatcher sendAppEventWithName:@"realm-test-names" body:nil];
Expand Down

0 comments on commit f6c0e83

Please sign in to comment.