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
[TIMOB-7568] CommonJS in Core #1373
Conversation
Added support for adding CommonJS modules to the core.
… for consistency with surrounding code.
[m setHost:self]; | ||
[modules setObject:m forKey:name]; | ||
[m release]; | ||
if (![m isJSModule]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm starting to wonder if we should refactor this so that all of moduleNamed goes straight to Require, do not pass go, do not collect $200. For one, the krollbridge require handles path-based module names while this version does not.
This is something we could revisit later, if needed. Something doesn't smell right about this, but I can't put my finger on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does require that path
always be toplevel, but I think that's expected for pre-compiled JS modules. The real question is whether or not this is intended to be used with pure JS modules that aren't precompiled into binary format - @dawsontoth can you give some input on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my intention, at least for this pass at solving the problem. Long term, I think it would be great to not have to precompile them to binary. It would be even greater if we could have a single common location for pure JS modules to be loaded by both Android and iOS. But for this pass, it's not necessary at all. Does that answer your question, @sptramer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure does.
I have a few reservations of this implementation, as it seems we're playing fast and loose here. |
Yup, I agree. It's also kind of an odd way to include a module... Can you enumerate any serious consequences for this, short term? What do you see as a more long term solution? It would be nice if there was a common place to dump a CommonJS module, and then both platforms could pick it up and do with it what they want. |
Code reviewed. This looks like an acceptable (for now) implementation for loading compiled CommonJS modules into the Titanium namespace, but we really do need to revisit module inclusion and especially require() infrastructure in the near future. Some minor changes do need to be made though. |
When you say "minor changes" do you mean the KrollObject casting issue you and Blain commented on? |
Yup. Avoid the cast and change the return type to |
By changing the return type to id instead of KrollObject*, the module doesn't need to be cast as the latter.
Updated. |
Code reviewed; APPROVED. Will wait for @BlainHamon to CR+FR. |
I can't put my finger on it, and I think we should streamline the require mechanism in general in the future. Having two completely different points in code where JS can be loaded in seems redundant at best. Also, I could have sworn addModule had a more important position earlier that required it to be a module. As it is, you're correct in that it's either discarded or immediately fed into a general purpose conversion. CR Accepted |
So far, FR failed, going to look into what's going on. |
Might be due to library not getting added to all targets. I might have missed a step in my testing details in the ticket... |
Okay, the test has been thus, to simulate, er, simulator. Test.js is a file in resources, not compiled. win.add(require('Test').retrieveSuccessLabel()) is successful. win.add(Ti.Test.retrieveSuccessLabel()) gives an exception about Ti.Test being undefined. Is this a valid use case? |
Nope. Check out this comment on the associated Jira ticket: |
It still bothers me that we're introducing an inconsistent mechanism here, that modules can be added in different ways but with different rules, and that the Ti namespace can be modified ad-hoc. Remind me again why we're making such a deep cut into Titanium instead of having the end dev go "Ti.foo = require('foo');"? And why is this called commonJS support, since commonJS's implementation is to use Require? Either way, there should be explicit documentation, where require('foo') works and Ti.foo does not, and vice versa. |
1) Dev should just require('test') the module rather than Ti.Test. This is how CommonJS modules are loaded. 2) But then any module can be included via Ti.Test rather than require('test'). That has security and support concerns. |
Clarification. I thought this was for commonJS. As Dawson said, this is actually to allow 'compiled JS' titanium modules in the same namespace as native compiled modules. The previous issues remain, as all third party compiled modules already slip into the Ti namespace inadvertently. We're just widening the barn door's opening a little. Anyways, CR+FR PASSED. |
[TIMOB-7568] CommonJS in Core
Added support for adding CommonJS modules to the core.