New doc system done for core
[io.git] / libs / iovm / io / Z_Importer.io
blob66f1769a99cf70150532fa6c547dc09e9fc6b43c
1 Importer := Object clone do(
2 //metadoc Importer description A simple search path based auto-importer.
4 //doc Importer paths List of paths the proto importer will check while searching for protos to load.
5 paths := method(FileImporter folders)
7 //doc Importer addSearchPath(path) Add a search path to the auto importer. Relative paths are made absolute before adding.
8 addSearchPath := method(p, paths appendIfAbsent(Path absolute(p) asSymbol))
10 //doc Importer removeSearchPath(path) Removes a search path from the auto importer. Relative paths should be removed from the same working directory as they were added.
11 removeSearchPath := method(p, paths remove(Path absolute(p) asSymbol))
13 //doc Importer FileImporter An Importer for local source files.
14 FileImporter := Object clone do(
15 importsFrom := "file"
17 folders := list("")
19 import := method(protoName,
20 if(?launchPath, folders appendIfAbsent(launchPath))
22 folders foreach(folder,
23 path := Path with(folder, protoName .. ".io") asSymbol
24 if(File with(path) exists,
25 Lobby doFile(path)
26 return true
29 false
33 //doc Importer AddonImporter An Importer for addon modules.
34 AddonImporter := Object clone do(
35 importsFrom := "dll"
37 import := method(protoName,
38 if(hasAddon := AddonLoader hasAddonNamed(protoName),
39 AddonLoader loadAddonNamed(protoName)
41 hasAddon
45 //doc Importer importers List of Importer objects.
46 importers := list(FileImporter, AddonImporter)
48 //doc Importer import(originalCallMessage) Imports an object or addon for the given Message.
49 import := method(originalCall,
50 protoName := originalCall message name
52 if(protoName at(0) isUppercase and(importer := importers detect(import(protoName))),
53 if(Lobby hasSlot(protoName) not,
54 Exception raiseFrom(originalCall, "Importer slot '" .. protoName .. "' missing after " .. importer importsFrom .. " load")
56 Lobby getSlot(protoName)
58 targetType := originalCall target type
59 Exception raiseFrom(originalCall, targetType .. " does not respond to '" .. protoName .. "'")
63 //doc Importer autoImportingForward A forward method implementation placed in the Lobby when Importing is turned on.
64 autoImportingForward := method(
65 Importer import(call)
68 //doc Importer turnOn Turns on the Importer. Returns self.
69 turnOn := method(
70 Lobby forward := self getSlot("autoImportingForward")
71 self
74 //doc Importer turnOff Turns off the Importer. Returns self.
75 turnOff := method(
76 Lobby removeSlot("forward")
77 self
80 // Auto Importer is on by default
81 turnOn