shape-runtime 0.3.2

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Native `http` module for making HTTP requests.
//!
//! Exports: http.get, http.delete (Stage C); http.post_text,
//! http.post_bytes, http.put_text, http.put_bytes (Stage D).
//!
//! All functions are async. Uses reqwest under the hood.
//! Policy gated: requires NetConnect permission.
//!
//! Stage C HashMap-marshal P1(b) migration (2026-05-07):
//! - Outer response shape `{status, headers, body, ok}` returns via
//!   `TypedReturn::OkObjectPairs` (Cluster #4 β shape, mirrors
//!   `arrow.metadata` precedent at `arrow_module.rs:127`).
//! - Inner `headers` field carries `HashMap<string, string>` payload via
//!   `ConcreteReturn::HashMapStringString` (insertion-order preserved).
//! - Options arg parsing uses `Vec<(Arc<String>, Arc<HeapValue>)>`
//!   FromSlot impl from Step 1 P1(b) infrastructure
//!   (`crates/shape-runtime/src/marshal.rs`, Stage C commit `36519f6`).
//!
//! Stage D N4 partial sign-off (2026-05-07; supervisor relay):
//! - `http.post`/`http.put` legacy shape (single fn with `body: any`)
//!   replaced by typed overloads via Shape API split
//!   (`stdlib-src/core/http.shape`):
//!     - `post_text(url, body: string, options)` — sets
//!       `Content-Type: text/plain; charset=utf-8`
//!     - `post_bytes(url, body: Array<int>, options)` — sets
//!       `Content-Type: application/octet-stream`
//!     - `put_text(url, body: string, options)` — same content-type as
//!       post_text
//!     - `put_bytes(url, body: Array<int>, options)` — same as post_bytes
//! - Body types map directly to existing `FromSlot` impls
//!   (`Arc<String>` at `marshal.rs:129`, `Vec<u8>` at `marshal.rs:330`)
//!   per supervisor's "mechanical typed marshal" framing.
//! - `http.post_json(url, body: object, options)` and `http.put_json`
//!   remain DEFERRED pending architectural sub-decision **N7 —
//!   HeapValue→JSON serializer for HTTP / object-output marshal
//!   contexts.** The `body: object` shape requires walking the
//!   polymorphic `Vec<(Arc<String>, Arc<HeapValue>)>` tree and producing
//!   a JSON string; per-variant serialization choices for Decimal,
//!   DataTable, Content, Temporal, TableView each represent a
//!   user-visible behavioral commitment that needs supervisor sign-off
//!   (architectural-adjacent helper, refused as bundled with Step 2 per
//!   the "no bundling architectural decisions" watchlist refusal).
//!   Surfaced via team-lead's relay batch.
//!
//! Tests deleted along with the legacy ValueWord-based fixtures, mirroring
//! the csv_module migration (commit `9f6b1d3`). New typed-marshal test
//! harness arrives with the shape-vm cleanup workstream.

use crate::marshal::register_typed_async_fn_3_full;
use crate::marshal::register_typed_async_fn_2_full;
use crate::module_exports::{ModuleExports, ModuleParam};
use crate::typed_module_exports::{ConcreteReturn, ConcreteType, TypedReturn};
use shape_value::heap_value::HeapValue;
use std::sync::Arc;

/// Build the schemaful HttpResponse pair-list returned by every http.*
/// function. Schema: `{status: int, headers: HashMap<string, string>,
/// body: string, ok: bool}`. Insertion order preserved per `ObjectPairs`
/// contract (`crates/shape-runtime/src/typed_module_exports.rs:117`).
fn build_response_pairs(
    status: u16,
    headers: Vec<(String, String)>,
    body: String,
) -> Vec<(String, ConcreteReturn)> {
    vec![
        ("status".to_string(), ConcreteReturn::I64(status as i64)),
        (
            "headers".to_string(),
            ConcreteReturn::HashMapStringString(headers),
        ),
        ("body".to_string(), ConcreteReturn::String(body)),
        (
            "ok".to_string(),
            ConcreteReturn::Bool((200..300).contains(&status)),
        ),
    ]
}

