Skip to content
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

Support for importing js files and modules via require #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ninjaprawn
Copy link

Modules are a key aspect in creating an application. In Node.js, modules can be installed and imported.

This PR adds support for importing js files, as well as modules that could come from Node packages.

What this PR does:

  • Modifies the require implementation
  • Adds a method to create a JSContext which is a duplicate of the default one
    • This is done as Apple has not implemented the -copyWithZone: method
  • Renamed the test-app folder to app
  • Changed the app group to a folder reference, so that files can be easily copied without having to manually add them to a Copy Files phase.

If you have any questions, feel free to ask :)

newContext[@"require"] = self.jsContext[@"require"];
newContext[@"process"] = self.jsContext[@"process"];
newContext[@"console"] = self.jsContext[@"console"];
[newContext evaluateScript:@"var exports = {};"]; // Evaluated so the developer doesnt have to
Copy link

@gimenete gimenete May 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, great work. This is a big step forwards.

To mimic Node.js behavior you should inject a module object that has an exports object initialized as an empty object. Pseudo-code:

module = @{ @"exports": @{} }
newContext[@"module"] = module;
newContext[@"exports"] = module.exports;
// 1.- evaluate
// 2.- pick the evaluated exports
return (id)[tmpContext objectForKeyedSubscript:@"module.exports"];

This way these two ways of populating exports will work:

exports.something = foo
module.exports = somethingCompletelyNew

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! Just a node I originally used exports due to this article.

I've done a bit more research on this, and it does seem a bit confusing about the whole exports vs module.exports.

Regarding your suggested changes, how would setting exports also change module.exports? Unless I'm missing something, we are only exposing module, unless you propose that we leave both, and we check which variable is populated...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my mistake. It should be

newContext[@"exports"] = module.exports;

I've changed it in the example. I'll try to help more on this later.

@gimenete
Copy link

gimenete commented May 9, 2017

I've created this PR: https://github.com/ninjaprawn/electrino/pull/1/files

That adds better support. Nevertheless the whole thing should follow this documentation:
https://nodejs.org/api/modules.html#modules_all_together

Some things that we will need to implement:

  • requires to relative paths should start with ./ or ../
  • Inject variables like __dirname and __filename
  • the module object has more things (such as the loaded attribute), and I think this is the object that should be stored in the require cache (not the exports one, although obviously module.exports is the thing that needs to be returned when requiring the module).
  • a module should only be loaded once. So first we should check if it exists in the require cache.
  • Right before evaluating a module it should be put in the require cache to avoid infinite loops if there are require cycles. For example: module A requires module B and this one requires A. The second time we require A, we check if it's in the cache and we don't load it again.

Better support for "module" and "exports"
@ninjaprawn
Copy link
Author

I think that once we've established where the project is heading, we will add those features in a reliable way.

@pojala
Copy link
Owner

pojala commented May 22, 2017

Hi @ninjaprawn @gimenete @kittsuphatt !

Thanks very much for this. (I failed at Github and just didn't notice this pull request until now.)

I'll take a look and merge ASAP.

@amilajack
Copy link
Collaborator

@ninjaprawn would be awesome if you can fix the merge conflicts. I can merge this after testing it out

@pojala
Copy link
Owner

pojala commented Jun 10, 2017

Oh man -- when I wrote "ASAP", I didn't mean to imply "20 days later". Sorry for that. I've just been swamped.

I can check out and fix the merge conflicts on this one tomorrow, I think.

@ninjaprawn
Copy link
Author

I won't be able to do much for the next couple of days (I'm flying back from WWDC to Australia, which is a long time). If nothing is done until then, I'll be more than happy to fix the conflicts

davidenke pushed a commit to davidenke/electrino that referenced this pull request Feb 7, 2018
Clean up JSBrowserWindowInstance constructor
@genesy
Copy link

genesy commented Nov 12, 2018

What ever happened to this project?

@amilajack
Copy link
Collaborator

@ninjaprawn is this PR ready to review?

@paramaggarwal
Copy link
Contributor

@ninjaprawn please help with the merge conflicts!

@ninjaprawn
Copy link
Author

sorry this took so long! should be good to go now

@paramaggarwal
Copy link
Contributor

@pojala let's move ahead with this PR! 😄

@genesy
Copy link

genesy commented Mar 12, 2020

this proj is still active?

@amilajack
Copy link
Collaborator

@gimenete Should this be closed since your PR was merged?

@gimenete
Copy link

@amilajack my PR improved things, but was not implementing everything. I'm unsure about the state of the current implementation.

The implementation should follow this spec https://nodejs.org/api/modules.html#modules_all_together

@amilajack
Copy link
Collaborator

@ninjaprawn Would love if you could the merge conflicts one last time. I'll definitely merge this right after you fix them.

@paramaggarwal
Copy link
Contributor

paramaggarwal commented Mar 20, 2020

@ninjaprawn could you help us out with the merge conflicts - we are looking to merge this in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants