-
Notifications
You must be signed in to change notification settings - Fork 677
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
Feature request: Support __attribute__((constructor)) for easy module loading #1722
Comments
@gabrielschulhof What is your use case (hardware, OS, toolchain, program)? Jerry-libc is a very limited/restricted libc variant. Are you really using it instead of the compiler's default libc? (I'm not saying you shouldn't, but you have to know/accept that jerry-libc is really just the bare minimum, at least for now.) |
The use case is simply to make mixing and matching different modules easy. That is, adding a new module should be as simple as adding a few source files to your project, without having to have those source files exports some well known entry point, and without having to modify your Consider the following example. You make one small change to your Makefile: ...
diff a/Makefile b/Makefile
+obj-$(MY_COOL_MODULE) = my_cool_module.c
... and now, without any further code changes, your project supports var my_cool_module = require( "my_cool_module" ); Without this feature, you would need ...
diff a/Makefile b/Makefile
+obj-$(MY_COOL_MODULE) = my_cool_module.c
...
diff a/main.c b/main.c
+#ifdef BUILD_WITH_MY_COOL_MODULE
+#include "my_cool_module.h"
+#endif /* def BUILD_WITH_MY_COOL_MODULE */
...
+#ifdef BUILD_WITH_MY_COOL_MODULE
+my_cool_module_init();
+#endif /* def BUILD_WITH_MY_COOL_MODULE */
... which is a lot harder to maintain. Also, Node.js uses constructors to start the process of loading modules, so this would increase interoperability with Node.js native addons - a project I'm currently working on 😀 I'm fairly certain that I'm using jerry-libc when the constructor doesn't work, because if I turn off I understand that jerry-libc is the bare minimum, which is why I would like to discuss the cost/benefit of adding this feature. The fact is that constructors are something that cannot be implemented by writing a few functions, because people (including me) would need to know assembly to implement that support, whereas features that can be expressed as a set of functions, even if they should go into a C library, need not go in if it is to be minimal, because they can be maintained outside - because they're just plain C code, and a lot more people know how to read/write/maintain that. And yes, the above example is also C code, so yes, it could be maintained without constructors, but it's all boilerplate which could be avoided with constructors. Besides, constructors are supported by quite a few platforms - all the ones Node.js runs on, at any rate. |
Question: could this be done in a way that the linker can cut the support when it is not used? At least the majority of it. If yes, I don't mind having this feature. |
@zherczeg I don't think so. But it could be a compile time option for jerry-libc whether to support constructors or not. (BTW, it ctors are supported, it's nice to support destructors, too, as they walk hand-in-hand, quite often.) |
@gabrielschulhof I get the usefulness of constructors. Where I'd like to pick your mind is why you'd like to use jerry-libc? Especially as you are mentioning Node.js native addons. Because of the minimalistic implementation of jerry-libc, the lack of constructors will be the least of your problems, I'd guess. You have been warned :) |
Actually, after module registration, things are pretty straight forward, because I'm implementing the new Node.js N-API which maps ECMAScript pretty closely. The only challenges are scopes (which I have implemented as linked lists), and externals (a primitive type not part of ES, but which both N-API-supporting engines (V8 and ChakraCore) expose in order to make passing pointers into JS easier than having to wrap objects). |
My ultimate goal is to use N-API to make it possible to build Node.js modules on JerryScript without any code changes. So, like, the same C-file will build against Node.js and against JerryScript. This could be good for both projects, because it would make Node.js addons more memory-consumption-conscious, and it would bring more addons to JerryScript. Of course it's all one big experiment 😃 |
@gabrielschulhof There are two major issues I see:
That being said, constructors (and destructors) can be supported by jerry-libc, most probably. I'll try to come up with an implementation in the coming days. |
@gabrielschulhof, have you tried to build with |
@akosthekiss I understand that JerryScript is a JS engine and not a full runtime. The fact though is that by its very nature of being designed for constrained environments it provides some features of a runtime - such as a C library. Also, don't forget that I'm not talking about Node.js C++ modules linked directly against V8. I'm talking about the new N-API that has just landed in Node.js master, which is a C interface that seeks to be independent of the underlying JS engine and tracking ES itself. We have already ported many of the most popular Node.js modules which were using the V8 API to N-API so that we can validate its function, performance, and completeness, and now I'm trying to see if it's possible to provide JerryScript's API as an implementation of N-API. @LaszloLango I can certainly build with a different C library. In fact, until a few days ago I didn't even know that the system C library will run constructors even if they are linked into an executable :) I thought constructors only work with |
@gabrielschulhof have a look at #1725, see whether it works for you. (BTW, one more potential issue came to my mind: I'm not sure how you'd like to register modules but note that a lot of jerry API functions cannot be called until |
@akosthekiss Aaaah, I'm glad you brought that up :) The module loading is broken into two steps. Indices into the pointer array are assigned at boot time in response to the constructor call. The module name is also added to a linked list for subsequent lookup by Only when This is in contrast to Node.js, where bootup and initialization are one and the same. But then in Node.js every module gets |
Closing as #1725 has landed |
Related to #1717 would it be possible to implement support for constructors (
__attribute__((constructor))
) in jerry's C library? I think that's where it needs to be implemented, in thejerry-asm.S
file.Having support for constructors makes it easy to register modules independently of the main program, since all you have to do is compile in a module and it will be available at runtime without any additional code.
The text was updated successfully, but these errors were encountered: