Giter Club home page Giter Club logo

webapikit's Introduction

WebAPIKit

🚨 Very experimental 🚨

Type-safe access to DOM and other Web APIs from Swift, using @webref/idl package to generate the bindings and JavaScriptKit to enable access to the actual objects.

webapikit's People

Contributors

j-f1 avatar kateinoigakukun avatar maxdesiatov avatar strega avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webapikit's Issues

Rename to WebAPIKit?

It looks we're going to host not only DOM APIs here, but all Web APIs. Maybe let's rename the library before we've tagged the first release, to avoid the confusion?

Finish support for subscripts

Subscript getters/setters are represented as separate nodes in WebIDL, so we’ll need to do a step to merge them before attempting to print them (since Swift requires that the getter and setter for a subscript be colocated)

https://github.com/swiftwasm/DOMKit/blob/45ed26d277a73ff09fc79fd39d6f59fce39fdffd/Sources/WebIDLToSwift/WebIDL%2BSwiftRepresentation.swift#L370-L381

For a related example, see AsyncOperation, which wraps a single IDLOperation to emit both a JSPromise-returning and an async version of the method.

Also, support for deleters should be added. Swift doesn’t have a concept of a deleter, so I guess the best approach would be to define a removeValue(forKey:) method, just like Dictionary?

Support callback interfaces

These are a Cursed Object in the WebIDL spec (https://webidl.spec.whatwg.org/#idl-callback-interfaces), and they’re essentially shorthand for a union between a function/closure type and an object with a property matching that function type. They are unfortunately important because EventListener is a callback interface (although NodeFilter and XPathNSResolver are also a callback interface, they are less commonly used). This PR depends on #32, probably.

https://github.com/swiftwasm/DOMKit/blob/070f99efca195eaa38f46beeb4dc46ca1d6ce04b/Sources/WebIDLToSwift/IDLBuilder.swift#L82-L88

Multiple library products and union types

Playing with the library a bit more, I think it would be great to split it into separate targets and products, if possible. As an example, people who don't need WebGL don't need to build WebGL wrappers, but I think it still makes sense to provide them.

IDK if WebIDL specifies dependencies between their "modules", so maybe we'd need to hardcode those on our side for things to work?

ShadowRoot append child incorrect

Now if I do this

let shadowRoot = rootView.attachShadow(init: .init(mode: .open, delegatesFocus: true, slotAssignment: .manual))
shadowRoot.ownerDocument?.body?.appendChild(node: childNode)

The shadow root is open correct, but the childNode wound't add to it.
But if I change the second line to

shadowRoot.jsValue.appendChild(node.jsValue)

It would work.

Function appendChild(node: Node) return wrong type

The appendChild(node: Node) for Node return type should be same as the child, not the parent. Now if you use the div element to add a span, it would cause error. I'm currently using a method below instead.

func addNode<T: Node>(_ node: T) -> T {
    let result = self.jsValue.appendChild(node.jsValue)
    return T.init(from: result)!
}

Represent at least some union types as protocols

Currently, WebGL 1.0, WebGL 2.0, and WebGPU can't be split into separate modules as targets, as enum RenderingContext wraps all available context types and needs to have all of them in scope.

If RenderingContext were a protocol, we could declare it in some "base" module and move GPUCanvasContext, WebGLRenderingContext, and WebGL2RenderingContext to their own respective modules adding a conformance on them to that protocol.

I also thought about subclassing, but that clearly won't work due to the lack of multiple class inheritance. These types already inherit from JSBridgedClass, and protocols are the suggested instruments of achieving multiple inheritance in Swift.

Source of specs?

@MaxDesiatov

With Tokamak you'll have to use the Fetch API, and there are no SwiftWasm bindings for it yet, as far as I'm aware. I guess we will have some in the near future as a part of the DOMKit library, but my understanding is that it's not there yet, and is not a part of WebIDL declarations included there, maybe @j-f1 could clarify? Also, bear in mind that fetch returns a Promise object, and the API for it is still being developed in swiftwasm/JavaScriptKit#62. Closure-based APIs in SwiftWasm are not very trivial to work with, as you have to be aware of the lifetime of your closure instances and manually release them. This is an inherent problem caused by the differences in how JavaScript (garbage collection) and Swift (reference counting) allocate memory for closures and their context.

@j-f1

Indeed — fetch is a separate spec that would have to be integrated. I’m not sure how the original maintainers got the .webidl files out of the specs, and I would like to follow the same process for fetch.

@MaxDesiatov

Yes, it would be great to know where the WebIDL specs come from, as I'd like many more APIs covered eventually, IndexedDB, WebGL (WebIDL for this one is maintained together with the human readable text spec apparently), Web Animations obviously, and whatever comes next that users would be interested in.

I’m guessing they were extracted from the .bs files by looking for the <pre class=idl> blocks. @Unkaputtbar @PSchmiedmayer do you have any more information on how this was done?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.