/// Extract optional headers from an `options: HashMap<string, *>` arg.
/// The options HashMap may contain a `"headers"` key whose value is itself
/// a `HashMap<string, string>` (`HeapValue::HashMap` variant). Walks the
/// outer pair list linearly looking for `"headers"`, then reads the
/// nested HashMap's keys/values buffers.
fn extract_headers(options: &[(Arc<String>, Arc<HeapValue>)]) -> Vec<(String, String)> {
    for (k, v) in options.iter() {
        if k.as_str() == "headers" {
            if let HeapValue::HashMap(kref) = &**v {
                // Wave 2 Round 3b C2-joint ckpt-4 (2026-05-14): per-V walk
                // for HashMap<string, string> (the canonical headers shape).
                // Other V variants return empty (caller's contract is
                // string headers; non-string Vs are a producer-side bug).
                use shape_value::heap_value::HashMapKindedRef;
                if let HashMapKindedRef::String(arc) = kref {
                    let n = arc.len();
                    let mut out = Vec::with_capacity(n);
                    for i in 0..n {
                        let key: String = unsafe {
                            let ptr = shape_value::v2::typed_array::TypedArray::get_unchecked(
                                arc.keys, i as u32,
                            );
                            shape_value::v2::string_obj::StringObj::as_str(ptr).to_owned()
                        };
                        let val: String = unsafe {
                            let v_ptr: *const shape_value::v2::string_obj::StringObj =
                                *(*arc.values).data.add(i);
                            shape_value::v2::string_obj::StringObj::as_str(v_ptr).to_owned()
                        };
                        out.push((key, val));
                    }
                    return out;
                }
                return Vec::new();
            }
        }
    }
    Vec::new()
}

/// Extract optional `timeout` (milliseconds) from the options HashMap.
/// Walks linearly for `"timeout"`; if present and integer, converts to a
/// `Duration`.
///
/// Currently accepts `HeapValue::BigInt` (i64-typed integer) values only.
/// `number`-typed (f64) timeout values surface as raw scalar slots in
/// post-bulldozer Shape and don't reach `HeapValue` — supporting them
/// would require either Shape user code passing an int (`5000` not
/// `5000.0`) OR a future `HeapValue::NativeScalar`-aware branch here.
/// Documented for follow-on if a consumer surfaces.
fn extract_timeout(
    options: &[(Arc<String>, Arc<HeapValue>)],
) -> Option<std::time::Duration> {
    for (k, v) in options.iter() {
        if k.as_str() == "timeout" {
            if let HeapValue::BigInt(ms) = &**v {
                let n = **ms;
                if n > 0 {
                    return Some(std::time::Duration::from_millis(n as u64));
                }
            }
        }
    }
    None
}

