• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/29_require

  1 The `require()` function loads the specified module, executes it and returns
  2 the returned value to the caller.
  3 
  4 The global array `REQUIRE_SEARCH_PATH` specifies the list of locations to
  5 check for a matching module file.
  6 
  7 The return value of a successfully loaded module is cached in a global
  8 registry, subsequent require calls with the same name will return the
  9 cached value.
 10 
 11 Throws an exception if the global `REQUIRE_SEARCH_PATH` variable is unset or
 12 not pointing to an array.
 13 
 14 Throws an exception if the requested module name cannot be found.
 15 
 16 Throws an exception if a module file could be found but not opened.
 17 
 18 Throws an exception if a module file could not be compiled.
 19 
 20 Returns the value returned by the invoked module code (typically an object).
 21 
 22 -- Testcase --
 23 {%
 24         push(REQUIRE_SEARCH_PATH, TESTFILES_PATH + '/*.uc');
 25 
 26         let mod1 = require("require.test.module");
 27         printf("require() #1 returned %.J\n\n", mod1);
 28 
 29         let mod2 = require("require.test.module");
 30         printf("require() #2 returned %.J\n\n", mod2);
 31 
 32         printf("Instances are identical: %s\n\n", mod1 === mod2);
 33 
 34         // deleting the entry from the global module registry forces reload
 35         delete global.modules["require.test.module"];
 36 
 37         let mod3 = require("require.test.module");
 38         printf("require() #3 returned %.J\n\n", mod3);
 39 
 40         printf("Instances are identical: %s\n\n", mod1 === mod3);
 41 %}
 42 -- End --
 43 
 44 -- File require/test/module.uc --
 45 print("This is require.test.module running!\n\n");
 46 
 47 return {
 48         greeting: function(name) {
 49                 printf("Hello, %s!\n", name);
 50         }
 51 };
 52 -- End --
 53 
 54 -- Expect stdout --
 55 This is require.test.module running!
 56 
 57 require() #1 returned {
 58         "greeting": "function(name) { ... }"
 59 }
 60 
 61 require() #2 returned {
 62         "greeting": "function(name) { ... }"
 63 }
 64 
 65 Instances are identical: true
 66 
 67 This is require.test.module running!
 68 
 69 require() #3 returned {
 70         "greeting": "function(name) { ... }"
 71 }
 72 
 73 Instances are identical: false
 74 
 75 -- End --
 76 
 77 
 78 A clobbered `REQUIRE_SEARCH_PATH` triggers an exception.
 79 
 80 -- Testcase --
 81 {%
 82         REQUIRE_SEARCH_PATH = null;
 83 
 84         require("test");
 85 %}
 86 -- End --
 87 
 88 -- Expect stderr --
 89 Runtime error: Global require search path not set
 90 In line 4, byte 16:
 91 
 92  `    require("test");`
 93   Near here --------^
 94 
 95 
 96 -- End --
 97 
 98 
 99 A not found module triggers an exception.
100 
101 -- Testcase --
102 {%
103         require("test");
104 %}
105 -- End --
106 
107 -- Expect stderr --
108 Runtime error: No module named 'test' could be found
109 In line 2, byte 16:
110 
111  `    require("test");`
112   Near here --------^
113 
114 
115 -- End --
116 
117 
118 A compilation error in the module triggers an exception.
119 
120 -- Testcase --
121 {%
122         push(REQUIRE_SEARCH_PATH, TESTFILES_PATH + '/*.uc');
123 
124         require("require.test.broken");
125 %}
126 -- End --
127 
128 -- File require/test/broken.uc --
129 // Unclosed object to force syntax error
130 return {
131 -- End --
132 
133 -- Expect stderr --
134 Runtime error: Unable to compile source file './files/require/test/broken.uc':
135 
136   | Syntax error: Expecting label
137   | In line 3, byte 1:
138   |
139   |  `return {`
140   |           ^-- Near here
141 
142 In line 4, byte 31:
143 
144  `    require("require.test.broken");`
145   Near here -----------------------^
146 
147 
148 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt