Skip to content

Commit bee5e51

Browse files
Dale-Blackclaude
andcommitted
JST-DOC-002: Supported functions page with runnable examples
Enhanced supported_functions.md with runnable code examples for each category (arithmetic, math, comparisons, booleans, strings, collections, control flow, structs, functions, IO). Added playground vs compiler note for functions that work differently in each context. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3b9fc3b commit bee5e51

1 file changed

Lines changed: 141 additions & 11 deletions

File tree

docs/src/supported_functions.md

Lines changed: 141 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# Supported Julia Functions in JavaScriptTarget.jl
1+
# Supported Functions
22

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).
76

87
---
98

@@ -18,13 +17,24 @@
1817
| `-` (Float64) | Supported | `a - b` | |
1918
| `*` (Float64) | Supported | `a * b` | |
2019
| `/` (Float64) | Supported | `a / b` | |
21-
| `-` (unary, Float64) | Supported | `-a` | |
20+
| `-` (unary) | Supported | `-a` | |
2221
| `div(a, b)` | Supported | `(a / b) \| 0` | Julia inlines to intrinsic |
2322
| `fld(a, b)` | Supported | Inlined arithmetic | Floor division |
2423
| `mod(a, b)` | Supported | Inlined arithmetic | Same sign as divisor |
2524
| `cld(a, b)` | Supported | Inlined arithmetic | Ceiling division |
2625
| `rem(a, b)` | Supported | `a % b` | |
2726

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+
2838
## Math Functions
2939

3040
| Function | Status | JS Output | Notes |
@@ -51,6 +61,18 @@
5161
| `max(a, b)` | Supported | `Math.max(a, b)` | |
5262
| `hypot(a, b)` | Supported | `Math.hypot(a, b)` | |
5363

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+
5476
## Comparisons
5577

5678
| Function | Status | JS Output | Notes |
@@ -63,6 +85,14 @@
6385
| `>=` | Supported | `>=` | |
6486
| `===` | Supported | `===` (primitives), `jl_egal` (immutable structs) | |
6587

88+
**Example:**
89+
```julia
90+
println(3 > 2)
91+
println(3 == 3)
92+
println(3 != 4)
93+
println(5 <= 5)
94+
```
95+
6696
## Boolean
6797

6898
| Function | Status | JS Output | Notes |
@@ -73,6 +103,21 @@
73103
| `&` (bitwise on Bool) | Partial | `a & b` | Returns number in JS, not boolean |
74104
| `\|` (bitwise on Bool) | Partial | `a \| b` | Returns number in JS, not boolean |
75105

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+
76121
## Bitwise (Int32)
77122

78123
| Function | Status | JS Output | Notes |
@@ -113,6 +158,26 @@
113158
| `replace(s, p=>r)` | Excluded || Julia inlines deeply |
114159
| `length(s)` | Partial || Julia's codepoint length differs from JS |
115160

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+
116181
## Collections
117182

118183
| Function | Status | JS Output | Notes |
@@ -131,6 +196,14 @@
131196
| `(a, b)` (Tuple) | Supported | `[a, b]` | |
132197
| `t[1]` (Tuple) | Supported | `t[0]` | |
133198

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+
134207
## Control Flow
135208

136209
| Function | Status | JS Output | Notes |
@@ -143,6 +216,28 @@
143216
| `try/catch` | Supported | `try/catch` | |
144217
| `error(msg)` | Supported | `throw new Error(msg)` | |
145218

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+
146241
## Structs
147242

148243
| Function | Status | JS Output | Notes |
@@ -154,13 +249,48 @@
154249
| Parametric types | Supported | Type erasure | `Box{Int32}``Box` |
155250
| Abstract type hierarchies | Supported | DFS pre-order type IDs | |
156251

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+
157269
## Functions
158270

159271
| Function | Status | JS Output | Notes |
160272
|---|---|---|---|
161273
| Multi-function modules | Supported | ESM/CJS/IIFE | |
162274
| Closures | Supported | JS function expressions | |
163275
| 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+
```
164294

165295
## IO
166296

@@ -171,8 +301,8 @@
171301

172302
## Known Limitations
173303

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

Comments
 (0)