Giter Site home page Giter Site logo

Comments (3)

evanw avatar evanw commented on May 27, 2024 2

Sure, I'll give the explanation a shot. The emscripten compiler has a strange JavaScript library format (the files passed via '--js-library'). Instead of a library being JavaScript code that is copied into the output like you'd expect, the "library" is actually code that is invoked at compile time by the compiler. From what I understand, the expectation is that the code should mutate the internal global object LibraryManager.library inside the compiler by adding all of your extra JavaScript functions as properties on that object.

All code passed via '--js-library' is called using something like eval() after the LibraryManager.library global is initialized but before the final JavaScript code is generated. After the library code is invoked, the emscripten compiler then presumably looks at all of the properties in LibraryManager.library and decides what JavaScript to emit during the dead code elimination pass. There are also some magical property names that change the behavior of the compiler. For example, if a given function "foo" uses other functions internally, you can add the "foo__deps" property to indicate which functions must also be treated as used if the function "foo" is used.

After I figured this out, it seemed silly to have to contort my code to fit this format and to write out all of the dependencies manually since that can all be figured out automatically assuming you don't use eval(). That's why I wrote this library, which automatically transforms normal JavaScript code into emscripten's special library format. I also added some example C++ code to the readme. Hopefully that helps!

from emscripten-library-generator.

sixman9 avatar sixman9 commented on May 27, 2024

Evan,
Thanks for getting back to me so quickly.

I think there may have been a little ambiguity in my question. The difference might have been not in what you're doing, but _why_ you're doing it. Basically, I couldn't quite grasp why external JavaScript files needed to be referenced from pre-existing, and presumably, unchanged C/C++ code (i.e. C/C++ code with no prior awareness of 3rd party, external, JavaScript). I think I get it now, and what you're actually doing is substituting (linking?) external C/C++-based library artifacts/calls with JavaScript based alternatives. This satisfies my 'why'.

For prosperity, and anyone possibly coming accross this question/answers, I'd like to leave the following links, which I found very helpful:

http://stackoverflow.com/questions/25800382/how-to-write-an-emscripten-shim-for-a-c-library

http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#implement-a-c-api-in-javascript

Assuming I now understand this*:

  1. Might it also be possible to come at this from another angle and to generate the necessary Emscripten library file(s) from the original '.h' header files?
  2. Could this latter approach (from header files) also allow for generation of JavaScript function call API interface skeleton code, ahead of the JavaScript implementation?

*(Thinking about it, simply generating the JavaScript API function calls, then running that through your generator would be enough, right?).

This Emscripten-related project, 'emStructObj' might offer some, forgive the pun, pointers regarding extracting useful [JavaScript] info from .h header files (the project is pure JavaScript, itself).

Cheers

from emscripten-library-generator.

evanw avatar evanw commented on May 27, 2024

I see. The difference in our case is that we're writing an app in emscripten from scratch instead of porting existing code. For example, we have a custom shim for the RegExp object instead of using std::regex to avoid downloading a regular expression library with our app since the browser already has one.

There are also other ways of doing this, such as embind or EM_ASM(), which are listed on the page you found. This library just provides a different way to accomplish the "Using a C API implemented in JavaScript" approach. For your use case of automatically generating an API from headers, it may be easier for you to generate and compile embind code. See http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html for more information.

from emscripten-library-generator.

Related Issues (3)

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.