1 The `loadfile()` function operates similar to `loadstring()` but reads the 2 input to compile from the specified file path instead. 3 4 It compiles the given file name into a ucode program and returns the resulting 5 entry function. 6 7 Throws an exception on compilation or file i/o failure. 8 9 Returns the compiled program entry function. 10 11 12 Compile a simple program with default options 13 14 -- Testcase -- 15 {% 16 let fn = loadfile('./files/test1.uc'); 17 fn(); 18 %} 19 -- End -- 20 21 -- File test1.uc -- 22 return 1 + 1; 23 -- End -- 24 25 -- Expect stdout -- 26 return 1 + 1; 27 -- End -- 28 29 30 Compile a program in raw mode 31 32 -- Testcase -- 33 {% 34 let fn = loadfile('./files/test2.uc', { raw_mode: true }); 35 fn(); 36 %} 37 -- End -- 38 39 -- File test2.uc -- 40 printf("%d\n", 1 + 1); 41 -- End -- 42 43 -- Expect stdout -- 44 2 45 -- End -- 46 47 48 Compile a program in template mode 49 50 -- Testcase -- 51 {% 52 let fn = loadfile('./files/test3.uc', { raw_mode: false }); 53 fn(); 54 %} 55 -- End -- 56 57 -- File test3.uc -- 58 {{ 1 + 1 }} 59 -- End -- 60 61 -- Expect stdout -- 62 2 63 -- End -- 64 65 66 Override module search path during compilation (import should fail due to empty path) 67 68 -- Testcase -- 69 {% 70 loadfile('./files/test4.uc', { 71 raw_mode: true, 72 module_search_path: [] 73 }); 74 %} 75 -- End -- 76 77 -- File test4.uc -- 78 import { readfile } from "fs"; 79 -- End -- 80 81 -- Expect stderr -- 82 Runtime error: Unable to compile source file './files/test4.uc': 83 84 | Syntax error: Unable to resolve path for module 'fs' 85 | In line 1, byte 30: 86 | 87 | `import { readfile } from "fs";` 88 | Near here -------------------^ 89 90 In line 5, byte 3: 91 92 ` });` 93 ^-- Near here 94 95 96 -- End -- 97 98 99 Force dynamic loading of unknown extensions at compile time (should succeed) 100 101 -- Testcase -- 102 {% 103 loadfile('./files/test5.uc', { 104 raw_mode: true, 105 force_dynlink_list: [ "doesnotexist" ] 106 }); 107 108 print("OK\n"); 109 %} 110 -- End -- 111 112 -- File test5.uc -- 113 import foo from "doesnotexist"; 114 -- End -- 115 116 -- Expect stdout -- 117 OK 118 -- End -- 119 120 121 Compiling a syntax error (should fail with syntax error exception) 122 123 -- Testcase -- 124 {% 125 loadfile('./files/test6.uc', { raw_mode: true }); 126 %} 127 -- End -- 128 129 -- File test6.uc -- 130 1 + 131 -- End -- 132 133 -- Expect stderr -- 134 Runtime error: Unable to compile source file './files/test6.uc': 135 136 | Syntax error: Expecting expression 137 | In line 2, byte 1: 138 | 139 | `1 +` 140 | ^-- Near here 141 142 In line 2, byte 49: 143 144 ` loadfile('./files/test6.uc', { raw_mode: true });` 145 Near here -----------------------------------------^ 146 147 148 -- End -- 149 150 151 Test loading precompiled bytecode 152 153 -- Testcase -- 154 {% 155 import { readlink } from 'fs'; 156 157 system(`${readlink('/proc/self/exe')} -T, -c -o ./files/test7.uc -e 'Hello world\n'`); 158 159 let fn = loadfile('./files/test7.uc'); 160 fn(); 161 %} 162 -- End -- 163 164 -- Expect stdout -- 165 Hello world 166 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt