-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathmod.rs
166 lines (137 loc) · 4.36 KB
/
mod.rs
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
pub mod encoder;
use rquickjs::{
function::Opt,
module::{Declarations, Exports, ModuleDef},
prelude::{Func, This},
Class, Ctx, Result, TypedArray, Value,
};
use crate::{
module::export_default,
utils::{
object::{bytes_to_typed_array, get_bytes},
result::ResultExt,
},
};
use self::encoder::{
bytes_from_b64, bytes_from_hex, bytes_to_b64_string, bytes_to_hex_string, Encoder,
};
pub struct HexModule;
impl HexModule {
pub fn encode<'js>(ctx: Ctx<'js>, buffer: Value<'js>) -> Result<String> {
let bytes = get_bytes(&ctx, buffer)?;
Ok(bytes_to_hex_string(&bytes))
}
pub fn decode(ctx: Ctx, encoded: String) -> Result<Value> {
let bytes = bytes_from_hex(encoded.as_bytes())
.or_throw_msg(&ctx, "Cannot decode unrecognized sequence")?;
bytes_to_typed_array(ctx, &bytes)
}
}
impl ModuleDef for HexModule {
fn declare(declare: &mut Declarations) -> Result<()> {
declare.declare(stringify!(encode))?;
declare.declare(stringify!(decode))?;
declare.declare("default")?;
Ok(())
}
fn evaluate<'js>(ctx: &Ctx<'js>, exports: &mut Exports<'js>) -> Result<()> {
export_default(ctx, exports, |default| {
default.set(stringify!(encode), Func::from(Self::encode))?;
default.set(stringify!(decode), Func::from(Self::decode))?;
Ok(())
})?;
Ok(())
}
}
#[derive(rquickjs::class::Trace)]
#[rquickjs::class]
pub struct TextEncoder {}
#[rquickjs::methods]
impl TextEncoder {
#[qjs(constructor)]
pub fn new_enc() -> Self {
Self {}
}
pub fn encode<'js>(&self, ctx: Ctx<'js>, string: String) -> Result<Value<'js>> {
TypedArray::new(ctx, string.as_bytes()).map(|m| m.into_value())
}
}
#[rquickjs::class]
#[derive(rquickjs::class::Trace)]
pub struct TextDecoder {
#[qjs(skip_trace)]
encoder: Encoder,
}
#[rquickjs::methods]
impl TextDecoder {
#[qjs(constructor)]
pub fn new_dec(ctx: Ctx<'_>, encoding: Opt<String>) -> Result<Self> {
let mut encoding = encoding.0.unwrap_or(String::from("utf-8"));
if encoding.is_empty() {
encoding = String::from("utf-8");
}
let encoder = Encoder::from_str(&encoding).or_throw(&ctx)?;
Ok(TextDecoder { encoder })
}
pub fn decode<'js>(&self, ctx: Ctx<'js>, buffer: Value<'js>) -> Result<String> {
let bytes = get_bytes(&ctx, buffer)?;
self.encoder.encode_to_string(&bytes).or_throw(&ctx)
}
}
#[rquickjs::class]
#[derive(rquickjs::class::Trace, Debug)]
pub struct StringBuilder {
#[qjs(skip_trace)]
value: String,
}
#[rquickjs::methods(rename_all = "camelCase")]
impl StringBuilder {
#[qjs(constructor)]
fn new_string_builder(capacity: Opt<usize>) -> Self {
Self {
value: String::with_capacity(capacity.0.unwrap_or(256)),
}
}
fn append<'js>(
this: This<Class<'js, Self>>,
_ctx: Ctx<'js>,
value: Value<'js>,
) -> Result<Class<'js, Self>> {
if value.is_string() {
let string: String = value.get()?;
this.borrow_mut().value.push_str(&string);
} else if value.is_number() {
let number: f64 = value.get()?;
this.borrow_mut().value.push_str(&number.to_string());
} else if value.is_bool() {
let boolean: bool = value.get()?;
this.0
.borrow_mut()
.value
.push_str(if boolean { "true" } else { "false" });
}
Ok(this.0)
}
#[allow(clippy::wrong_self_convention)]
fn to_string(&mut self) -> String {
self.value.clone()
}
}
pub fn atob<'js>(ctx: Ctx<'js>, encoded_value: String) -> Result<String> {
let vec = bytes_from_b64(encoded_value.as_bytes()).or_throw(&ctx)?;
Ok(unsafe { String::from_utf8_unchecked(vec) })
}
pub fn btoa(value: String) -> String {
bytes_to_b64_string(value.as_bytes())
}
pub fn init(ctx: &Ctx<'_>) -> Result<()> {
let globals = ctx.globals();
globals.set("atob", Func::from(atob))?;
globals.set("btoa", Func::from(btoa))?;
Class::<TextEncoder>::define(&globals)?;
Class::<TextDecoder>::define(&globals)?;
Class::<StringBuilder>::define(&globals)?;
Ok(())
}