about summary refs log tree commit diff stats
path: root/js/baba-yaga/examples/js-interop-simple.baba
blob: b937503a66cf2ec7f8d8868124829fcda1959e6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// js-interop-simple.baba - Simple JavaScript interop demonstration

io.out "=== JavaScript Interop Demo ===";
io.out "";

// Mathematical operations
io.out "Math Operations:";
absResult : io.callJS "Math.abs" [-42];
io.out absResult;

minResult : io.callJS "Math.min" [10, 5, 8];
io.out minResult;

maxResult : io.callJS "Math.max" [10, 5, 8];  
io.out maxResult;

io.out "";

// JSON operations
io.out "JSON Operations:";
jsonStr : "{\"name\": \"Alice\", \"age\": 30}";
parseResult : io.callJS "JSON.parse" [jsonStr];
io.out parseResult;

// Property access
propResult : when parseResult is
  Ok obj then io.getProperty obj "name"
  Err msg then Err msg;

io.out propResult;

io.out "";

// Array operations  
io.out "Array Operations:";
babaList : [1, 2, 3, 4, 5];
jsArray : io.listToJSArray babaList;
jsonResult : io.callJS "JSON.stringify" [jsArray];
io.out jsonResult;

io.out "";

// Error handling
io.out "Error Handling:";
errorResult : io.callJS "invalidFunction" [];
io.out errorResult;

io.out "";
io.out "Demo complete!";