/// Create the `http` module with async HTTP request functions.
pub fn create_http_module() -> ModuleExports {
    let mut module = ModuleExports::new("std::core::http");
    module.description = "HTTP client for making web requests".to_string();

    let url_param = ModuleParam {
        name: "url".to_string(),
        type_name: "string".to_string(),
        required: true,
        description: "URL to request".to_string(),
        ..Default::default()
    };

    let options_param = ModuleParam {
        name: "options".to_string(),
        type_name: "HashMap<string, any>".to_string(),
        required: false,
        description: "Request options: { headers?: HashMap, timeout?: int }"
            .to_string(),
        default_snippet: Some("{}".to_string()),
        ..Default::default()
    };

    let response_ty =
        ConcreteType::Result(Box::new(ConcreteType::Named("HttpResponse".to_string())));

    // http.get(url: string, options?: HashMap) -> Result<HttpResponse>
    register_typed_async_fn_2_full::<_, _, Arc<String>, Vec<(Arc<String>, Arc<HeapValue>)>>(
        &mut module,
        "get",
        "Perform an HTTP GET request",
        [url_param.clone(), options_param.clone()],
        response_ty.clone(),
        |url: Arc<String>, options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut builder = reqwest::Client::new().get(url.as_str());

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.get() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body = resp
                .text()
                .await
                .map_err(|e| format!("http.get() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body,
            )))
        },
    );

    // http.delete(url: string, options?: HashMap) -> Result<HttpResponse>
    register_typed_async_fn_2_full::<_, _, Arc<String>, Vec<(Arc<String>, Arc<HeapValue>)>>(
        &mut module,
        "delete",
        "Perform an HTTP DELETE request",
        [url_param, options_param],
        response_ty,
        |url: Arc<String>, options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut builder = reqwest::Client::new().delete(url.as_str());

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.delete() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body = resp
                .text()
                .await
                .map_err(|e| format!("http.delete() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body,
            )))
        },
    );

    // Stage D N4 partial sign-off: 4 typed overloads via Shape API
    // split. Each body type is a fixed-arity register_typed_async_fn_3
    // with one specific body type per overload, per supervisor's
    // "mechanical typed marshal" framing. Reuses build_response_pairs +
    // extract_headers + extract_timeout from the get/delete path.

    let url_param_3 = ModuleParam {
        name: "url".to_string(),
        type_name: "string".to_string(),
        required: true,
        description: "URL to request".to_string(),
        ..Default::default()
    };
    let options_param_3 = ModuleParam {
        name: "options".to_string(),
        type_name: "HashMap<string, any>".to_string(),
        required: false,
        description: "Request options: { headers?: HashMap, timeout?: int }"
            .to_string(),
        default_snippet: Some("{}".to_string()),
        ..Default::default()
    };
    let body_text_param = ModuleParam {
        name: "body".to_string(),
        type_name: "string".to_string(),
        required: true,
        description: "Request body as a string (sent verbatim)".to_string(),
        ..Default::default()
    };
    let body_bytes_param = ModuleParam {
        name: "body".to_string(),
        type_name: "Array<int>".to_string(),
        required: true,
        description: "Request body as a byte array".to_string(),
        ..Default::default()
    };
    let response_ty_3 =
        ConcreteType::Result(Box::new(ConcreteType::Named("HttpResponse".to_string())));

    // http.post_text(url: string, body: string, options?: HashMap) -> Result<HttpResponse>
    register_typed_async_fn_3_full::<
        _,
        _,
        Arc<String>,
        Arc<String>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
    >(
        &mut module,
        "post_text",
        "Perform an HTTP POST request with a text body",
        [
            url_param_3.clone(),
            body_text_param.clone(),
            options_param_3.clone(),
        ],
        response_ty_3.clone(),
        |url: Arc<String>,
         body: Arc<String>,
         options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut builder = reqwest::Client::new()
                .post(url.as_str())
                .header(
                    reqwest::header::CONTENT_TYPE,
                    "text/plain; charset=utf-8",
                )
                .body(body.as_str().to_string());

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.post_text() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body_out = resp
                .text()
                .await
                .map_err(|e| format!("http.post_text() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body_out,
            )))
        },
    );

    // http.post_bytes(url: string, body: Array<int>, options?: HashMap) -> Result<HttpResponse>
    register_typed_async_fn_3_full::<
        _,
        _,
        Arc<String>,
        Vec<u8>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
    >(
        &mut module,
        "post_bytes",
        "Perform an HTTP POST request with a binary body",
        [
            url_param_3.clone(),
            body_bytes_param.clone(),
            options_param_3.clone(),
        ],
        response_ty_3.clone(),
        |url: Arc<String>,
         body: Vec<u8>,
         options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut builder = reqwest::Client::new()
                .post(url.as_str())
                .header(
                    reqwest::header::CONTENT_TYPE,
                    "application/octet-stream",
                )
                .body(body);

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.post_bytes() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body_out = resp
                .text()
                .await
                .map_err(|e| format!("http.post_bytes() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body_out,
            )))
        },
    );

    // http.put_text(url: string, body: string, options?: HashMap) -> Result<HttpResponse>
    register_typed_async_fn_3_full::<
        _,
        _,
        Arc<String>,
        Arc<String>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
    >(
        &mut module,
        "put_text",
        "Perform an HTTP PUT request with a text body",
        [
            url_param_3.clone(),
            body_text_param,
            options_param_3.clone(),
        ],
        response_ty_3.clone(),
        |url: Arc<String>,
         body: Arc<String>,
         options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut builder = reqwest::Client::new()
                .put(url.as_str())
                .header(
                    reqwest::header::CONTENT_TYPE,
                    "text/plain; charset=utf-8",
                )
                .body(body.as_str().to_string());

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.put_text() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body_out = resp
                .text()
                .await
                .map_err(|e| format!("http.put_text() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body_out,
            )))
        },
    );

    // http.put_bytes(url: string, body: Array<int>, options?: HashMap) -> Result<HttpResponse>
    register_typed_async_fn_3_full::<
        _,
        _,
        Arc<String>,
        Vec<u8>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
    >(
        &mut module,
        "put_bytes",
        "Perform an HTTP PUT request with a binary body",
        [url_param_3, body_bytes_param, options_param_3],
        response_ty_3,
        |url: Arc<String>,
         body: Vec<u8>,
         options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut builder = reqwest::Client::new()
                .put(url.as_str())
                .header(
                    reqwest::header::CONTENT_TYPE,
                    "application/octet-stream",
                )
                .body(body);

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.put_bytes() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body_out = resp
                .text()
                .await
                .map_err(|e| format!("http.put_bytes() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body_out,
            )))
        },
    );

    // N7 ε disposition (REFINEMENT-3A γ + a, 2026-05-07): post_json /
    // put_json take `body: object` which lands in this body as
    // `Vec<(Arc<String>, Arc<HeapValue>)>` via the existing FromSlot
    // impl at `crates/shape-runtime/src/marshal.rs:624`
    // (`NATIVE_KIND = NativeKind::Ptr(HeapKind::HashMap)` — supervisor's
    // load-bearing finding: the HashMap container anchors the slot kind
    // structurally, side-stepping the N4-α single-any wildcard refusal
    // that blocks the 5 single-any consumers C7/C10-C13 deferred to the
    // n7-single-any-input-resolution follow-on workstream).
    //
    // Body algorithm: build `JsonValue::Object` by walking each HashMap
    // pair via `heap_to_json_value(&v)?` (C2) → `json_value_to_serde_json`
    // (C3) → `serde_json::to_string(&serde_json_v)?` → reqwest body with
    // `Content-Type: application/json`. Insertion order preserved per
    // ObjectPairs contract.

    let url_param_post_json = ModuleParam {
        name: "url".to_string(),
        type_name: "string".to_string(),
        required: true,
        description: "URL to request".to_string(),
        ..Default::default()
    };
    let body_object_param_post = ModuleParam {
        name: "body".to_string(),
        type_name: "object".to_string(),
        required: true,
        description: "Request body as an object (sent as JSON)".to_string(),
        ..Default::default()
    };
    let options_param_post_json = ModuleParam {
        name: "options".to_string(),
        type_name: "HashMap<string, any>".to_string(),
        required: false,
        description: "Request options: { headers?: HashMap, timeout?: int }"
            .to_string(),
        default_snippet: Some("{}".to_string()),
        ..Default::default()
    };
    let response_ty_post_json =
        ConcreteType::Result(Box::new(ConcreteType::Named("HttpResponse".to_string())));

    // http.post_json(url: string, body: object, options?: HashMap) -> Result<HttpResponse>
    register_typed_async_fn_3_full::<
        _,
        _,
        Arc<String>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
    >(
        &mut module,
        "post_json",
        "Perform an HTTP POST request with a JSON body",
        [
            url_param_post_json.clone(),
            body_object_param_post,
            options_param_post_json.clone(),
        ],
        response_ty_post_json.clone(),
        |url: Arc<String>,
         body: Vec<(Arc<String>, Arc<HeapValue>)>,
         options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut json_pairs: Vec<(String, crate::json_value::JsonValue)> =
                Vec::with_capacity(body.len());
            for (k, v) in body.iter() {
                json_pairs.push(((**k).clone(), crate::json_value::heap_to_json_value(v)?));
            }
            let json_value = crate::json_value::JsonValue::Object(json_pairs);
            let serde_json_v = crate::json_value::json_value_to_serde_json(&json_value);
            let body_str = serde_json::to_string(&serde_json_v)
                .map_err(|e| format!("http.post_json() body serialization failed: {}", e))?;

            let mut builder = reqwest::Client::new()
                .post(url.as_str())
                .header(reqwest::header::CONTENT_TYPE, "application/json")
                .body(body_str);

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.post_json() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body_out = resp
                .text()
                .await
                .map_err(|e| format!("http.post_json() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body_out,
            )))
        },
    );

    // http.put_json(url: string, body: object, options?: HashMap) -> Result<HttpResponse>
    let body_object_param_put = ModuleParam {
        name: "body".to_string(),
        type_name: "object".to_string(),
        required: true,
        description: "Request body as an object (sent as JSON)".to_string(),
        ..Default::default()
    };
    register_typed_async_fn_3_full::<
        _,
        _,
        Arc<String>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
        Vec<(Arc<String>, Arc<HeapValue>)>,
    >(
        &mut module,
        "put_json",
        "Perform an HTTP PUT request with a JSON body",
        [url_param_post_json, body_object_param_put, options_param_post_json],
        response_ty_post_json,
        |url: Arc<String>,
         body: Vec<(Arc<String>, Arc<HeapValue>)>,
         options: Vec<(Arc<String>, Arc<HeapValue>)>| async move {
            let mut json_pairs: Vec<(String, crate::json_value::JsonValue)> =
                Vec::with_capacity(body.len());
            for (k, v) in body.iter() {
                json_pairs.push(((**k).clone(), crate::json_value::heap_to_json_value(v)?));
            }
            let json_value = crate::json_value::JsonValue::Object(json_pairs);
            let serde_json_v = crate::json_value::json_value_to_serde_json(&json_value);
            let body_str = serde_json::to_string(&serde_json_v)
                .map_err(|e| format!("http.put_json() body serialization failed: {}", e))?;

            let mut builder = reqwest::Client::new()
                .put(url.as_str())
                .header(reqwest::header::CONTENT_TYPE, "application/json")
                .body(body_str);

            for (k, v) in extract_headers(&options) {
                builder = builder.header(&k, &v);
            }
            if let Some(timeout) = extract_timeout(&options) {
                builder = builder.timeout(timeout);
            }

            let resp = builder
                .send()
                .await
                .map_err(|e| format!("http.put_json() failed: {}", e))?;

            let status = resp.status().as_u16();
            let headers: Vec<(String, String)> = resp
                .headers()
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect();
            let body_out = resp
                .text()
                .await
                .map_err(|e| format!("http.put_json() body read failed: {}", e))?;

            Ok(TypedReturn::OkObjectPairs(build_response_pairs(
                status, headers, body_out,
            )))
        },
    );

    module
}