ELoader

From Erights

(Difference between revisions)
Jump to: navigation, search
m
(Using multiple modules)
 
(One intermediate revision not shown)
Line 1: Line 1:
When you run a .e or (.updoc) file, it is given a loader called '''<this>''' which can be used to import .emaker files from the same directory (or from a sub-directory).
When you run a .e or (.updoc) file, it is given a loader called '''<this>''' which can be used to import .emaker files from the same directory (or from a sub-directory).
-
For example:
+
For example, create a new directory and add two files. main.e is the main program:
   # main.e
   # main.e
Line 7: Line 7:
   def obj := makeMyObject()
   def obj := makeMyObject()
   ...
   ...
 +
 +
makeMyObject.emaker is a support file:
   # makeMyObject.emaker
   # makeMyObject.emaker
Line 12: Line 14:
     ...
     ...
   }
   }
 +
 +
Files imported using '''<this>''' also have the loader in their environment, and can import further files using it.
 +
 +
An ELoader also allows access to the raw files (use '''<this>.getRoot()''' to get a read-only directory object). This is useful for accessing icons, documentation, etc.
= Using multiple modules =
= Using multiple modules =
Line 17: Line 23:
If a program is made up of multiple modules, a separate loader can be created for each one. Each module is given its own loader as '''<this>''', as well as loaders for its dependencies. For example, here we have two modules. "prog" is a program that depends on a library called "libfoo".
If a program is made up of multiple modules, a separate loader can be created for each one. Each module is given its own loader as '''<this>''', as well as loaders for its dependencies. For example, here we have two modules. "prog" is a program that depends on a library called "libfoo".
 +
  def makeTraceln := <unsafe:org.erights.e.elib.debug.makeTraceln>
 +
  def makeELoader := <elang:interp.ELoaderAuthor>(makeTraceln)
 +
 
   def <libfoo> := makeELoader(<file:.../libfoo>, ["this__uriGetter" => <libfoo>], "libfoo$")
   def <libfoo> := makeELoader(<file:.../libfoo>, ["this__uriGetter" => <libfoo>], "libfoo$")
   def <prog> := makeELoader(<file:.../prog>, ["this__uriGetter" => <prog>, => <libfoo>], "prog$")
   def <prog> := makeELoader(<file:.../prog>, ["this__uriGetter" => <prog>, => <libfoo>], "prog$")
Line 27: Line 36:
You could also wrap the '''<libfoo>''' given to prog to only expose its public API.
You could also wrap the '''<libfoo>''' given to prog to only expose its public API.
-
This avoids the need to set classpath and avoids conflicts between different packages.
+
This avoids the need to set classpath and avoids conflicts between different packages. For example, you could depend on two libraries which, internally, require different versions of libfoo. Each library would see the correct version of libfoo as '''<libfoo>'''.
 +
 
 +
In future, it is expected that a separate ''injector'' library will take care of selecting and downloading modules and wiring them together.
 +
 
 +
See [http://0install.net/ebox.html 0install.net's EBox demo] for a working example.
= See also =
= See also =
-
See [http://www.eros-os.org/pipermail/e-lang/2010-February/013416.html] for details.
+
* http://switchb.org/darcs/e-modules/
 +
* http://www.eros-os.org/pipermail/e-lang/2010-February/013416.html -- original discussion

Latest revision as of 09:49, 11 April 2011

When you run a .e or (.updoc) file, it is given a loader called <this> which can be used to import .emaker files from the same directory (or from a sub-directory).

For example, create a new directory and add two files. main.e is the main program:

 # main.e
 def makeMyObject := <this:makeMyObject>
 def obj := makeMyObject()
 ...

makeMyObject.emaker is a support file:

 # makeMyObject.emaker
 def makeMyObject() {
   ...
 }

Files imported using <this> also have the loader in their environment, and can import further files using it.

An ELoader also allows access to the raw files (use <this>.getRoot() to get a read-only directory object). This is useful for accessing icons, documentation, etc.

Using multiple modules

If a program is made up of multiple modules, a separate loader can be created for each one. Each module is given its own loader as <this>, as well as loaders for its dependencies. For example, here we have two modules. "prog" is a program that depends on a library called "libfoo".

 def makeTraceln := <unsafe:org.erights.e.elib.debug.makeTraceln>
 def makeELoader := <elang:interp.ELoaderAuthor>(makeTraceln)
 
 def <libfoo> := makeELoader(<file:.../libfoo>, ["this__uriGetter" => <libfoo>], "libfoo$")
 def <prog> := makeELoader(<file:.../prog>, ["this__uriGetter" => <prog>, => <libfoo>], "prog$")
 
 def app := <prog:makeApplication>(...)
  • Within libfoo, the library can load other emakers from itself using e.g. <this:somefile>.
  • Within prog, a file can load other emakers from prog using e.g. <this:somefile>, or from libfoo using <libfoo:somefile>.

You could also wrap the <libfoo> given to prog to only expose its public API.

This avoids the need to set classpath and avoids conflicts between different packages. For example, you could depend on two libraries which, internally, require different versions of libfoo. Each library would see the correct version of libfoo as <libfoo>.

In future, it is expected that a separate injector library will take care of selecting and downloading modules and wiring them together.

See 0install.net's EBox demo for a working example.

See also

Personal tools
more tools