feat: support native ES classes with lazy registration, including static usage before construction#403
feat: support native ES classes with lazy registration, including static usage before construction#403NathanWalker wants to merge 2 commits into
Conversation
…tic usage before construction
Plain ES classes extending native types (class JSClass extends NSObject {})
now register their Objective-C subclass lazily on first native use, without
requiring the @nativeclass decorator or ES5 downleveling. Registration is
triggered not only by construction (new/new.target) but by any static touch:
JSClass.alloc().init(), JSClass.new(), inherited static methods and
properties, and passing JSClass directly to native APIs expecting Class or
id arguments. A global no-op NativeClass keeps existing decorated code
working unchanged.
|
Warning Review limit reached
Next review available in: 10 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR adds support for plain ES ChangesES class native interop support
Estimated code review effort: 4 (Complex) | ~60 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| var object = new ESSimpleObject(); | ||
| expect(object.constructor).toBe(ESSimpleObject); | ||
| expect(object instanceof ESSimpleObject).toBe(true); | ||
| expect(object instanceof TNSDerivedInterface).toBe(true); | ||
| expect(object instanceof NSObject).toBe(true); |
There was a problem hiding this comment.
does this call alloc().init()? I'm not sure about the implications of calling new Something() when the native object might have different ideas for initalization.
would ESSimpleObject.alloc().init() call the constructor? what about the constructor arguments?
There was a problem hiding this comment.
Yes in the default case. super() lands in the exact same ArgConverter::ConstructObject path that every NativeScript construction (new NSObject(), legacy .extend() classes) has always used. The only new behavior is that new.target resolution makes it alloc the derived ObjC class instead of the base. The dispatch inside ConstructObject is:
if (result == nil && interfaceMeta != nullptr && info.Length() > 0) {
std::vector<Local<Value>> args;
const MethodMeta* initializer =
ArgConverter::FindInitializer(context, klass, interfaceMeta, info, args);
result = [klass alloc];
V8VectorArgs vectorArgs(args);
result = Interop::CallInitializer(context, initializer, result, klass, vectorArgs);
}
if (result == nil) {
result = [[klass alloc] init];
}
So for a native class with "different ideas about initialization," the existing constructor-to-initializer matching still applies: super() with no arguments is literally [[DerivedClass alloc] init], while super(args...) runs FindInitializer, which matches the arguments against the class's initWith… selectors from metadata and calls the matched designated initializer. The key detail is that what reaches the native initializer is whatever you pass to super(...), not what's passed to new. The JS constructor body is in control, same as any ES class. If a native base has no usable zero-arg init, the author passes matching args to super(...) or uses the explicit alloc().initWithX(...) pattern, exactly as before.
alloc() triggers lazy registration (creating the ObjC class and installing method/accessor overrides; a metadata operation only) and returns an uninitialized instance; .init() is then an ordinary message send.
There was a problem hiding this comment.
Pushed up 2 additional test cases that hopefully clarify that: 75a9934
Makes plain ES2015+ classes that extend native types work directly on iOS, without the
@NativeClassdecorator or ES5 downleveling:Construction is not the only way a class first crosses into native code. All of the following now trigger lazy registration, before any instance has ever been created:
See tests in
TestRunner/app/tests/Inheritance/ESClassTests.js.Summary by CodeRabbit
New Features
classinheritance with native types.newandalloc().init().supercalls.NativeClasshelper for class decoration and protocol configuration.Bug Fixes