-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
requiring a module in nodejs #4032
Comments
This does look more natural for node.js usage. Should be simple to add those exports, we can check at runtime if |
I'm not sure where to start... could you give some direction? |
Sure. For the memory init file, see |
In |
If you write It can be useful to look at What do you mean "to the output file"? If you add |
Got it. Thanks. |
In |
ok, I'll have to learn a bit of python. I'm up to this. |
I got everything setup. console.log({{{ EXPORTED_FUNCTIONS }}}); This is my c test file #include<stdio.h>
__attribute__((used))
int api1() {
return 101;
}
__attribute__((used))
int api2() {
return 102;
}
__attribute__((used))
int api3() {
return 103;
}
node a.out.js
console.log([object Object]);
^^^^^^
SyntaxError: Unexpected identifier How do I tell the preprocessor to give me the array value and not it's type (not it's |
You can use |
I tried that, didn't work. Anyways the solution is simpler then I thought, all I really have to export is the module name: module.exports.{{{ EXPORT_NAME }}} = {{{ EXPORT_NAME }}}; Since you would also need all the emscripten APIs like |
Makes sense, and nice it's not a big change. |
I've made this changed: https://github.com/noamtcohen/emscripten/blob/master/src/shell.js#L115 |
Just add another test at the end of that file. For example, at the end right now (on incoming) is
which will run emcc on "hello_world.c" in |
Alon I'm sorry, just looked at the code again and found this test: https://github.com/kripken/emscripten/tree/master/tests/Module-exports |
I'm sorry for not remembering that... Why then didn't this just work for you? Perhaps all we need to do is improve the documentation? |
There are a few ways to export an object in node: module.export.obj = {}
// I would then require in the using file:
var m = require("./the-file.js").obj;
/* or */
module.export = {}
// And then:
var m = require("./the-file.js"); The first one gives you more flexibility as to what you are exporting and this is what I was looking for. |
I see, thanks. Do you want to submit a PR to update the docs, perhaps? Something in the "interacting with code" page might make sense. |
Sure, where are the docs? this is what I found but where is the content? https://github.com/kripken/emscripten/blob/42fb486c53d627b203b77af6e5d0c088c0ad03ad/site/source/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.rst#id2 |
Yes, that link is to the content. It is built using Sphinx into the documentation on the emscripten.org site. A PR with additions to that file is exactly what we need, and yes, to incoming. |
Looks nice! Do you want to open a pull request to the |
I'm glad you like it. I have to ask, I committed this to the |
What you committed sounds right: First you commit to your |
I messed-up the repo and couldn't understand how to clean it up so I just re-forked and created a new pull request: #4059 Regarding you question about out of context: |
Ok, pull landed so I think we can close this. |
longest 40 lines I ever wrote... |
Heh, I hope that wasn't too annoying for you. Sometimes it takes a while to figure out what to do, and the proper thing in the end is small :) |
It wasn't annoying at all, I had fun. Thanks! |
is the node exporting (via module.exports) supposed to work in conjunction with MODULARIZE=1 ? |
@sijakret Which version of emscripten are you using? That sounds like how it used to be, but it's been fixed for a while. |
@curiousdannii |
@sijakret That's over two years old. There's been like 60 releases since then. You need to update emscripten :) |
omg, this is so stupid.. Got a recent version of the portable SDK and the update + wasm build worked right away. |
Apologies for this - in the history there was one update of |
Hi,
I would like to load a module and use it from nodejs.
Lets say I comile a c source file like this:
What I do now in
module.js
I useeval
to load the script and export the Emscripten Module:My
main.js
file looks like this:A better way would be to require the emscripten output file:
And that means that I have to give emcc a pre-js that would like this:
And I think the prejs could be generate by emcc since it mirrors the
EXPORTED_FUNCTIONS
flag, and I'm thinking maybe to add a flag to emcc like--node-module 1
that would tell emcc to emit this pre js.Now lets say I invoke emcc like this:
If I want to use a memory init file, emscripten should be able to load this file via NODEFS.
And lastly if I want to MODULARIZE then emcc should generate this pre js:
So then when I want to use the module I would do this:
The text was updated successfully, but these errors were encountered: