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

Function alloc

modules/llrt_buffer/src/buffer.rs:103–136  ·  view source on GitHub ↗

Static Methods

(
    ctx: Ctx<'js>,
    length: usize,
    fill: Opt<Value<'js>>,
    encoding: Opt<String>,
)

Source from the content-addressed store, hash-verified

101
102// Static Methods
103fn alloc<'js>(
104 ctx: Ctx<'js>,
105 length: usize,
106 fill: Opt<Value<'js>>,
107 encoding: Opt<String>,
108) -> Result<Value<'js>> {
109 if let Some(value) = fill.0 {
110 if let Some(value) = value.as_string() {
111 let string = value.to_string()?;
112
113 if let Some(encoding) = encoding.0 {
114 let encoder = Encoder::from_str(&encoding).or_throw(&ctx)?;
115 let bytes = encoder.decode_from_string(string).or_throw(&ctx)?;
116 return alloc_byte_ref(&ctx, &bytes, length);
117 }
118
119 let byte_ref = string.as_bytes();
120
121 return alloc_byte_ref(&ctx, byte_ref, length);
122 }
123 if let Some(value) = value.as_int() {
124 let bytes = vec![value as u8; length];
125 return Buffer(bytes).into_js(&ctx);
126 }
127 if let Some(obj) = value.as_object() {
128 if let Some(ob) = ObjectBytes::from_array_buffer(obj)? {
129 let bytes = ob.as_bytes(&ctx)?;
130 return alloc_byte_ref(&ctx, bytes, length);
131 }
132 }
133 }
134
135 Buffer(vec![0; length]).into_js(&ctx)
136}
137
138fn alloc_byte_ref<'js>(ctx: &Ctx<'js>, byte_ref: &[u8], length: usize) -> Result<Value<'js>> {
139 let mut bytes = vec![0; length];

Callers 2

alloc_unsafe_slowFunction · 0.85
readMethod · 0.85

Calls 8

alloc_byte_refFunction · 0.85
or_throwMethod · 0.80
decode_from_stringMethod · 0.80
as_objectMethod · 0.80
BufferClass · 0.70
to_stringMethod · 0.45
as_bytesMethod · 0.45
into_jsMethod · 0.45

Tested by

no test coverage detected