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

WIP: Support for Embedded Swift (v2) #263

Merged
merged 9 commits into from
Oct 14, 2024

Conversation

sliemeobn
Copy link
Contributor

thanks to @MihaelIsaev for starting the other branch, I was inspired to try on my own by it.

I left String untouched (the entire JS runtime really), but I had to move a few things out of the C lib (ideally we can get rid of the entire thing eventually).

Also, the C header bit field with stuff does not seem to play well with Embedded (do not know why, pushed it into Swift for now)

there is an Examples/Embedded demo that shows how it wants to be built.

my hope is that, one day:

  1. SwiftPM learns to set the flags on its own with a dedicated "Embedded" mode
  2. variadic generics will be supported

for now, a single unsafe flag needs to be added in the package.swift, and bundle resources do not work (SwiftPM will generate code for Foundation bundles).

In general, JavaScriptKit could use a bit of modernization and replace of all the "anys" with variadic generics.
Also, I don't see the value of using class hierarchies (does not play well with embedded) - but I tried to leave as much untouched as possible.

Promises, throwing functions, and typed arrays are left unsupported entirely for now, the rest should work with almost no API surface change.

@sliemeobn sliemeobn changed the title Support for Embedded Swift (v2) WIP: Support for Embedded Swift (v2) Oct 8, 2024
@sliemeobn
Copy link
Contributor Author

sliemeobn commented Oct 8, 2024

also @MihaelIsaev, I think your EmbeddedFoundation package is a great idea and a great start.
for whatever reason I ended up needing strlen as well, and memmove is needed to get string interpolation going.

do you think you could add these from the wasi/musl libc as well, and add proper license attribution to your package?
also, IMHO the name "Foundation" is very confusing. maybe we can come up with a better name?


// C bit fields seem to not work with Embedded
// in "normal mode" this is defined as a C struct
private struct JavaScriptValueKindAndFlags {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to unconditionally use regular bit masking instead of bit fields even for non-Embedded mode for portability. Could you extract this refactoring as a separate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to clarify, do you mean to use the swift-based "decoding" as I wrote here for embedded in general? (ie: just pass an "unsigned short" through the C shim?)

Sources/JavaScriptKit/FundamentalObjects/JSSymbol.swift Outdated Show resolved Hide resolved
@@ -234,3 +235,20 @@ func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {
JSClosure.sharedClosures[hostFuncRef] = nil
}
#endif

#if hasFeature(Embedded)
// cdecls currently don't work in embedded, and expose for wasm only works >=6.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason using cdecl instead of silgen_name here, so I think we can use silgen_name and call those functions with Swift calling convention. I'll address this after merging your change, so don't care about this comment, just a note for me.

@kateinoigakukun
Copy link
Member

I think it's a much mergeable approach. Happy to merge once CI passed and a few my concerns are resolved. Thank you for tackling on this!

@sliemeobn
Copy link
Contributor Author

thanks for reviewing, I'll try address the comments later today, they all makes sense.

the biggest issue with 5.10 compatibility I have right now is that the swift compiler seems to freak out about @_expose(wasm) even when it is #ifed away.

I'll try my luck with @_used on the cdecls later, maybe we stick to the C shims for exposing to wasm untouched for now...

@sliemeobn
Copy link
Contributor Author

@kateinoigakukun with CI passing, are there any issues left you'd want me to address before considering a merge?

@kateinoigakukun
Copy link
Member

Sorry I just missed the CI status update

@kateinoigakukun kateinoigakukun merged commit 459d1e9 into swiftwasm:main Oct 14, 2024
16 checks passed
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 this pull request may close these issues.

2 participants