|
| 1 | +(* SPDX-License-Identifier: MPL-2.0 *) |
| 2 | +(* SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell *) |
| 3 | + |
| 4 | +(** #832: the JS-text backend ([Js_codegen]) refuses, at compile time and by |
| 5 | + name, a builtin its runtime prelude does not define. |
| 6 | +
|
| 7 | + Before the fix such a program compiled and the emitted script stopped |
| 8 | + with [ReferenceError: int_to_string is not defined] when the call was |
| 9 | + reached. These tests drive the real front end (parse, resolve, |
| 10 | + typecheck) into [Js_codegen.codegen_js] and check the refusal, the names |
| 11 | + it reports, and that a program binding of the same name is not refused. *) |
| 12 | + |
| 13 | +open Affinescript |
| 14 | + |
| 15 | +let compile_js = Test_int_div_js.compile_js |
| 16 | +let contains = Test_int_div_js.contains |
| 17 | + |
| 18 | +let refused ~names src = |
| 19 | + match compile_js src with |
| 20 | + | Ok _ -> |
| 21 | + Alcotest.failf |
| 22 | + "expected a compile-time refusal naming %s, but the JS backend emitted \ |
| 23 | + a script (#832 has regressed)" |
| 24 | + (String.concat ", " names) |
| 25 | + | Error msg -> |
| 26 | + Alcotest.(check bool) ("is a JS codegen refusal: " ^ msg) true |
| 27 | + (contains ~needle:"JS codegen error" msg |
| 28 | + && contains ~needle:"not provided by the JS-text backend" msg |
| 29 | + && contains ~needle:"#832" msg); |
| 30 | + List.iter (fun n -> |
| 31 | + Alcotest.(check bool) ("refusal names `" ^ n ^ "`: " ^ msg) true |
| 32 | + (contains ~needle:("`" ^ n ^ "`") msg)) |
| 33 | + names |
| 34 | + |
| 35 | +let accepted src : string = |
| 36 | + match compile_js src with |
| 37 | + | Ok js -> js |
| 38 | + | Error msg -> Alcotest.failf "expected the JS backend to compile, got: %s" msg |
| 39 | + |
| 40 | +(* 1. The program from the issue. *) |
| 41 | +let issue_example () = |
| 42 | + refused ~names:[ "int_to_string"; "len" ] |
| 43 | + "fn main() -> () { println(int_to_string(len(\"abc\"))); }\n" |
| 44 | + |
| 45 | +(* 2. Each string builtin named in #831/#832 is refused by its own name, and |
| 46 | + the refusal names nothing the program did not use. *) |
| 47 | +let string_builtins = |
| 48 | + [ ("len", "fn f(s: String) -> Int { len(s) }"); |
| 49 | + ("string_length", "fn f(s: String) -> Int { string_length(s) }"); |
| 50 | + ("string_get", "fn f(s: String) -> Char { string_get(s, 0) }"); |
| 51 | + ("string_sub", "fn f(s: String) -> String { string_sub(s, 0, 1) }"); |
| 52 | + ("string_find", "fn f(s: String) -> Int { string_find(s, \"a\") }"); |
| 53 | + ("string_char_code_at", "fn f(s: String) -> Int { string_char_code_at(s, 0) }"); |
| 54 | + ("char_to_int", "fn f(c: Char) -> Int { char_to_int(c) }"); |
| 55 | + ("int_to_string", "fn f(n: Int) -> String { int_to_string(n) }"); |
| 56 | + ("float_to_string", "fn f(x: Float) -> String { float_to_string(x) }"); |
| 57 | + ("to_uppercase", "fn f(s: String) -> String { to_uppercase(s) }"); |
| 58 | + ("trim", "fn f(s: String) -> String { trim(s) }") ] |
| 59 | + |
| 60 | +let each_string_builtin () = |
| 61 | + List.iter (fun (name, src) -> |
| 62 | + refused ~names:[ name ] (src ^ "\n"); |
| 63 | + (match compile_js (src ^ "\n") with |
| 64 | + | Error msg -> |
| 65 | + List.iter (fun (other, _) -> |
| 66 | + if other <> name then |
| 67 | + Alcotest.(check bool) |
| 68 | + (Printf.sprintf "refusal of `%s` does not name `%s`" name other) |
| 69 | + false (contains ~needle:("`" ^ other ^ "`") msg)) |
| 70 | + string_builtins |
| 71 | + | Ok _ -> ())) |
| 72 | + string_builtins |
| 73 | + |
| 74 | +(* 3. Closed world: every name the resolver seeds as a builtin is either |
| 75 | + bound by the emitted runtime or refused. A builtin added to the resolver |
| 76 | + later is covered without touching this test. Parse only: the check must |
| 77 | + not depend on the type checker having run. *) |
| 78 | +let prelude_binds name = |
| 79 | + List.exists (fun line -> |
| 80 | + let starts p = |
| 81 | + String.length line >= String.length p |
| 82 | + && String.sub line 0 (String.length p) = p in |
| 83 | + starts ("const " ^ name ^ " ") || starts ("let " ^ name ^ " ")) |
| 84 | + (String.split_on_char '\n' Js_codegen.prelude) |
| 85 | + |
| 86 | +let every_resolver_builtin_bound_or_refused () = |
| 87 | + let syms = Symbol.create () in |
| 88 | + Resolve.seed_builtins syms; |
| 89 | + let names = |
| 90 | + Hashtbl.fold (fun _ (s : Symbol.symbol) acc -> s.sym_name :: acc) |
| 91 | + syms.Symbol.all_symbols [] |
| 92 | + |> List.sort_uniq compare |
| 93 | + in |
| 94 | + Alcotest.(check bool) "the resolver seeds builtins" true (List.length names > 40); |
| 95 | + let refused_n = ref 0 and bound_n = ref 0 in |
| 96 | + List.iter (fun name -> |
| 97 | + let prog = |
| 98 | + Parse_driver.parse_string ~file:"<closed-world>" |
| 99 | + (Printf.sprintf "fn probe() -> Int { let f = %s; 0 }\n" name) |
| 100 | + in |
| 101 | + match Js_codegen.codegen_js prog (Symbol.create ()) with |
| 102 | + | Error msg -> |
| 103 | + incr refused_n; |
| 104 | + Alcotest.(check bool) ("refusal names `" ^ name ^ "`") true |
| 105 | + (contains ~needle:("`" ^ name ^ "`") msg); |
| 106 | + Alcotest.(check bool) |
| 107 | + ("`" ^ name ^ "` is refused, so the prelude must not bind it") false |
| 108 | + (prelude_binds name) |
| 109 | + | Ok js -> |
| 110 | + incr bound_n; |
| 111 | + (* Accepted: the emitted script must bind the name at top level. *) |
| 112 | + Alcotest.(check bool) |
| 113 | + ("`" ^ name ^ "` is accepted, so the script must bind it") true |
| 114 | + (contains ~needle:("\nconst " ^ name ^ " ") js |
| 115 | + || contains ~needle:("\nlet " ^ name ^ " ") js)) |
| 116 | + names; |
| 117 | + Alcotest.(check bool) "some builtins are refused" true (!refused_n > 0); |
| 118 | + Alcotest.(check bool) "print/println/read_line are bound" true (!bound_n >= 3) |
| 119 | + |
| 120 | +(* 4. The prelude's own builtins still compile. *) |
| 121 | +let provided_builtins_compile () = |
| 122 | + let js = accepted "fn main() -> () { print(\"a\"); println(\"b\"); let r = read_line(); }\n" in |
| 123 | + Alcotest.(check bool) "main is emitted" true (contains ~needle:"function main()" js) |
| 124 | + |
| 125 | +(* 5. A program binding of the same name is not the builtin: no refusal. *) |
| 126 | +let program_bindings_shadow () = |
| 127 | + ignore (accepted |
| 128 | + "fn len(s: String) -> Int { 7 }\n\ |
| 129 | + fn main() -> Int { len(\"abc\") }\n"); |
| 130 | + ignore (accepted "fn f(len: Int) -> Int { len + 1 }\n"); |
| 131 | + ignore (accepted "fn f() -> Int { let trim = 3; trim + 1 }\n"); |
| 132 | + ignore (accepted "fn f(xs: [Int]) -> Int { let mut t = 0; for len in xs { t = t + len; } t }\n"); |
| 133 | + ignore (accepted "fn f() -> Int { let g = fn(len: Int) => len + 1; g(1) }\n"); |
| 134 | + ignore (accepted |
| 135 | + "enum Box { Full(Int), Empty }\n\ |
| 136 | + fn f(o: Box) -> Int { match o { Full(len) => len, Empty => 0 } }\n"); |
| 137 | + ignore (accepted "const trim: Int = 4;\nfn f() -> Int { trim }\n") |
| 138 | + |
| 139 | +(* 6. A binder hides the builtin only in its own scope. *) |
| 140 | +let shadow_is_scoped () = |
| 141 | + refused ~names:[ "len" ] |
| 142 | + "fn g(len: Int) -> Int { len }\n\ |
| 143 | + fn f(s: String) -> Int { len(s) }\n"; |
| 144 | + refused ~names:[ "len" ] |
| 145 | + "fn f(s: String) -> Int { let n = len(s); let len = 3; n + len }\n" |
| 146 | + |
| 147 | +(* 7. The reference is found wherever it sits: lambda body, match arm, |
| 148 | + constant initialiser, loop body. *) |
| 149 | +let nested_references () = |
| 150 | + refused ~names:[ "int_to_string" ] |
| 151 | + "fn f() -> String { let g = fn(n: Int) => int_to_string(n); g(1) }\n"; |
| 152 | + refused ~names:[ "len" ] |
| 153 | + "enum Box { Full(String), Empty }\n\ |
| 154 | + fn f(o: Box) -> Int { match o { Full(s) => len(s), Empty => 0 } }\n"; |
| 155 | + refused ~names:[ "len" ] "const N: Int = len(\"abc\");\n"; |
| 156 | + refused ~names:[ "len" ] |
| 157 | + "fn f(s: String) -> Int { let mut i = 0; while i < len(s) { i = i + 1; } i }\n" |
| 158 | + |
| 159 | +(* 8. A program with no builtin reference is untouched. *) |
| 160 | +let no_builtins_unaffected () = |
| 161 | + let js = accepted "fn add(a: Int, b: Int) -> Int { a + b }\n" in |
| 162 | + Alcotest.(check bool) "add is emitted" true (contains ~needle:"function add(a, b)" js) |
| 163 | + |
| 164 | +let tests = [ |
| 165 | + Alcotest.test_case "issue #832 example is refused by name" `Quick issue_example; |
| 166 | + Alcotest.test_case "each string builtin is refused by its own name" `Quick each_string_builtin; |
| 167 | + Alcotest.test_case "every resolver builtin is bound or refused" `Quick every_resolver_builtin_bound_or_refused; |
| 168 | + Alcotest.test_case "prelude builtins still compile" `Quick provided_builtins_compile; |
| 169 | + Alcotest.test_case "a program binding shadows the builtin" `Quick program_bindings_shadow; |
| 170 | + Alcotest.test_case "shadowing is scoped" `Quick shadow_is_scoped; |
| 171 | + Alcotest.test_case "nested references are refused" `Quick nested_references; |
| 172 | + Alcotest.test_case "program without builtins is unaffected" `Quick no_builtins_unaffected; |
| 173 | +] |
0 commit comments