-
Notifications
You must be signed in to change notification settings - Fork 0
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
Module system #9
Comments
I don't really like the System.resolve idea. It seems like a lot of unnecessary boiler-plate to me. |
Code in engine, but it does resolve the library stuff. And it is only code in the engine, not in the game itself. It is also used for the file-system sandboxing. |
I don't believe the fs should be sandboxed, though. |
Hmm. The whole sphere js stuff just feels so fragile. I do really like the idea of having ~/ as the directory with user stuff (App Data / Application Support), because it is writable. Remember that for OSX, I can't/not supposed to write to the .app folder, which is where the engine and all code resides. Did you EVER write something outside your app directory with a sphere game? Please give me 2 good examples. 😉 |
We don't need to expose anything sys-fs-based directly to the game maker. They only need to be able to traverse fs freely, and know that if the player inputs a valid path it will work in the engine. Cases of using more than just the current game's directory would be to import images (for instance, as a banner or avatar), importing data from other games (like converting saves from a prequel), reading music from the user's music collection as background music, etc. It's not that these are things that would be outright promoted. They are, of course, unlikely. But I don't believe they should be prohibited, either. Not knowing what this freedom is really good for is not a good reason to take it away. |
Hmm you are right, there. But I still want Then if a query starts with @/ it is replac, otherwise it is assumed to be a real path. ANOTHER, cleaner, possibility, is to use System.resolve for resolving stuff in require(). and making the function public. Now if the game-maker wants to user the resolver to find the file 'cache', it can use new RawFile(System.resolve('cache')). (Names are not set in stone). |
The interesting parts of the module system in node.js are at https://github.com/joyent/node/blob/master/lib/module.js, for the ones interested. I am currently trying to implement some. |
So today I started implementing my module system. It took some time to work it out. I used many concepts from node.js, which I also had to figure out as they have a much more complicated setup (they have native modules, which are compiled at compile-time and delivered in the binary. I did not do this). Now I need to clean things up, heavily. And then I will move ObjC code to JS and minify my ObjC stuff. Even though I call the native runInThisContext(), I think all of you can do that. @FlyingJester can use the same stuff (V8 Context, Scropt, Compiler, Run), and for @Radnen: it is essentially executing the code in the current JavaScript context and taking its return value. Loading a script as I hope you guys understand some of the code. |
I've begun to implement a proper CommonJS module system in minisphere. A prototype implementation is in minisphere 3.0, but is rather limited and can only load designated modules (those included with the engine and those in Ideally the system will work more like node.js: Designated modules would be loaded without an extension like they are now, e.g. Despite my initial misgivings, the module system is a good thing to have. The problem with If you still want global access to a module like you'd have with RequireScript, it's easy enough to import it globally (not recommended to do this inside of a module, though): global.link = require(`link`); I've found that the real strength, though, is your dependencies can be specified as locally as necessary and you get lazy loading for free. For example if the function below is never called, the module doesn't need to be loaded, potentially saving memory and minimizing initial load times: function getEatenByThePig()
{
var bfp = require('bigFatPig');
bfp.devour(me);
} The first request for a given module is cached and future The only real downside is some additional verbosity for constructor calls: var audio = require('audio');
var sound = new audio.Sound('sounds/munch.wav');
var mixer = new audio.Mixer(44100, 16, 2);
sound.play(mixer); But given all the benefits mentioned above, that's a small price to pay in the grand scheme of things. |
Hi! Nice. Quick correction: in Node, you load local files when you start with a relatie path. That is, you start with ./ or ../. Not sure about x/. But you for sure do not need to add an extension because that actual file might not exist. Greetings,
|
I know how Node's module loader works, I've looked at the algorithm specification more than a few times. :) I think it's too complicated though, what I described above was to simplify the module loader and make it more deterministic and predictable. Sphere programmers are used to working with filenames directly, so it wouldn't be outlandish to do |
In the recent light of #8, I should make an issue on the module system that I have in mind.
I want to implement the node.js-like module system:
A module looks like this (it is always a separate file, which increases quality of code and makes code style better):
This exposes the concat function in the module. So you can choose public and private functions.
What are the advantages of such system with encapsulations?
Internally,
require()
(andinclude()
for that matter) calls uponSystem.resolve("link","js","scripts")
to find the actual path, and then calls into native to load the data. When loading the javascript, one can't simply doeval()
. First off because eval is extremely evil. And next, because encapsulation is needed. Each module must also be passed the location of the module and its name. I will write more about this.The actual implementation will probably depend on your java script engine.
The text was updated successfully, but these errors were encountered: