WICG / webusb

Connecting hardware to the web.
https://wicg.github.io/webusb/
Other
1.32k stars 132 forks source link

Wasm Port #154

Closed MelbourneDeveloper closed 5 years ago

MelbourneDeveloper commented 5 years ago

Please port this library to wasm

karelbilek commented 5 years ago

...webusb is not a library, it's a browser API, currently supported by just Chrome

If you mean accessing WebUSB API from webassembly, not sure if it is a good idea, but I don't really understand webassembly that deeply

On Wed, Dec 5, 2018, 2:41 PM Christian Findlay <notifications@github.com wrote:

Please port this library to wasm

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/WICG/webusb/issues/154, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZ8dAOjqxnvTFkC2Hk969woteVQgU5ks5u13iYgaJpZM4ZCKtq .

reillyeon commented 5 years ago

Web Assembly does not currently provide direct access to Web APIs. Rather, calls need to be proxied through pure JavaScript functions provided to the Web Assembly module. In this way you can use the WebUSB API from inside Web Assembly.

This makes sense if you are porting an application that uses, for example, libusb to talk to devices. You can create a shim version of the libusb interface that calls out to WebUSB instead.

Closing this issue as this is already possible and is out of scope for the specification.

MelbourneDeveloper commented 5 years ago

@reillyeon , can you please elaborate a little further on what you just said.

if you are porting an application that uses, for example, libusb to talk to devices. You can create a shim version of the libusb interface that calls out to WebUSB instead

Closing this issue as this is already possible and is out of scope for the specification.

What do you mean by a shim version?

reillyeon commented 5 years ago

Either an actual copy of libusb with a new WebUSB "operating system" backend which calls out from WebAssembly to JavaScript or a new library with the same API as libusb which does the same. This is how all access to web APIs works from WebAssembly.

You don't need to make the interface look like libusb. That's just a convenience for porting existing code that is written against that library. If you just want to use WebUSB from WebAssembly all you need is to provide those pure JavaScript functions that can be called from WebAssembly in order to access it. Eventually this won't be needed but a general mapping between WebAssembly and web APIs exposed to JavaScript has not yet been developed.

MelbourneDeveloper commented 5 years ago

@reillyeon yes, the idea would be to compile libusb with something like Emscripten. https://github.com/kripken/emscripten

Are you saying that there's no theoretical reason why this couldn't be done?

reillyeon commented 5 years ago

It absolutely can be done.

MelbourneDeveloper commented 5 years ago

I guess part of what I am asking is, what is special about WebUSB that means that the JavaScript sandbox says "Yep, OK, you can talk to a USB device"?

I take it, nothing?

reillyeon commented 5 years ago

That question doesn't really make sense. WebUSB is a set of functions/objects that allow you to request access to and interact with a USB device. It is the same as an object like XMLHttpRequest that lets you make an HTTP request. It's just functionality provided by the browser for use by JavaScript code.

MelbourneDeveloper commented 5 years ago

Sure. And, I think that if I understand you correctly, a C library like libusb could be compiled to wasm with Emscripten to achieve basically the same thing as WebUSB. Is my interpretation of what you are saying correct, or am I just getting confused?

MelbourneDeveloper commented 5 years ago

The reason I'm trying to confirm is that a lot of people assume that there's some kind of wall that explicitly blocks JavaScript or Wasm from talking to a USB device. And magically, WebUSB just opens up a hole in that wall so that access can be achieved.

adriweb commented 5 years ago

Sure. And, I think that if I understand you correctly, a C library like libusb could be compiled to wasm with Emscripten to achieve basically the same thing as WebUSB. Is my interpretation of what you are saying correct, or am I just getting confused?

No, a wasm-compiled libusb that calls (through JS) the WebUSB APIs as its "backend", because it still has to access USB layers somehow.

Possibly more or less like https://github.com/Batov/libusb_chrome_api/tree/master/chrome_app, but with the actual WebUSB APIs used instead of the chrome.usb things if I'm reading correctly.

MelbourneDeveloper commented 5 years ago

That's what I mean. So, WebUSB is the only possible access to USB. Any library that wants to talk to a device - whether it be JavaScript or wasm, needs to use WebUSB, and the Chrome browser will never allow anything other than WebUSB?

reillyeon commented 5 years ago

The reason this question doesn't make sense is that without WebUSB the concept of a USB device does not exist for JavaScript or wasm. Your code could not say "I want to access this USB device" and the browser would say, "no." The question simply could not be asked because the words did not exist in the language until the WebUSB API was defined.

MelbourneDeveloper commented 5 years ago

I think I understand what you are saying. There's nothing lower than WebUSB in the JavaScript/wasm runtime that would allow any library to talk to a USB device.

reillyeon commented 5 years ago

Correct.

MelbourneDeveloper commented 5 years ago

But, following on from your earlier point, I library could be compiled for wasm which routes to WebUSB.