Hi,

On Thu, May 21, 2015 at 12:50 AM, Satyajit Sahoo <satyajit.ha...@gmail.com>
wrote:

> Yeah modules are a must. Right now we need to do the following,
>
> imports.searchPath.unshift(".");
>
> const ModName  = imports.filename
>
>
> Then all functions, variables etc become available under our constant
> *ModName,*which is not what we always want. Many modules might want to
> expose just one or 2 objects/methods while keeping everything private.
>

There is a hack that at least works in GJS 3.14 that will allow you to do
this; at the end of your module, assign your exports object to
imports.yourModuleName:

imports.yourModuleName = objectContainingYourPublicAPI;

However I wouldn't really recommend this hack, it smells like
"unsupported". Although I certainly agree an official ES6-certified way to
do this would be nice, I don't think it's such a disaster that we don't
have it. Certainly in most cases you can just prefix your private symbols
with an underscore?

This also has another problem, since there is no way to know the current
> directory in which the file is situated, you really cannot have modules in
> a subdirectory.
>

Again a hack is possible; this one is probably safer to use than the above
one. Create a new Error() and examine its stack to find out where the
current file is:

let e = new Error();
let stack = e.stack.split('\n');
stack.pop();  // remove last newline
let frame = stack[stack.length - 1];
let [location, fileLine] = frame.split('@');
let [currentFilePath, lineNumber] = frame.split(':');

(adapted from [1] which itself is adapted from somewhere in the Gnome
codebase, I forget where)

ES6 modules will solve this problem. But then what will happen to the
> current method? It'll suck to use *imports* for the core libraries and
> the *import ModName from filename* syntax for third party libraries.
>

I think it's reasonable to assume that when ES6 modules are landed, then
GJS's modules will be discontinued so that there are not two competing
module systems active at the same time. Give the developers some credit :-)

On 20 May 2015 at 19:39, Matěj Cepl <mc...@cepl.eu> wrote:
>
>> On 2015-03-07, 19:54 GMT, Emmanuele Bassi wrote:
>> >> it would be nice that developer documentation of gjs would be extended,
>> >> also with examples of how to write a module in a *modern* and *easy*
>> >> way...
>> >
>> > What do you mean "write a module in a modern and easy way"?
>> > What kind of module?
>> >
>> > Not saying that the documentation is good — because it isn't — but it
>> > would help to specify what you want to achieve, and what were the
>> > obstacles you met. I'm sure we can make the documentation at least
>> > passable, if not good.
>>
>> Perhaps he is talking about ES6? I believe when it will be in
>> the Spidermonkey (or whatever monkey is now running underneath
>> GJS) we will have modules, classes, etc. as well. See
>> https://hacks.mozilla.org/category/es6-in-depth/ and
>> https://bugzilla.mozilla.org/show_bug.cgi?id=694100
>>
>> I cannot get it soon enough, for example.
>>
>
Please note that none of the major browser JavaScript engines support ES6
modules yet [2] so I think you might be waiting for a while. For example,
the bug for getting it into SpiderMonkey is here: [3] Until that time, I
suggest to do what everyone else seems to do and use an ES6 transpiler.

For reference, here is a list of ES6 features and which versions of
SpiderMonkey support them: [4] GJS is powered by SpiderMonkey 24, so we
have everything listed as available in Firefox 24 and earlier. I myself am
a big fan of arrow functions in GJS.

I can also urge you to help out, if you want things to move faster. AFAIK
the current GJS maintainers can't spend a lot of time on it or would rather
step down from the maintainer position. It would be great to be able to
port GJS to SpiderMonkey 31 and later 38 when it is released this year, so
that when modules land in SpiderMonkey, GJS is ready for them; but there
isn't enough volunteer power to do so. Alternatively, as Emmanuele
mentioned, I'm sure your help would also be appreciated on GNode. Or, if
you are a _really_ hardcore Javascript hacker, go land ES6 modules in
SpiderMonkey ;-)

[1] https://github.com/ptomato/qjs/blob/master/src/q.js#L9
[2]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
[3] https://bugzilla.mozilla.org/show_bug.cgi?id=568953
[4]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla

Regards,
-- 
Philip
_______________________________________________
javascript-list mailing list
javascript-list@gnome.org
https://mail.gnome.org/mailman/listinfo/javascript-list

Reply via email to