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

Wasm Port #154

Closed
MelbourneDeveloper opened this issue Dec 5, 2018 · 16 comments
Closed

Wasm Port #154

MelbourneDeveloper opened this issue Dec 5, 2018 · 16 comments

Comments

@MelbourneDeveloper
Copy link

Please port this library to wasm

@karelbilek
Copy link

karelbilek commented Dec 5, 2018 via email

@reillyeon
Copy link
Collaborator

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
Copy link
Author

@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
Copy link
Collaborator

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
Copy link
Author

@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
Copy link
Collaborator

It absolutely can be done.

@MelbourneDeveloper
Copy link
Author

MelbourneDeveloper commented Dec 5, 2018

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Author

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
Copy link

adriweb commented Dec 5, 2018

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

Correct.

@MelbourneDeveloper
Copy link
Author

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

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

No branches or pull requests

4 participants