|
1 | | -# Supported Julia Functions in JavaScriptTarget.jl |
| 1 | +# Supported Functions |
2 | 2 |
|
3 | | -## Status Legend |
4 | | -- **Supported**: Compiles and runs correctly in Node.js |
5 | | -- **Partial**: Compiles but with limitations |
6 | | -- **Excluded**: Cannot be compiled (depends on C runtime, FFI, etc.) |
| 3 | +This page lists every Base function's compilation status: **Supported** (compiles and runs correctly), **Partial** (compiles with limitations), or **Excluded** (cannot be compiled). |
| 4 | + |
| 5 | +Each supported function includes a runnable example you can paste into the [Playground](index.md). |
7 | 6 |
|
8 | 7 | --- |
9 | 8 |
|
|
18 | 17 | | `-` (Float64) | Supported | `a - b` | | |
19 | 18 | | `*` (Float64) | Supported | `a * b` | | |
20 | 19 | | `/` (Float64) | Supported | `a / b` | | |
21 | | -| `-` (unary, Float64) | Supported | `-a` | | |
| 20 | +| `-` (unary) | Supported | `-a` | | |
22 | 21 | | `div(a, b)` | Supported | `(a / b) \| 0` | Julia inlines to intrinsic | |
23 | 22 | | `fld(a, b)` | Supported | Inlined arithmetic | Floor division | |
24 | 23 | | `mod(a, b)` | Supported | Inlined arithmetic | Same sign as divisor | |
25 | 24 | | `cld(a, b)` | Supported | Inlined arithmetic | Ceiling division | |
26 | 25 | | `rem(a, b)` | Supported | `a % b` | | |
27 | 26 |
|
| 27 | +**Example:** |
| 28 | +```julia |
| 29 | +function arithmetic_demo(a, b) |
| 30 | + println(a + b) |
| 31 | + println(a * b) |
| 32 | + println(a / b) |
| 33 | + println(a ^ 2) |
| 34 | +end |
| 35 | +arithmetic_demo(3, 4) |
| 36 | +``` |
| 37 | + |
28 | 38 | ## Math Functions |
29 | 39 |
|
30 | 40 | | Function | Status | JS Output | Notes | |
|
51 | 61 | | `max(a, b)` | Supported | `Math.max(a, b)` | | |
52 | 62 | | `hypot(a, b)` | Supported | `Math.hypot(a, b)` | | |
53 | 63 |
|
| 64 | +**Example:** |
| 65 | +```julia |
| 66 | +function math_demo(x) |
| 67 | + println(sin(x)) |
| 68 | + println(sqrt(x)) |
| 69 | + println(floor(x)) |
| 70 | + println(abs(-x)) |
| 71 | + println(min(x, 10.0)) |
| 72 | +end |
| 73 | +math_demo(3.14) |
| 74 | +``` |
| 75 | + |
54 | 76 | ## Comparisons |
55 | 77 |
|
56 | 78 | | Function | Status | JS Output | Notes | |
|
63 | 85 | | `>=` | Supported | `>=` | | |
64 | 86 | | `===` | Supported | `===` (primitives), `jl_egal` (immutable structs) | | |
65 | 87 |
|
| 88 | +**Example:** |
| 89 | +```julia |
| 90 | +println(3 > 2) |
| 91 | +println(3 == 3) |
| 92 | +println(3 != 4) |
| 93 | +println(5 <= 5) |
| 94 | +``` |
| 95 | + |
66 | 96 | ## Boolean |
67 | 97 |
|
68 | 98 | | Function | Status | JS Output | Notes | |
|
73 | 103 | | `&` (bitwise on Bool) | Partial | `a & b` | Returns number in JS, not boolean | |
74 | 104 | | `\|` (bitwise on Bool) | Partial | `a \| b` | Returns number in JS, not boolean | |
75 | 105 |
|
| 106 | +**Example:** |
| 107 | +```julia |
| 108 | +println(!false) |
| 109 | +println(true) |
| 110 | +function check(x) |
| 111 | + if x > 0 |
| 112 | + return true |
| 113 | + else |
| 114 | + return false |
| 115 | + end |
| 116 | +end |
| 117 | +println(check(5)) |
| 118 | +println(check(-3)) |
| 119 | +``` |
| 120 | + |
76 | 121 | ## Bitwise (Int32) |
77 | 122 |
|
78 | 123 | | Function | Status | JS Output | Notes | |
|
113 | 158 | | `replace(s, p=>r)` | Excluded | — | Julia inlines deeply | |
114 | 159 | | `length(s)` | Partial | — | Julia's codepoint length differs from JS | |
115 | 160 |
|
| 161 | +**Example (compiler):** |
| 162 | +```julia |
| 163 | +import JavaScriptTarget as JST |
| 164 | + |
| 165 | +greet(name::String) = "Hello " * name * "!" |
| 166 | +result = JST.compile(greet, (String,)) |
| 167 | +println(result.js) |
| 168 | +``` |
| 169 | + |
| 170 | +**Example (playground):** |
| 171 | +```julia |
| 172 | +println(string("Hello", " ", "World")) |
| 173 | +println(uppercase("abc")) |
| 174 | +println(startswith("foobar", "foo")) |
| 175 | +println(length("test")) |
| 176 | +``` |
| 177 | + |
| 178 | +!!! note "Playground vs Compiler" |
| 179 | + The playground's codegen handles `uppercase`, `lowercase`, `strip`, `split`, and `replace` as named function calls mapped to JS builtins. These work in the playground but not via `compile()` where Julia inlines them. |
| 180 | + |
116 | 181 | ## Collections |
117 | 182 |
|
118 | 183 | | Function | Status | JS Output | Notes | |
|
131 | 196 | | `(a, b)` (Tuple) | Supported | `[a, b]` | | |
132 | 197 | | `t[1]` (Tuple) | Supported | `t[0]` | | |
133 | 198 |
|
| 199 | +**Example:** |
| 200 | +```julia |
| 201 | +a = [10, 20, 30, 40, 50] |
| 202 | +println(a[1]) |
| 203 | +println(a[3]) |
| 204 | +println(length(a)) |
| 205 | +``` |
| 206 | + |
134 | 207 | ## Control Flow |
135 | 208 |
|
136 | 209 | | Function | Status | JS Output | Notes | |
|
143 | 216 | | `try/catch` | Supported | `try/catch` | | |
144 | 217 | | `error(msg)` | Supported | `throw new Error(msg)` | | |
145 | 218 |
|
| 219 | +**Example:** |
| 220 | +```julia |
| 221 | +function fizzbuzz(n) |
| 222 | + for i in 1:n |
| 223 | + if i > 15 * (i / 15 |> floor) |
| 224 | + if i > 3 * (i / 3 |> floor) |
| 225 | + if i > 5 * (i / 5 |> floor) |
| 226 | + println(i) |
| 227 | + else |
| 228 | + println("Buzz") |
| 229 | + end |
| 230 | + else |
| 231 | + println("Fizz") |
| 232 | + end |
| 233 | + else |
| 234 | + println("FizzBuzz") |
| 235 | + end |
| 236 | + end |
| 237 | +end |
| 238 | +fizzbuzz(15) |
| 239 | +``` |
| 240 | + |
146 | 241 | ## Structs |
147 | 242 |
|
148 | 243 | | Function | Status | JS Output | Notes | |
|
154 | 249 | | Parametric types | Supported | Type erasure | `Box{Int32}` → `Box` | |
155 | 250 | | Abstract type hierarchies | Supported | DFS pre-order type IDs | | |
156 | 251 |
|
| 252 | +**Example:** |
| 253 | +```julia |
| 254 | +struct Point |
| 255 | + x |
| 256 | + y |
| 257 | +end |
| 258 | + |
| 259 | +function distance(p) |
| 260 | + return sqrt(p.x * p.x + p.y * p.y) |
| 261 | +end |
| 262 | + |
| 263 | +p = Point(3.0, 4.0) |
| 264 | +println(p.x) |
| 265 | +println(p.y) |
| 266 | +println(distance(p)) |
| 267 | +``` |
| 268 | + |
157 | 269 | ## Functions |
158 | 270 |
|
159 | 271 | | Function | Status | JS Output | Notes | |
160 | 272 | |---|---|---|---| |
161 | 273 | | Multi-function modules | Supported | ESM/CJS/IIFE | | |
162 | 274 | | Closures | Supported | JS function expressions | | |
163 | 275 | | Higher-order (concrete callee) | Supported | Direct call | | |
| 276 | +| Pipe operator (`\|>`) | Supported | `f(x)` | | |
| 277 | + |
| 278 | +**Example:** |
| 279 | +```julia |
| 280 | +function square(x) |
| 281 | + return x * x |
| 282 | +end |
| 283 | + |
| 284 | +function sum_of_squares(n) |
| 285 | + s = 0 |
| 286 | + for i in 1:n |
| 287 | + s = s + square(i) |
| 288 | + end |
| 289 | + return s |
| 290 | +end |
| 291 | + |
| 292 | +println(sum_of_squares(10)) |
| 293 | +``` |
164 | 294 |
|
165 | 295 | ## IO |
166 | 296 |
|
|
171 | 301 |
|
172 | 302 | ## Known Limitations |
173 | 303 |
|
174 | | -1. **Cross-referencing phi nodes in loops**: Functions with patterns like `a, b = b, a+b` (fibonacci) produce incorrect results due to sequential phi assignment. Needs temp variables. |
175 | | -2. **Deeply inlined Base functions**: `push!`, `pop!`, `haskey`, `uppercase`, `lowercase`, `strip`, `occursin`, `split`, `replace` get inlined by Julia into internal operations that can't be mapped to JS. These need either runtime wrappers or `optimize=false` mode. |
176 | | -3. **Bool bitwise ops**: `&` and `|` on Bool produce numbers (0/1) in JS, not booleans. |
177 | | -4. **Int32 abs**: Uses `flipsign_int` intrinsic not yet mapped. |
178 | | -5. **String length**: Julia counts codepoints, JS `.length` counts UTF-16 code units. |
| 304 | +1. **Deeply inlined Base functions**: `push!`, `pop!`, `haskey`, `uppercase`, `lowercase`, `strip`, `occursin`, `split`, `replace` get inlined by Julia into internal operations that can't be mapped to JS via `compile()`. The playground handles these as named functions. |
| 305 | +2. **Bool bitwise ops**: `&` and `|` on Bool produce numbers (0/1) in JS, not booleans. |
| 306 | +3. **Int32 abs**: Uses `flipsign_int` intrinsic not yet mapped. |
| 307 | +4. **String length**: Julia counts codepoints, JS `.length` counts UTF-16 code units. |
| 308 | +5. **Nested try/catch**: Single-level try/catch works; nested try/catch may not work correctly in the playground. |
0 commit comments