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