about summary refs log tree commit diff stats
path: root/js/baba-yaga/examples/js-interop-demo.baba
blob: cb23ba0b5e573302f18d9549c4ab47d0b16ee3ec (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// js-interop-demo.baba - Demonstration of JavaScript interop features

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

// 1. Mathematical operations
io.out "1. Mathematical Operations:";
absResult : io.callJS "Math.abs" [-42];
minResult : io.callJS "Math.min" [10, 5, 8, 15];
maxResult : io.callJS "Math.max" [10, 5, 8, 15];

io.out ("Math.abs(-42) = " .. when absResult is Ok x then x _ then "error");
io.out ("Math.min([10,5,8,15]) = " .. when minResult is Ok x then x _ then "error");
io.out ("Math.max([10,5,8,15]) = " .. when maxResult is Ok x then x _ then "error");
io.out "";

// 2. JSON processing
io.out "2. JSON Processing:";
jsonData : "{\"name\": \"Alice\", \"age\": 30, \"scores\": [85, 92, 78]}";
parseResult : io.callJS "JSON.parse" [jsonData];

when parseResult is
  Ok jsObj then (
    io.out "Successfully parsed JSON object";
    
    // Convert to Baba Yaga table
    tableResult : io.objectToTable jsObj;
    when tableResult is
      Ok table then (
        io.out "Converted to Baba Yaga table";
        
        // Access properties
        nameResult : io.getProperty jsObj "name";
        ageResult : io.getProperty jsObj "age";
        
        when (nameResult, ageResult) is
          (Ok name, Ok age) then (
            io.out ("Name: " .. name);
            io.out ("Age: " .. age);
          )
          _ then io.out "Error accessing properties";
      )
      Err msg then io.out ("Table conversion error: " .. msg);
  )
  Err msg then io.out ("JSON parse error: " .. msg);

io.out "";

// 3. Array processing
io.out "3. Array Processing:";
babaList : [1, 2, 3, 4, 5];
jsArray : io.listToJSArray babaList;
stringifyResult : io.callJS "JSON.stringify" [jsArray];

when stringifyResult is
  Ok jsonStr then io.out ("Baba Yaga list as JSON: " .. jsonStr)
  Err msg then io.out ("Stringify error: " .. msg);

// Process JavaScript array
jsArrayStr : "[10, 20, 30, 40, 50]";
jsParseResult : io.callJS "JSON.parse" [jsArrayStr];

when jsParseResult is
  Ok jsArr then (
    listResult : io.jsArrayToList jsArr;
    when listResult is
      Ok babaArr then (
        // Process with Baba Yaga functions
        doubled : map (x -> x * 2) babaArr;
        filtered : filter (x -> x > 50) doubled;
        
        io.out ("Processed array: " .. join ", " (map (x -> x) filtered));
      )
      Err msg then io.out ("List conversion error: " .. msg);
  )
  Err msg then io.out ("Array parse error: " .. msg);

io.out "";

// 4. Error handling demonstration
io.out "4. Error Handling:";
errorResult : io.callJS "nonExistentFunction" [42];
when errorResult is
  Ok _ then io.out "Unexpected success"
  Err msg then io.out ("Expected error caught: " .. msg);

io.out "";
io.out "=== Demo Complete ===";
io.out "";
io.out "This demonstrates safe JavaScript interop with:";
io.out "- Mathematical operations (Math.abs, Math.min, Math.max)";
io.out "- JSON parsing and stringification"; 
io.out "- Type conversions between JS and Baba Yaga";
io.out "- Array processing pipelines";
io.out "- Explicit error handling with Result types";