MCPcopy Create free account
hub / github.com/Ray-D-Song/lexe / alloc

Function alloc

modules/llrt_buffer/src/buffer.rs:92–125  ·  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

90
91// Static Methods
92fn alloc<'js>(
93 ctx: Ctx<'js>,
94 length: usize,
95 fill: Opt<Value<'js>>,
96 encoding: Opt<String>,
97) -> Result<Value<'js>> {
98 if let Some(value) = fill.0 {
99 if let Some(value) = value.as_string() {
100 let string = value.to_string()?;
101
102 if let Some(encoding) = encoding.0 {
103 let encoder = Encoder::from_str(&encoding).or_throw(&ctx)?;
104 let bytes = encoder.decode_from_string(string).or_throw(&ctx)?;
105 return alloc_byte_ref(&ctx, &bytes, length);
106 }
107
108 let byte_ref = string.as_bytes();
109
110 return alloc_byte_ref(&ctx, byte_ref, length);
111 }
112 if let Some(value) = value.as_int() {
113 let bytes = vec![value as u8; length];
114 return Buffer(bytes).into_js(&ctx);
115 }
116 if let Some(obj) = value.as_object() {
117 if let Some(ob) = ObjectBytes::from_array_buffer(obj)? {
118 let bytes = ob.as_bytes(&ctx)?;
119 return alloc_byte_ref(&ctx, bytes, length);
120 }
121 }
122 }
123
124 Buffer(vec![0; length]).into_js(&ctx)
125}
126
127fn alloc_byte_ref<'js>(ctx: &Ctx<'js>, byte_ref: &[u8], length: usize) -> Result<Value<'js>> {
128 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