jprendes / emception

Run Emscripten in the browser
Other
285 stars 35 forks source link

[Feature Request] Make/export Emception as a library. #7

Open james-pre opened 1 year ago

james-pre commented 1 year ago

This is a really cool project, though it's difficult to use a part of a project due to the lengthy build and compilation. Could we have Emception as a library?

For example (on a website):

<script src="emception.min.js"></script>
<script>
  let emception = new Emception();
  emception.run('echo "check out this!"').then(stdout => {
    //do stuff
  }).catch(stderr => {
    //do stuff
  });

</script>

A few things to note:

Why do we need a library?

james-pre commented 1 year ago

@jprendes What do you think? If I can fix #8, I can try to do this myself.

jprendes commented 1 year ago

Definitely! PR's are welcome! You can find pre-built artifacts in the 'demo' branch.

cesare-montresor commented 1 year ago

@jprendes @dr-vortex Are there any updates on making this awesome project easy to integrate?

I'm asking because I'm currently developing an educational coding app for learning Algorithms and Complexity: LIVE DEMO: https://talco-team.github.io/TALightDesktop/

It's almost at RC1, at the moment I'm finishing off version one, using dyodide, which so far, it's working pretty well.

I really would like to add support for C/C++ once this project becomes stable. Emception is probably the most mature result I've seen out there, however for the time being, I'm having issues just compiling it on my local machine, with or without docker, (same issues as described in https://github.com/jprendes/emception/issues/11)

james-pre commented 1 year ago

@cesare-montresor JSCPP is really cool: https://github.com/felixhao28/JSCPP

cesare-montresor commented 1 year ago

@cesare-montresor JSCPP is really cool: https://github.com/felixhao28/JSCPP

I like that project, but I like the idea of having gcc

alienself commented 1 year ago

@cesare-montresor were you able to build it? I am also unable to create a build because of python errors:

The necessary bits to build these optional modules were not found:
_bz2                  _ctypes               _ctypes_test
_curses               _curses_panel         _dbm
_gdbm                 _hashlib              _lzma
_ssl                  _tkinter              _uuid
readline
To find the necessary bits, look in configure.ac and config.log.

Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

configure: error: invalid or missing build python binary
make: make -j32
make: *** No targets specified and no makefile found.  Stop.
cesare-montresor commented 1 year ago

@cesare-montresor were you able to build it? I am also unable to create a build because of python errors:

The necessary bits to build these optional modules were not found:
_bz2                  _ctypes               _ctypes_test
_curses               _curses_panel         _dbm
_gdbm                 _hashlib              _lzma
_ssl                  _tkinter              _uuid
readline
To find the necessary bits, look in configure.ac and config.log.

Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

configure: error: invalid or missing build python binary
make: make -j32
make: *** No targets specified and no makefile found.  Stop.

Yes, same issue, probably the docker machine needs an update.

alienself commented 1 year ago

@cesare-montresor did you come across a similar project that works with emscripten? I wish we could have something official from the emscripten folks or at least something more stable with an easy build step. Be able to compile C++ directly in the browser would definitely be a great step forward

@jprendes any chance to get a fix?

james-pre commented 1 year ago

@alienself @cesare-montresor

I can't remember where I saw it, but the python errors have to do with the newer version of pyodide being used when we try to build being different from the one @jprendes used. A hardcoded commit id in the pyodide clone/checkout should fix it.

jcbhmr commented 1 year ago

Here's some of my un-educated ideas for how an API might be modeled after some other existing "similarish" APIs:

Here's something like Node.js' node:vm API:

import { CPPSourceTextModule, CPPSTLLinker } from "./emception.js";

const module = new CPPSourceTextModule(String.raw`
#include <string>
#include <iostream>

int main() {
  std::string message = "Hello world!";
  std::cout << message << "\n";
}
`);
await module.link(CPPSTLLinker);
await module.evaluate();
// Any exports (if isn't main module)
console.log(module.namespace);

Or, maybe something more like the current WASM pipeline:

import { Emception } from "./emception.js";

const module = await Emception.compileStreaming(fetch("./index.cpp"));
// Somehow fetch all required functions...?
console.log(Emception.Module.imports(module));
const instance = await Emception.instantiate(module, { ... });
const exitCode = instance.exports.main(1, ["/hello"]);

Then again, I think this is a bit too low-level for the kind of abstract C++ compilation that most users want. Something higher-level is probably desired.

Or maybe something like what Pyodide does (pyodide.runPython() or https://pyodide.org/en/stable/usage/loading-custom-python-code.html)

import { unpackArchive, cppimport, runCPP } from "./emception.js";

await runCPP(`
#include <string>
#include <iostream>

int main() {
  std::string message = "Hello world!";
  std::cout << message << "\n";
}
`);

const response = await fetch("https://example.org/your_package.tar.gz");
const buffer = await response.arrayBuffer();
await unpackArchive(buffer);
cppimport("your_package");

Then again, you could implement them all if you really wanted to! 😉 These are just brainstorming ideas that I had and wanted to share.

james-pre commented 2 months ago

... @dr-vortex Are there any updates on making this awesome project easy to integrate?

@cesare-montresor I've updated my GitHub username from @dr-vortex to @james-pre. Please update your comment, and I can delete this one. I'm writing this comment when I can't make the change myself.