Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate if supporting BlocksRuntime is viable #658

Closed
MaxDesiatov opened this issue Apr 13, 2020 · 6 comments · Fixed by #1000
Closed

Investigate if supporting BlocksRuntime is viable #658

MaxDesiatov opened this issue Apr 13, 2020 · 6 comments · Fixed by #1000

Comments

@MaxDesiatov
Copy link

This seems to be a requirement for Foundation, related to #592 and #597

@MaxDesiatov MaxDesiatov added the in progress The issue is currently being worked on label Apr 16, 2020
@MaxDesiatov
Copy link
Author

The main culprit right now seems to be the _Block_use_RR function, which uses either dlsym on Linux or process API on Windows. Obviously, neither makes sense on WASI, but I'm not sure what to replace it with.

Foundation actually contains some stubs for libdispatch, but a lot of the APIs are block-heavy. Clang automatically adds a symbol reference to _NSConcreteStackBlock from BlocksRuntime to any use of blocks, which then leads to this error during linking:

undefined symbol: _NSConcreteStackBlock

@MaxDesiatov MaxDesiatov removed the in progress The issue is currently being worked on label Apr 16, 2020
@kateinoigakukun
Copy link
Member

@MaxDesiatov Thanks for great investigation!
As far as I saw, it seems _Block_use_RR is only used for Objective-C, so can we avoid to use that or link objc_destructInstance statically?

@MaxDesiatov
Copy link
Author

MaxDesiatov commented Apr 17, 2020

I wasn't able to find a definition of objc_destructInstance anywhere and it was puzzling because what definition is used in Windows builds then? Now I'm not so sure Windows builds use that, although the presence of #if TARGET_OS_WIN32 in Foundation's BlocksRuntime is interesting. Turns out there's a lot of code duplication, there's another copy of BlocksRuntime in libdispatch and yet another one in LLVM's compiler-rt, the latter one being the most clean without any objc_destructInstance.

Maybe the one from compiler-rt is what we could use, but again, the amount of ifdefs needed in CoreFoundation and Foundation to succesfully compile is staggering. I personally am abandoning my attempts to make Foundation work, I don't think that demand for NSArray, NSDictionary will be high enough, and Date, Data and URL can be relatively easily reimplemented from scratch without all that cruft (or one could use the existing implementations from SwiftFoundation).

I think even when we have properly working DCE and LTO, the code size of Foundation and CoreFoundation will still be significant enough in our constrained browser environment, so stuff like DateFormatter and NumberFormatter probably should be bridged to browser implementations via JavaScriptKit to avoid shipping another copy of this functionality reimplemented in C and/or Swift.

The only reason why I wanted to make Foundation work is XCTest, but after having another look at it its use of Foundation seems to be very limited, so I'm currently trying to ifdef that out.

@karwa
Copy link

karwa commented Apr 24, 2020

I wasn't able to find a definition of objc_destructInstance anywhere and it was puzzling because what definition is used in Windows builds then? Now I'm not so sure Windows builds use that, although the presence of #if TARGET_OS_WIN32 in Foundation's BlocksRuntime is interesting.

I‘m 99% sure that part of the Windows port is wrong; objc_destructInstance is an Obj-C runtime method and so will only ever actually be found on Darwin platforms. Did you try just using an #if to remove that line?

@compnerd does this symbol ever resolve to anything on windows?

@MaxDesiatov
Copy link
Author

My current conclusion is that Windows uses the implementation from compiler-rt, but I don't have a direct confirmation of that. Would be fantastic if Saleem explained how that actually works 🙏

@compnerd
Copy link

The blocks runtime in foundation is unused everywhere, the canonical definition of blocks runtime resides in libdispatch and most definitely is 100% correct (its been tested is extensively on non-trivial programs which are in production).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants