MCPcopy Create free account
hub / github.com/awslabs/llrt / iterate

Function iterate

libs/llrt_json/src/stringify.rs:423–551  ·  view source on GitHub ↗
(
    context: &mut StringifyContext<'_, 'js>,
    new_value: Option<Value<'js>>,
)

Source from the content-addressed store, hash-verified

421}
422
423fn iterate<'js>(
424 context: &mut StringifyContext<'_, 'js>,
425 new_value: Option<Value<'js>>,
426) -> Result<()> {
427 let mut add_comma;
428 let mut value_written;
429 let elem = new_value.as_ref().unwrap_or(context.value);
430 let depth = context.depth;
431 let ctx = context.ctx;
432 let indentation = context.indentation;
433 match elem.type_of() {
434 Type::Object | Type::Exception | Type::Proxy => {
435 let js_object = unsafe { elem.as_object().unwrap_unchecked() };
436 if js_object.contains_key(PredefinedAtom::ToJSON)? {
437 return run_to_json(context, js_object);
438 }
439
440 //only start detect circular reference at this level
441 if depth > CIRCULAR_REF_DETECTION_DEPTH {
442 detect_circular_reference(
443 ctx,
444 js_object,
445 context.key,
446 context.index,
447 context.parent,
448 context.ancestors,
449 context.itoa_buffer,
450 )?;
451 }
452
453 context.result.push('{');
454
455 value_written = false;
456
457 // Collect keys: js_object.keys() uses JS_GetOwnPropertyNames which can fail for
458 // Proxy objects. Fall back to Object.keys() in that case.
459 let keys: Vec<String> = {
460 let collected: Vec<String> = js_object.keys::<String>().flatten().collect();
461 if collected.is_empty() {
462 // Clear any pending exception and try Object.keys() for Proxy support
463 ctx.catch();
464 ctx.globals()
465 .get::<_, Object>("Object")
466 .ok()
467 .and_then(|o| o.get::<_, Function>("keys").ok())
468 .and_then(|f| f.call::<_, Vec<String>>((js_object.clone(),)).ok())
469 .unwrap_or_default()
470 } else {
471 collected
472 }
473 };
474
475 for key in keys {
476 let val = js_object.get(&key)?;
477
478 add_comma = append_value(
479 &mut StringifyContext {
480 ctx,

Callers 2

append_valueFunction · 0.85

Calls 10

run_to_jsonFunction · 0.85
append_valueFunction · 0.85
write_indentationFunction · 0.85
as_objectMethod · 0.80
pushMethod · 0.80
okMethod · 0.80
is_emptyMethod · 0.45
cloneMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected