Static Methods
(
ctx: Ctx<'js>,
length: usize,
fill: Opt<Value<'js>>,
encoding: Opt<String>,
)
| 101 | |
| 102 | // Static Methods |
| 103 | fn 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 | |
| 138 | fn alloc_byte_ref<'js>(ctx: &Ctx<'js>, byte_ref: &[u8], length: usize) -> Result<Value<'js>> { |
| 139 | let mut bytes = vec![0; length]; |
no test coverage detected