You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Successful cloning a repo using libgit2 compiled to JavaScript with Emscripten #4376
But you'd rather prefer a full transport? Currently the http transport is used and works fine - just had to disable some lines of code. E.g. the http parser is not needed with emscripten as we already have one in the browser. Maybe better to introduce a configuration switch to turn off the http parser?
So the reason why there are those http issues is because the browser already performs some parsing?
What happens if the same emscripten compiled output is run inside node, does it has the same problems? I intend to run libgit2 via emscripten on node primarily.
Also emscripten makes it that munmap will msync the mapped area, before deallocating it (it was allocated). This doesn't appear to be part of the standard. It doesn't seem possible for libgit2 to emulate MAP_SHARED because the call has to return an address back into the heap, and there's no way to intercept pointer dereferencing into the heap to make it actually address some page on disk, as this other guy said (https://groups.google.com/forum/#!topic/emscripten-discuss/63jY4CTbL6k) you would need to emulate your own memory management within libgit2. The end result is to enforce that all platforms running libgit2 needs to have some sort of mmap, or that to replace all uses of mmap with the standard read and write calls. Alternatively, one could also always use msync after every read/write into the mmaped area.
I tried to figure out what would be the quickest way to create a soft-MMU for emscripten, and my experiments lead me to create https://github.com/MatrixAI/js-permaproxy It isn't impossible to replace/extend Emscripten's heap architecture with a soft-MMU based on ES6 proxies on Uint8Arrays that would properly emulate MAP_SHARED, but proxies are not very performant, I think possibly 2 orders of magnitude slower on my Chrome browser. There's also the issue that Uint8Arrays are just views, while ArrayBuffer is the actual data, and there's no way to Proxy mutations to ArrayBuffer. I think instead one should build a single "VirtualMemory" construct based on DataView instead, and rework all of the typed array accesses to be based on a single DataView. The best reference to do this fast would be QEMU's emulation (which has address translation buffers). But this is going to be a big task, and I'm not prepared to dive into this.
Did some adjustments and added a build option so that it shouldn't interfere with existing code/other target platforms. For now the NO_MMAP is not usable, so leaving my hack in there - but at least it's only active when using the emscripten build option.
Hope you can use this @ethomson , and let me know if further adjustments are required.
Ideally I would like to use a node git library that does not have any native components, because this makes it more maintainable. Right now the latest nodegit from npm doesn't build against node 12. Plus, since it requires recompiling, it makes deployment a little trickier. At work I'm trying to webpack tools into a single JS file with a shebang, paired with a statically linked node binary, but I can't do that when nodegit is in the mix.
Could nodegit use this precompiled WASM binary of libgit2 under the hood?
Not out of the box, you'd have to change the exposed API of the wasm library to match that of nodegit, or at least the functions your application is using.
Activity
petersalomonsen commentedon Nov 8, 2017
What would be required in order to merge the mentioned edits back to the upstream libgit2 repository?
ethomson commentedon Nov 8, 2017
Speaking generally, I'm interested in having emscripten support. I think that would be neat.
As is noted in emscripten-core/emscripten#4902, that needs to be cleaned up. eg:
cmake -DTHREADSAFE=OFF).NO_MMAP)petersalomonsen commentedon Nov 8, 2017
Thanks for the pointers @ethomson
Will have a look at it and see what I can do.
Regarding 3, I changed to registering a tls stream for emscripten like this:
git_stream_register_tls(git_open_emscripten_stream);But you'd rather prefer a full transport? Currently the http transport is used and works fine - just had to disable some lines of code. E.g. the http parser is not needed with emscripten as we already have one in the browser. Maybe better to introduce a configuration switch to turn off the http parser?
CMCDragonkai commentedon Nov 8, 2017
So the reason why there are those http issues is because the browser already performs some parsing?
What happens if the same emscripten compiled output is run inside node, does it has the same problems? I intend to run libgit2 via emscripten on node primarily.
CMCDragonkai commentedon Nov 8, 2017
@ethomson The mmap emulation denies the use of
MAP_SHAREDlibgit2/src/posix.c
Lines 245 to 248 in 28c8950
Is there any part of libgit2 that relies on
MAP_SHARED? A quick search over the codebase shows the indexer and fileops usingMAP_SHARED. By usingNO_MMAP, does this result in problems? (https://github.com/libgit2/libgit2/search?utf8=%E2%9C%93&q=GIT_MAP_SHARED&type=)I just discovered other people who have similar problems with mmap on libgit2:
Also emscripten makes it that
munmapwill msync the mapped area, before deallocating it (it was allocated). This doesn't appear to be part of the standard. It doesn't seem possible for libgit2 to emulateMAP_SHAREDbecause the call has to return an address back into the heap, and there's no way to intercept pointer dereferencing into the heap to make it actually address some page on disk, as this other guy said (https://groups.google.com/forum/#!topic/emscripten-discuss/63jY4CTbL6k) you would need to emulate your own memory management within libgit2. The end result is to enforce that all platforms running libgit2 needs to have some sort of mmap, or that to replace all uses of mmap with the standard read and write calls. Alternatively, one could also always use msync after every read/write into the mmaped area.petersalomonsen commentedon Nov 8, 2017
@CMCDragonkai Node also has a builtin http parser ( https://nodejs.org/api/http.html ) so you would also want to bypass the libgit2 http parser when running in Node. Would have to adjust to the Node http API, but the concept is more or less the same as in the browser.
petersalomonsen commentedon Nov 9, 2017
ERROR 2: trying to map shared-writeable:@CMCDragonkai @ethomson @vadimkantorov
CMCDragonkai commentedon Nov 10, 2017
I tried to figure out what would be the quickest way to create a soft-MMU for emscripten, and my experiments lead me to create https://github.com/MatrixAI/js-permaproxy It isn't impossible to replace/extend Emscripten's heap architecture with a soft-MMU based on ES6 proxies on Uint8Arrays that would properly emulate
MAP_SHARED, but proxies are not very performant, I think possibly 2 orders of magnitude slower on my Chrome browser. There's also the issue that Uint8Arrays are just views, while ArrayBuffer is the actual data, and there's no way to Proxy mutations to ArrayBuffer. I think instead one should build a single "VirtualMemory" construct based on DataView instead, and rework all of the typed array accesses to be based on a single DataView. The best reference to do this fast would be QEMU's emulation (which has address translation buffers). But this is going to be a big task, and I'm not prepared to dive into this.petersalomonsen commentedon Nov 10, 2017
Did some adjustments and added a build option so that it shouldn't interfere with existing code/other target platforms. For now the NO_MMAP is not usable, so leaving my hack in there - but at least it's only active when using the emscripten build option.
Hope you can use this @ethomson , and let me know if further adjustments are required.
See pull request here:
#4400
petersalomonsen commentedon Mar 21, 2018
Now using webassembly and webworkers. A simple video demo can be seen here: https://youtu.be/rcBluzpUWE4
Also started initial work on a simple file-explorer frontend: https://github.com/fintechneo/angular-git-filebrowser
(which is the same as shown in the video)
vadimkantorov commentedon Mar 21, 2018
With a proxy to bypass CORS?
petersalomonsen commentedon Mar 21, 2018
@vadimkantorov Yes there's a proxy in that file-explorer project, so you can test it directly from there.
petersalomonsen commentedon Mar 22, 2018
Demo of creating files and folders and pushing to github: https://youtu.be/qnCucFguDwE
sbeaupre commentedon Jun 10, 2018
@ethomson did you have time to evaluate the PR from @petersalomonsen above? I see that some CI checks have failed...
petersalomonsen commentedon Mar 11, 2019
@CMCDragonkai I've added a http transport for nodejs now. Example of cloning a repo is here:
https://github.com/fintechneo/libgit2/blob/emscripten_nodejs_http_transport/emscripten_hacks/jstests/nodejsclone.js
The stream code is here:
https://github.com/fintechneo/libgit2/blob/emscripten_nodejs_http_transport/src/streams/emscripten_nodejs.c
I'm creating a child process for the http request with
spawnSyncto be able to make the request synchronously.Will merge this into my pull request soon (after some more testing)
petersalomonsen commentedon Mar 11, 2019
@sbeaupre The CI checks pass just fine now: #4400 ( has been working for a while now ).
petersalomonsen commentedon Jul 1, 2019
Published an npm package with webassembly binaries of libgit2 today:
https://www.npmjs.com/package/libgit2.js
Exposed functions are limited to what can be found here: https://github.com/fintechneo/libgit2/blob/master/emscripten_hacks/jslib.c
cspotcode commentedon Aug 3, 2019
Can this be combined with nodegit?
Ideally I would like to use a node git library that does not have any native components, because this makes it more maintainable. Right now the latest nodegit from npm doesn't build against node 12. Plus, since it requires recompiling, it makes deployment a little trickier. At work I'm trying to webpack tools into a single JS file with a shebang, paired with a statically linked node binary, but I can't do that when nodegit is in the mix.
Could nodegit use this precompiled WASM binary of libgit2 under the hood?
petersalomonsen commentedon Aug 3, 2019
Not out of the box, you'd have to change the exposed API of the wasm library to match that of nodegit, or at least the functions your application is using.
That said, if you know a little C, should be easy to tweak the API exposed to JS in this file https://github.com/fintechneo/libgit2/blob/master/emscripten_hacks/jslib.c to match your application. See the methods marked with
EMSCRIPTEN_KEEPALIVEwhich is the way you can expose C methods to JS.ethomson commentedon Apr 1, 2020
wasm-git is the answer. Thanks, @petersalomonsen!